Next4Me
 All Classes Files Functions Variables
UserAccount.php
Go to the documentation of this file.
1 <?php
16 class Application_Model_UserAccount extends Zend_Db_Table_Abstract{
17 
18  protected $_name="UserAccount";
19  protected $_primary="IdUserAccount";
20  //==== Start doLogin =========================================================================
30  function doLogin($username, $password, $chanel){
31 
32  $sql = $this->fetchRow($this->select()->setIntegrityCheck(false)
33  ->from("UserAccount","*")
34  ->where("UserName like binary ?",$username)
35  ->where("Password like binary ?",$password)
36  ->where("Active=1")
37  );
38 
39  if(isset($sql->IdUserAccount)){
40  $idUserAccount=$sql->IdUserAccount;
41  }else{
42  $idUserAccount=0;
43  }
44  if($idUserAccount>0){
45  $response = array("resultId"=>"0","resultCode"=>array("token"=>md5("sec_t".$idUserAccount."t3xt".microtime()),"IdUserAccount"=>$idUserAccount));
46  }else{
47  $response = array("resultId"=>"1","resultCode"=>"Invalid username or password.");
48  }
49 
50  return $response;
51 
52  }
53  //==== End doLogin =========================================================================
54 
55  //==== Start getUserAccount =======================================================================
69  function getUserAccount($entity){
70  $resultId = 0;
71  $page="UserAccount.php";
72  $method="getUserAccount";
73  $result=json_encode($entity);
74  $message="";
75  $type="1";
76  $this->ErrorCode = new Application_Model_ErrorCode;
77  $this->Log = new Application_Model_Log;
78  $this->Paging=new Application_Model_Paging;
79 
80  try{
81  $pg=$this->Paging->getPageDefault('UserAccount');
82 
83  $data=array();
84  if(!isset($entity['IdUserAccount']))
85  {
86  $resultId = 3157;
87  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
88  $resultCode=sprintf($r['Message'], "IdUserAccount");
89  $message=sprintf($r['Message'], "IdUserAccount");
90  $type=3;
91  }else{$data['IdUserAccount']=$entity['IdUserAccount'];}
92 
93  if(!isset($entity['UserAccountType'])){
94  $resultId = 3157;
95  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
96  $resultCode=sprintf($r['Message'], "UserAccountType");
97  $message=sprintf($r['Message'], "UserAccountType");
98  $type=3;
99  }else{$data['UserAccountType']=$entity['UserAccountType'];}
100 
101  if(!isset($entity['pageSize'])){
102  $_pageSize=$pg['pageSize'];
103  }else{
104  $_pageSize=$entity['pageSize'];
105  }
106 
107  if(!isset($entity['pageNo'])){
108  $_pageNo=$pg['pageNo'];
109  }else{$_pageNo=$entity['pageNo'];}
110 
111  if(!isset($entity['orderColumn'])){
112  $_orderColumn=$pg['orderColumn'];
113  }else{$_orderColumn=$entity['orderColumn'];}
114 
115  if(!isset($entity['orderType'])){
116  $_orderType=$pg['orderType'];
117  }else{
118  switch($entity['orderType'])
119  {
120  case 'ASC':
121  $_orderType="ASC";
122  break;
123  case 'DESC':
124  $_orderType="DESC";
125  break;
126  default:
127  $_orderType="DESC";
128  break;
129  }
130  }
131 
132  if(!isset($entity['orderColumn'])){
133  $_orderColumn=$pg['orderColumn'];
134  }else{
135  switch($entity['orderColumn'])
136  {
137  case 'IdUserAccount':
138  $_orderColumn="IdUserAccount";
139  break;
140  case 'UserName':
141  $_orderColumn="UserName";
142  break;
143  case 'Password':
144  $_orderColumn="Password";
145  break;
146  case 'RecoveryAnswer':
147  $_orderColumn="RecoveryAnswer";
148  break;
149  case 'RecoveryQuestion':
150  $_orderColumn="RecoveryQuestion";
151  break;
152  case 'Email':
153  $_orderColumn="Email";
154  break;
155  case 'Active':
156  $_orderColumn="Active";
157  break;
158  case 'LastLogInIp':
159  $_orderColumn="LastLogInIp";
160  break;
161  case 'LastLogInDate':
162  $_orderColumn="LastLogInDate";
163  break;
164  case 'Fail':
165  $_orderColumn="Fail";
166  break;
167  case 'IdContactMyself':
168  $_orderColumn="IdContactMyself";
169  break;
170  case 'UserAccountType':
171  $_orderColumn="UserAccountType";
172  break;
173  default:
174  $_orderColumn=$orderColumn;
175  break;
176  }
177 
178  }
179 
180  if($resultId==0){
181  $IdUserAccount=(int)$data['IdUserAccount'];
182 
183 
184  $count = $this->fetchRow($this->select()->setIntegrityCheck(false)
185  ->from("UserAccount",array("total"=>"COUNT(IdUserAccount)"))
186  ->where("IdUserAccount=$IdUserAccount OR 0=$IdUserAccount")
187  );
188  if($count->total==0){
189  $resultId = 3123;
190  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
191  $resultCode=$r['Message'];
192  $message=$r['Message'];
193  $type=3;
194  }else{
195 
196  $offset=$this->Paging->getOffset($count->total, $_pageSize, $_pageNo);
197 
198  $rows = $this->fetchAll($this->select()->setIntegrityCheck(false)
199  ->from("UserAccount","*")
200  ->where("IdUserAccount=$IdUserAccount OR 0=$IdUserAccount")
201  ->limit($_pageSize, $offset['start'])
202  ->order($_orderColumn." ".$_orderType)
203  );
204 
205  foreach($rows as $row)
206  {
207  $resultEntity[]=array("IdUserAccount"=>$row->IdUserAccount,
208  "UserName"=>$row->UserName,
209  "Password"=>$row->Password,
210  "RecoveryAnswer"=>$row->RecoveryAnswer,
211  "RecoveryQuestion"=>$row->RecoveryQuestion,
212  "Email"=>$row->Email,
213  "Active"=>$row->Active,
214  "LastLogInIp"=>$row->LastLogInIp,
215  "LastLogInDate"=>$row->LastLogInDate,
216  "Fail"=>$row->Fail,
217  "IdContactMyself"=>$row->IdContactMyself,
218  "UserAccountType"=>$row->UserAccountType,
219  "Language"=>$row->Language);
220  }
221  $resultCode=array("pageNo"=>$_pageNo,
222  "pageSize"=>$_pageSize,
223  "pageCount"=>$offset['maxPage'],
224  "orderColumn"=>$_orderColumn,
225  "orderType"=>$_orderType,"entity"=>$resultEntity) ;
226  }
227  }
228  }
229  catch(Exception $e){
230  $resultCode=$e->getMessage();
231  $resultId=101;
232  $type=3;
233  }
234  $this->Log->save($page,$method,$result,$resultId.'=>'.json_encode($resultCode),$type);
235  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
236  return $response;
237  }
238 
239  //==== End getUserAccount =========================================================================
240 
241  //==== Start deleteUserAccount ====================================================================
242  function deleteUserAccount($entity)
243  {
244  $resultId = 0;
245  $page="UserAccount.php";
246  $method="cleanUserAccount";
247  $result=json_encode($entity);
248  $message="";
249  $type="1";
250  $this->ErrorCode = new Application_Model_ErrorCode;
251  $this->Log = new Application_Model_Log;
252  $this->Contact = new Application_Model_Contact();
253  $this->Email = new Application_Model_Email();
254  $this->Phone = new Application_Model_Phone();
255  $this->Address = new Application_Model_Address();
256  $this->Asset = new Application_Model_Asset();
257  $this->AssetProperty = new Application_Model_AssetProperty();
258  $this->Channel = new Application_Model_Channel();
259  $this->Context = new Application_Model_Context();
260  $this->ContextCondition = new Application_Model_ContextCondition();
261  $this->ContextFactor = new Application_Model_ContextFactor();
262  $this->Microflow = new Application_Model_Microflow();
263  $this->MicroflowAction = new Application_Model_MicroflowAction();
264  $this->MicroflowActionAlert = new Application_Model_MicroflowActionAlert();
265  $this->MicroflowActionProperty = new Application_Model_MicroflowActionProperty();
266  $this->MicroflowActionResult = new Application_Model_MicroflowActionResult();
267  $this->MicroflowNextAction = new Application_Model_MicroflowNextAction();
268  try{
269  if(!isset($entity['IdUserAccount'])){
270  $resultId = 3212;
271  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
272  $resultCode=sprintf($r['Message'], "IdUserAccount");
273  $message=sprintf($r['Message'], "IdUserAccount");
274  $type=3;
275  }else{$data['IdUserAccount']=$entity['IdUserAccount'];}
276 
277  if(!isset($entity['UserAccountType'])){
278  $resultId = 3212;
279  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
280  $resultCode=sprintf($r['Message'], "UserAccountType");
281  $message=sprintf($r['Message'], "UserAccountType");
282  $type=3;
283  }else{$data['UserAccountType']=$entity['UserAccountType'];}
284  if($resultId==0){
285  if($data['UseraAccountType']!=0){
286  $resultId=3213;
287  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
288  $resultCode=$r['Message'];
289  $message=$r['Message'];
290  $type=3;
291  }
292  }
293  if($resultId==0){
294  $this->MicroflowNextAction->deleteMicroflowNextAction(array("IdMicroflowNextAction"=>0,
295  "IdNextActionMf"=>0,
296  "IdUserAccount"=>$data['IdUserAccount'],
297  "UserAccountType"=>0));
298  $this->MicroflowActionResult->deleteMicroflowActionResult(array("IdMicroflowActionResult"=>0,
299  "IdMicroflowAction"=>0,
300  "IdUserAccount"=>$data['IdUserAccount'],
301  "UserAccountType"=>0));
302  $this->MicroflowActionProperty->deleteMicroflowActionProperty(array("IdMicroflowActionProperty"=>0,
303  "IdMicroflowAction"=>0,
304  "IdUserAccount"=>$data['IdUserAccount'],
305  "UserAccountType"=>0));
306  $this->MicroflowActionAlert->deleteMicroflowActionAlert(array("IdMicroflowActionAlert"=>0,
307  "IdMicroflowAction"=>0,
308  "IdUserAccount"=>$data['IdUserAccount'],
309  "UserAccountType"=>0));
310  $this->MicroflowAction->deleteMicroflowAction(array("IdMicroflowAction"=>0,
311  "IdMicroflow"=>0,
312  "IdUserAccount"=>$data['IdUserAccount'],
313  "UserAccountType"=>0));
314  $this->Microflow->deleteMicroflow(array("IdMicroflow"=>0,
315  "IdUserAccount"=>$data['IdUserAccount'],
316  "UserAccountType"=>0));
317  $this->ContextFactor->deleteContextFactor(array("IdContextFactor"=>0,
318  "IdContextCondition"=>0,
319  "IdUserAccount"=>$data['IdUserAccount'],
320  "UserAccountType"=>0));
321  $this->ContextCondition->deleteContextCondition(array("IdContextCondition"=>0,
322  "IdContext"=>0,
323  "IdUserAccount"=>$data['IdUserAccount'],
324  "UserAccountType"=>0));
325  $this->Context->deleteContext(array("IdContext"=>0,
326  "IdUserAccount"=>$data['IdUserAccount'],
327  "UserAccountType"=>0));
328  $this->Channel->deleteChannel(array("IdChannel"=>0,
329  "IdChannelType="=>0,
330  "IdUserAccount"=>$data['IdUserAccount'],
331  "UserAccountType"=>0));
332  $this->AssetProperty->deleteAssetProperty(array("IdAssetProperty"=>0,
333  "IdAsset"=>0,
334  "IdUserAccount"=>$data['IdUserAccount'],
335  "UserAccountType"=>0));
336  $this->Asset->deleteAsset(array("IdAsset"=>0,
337  "IdUserAccount"=>$data['IdUserAccount'],
338  "UserAccountType"=>0));
339  $this->Address->deleteAddress(array("IdAddress"=>0,
340  "IdContact"=>0,
341  "IdUserAccount"=>$data['IdUserAccount'],
342  "UserAccountType"=>0));
343  $this->Phone->deletePhone(array("IdPhone"=>0,
344  "IdContact"=>0,
345  "IdUserAccount"=>$data['IdUserAccount'],
346  "UserAccountType"=>0));
347  $this->Email->deleteEmail(array("IdEmail"=>0,
348  "IdContact"=>0,
349  "IdUserAccount"=>$data['IdUserAccount'],
350  "UserAccountType"=>0));
351  $this->Contact->deleteContact(array("IdContact"=>0,
352  "IdUserAccount"=>$data['IdUserAccount'],
353  "UserAccountType"=>0));
354  $k=$this->delete(array("IdUserAccount=?",$data['IdUserAccount']));
355  $resultCode="Date sterse";
356  }
357  }
358  catch(Exception $e){
359  $resultCode=$e->getMessage();
360  $resultId=101;
361  $type=3;
362  }
363  $this->Log->save($page,$method,$result,$resultId.'=>'.$resultCode,$type);
364  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
365  return $response;
366  }
367  //==== End deleteUserAccount ======================================================================
368 
369  //==== Start cleanUserAccount =====================================================================
370  function cleanUserAccount()
371  {
372  $resultId = 0;
373  $page="UserAccount.php";
374  $method="cleanUserAccount";
375  $result=json_encode();
376  $message="";
377  $type="1";
378  $this->ErrorCode = new Application_Model_ErrorCode;
379  $this->Log = new Application_Model_Log;
380  $this->Contact = new Application_Model_Contact();
381  $this->SendMail = new Application_Model_SendMail();
382  try{
383  $resultCode ="Date curatate";
384  $cfg=Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();
385  $days=$cfg['global']['newUserAccount']['days'];
386 
387  $date = new Zend_Date();
388  $rows = $this->fetchAll($this->select()->setIntegrityCheck(false)
389  ->from("UserAccount","*")
390  ->where("Active=0")
391  ->where("LastLogInDate<?",$date->subDay($days)->toString('Y-MM-dd HH:mm:ss')));
392  foreach($rows as $row)
393  {
394  $this->Contact->deleteContact(array("IdContact"=>0, "IdUserAccount"=>$row->IdUserAccount,"UserAccountType"=>1));
395  $k=$this->delete(array("IdUserAccount=?",$row->IdUserAccount));
396  }
397  }
398  catch(Exception $e){
399  $resultCode=$e->getMessage();
400  $resultId=101;
401  $type=3;
402  }
403  $this->Log->save($page,$method,$result,$resultId.'=>'.$resultCode,$type);
404  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
405  return $response;
406  }
407  //==== End cleanUserAccount =======================================================================
408  //==== Start addEditUserAccount ===================================================================
435  function addEditUserAccount($entity) {
436  // function implementation
437  $resultId = 0;
438  $page="UserAccount.php";
439  $method="addEditUserAccount";
440  $result=json_encode($entity);
441  $message="";
442  $type="1";
443  $this->ErrorCode = new Application_Model_ErrorCode;
444  $this->Log = new Application_Model_Log;
445 
446  try{
447  $data=array();
448  if(!isset($entity['IdUserAccount'])){
449  $resultId = 3121;
450  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
451  $resultCode=sprintf($r['Message'], "IdUserAccount");
452  $message=sprintf($r['Message'], "IdUserAccount");
453  $type=3;
454  }else{$data['IdUserAccount']=$entity['IdUserAccount'];}
455 
456  if(!isset($entity['UserName'])){
457  if($resultId==0 && (int)$entity['IdUserAccount']==0){
458  $resultId = 3121;
459  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
460  $resultCode=sprintf($r['Message'], "UserName");
461  $message=sprintf($r['Message'], "UserName");
462  $type=3;
463  }
464  }else{$data['UserName']=$entity['UserName'];}
465 
466  if(!isset($entity['Password'])){
467  if($resultId==0 && (int)$entity['IdUserAccount']==0){
468  $resultId = 3121;
469  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
470  $resultCode=sprintf($r['Message'], "Password");
471  $message=sprintf($r['Message'], "Password");
472  $type=3;
473  }
474  }else{$data['Password']=$entity['Password'];}
475 
476  if(!isset($entity['RecoveryAnswer'])){
477  if($resultId==0 && (int)$entity['IdUserAccount']==0){
478  $resultId = 3121;
479  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
480  $resultCode=sprintf($r['Message'], "RecoveryAnswer");
481  $message=sprintf($r['Message'], "RecoveryAnswer");
482  $type=3;
483  }
484  }else{$data['RecoveryAnswer']=$entity['RecoveryAnswer'];}
485 
486  if(!isset($entity['RecoveryQuestion'])){
487  if($resultId==0 && (int)$entity['IdUserAccount']==0){
488  $resultId = 3121;
489  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
490  $resultCode=sprintf($r['Message'], "RecoveryQuestion");
491  $message=sprintf($r['Message'], "RecoveryQuestion");
492  $type=3;
493  }
494  }else{$data['RecoveryQuestion']=$entity['RecoveryQuestion'];}
495 
496  if(!isset($entity['Email'])){
497  if($resultId==0 && (int)$entity['IdUserAccount']==0){
498  $resultId = 3121;
499  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
500  $resultCode=sprintf($r['Message'], "Email");
501  $message=sprintf($r['Message'], "Email");
502  $type=3;
503  }
504  }else{$data['Email']=$entity['Email'];}
505 
506  if(!isset($entity['Active'])){
507  if($resultId==0 && (int)$entity['IdUserAccount']==0){
508  $resultId = 3121;
509  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
510  $resultCode=sprintf($r['Message'], "Active");
511  $message=sprintf($r['Message'], "Active");
512  $type=3;
513  }
514  }else{$data['Active']=$entity['Active'];}
515 
516  if(!isset($entity['LastLogInIp'])){
517  if($resultId==0 && (int)$entity['IdUserAccount']==0){
518  $resultId = 3121;
519  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
520  $resultCode=sprintf($r['Message'], "LastLogInIp");
521  $message=sprintf($r['Message'], "LastLogInIp");
522  $type=3;
523  }
524  }else{$data['LastLogInIp']=$entity['LastLogInIp'];}
525 
526  if(!isset($entity['LastLogInDate'])){
527  if($resultId==0 && (int)$entity['IdUserAccount']==0){
528  $resultId = 3121;
529  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
530  $resultCode=sprintf($r['Message'], "LastLogInDate");
531  $message=sprintf($r['Message'], "LastLogInDate");
532  $type=3;
533  }
534  }else{$data['LastLogInDate']=$entity['LastLogInDate'];}
535 
536  if(!isset($entity['Fail'])){
537  if($resultId==0 && (int)$entity['IdUserAccount']==0){
538  $resultId = 3121;
539  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
540  $resultCode=sprintf($r['Message'], "Fail");
541  $message=sprintf($r['Message'], "Fail");
542  $type=3;
543  }
544  }else{$data['Fail']=$entity['Fail'];}
545 
546  if(!isset($entity['IdContactMyself'])){
547  if($resultId==0 && (int)$entity['IdUserAccount']==0){
548  $resultId = 3121;
549  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
550  $resultCode=sprintf($r['Message'], "IdContactMyself");
551  $message=sprintf($r['Message'], "IdContactMyself");
552  $type=3;
553  }
554  }else{$data['IdContactMyself']=$entity['IdContactMyself'];}
555  if(!isset($entity['Language'])){
556  if($resultId==0 && (int)$entity['IdUserAccount']==0){
557  $resultId = 3121;
558  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
559  $resultCode=sprintf($r['Message'], "Language");
560  $message=sprintf($r['Message'], "Language");
561  $type=3;
562  }
563  }else{$data['Language']=$entity['Language'];}
564 
565  if(!isset($entity['UserAccountType'])){
566  if($resultId==0 && (int)$entity['IdUserAccount']==0){
567  $resultId = 3121;
568  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
569  $resultCode=sprintf($r['Message'], "UserAccountType");
570  $message=sprintf($r['Message'], "UserAccountType");
571  $type=3;
572  }
573  }else{$data['UserAccountType']=$entity['UserAccountType'];}
574  if(!isset($data)|| $data==null)
575  {
576  $resultId = 3121;
577  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
578  $resultCode=sprintf($r['Message'], "UserName,Password,RecoveryAnswer,RecoveryQuestion,Email,Active,LastLogInIp,LastLogInDate,Fail,IdContactMyself,UserAccountType, Language");
579  $message=sprintf($r['Message'], "UserName,Password,RecoveryAnswer,RecoveryQuestion,Email,Active,LastLogInIp,LastLogInDate,Fail,IdContactMyself,UserAccountType, Language");
580  $type=3;
581  }
582  if($resultId==0){
583 
584  $IdUserAccount=(int)$data['IdUserAccount'];
585 
586  if($IdUserAccount!=0){
587  $rows = $this->fetchRow($this->select()->setIntegrityCheck(false)
588  ->from("UserAccount","*")
589  ->where("IdUserAccount=?",$IdUserAccount)
590  );
591  if($rows==null){
592  $resultId = 3122;
593  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
594  $resultCode=$r['Message'];
595  $message=$r['Message'];
596  $type=3;
597  }
598  }
599  }
600  if($resultId==0){
601  if ($IdUserAccount==0){
602  $k=$this->insert($data);
603  $k=$this->getAdapter()->lastInsertId();
604  $resultCode=$k;
605  }else{
606  $this->update($data,$this->getAdapter()->quoteInto('IdUserAccount=?',$IdUserAccount));
607  $k=$IdUserAccount;
608  $resultCode=$k;
609 
610  }
611  }
612  }
613  catch(Exception $e){
614  $resultCode=$e->getMessage();
615  $resultId=101;
616  $type=3;
617  }
618  $this->Log->save($page,$method,$result,$resultId.'=>'.$resultCode,$type);
619  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
620  return $response;
621  }
622  //==== End addEditUserAccount =====================================================================
623 
624  //==== Start newUserAccount =======================================================================
648  function newUserAccount($entity){
649  $resultId = 0;
650  $page="UserAccount.php";
651  $method="addEditUserAccount";
652  $result=json_encode($entity);
653  $message="";
654  $type="1";
655  $this->ErrorCode = new Application_Model_ErrorCode;
656  $this->Log = new Application_Model_Log;
657  $this->Contact = new Application_Model_Contact();
658  $this->SendMail = new Application_Model_SendMail();
659 
660  try{
661  $cfg=Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();
662  $minutes=$cfg['global']['newUserAccount']['minutes'];
663  $inactive=$cfg['global']['newUserAccount']['inactive'];
664  $ageMin=$cfg['global']['newUserAccount']['ageMin'];
665  $ageMax=$cfg['global']['newUserAccount']['ageMax'];
666  $confirmationUrl=$cfg['global']['confirmationUrl'];
667  //echo $rootUrl;
668  //exit();
669  $data=array();
670  if(!isset($entity['UserName'])){
671  if($resultId==0){
672  $resultId = 3197;
673  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
674  $resultCode=sprintf($r['Message'], "UserName");
675  $message=sprintf($r['Message'], "UserName");
676  $type=3;
677  }
678  }else{
679  $validName = new Zend_Validate_StringLength(array('min' => 3, 'max' => 30));
680  if($validName->isValid($entity['UserName'])){
681  $data['UserName']=$entity['UserName'];
682  }else{
683  $resultId = 3199;
684  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
685  $resultCode=sprintf($r['Message'], "UserName");
686  $message=sprintf($r['Message'], "UserName");
687  $type=3;
688  }
689  }
690 
691  if(!isset($entity['Password'])){
692  if($resultId==0){
693  $resultId = 3197;
694  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
695  $resultCode=sprintf($r['Message'], "Password");
696  $message=sprintf($r['Message'], "Password");
697  $type=3;
698  }
699  }else{
700  $validName = new Zend_Validate_StringLength(array('min' => 3, 'max' => 30));
701  if($validName->isValid($entity['Password'])){
702  $data['Password']=$entity['Password'];
703  }else{
704  $resultId = 3199;
705  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
706  $resultCode=sprintf($r['Message'], "Password");
707  $message=sprintf($r['Message'], "Password");
708  $type=3;
709  }
710  }
711 
712 
713  if(!isset($entity['Email'])){
714  if($resultId==0){
715  $resultId = 3197;
716  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
717  $resultCode=sprintf($r['Message'], "Email");
718  $message=sprintf($r['Message'], "Email");
719  $type=3;
720  }
721  }else{
722  $validName = new Zend_Validate_EmailAddress();
723  if($validName->isValid($entity['Email'])){
724  $data['Email']=$entity['Email'];
725  }else{
726  $resultId = 3199;
727  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
728  $resultCode=sprintf($r['Message'], "Email");
729  $message=sprintf($r['Message'], "Email");
730  $type=3;
731  }
732  }
733 
734 
735  if(!isset($entity['IpAddress'])){
736  if($resultId==0){
737  $resultId = 3197;
738  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
739  $resultCode=sprintf($r['Message'], "IpAddress");
740  $message=sprintf($r['Message'], "IpAddress");
741  $type=3;
742  }
743  }else{
744  $validIP = new Zend_Validate_Hostname(Zend_Validate_Hostname::ALLOW_IP);
745  if($validIP->isValid($entity['IpAddress'])){
746  $data['IpAddress']=$entity['IpAddress'];
747  }else{
748  $resultId = 3199;
749  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
750  $resultCode=sprintf($r['Message'], "IpAddress");
751  $message=sprintf($r['Message'], "IpAddress");
752  $type=3;
753  }
754  }
755  if($resultId==0){
756  $IpAddress=$data['IpAddress'];
757  $date = new Zend_Date();
758  $count = $this->fetchRow($this->select()->setIntegrityCheck(false)
759  ->from("UserAccount",array("total"=>"COUNT(IdUserAccount)"))
760  ->where("LastLogInIp=?",$IpAddress)
761  ->where("Active=0")
762  ->where("LastLogInDate>?",$date->subMinute($minutes)->toString('Y-MM-dd HH:mm:ss'))
763  ->where("LastLogInDate<?",$date->addMinute($minutes)->toString('Y-MM-dd HH:mm:ss'))
764  );
765  if($count->total>$inactive){
766  $resultId = 3198;
767  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
768  $resultCode=$r['Message'];
769  $message=$r['Message'];
770  $type=3;
771  }
772  }
773 
774 
775  if($resultId==0){
776  $count = $this->fetchRow($this->select()->setIntegrityCheck(false)
777  ->from("UserAccount",array("total"=>"COUNT(IdUserAccount)"))
778  ->where("Email=?", $data['Email'])
779 
780  );
781  if($count->total>0){
782  $resultId = 3201;
783  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
784  $resultCode=$r['Message'];
785  $message=$r['Message'];
786  $type=3;
787  }
788  }
789 
790  if($resultId==0){
791  $count = $this->fetchRow($this->select()->setIntegrityCheck(false)
792  ->from("UserAccount",array("total"=>"COUNT(IdUserAccount)"))
793  ->where("UserName=?", $data['UserName'])
794 
795  );
796  if($count->total>0){
797  $resultId = 3202;
798  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
799  $resultCode=$r['Message'];
800  $message=$r['Message'];
801  $type=3;
802  }
803  }
804  if($resultId==0){
805  $k=$this->insert(array("UserName"=>$data['UserName'],
806  "Password"=>$data['Password'],
807  "Email"=>$data['Email'],
808  "LastLogInIp"=>$data['IpAddress'],
809  "LastLogInDate"=> Zend_Date::now()->toString('Y-MM-dd HH:mm:ss'),
810  "Fail"=>"0",
811  "IdContactMyself"=>"0",
812  "UserAccountType"=>"1",
813  "Guid"=>md5(uniqid($data['UserName'],true))));
814  if($k>0){
815  $userAccountId = $this->getAdapter()->lastInsertId();
816 
817  }else{
818  $resultId=1;
819  }
820  }
821  if($resultId==0){
822  $insContact=$this->Contact->addEditContact(array("IdContact"=>"0",
823  "Name"=>$data['Name'],
824  "Surname"=>$data['SurName'],
825  "Birthday"=>$data['Birthday'],
826  "Gender"=>$data['Gender'],
827  "Company"=>"",
828  "IdUserAccount"=>$userAccountId,
829  "UserAccountType"=>"1"));
830  if($insContact['resultId']=="0"){
831  $myContact=$insContact['resultCode'];
832  $this->update(array("IdContactMyself"=>$myContact ),$this->getAdapter()->quoteInto('IdUserAccount=?',$userAccountId));
833  }else{
834  $resultId=$insContact['resultId'];
835  $resultCode=$insContact['resultCode'];
836  }
837  }
838  if($resultId==0){
839  $rs = $this->fetchRow($this->select()->setIntegrityCheck(false)
840  ->from("UserAccount","*")
841  ->where("IdUserAccount=$userAccountId")
842  );
843  if($rs!=null){
844  $html="<html><body><a href='".str_replace("%token%", $rs->Guid, $confirmationUrl)."'>Activeaza</a></body></html>";
845  $toEmail=$data['Email'];
846  $toName=$data['Name']." ".$data['SurName'];
847  $subject="Bine ai venit in N4M";
848  $mail=$this->SendMail->send($html, $toEmail, $toName, $subject);
849  //echo "<pre>".print_r($mail)."</pre>";
850  if ($mail['resultId']!="0"){
851  $resultId = 101;
852  $resultCode = $mail['resultCode'];
853  }
854  }else{
855  $resultId=101;
856  $resultCode="Error";
857  }
858  }
859  if($resultId==0){
860  $resultCode="Account created";
861  }
862  }
863  catch(Exception $e){
864  $resultCode=$e->getMessage();
865  $resultId=101;
866  $type=3;
867  }
868  $this->Log->save($page,$method,$result,$resultId.'=>'.$resultCode,$type);
869  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
870  return $response;
871  }
872 
873  //==== End newUserAccount =========================================================================
874 
875  //==== Start activateUserAccount ==================================================================
884  function activateUserAccount($entity){
885  $resultId = 0;
886  $page="UserAccount.php";
887  $method="addEditUserAccount";
888  $result=json_encode($entity);
889  $message="";
890  $type="1";
891  $this->ErrorCode = new Application_Model_ErrorCode;
892  $this->Log = new Application_Model_Log;
893  $this->SendMail = new Application_Model_SendMail();
894  try{
895  $cfg=Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOptions();
896  $minutes=$cfg['global']['newUserAccount']['timeToActivate'];
897 
898  $count = $this->fetchRow($this->select()->setIntegrityCheck(false)
899  ->from("UserAccount","*")
900  ->where("Guid=?",$entity['key'])
901  ->where("Active=0")
902  );
903  if($count==null){
904  $resultId = 3203;
905  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
906  $resultCode=$r['Message'];
907  $message=$r['Message'];
908  $type=3;
909  }else{
910 
911  $dataNow= Zend_Date::now();
912  $createdDate= new Zend_Date();
913  $createdDate->set($count->LastLogInDate);
914  $difference=$dataNow->sub($createdDate);
915  $measure = new Zend_Measure_Time($difference->toValue(), Zend_Measure_Time::SECOND);
916  $measure->convertTo(Zend_Measure_Time::MINUTE);
917  $time=$measure->getValue();
918  if($time<$minutes){
919  $k=$this->update(array("Active"=>"1",
920  "LastLogInIp"=>$entity['ip'],
921  "LastLogInDate"=> Zend_Date::now()->toString('Y-MM-dd HH:mm:ss') ),$this->getAdapter()->quoteInto('Guid=?',$entity['key']));
922  if($k>0){
923  $resultCode="Cont activat";
924  }else{
925  $resultCode="Error";
926  $resultId=101;
927  }
928  }else{
929  $resultId = 3204;
930  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
931  $resultCode=$r['Message'];
932  $message=$r['Message'];
933  $type=3;
934  }
935  }
936  }
937  catch(Exception $e){
938  $resultCode=$e->getMessage();
939  $resultId=101;
940  $type=3;
941  }
942  $this->Log->save($page,$method,$result,$resultId.'=>'.$resultCode,$type);
943  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
944  return $response;
945  }
946  //==== End activateUserAccount ====================================================================
947 
948  //==== Start checkEmail ===========================================================================
958  function checkEmail($entity)
959  {
960  $resultId = 0;
961  $page="UserAccount.php";
962  $method="checkEmail";
963  $result=json_encode($entity);
964  $message="";
965  $type="1";
966  $this->ErrorCode = new Application_Model_ErrorCode;
967  $this->Log = new Application_Model_Log;
968  try{
969  $resultCode = "Adresa de email nu este folosita";
970  if(!isset($entity['Email'])){
971  if($resultId==0){
972  $resultId = 3214;
973  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
974  $resultCode=sprintf($r['Message'], "Email");
975  $message=sprintf($r['Message'], "Email");
976  $type=3;
977  }
978  }else{
979  $validName = new Zend_Validate_EmailAddress();
980  if($validName->isValid($entity['Email'])){
981  $data['Email']=$entity['Email'];
982  }else{
983  $resultId = 3215;
984  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
985  $resultCode=sprintf($r['Message'], "Email");
986  $message=sprintf($r['Message'], "Email");
987  $type=3;
988  }
989  }
990  if($resultId==0){
991  $rs = $this->fetchRow($this->select()->setIntegrityCheck(false)
992  ->from("UserAccount","*")
993  ->where("Email=?", $data['Email'])
994  );
995  if($rs!=null){
996  $resultId = 3216;
997  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
998  $resultCode=sprintf($r['Message'], $data['Email']);
999  $message=sprintf($r['Message'], $data['Email']);
1000  $type=3;
1001  }
1002  }
1003 
1004  }
1005  catch(Exception $e){
1006  $resultCode=$e->getMessage();
1007  $resultId=101;
1008  $type=3;
1009  }
1010  $this->Log->save($page,$method,$result,$resultId.'=>'.$resultCode,$type);
1011  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
1012  return $response;
1013  }
1014  //==== End checkEmail =============================================================================
1015 
1016  //==== Start checkUserName ========================================================================
1026  function checkUserName($entity)
1027  {
1028  $resultId = 0;
1029  $page="UserAccount.php";
1030  $method="checkUserName";
1031  $result=json_encode($entity);
1032  $message="";
1033  $type="1";
1034  $this->ErrorCode = new Application_Model_ErrorCode;
1035  $this->Log = new Application_Model_Log;
1036  try{
1037  $resultCode = "UserName-ul nu este folosit";
1038  if(!isset($entity['UserName'])){
1039  if($resultId==0){
1040  $resultId = 3217;
1041  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
1042  $resultCode=sprintf($r['Message'], "UserName");
1043  $message=sprintf($r['Message'], "UserName");
1044  $type=3;
1045  }
1046  }else{
1047  $validName = new Zend_Validate_StringLength(array('min' => 3, 'max' => 30));
1048  if($validName->isValid($entity['UserName'])){
1049  $data['UserName']=$entity['UserName'];
1050  }else{
1051  $resultId = 3218;
1052  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
1053  $resultCode=sprintf($r['Message'], "UserName");
1054  $message=sprintf($r['Message'], "UserName");
1055  $type=3;
1056  }
1057  }
1058  if($resultId==0){
1059  $rs = $this->fetchRow($this->select()->setIntegrityCheck(false)
1060  ->from("UserAccount","*")
1061  ->where("UserName=?", $data['UserName'])
1062  );
1063  if($rs!=null){
1064  $resultId = 3219;
1065  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
1066  $resultCode=sprintf($r['Message'], $data['UserName']);
1067  $message=sprintf($r['Message'], $data['UserName']);
1068  $type=3;
1069  }
1070  }
1071 
1072  }
1073  catch(Exception $e){
1074  $resultCode=$e->getMessage();
1075  $resultId=101;
1076  $type=3;
1077  }
1078  $this->Log->save($page,$method,$result,$resultId.'=>'.$resultCode,$type);
1079  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
1080  return $response;
1081  }
1082  //==== End checkUserName ==========================================================================
1083 
1084  }