Next4Me
 All Classes Files Functions Variables
Contact.php
Go to the documentation of this file.
1 <?php
18 class Application_Model_Contact extends Zend_Db_Table_Abstract {
19  //put your code here
20  protected $_name="Contact";
21  protected $_primary="IdContact";
22 
23  //==== Start addEditContact =========================================================================
40  function addEditContact($entity) {
41  // function implementation
42  $resultId = 0;
43  $page="Contact.php";
44  $method="addEditContact";
45  $result=json_encode($entity);
46  $message="";
47  $type="1";
48  $this->ErrorCode = new Application_Model_ErrorCode;
49  $this->Log = new Application_Model_Log;
50 
51  try{
52  $data=array();
53  if(!isset($entity['IdContact'])){
54  $resultId = 3109;
55  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
56  $resultCode=sprintf($r['Message'], "IdContact");
57  $message=sprintf($r['Message'], "IdContact");
58  $type=3;
59  }else{$data['IdContact']=$entity['IdContact'];}
60 
61  if(!isset($entity['Name'])){
62  if($resultId==0 && (int)$entity['IdContact']==0){
63  $resultId = 3109;
64  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
65  $resultCode=sprintf($r['Message'], "Name");
66  $message=sprintf($r['Message'], "Name");
67  $type=3;
68  }
69  }else{$data['Name']=$entity['Name'];}
70 
71  if(!isset($entity['Surname'])){
72  if($resultId==0 && (int)$entity['IdContact']==0){
73  $resultId = 3109;
74  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
75  $resultCode=sprintf($r['Message'], "Surname");
76  $message=sprintf($r['Message'], "Surname");
77  $type=3;
78  }
79  }else{$data['Surname']=$entity['Surname'];}
80 
81  if(!isset($entity['Company'])){
82  if($resultId==0 && (int)$entity['IdContact']==0){
83  $resultId = 3109;
84  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
85  $resultCode=sprintf($r['Message'], "Company");
86  $message=sprintf($r['Message'], "Company");
87  $type=3;
88  }
89  }else{$data['Company']=$entity['Company'];}
90 
91  if(!isset($entity['Birthday'])){
92  if($resultId==0 && (int)$entity['IdContact']==0){
93  $resultId = 3109;
94  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
95  $resultCode=sprintf($r['Message'], "Birthday");
96  $message=sprintf($r['Message'], "Birthday");
97  $type=3;
98  }
99  }else{$data['Birthday']=$entity['Birthday'];}
100 
101  if(!isset($entity['Gender'])){
102  if($resultId==0 && (int)$entity['IdContact']==0){
103  $resultId = 3109;
104  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
105  $resultCode=sprintf($r['Message'], "Gender");
106  $message=sprintf($r['Message'], "Gender");
107  $type=3;
108  }
109  }else{$data['Gender']=$entity['Gender'];}
110 
111  if(!isset($entity['IdUserAccount'])){
112  $resultId = 3109;
113  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
114  $resultCode=sprintf($r['Message'], "IdUserAccount");
115  $message=sprintf($r['Message'], "IdUserAccount");
116  $type=3;
117  }else{$data['IdUserAccount']=$entity['IdUserAccount'];}
118 
119  if(!isset($entity['UserAccountType'])){
120  $resultId = 3109;
121  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
122  $resultCode=sprintf($r['Message'], "UserAccountType");
123  $message=sprintf($r['Message'], "UserAccountType");
124  $type=3;
125  }else{$data['UserAccountType']=$entity['UserAccountType'];}
126 
127  if(!isset($data)|| $data==null)
128  {
129  $resultId = 3109;
130  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
131  $resultCode=sprintf($r['Message'], "Name,Surname,Company,Birthday,Gender,IdUserAccount");
132  $message=sprintf($r['Message'], "Name,Surname,Company,Birthday,Gender,IdUserAccount");
133  $type=3;
134  }
135  if($resultId==0){
136  $IdContact=(int)$data['IdContact'];
137 
138  if($data['UserAccountType']=="0"){
139  $IdUserAccount=0;
140  }else{
141  $IdUserAccount=(int)$data['IdUserAccount'];
142  }
143 
144  if($IdContact!="0"){
145  $rows = $this->fetchRow($this->select()->setIntegrityCheck(false)
146  ->from("Contact","*")
147  ->where("IdContact=?",$entity['IdContact'])
148  ->where("IdUserAccount=$IdUserAccount OR 0=$IdUserAccount")
149  );
150  if($rows==null){
151  $resultId = 3110;
152  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
153  $resultCode=$r['Message'];
154  $message=$r['Message'];
155  $type=3;
156  }}
157  }
158  if($resultId==0){
159  unset($data['UserAccountType']);
160  if ($IdContact=="0"){
161  $k=$this->insert($data);
162  $k=$this->getAdapter()->lastInsertId();
163  $resultCode=$k;
164  }else{
165  unset($data['IdUserAccount']);
166  $this->update($data,$this->getAdapter()->quoteInto('IdContact=?',$IdContact));
167  $k=$entity['IdContact'];
168  $resultCode=$k;
169 
170  }
171  }
172  }
173  catch(Exception $e){
174  $resultCode=$e->getMessage();
175  $resultId=101;
176  $type=3;
177  }
178  $this->Log->save($page,$method,$result,$resultId.'=>'.$resultCode,$type);
179  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
180  return $response;
181  }
182  //============ End addEditContact ==============================================================
183 
184 
185  //==== Start deleteContact =========================================================================
196  function deleteContact($entity) {
197  // function implementation
198  $resultId = 0;
199  $page="Contact.php";
200  $method="deleteContact";
201  $result=json_encode($entity);
202  $message="";
203  $type="1";
204  $this->ErrorCode = new Application_Model_ErrorCode;
205  $this->Log = new Application_Model_Log;
206 
207  try{
208  $IdContact=0;
209  if(!isset($entity['IdContact'])){
210  $resultId = 3157;
211  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
212  $resultCode=sprintf($r['Message'], "IdContact");
213  $message=sprintf($r['Message'], "IdContact");
214  $type=3;
215  }else{$data['IdContact']=$entity['IdContact'];}
216 
217  if(!isset($entity['IdUserAccount'])){
218  $resultId = 3157;
219  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
220  $resultCode=sprintf($r['Message'], "IdUserAccount");
221  $message=sprintf($r['Message'], "IdUserAccount");
222  $type=3;
223  }else{$data['IdUserAccount']=$entity['IdUserAccount'];}
224 
225  if(!isset($entity['UserAccountType'])){
226  $resultId = 3157;
227  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
228  $resultCode=sprintf($r['Message'], "UserAccountType");
229  $message=sprintf($r['Message'], "UserAccountType");
230  $type=3;
231  }else{$data['UserAccountType']=$entity['UserAccountType'];}
232 
233  if(!isset($data)|| $data==null)
234  {
235  $resultId = 3157;
236  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
237  $resultCode=sprintf($r['Message'], "IdContact,UserAccountType,IdUserAccount");
238  $message=sprintf($r['Message'], "IdContact,UserAccountType,IdUserAccount");
239  $type=3;
240  }
241 
242  if($resultId==0)
243  {
244  $IdContact=(int)$data['IdContact'];
245  $IdUserAccount=(int)$data['IdUserAccount'];
246 
247 
248  $rows = $this->fetchAll($this->select()->setIntegrityCheck(false)
249  ->from("Contact","*")
250  ->where("IdContact=$IdContact OR 0=$IdContact")
251  ->where("IdUserAccount=$IdUserAccount")
252  );
253  if(count($rows)==0){
254  $resultId = 3111;
255  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
256  $resultCode=$r['Message'];
257  $message=$r['Message'];
258  $type=3;
259  }else{
260  foreach ($rows as $row){
261  if($resultId==0){
262  $chld=$this->fetchRow($this->select()->setIntegrityCheck(false)
263  ->from("Address","*")
264  ->where("IdContact=?",$row->IdContact)
265  );
266  if($chld!=null){
267  $resultId = 4013;
268  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
269  $resultCode=$r['Message'];
270  $message=$r['Message'];
271  $type=3;
272  }
273  if($resultId==0){
274  $chld=$this->fetchRow($this->select()->setIntegrityCheck(false)
275  ->from("Email","*")
276  ->where("IdContact=?",$row->IdContact)
277  );
278  if($chld!=null){
279  $resultId = 4013;
280  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
281  $resultCode=$r['Message'];
282  $message=$r['Message'];
283  $type=3;
284  }
285  }
286  if($resultId==0){
287  $chld=$this->fetchRow($this->select()->setIntegrityCheck(false)
288  ->from("Phone","*")
289  ->where("IdContact=?",$row->IdContact)
290  );
291  if($chld!=null){
292  $resultId = 4013;
293  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
294  $resultCode=$r['Message'];
295  $message=$r['Message'];
296  $type=3;
297  }
298  }
299  }
300  }
301  }
302  }
303 
304  if ($resultId==0){
305 
306  $k=$this->delete(array("IdUserAccount=$IdUserAccount",
307  "IdContact=$IdContact OR 0=$IdContact"));
308  $resultCode="Date sterse";
309  }
310 
311  }
312  catch(Exception $e){
313  $resultCode=$e->getMessage();
314  $resultId=101;
315  $type=3;
316  }
317  $this->Log->save($page,$method,$result,$resultId.'=>'.$resultCode,$type);
318  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
319  return $response;
320  }
321  //============ End deleteContact ==============================================================
322 
323 
324  //==== Start getContact =========================================================================
342  function getContact($entity) {
343  // function implementation
344  $resultId = 0;
345  $page="Contact.php";
346  $method="getContact";
347  $result=json_encode($entity);
348  $message="";
349  $type="1";
350  $this->ErrorCode = new Application_Model_ErrorCode;
351  $this->Log = new Application_Model_Log;
352  $this->Paging = new Application_Model_Paging;
353 
354  try{
355  $pg=$this->Paging->getPageDefault('Contact');
356 
357  $data=array();
358  if(!isset($entity['IdContact']))
359  {
360  $resultId = 3130;
361  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
362  $resultCode=sprintf($r['Message'], "IdContact");
363  $message=sprintf($r['Message'], "IdContact");
364  $type=3;
365  }else{$data['IdContact']=$entity['IdContact'];}
366 
367  if(!isset($entity['IdUserAccount'])){
368  $resultId = 3130;
369  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
370  $resultCode=sprintf($r['Message'], "IdUserAccount");
371  $message=sprintf($r['Message'], "IdUserAccount");
372  $type=3;
373  }else{$data['IdUserAccount']=$entity['IdUserAccount'];}
374 
375  if(!isset($entity['UserAccountType'])){
376  $resultId = 3130;
377  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
378  $resultCode=sprintf($r['Message'], "UserAccountType");
379  $message=sprintf($r['Message'], "UserAccountType");
380  $type=3;
381  }else{$data['UserAccountType']=$entity['UserAccountType'];}
382 
383  if(!isset($entity['pageSize'])){
384  $_pageSize=$pg['pageSize'];
385  }else{
386  $_pageSize=$entity['pageSize'];
387  }
388 
389  if(!isset($entity['pageNo'])){
390  $_pageNo=$pg['pageNo'];
391  }else{$_pageNo=$entity['pageNo'];}
392 
393  if(!isset($entity['orderColumn'])){
394  $_orderColumn=$pg['orderColumn'];
395  }else{$_orderColumn=$entity['orderColumn'];}
396 
397  if(!isset($entity['orderType'])){
398  $_orderType=$pg['orderType'];
399  }else{
400  switch($entity['orderType'])
401  {
402  case 'ASC':
403  $_orderType="ASC";
404  break;
405  case 'DESC':
406  $_orderType="DESC";
407  break;
408  default:
409  $_orderType="DESC";
410  break;
411  }
412  }
413 
414  if(!isset($entity['orderColumn'])){
415  $_orderColumn=$pg['orderColumn'];
416  }else{
417  switch($entity['orderColumn'])
418  {
419  case 'IdContact':
420  $_orderColumn="IdContact";
421  break;
422  case 'Name':
423  $_orderColumn="Name";
424  break;
425  case 'Surname':
426  $_orderColumn="Surname";
427  break;
428  case 'Company':
429  $_orderColumn="Company";
430  break;
431  case 'Birthday':
432  $_orderColumn="Birthday";
433  break;
434  case 'Gender':
435  $_orderColumn="Gender";
436  break;
437  case 'IdUserAccount':
438  $_orderColumn="IdUserAccount";
439  break;
440  default:
441  $_orderColumn=$orderColumn;
442  break;
443  }
444 
445  }
446  if($resultId==0){
447  //if($data['UserAccountType']=="0"){
448  // $IdUserAccount=0;
449  //}else{
450  $IdUserAccount=(int)$data['IdUserAccount'];
451  //}
452  $IdContact=(int)$data['IdContact'];
453  //echo $IdUserAccount."->asta e useru<br>";
454 
455  $count = $this->fetchRow($this->select()->setIntegrityCheck(false)
456  ->from("Contact",array("total"=>"COUNT(IdContact)"))
457  ->where("IdUserAccount=$IdUserAccount OR 0=$IdUserAccount")
458  ->where("IdContact=$IdContact OR 0=$IdContact")
459  );
460 
461  if($count->total==0){
462  $resultId = 3112;
463  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
464  $resultCode=$r['Message'];
465  $message=$r['Message'];
466  $type=3;
467  }else{
468 
469  $offset=$this->Paging->getOffset($count->total, $_pageSize, $_pageNo);
470 
471  $rows = $this->fetchAll($this->select()->setIntegrityCheck(false)
472  ->from("Contact","*")
473  ->where("IdUserAccount=$IdUserAccount OR 0=$IdUserAccount")
474  ->where("IdContact=$IdContact OR 0=$IdContact")
475  ->limit($_pageSize, $offset['start'])
476  ->order($_orderColumn." ".$_orderType)
477  );
478 
479  foreach($rows as $row){
480  $resultEntity[]=array("IdContact"=>$row->IdContact,
481  "Name"=>$row->Name,
482  "Surname"=>$row->Surname,
483  "Company"=>$row->Company,
484  "Birthday"=>$row->Birthday,
485  "Gender"=>$row->Gender,
486  "IdUserAccount"=>$row->IdUserAccount);
487  }
488 
489  $resultCode=array("pageNo"=>$_pageNo,
490  "pageSize"=>$_pageSize,
491  "pageCount"=>$offset['maxPage'],
492  "orderColumn"=>$_orderColumn,
493  "orderType"=>$_orderType,"entity"=>$resultEntity) ;
494 
495  }
496  }
497  }
498  catch(Exception $e){
499  $resultCode=$e->getMessage();
500  $resultId=101;
501  $type=3;
502  }
503  $this->Log->save($page,$method,$result,$resultId.'=>'.json_encode($resultCode),$type);
504  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
505  return $response;
506  }
507  //============ End getContact ==============================================================
508 }