Next4Me
 All Classes Files Functions Variables
Session.php
Go to the documentation of this file.
1 <?php
11 class Application_Model_Session extends Zend_Db_Table_Abstract{
12 
13  protected $_name="ControlSession";
14  protected $_primary="IdControlSession";
15  //==== Start doInsert =========================================================================
23  function doInsert($idUserAccount, $token, $chanel){
24  $data = array(
25  'LastActionTime' => date('Y-m-d H:i:s',time()),
26  'Token' => $token,
27  'IdUserAccount' => $idUserAccount,
28  'Device' => $chanel
29  );
30 
31  $this->insert($data);
32  }
33  //==== End doInsert =========================================================================
34  //
35  //==== Start checkSession =========================================================================
42  function checkSession($token){
43  try{
44 
45  $sql = $this->fetchRow($this->select()->setIntegrityCheck(false)
46  ->from("ControlSession","*")
47  ->where("Token=?",$token)
48  // ->where("Unix_Timestamp(LastActionTime)>=?",time()-60*60*2)
49  );
50  if(isset($sql))
51  {
52  $s= $this->fetchRow($this->select()->setIntegrityCheck(false)
53  ->from("UserAccount","*")
54  ->where("IdUserAccount=?",$sql->IdUserAccount)
55  // ->where("Unix_Timestamp(LastActionTime)>=?",time()-60*60*2)
56  );
57  $response=array("IdUserAccount"=>$sql->IdUserAccount,
58  "Token"=>$sql->Token,
59  "UserAccountType"=>$s->UserAccountType,
60  "LastActionTime"=>$sql->LastActionTime,
61  "IdControlSession"=>$sql->IdControlSession,
62  "Lat"=>$sql->Lat,
63  "Long"=>$sql->Long,
64  "Device"=>$sql->Device,
65  "Activity"=>$sql->Activity,
66  "Speed"=>$sql->Speed);
67  }else{
68  $response=array("IdUserAccount"=>-1,"Token"=>"","UserAccountType"=>"","mesaj"=>"Token invalid");
69  }
70  //$select=$this->select()->where("Token=?",$token) ->where("Unix_Timestamp(LastActionTime)>=?",time()-60*60*2);
71  //$sql = $this->fetchRow($select);
72  //var_dump($token);
73  //var_dump($sql);
74  }
75  catch(Exception $e){
76  $resultCode=$e->getMessage();
77  $resultId=-101;
78  $type=3;
79  $response=array("IdUserAccount"=>$resultId,"mesaj"=>$resultCode);
80  }
81  //$this->Log->save($page,$method,$result,$resultId.'=>'.$resultCode,$type);
82 
83  return $response;
84 
85  }
86  //==== End checkSession =========================================================================
87  //
88  //==== Start updateSession =========================================================================
95  function updateSession($token){
96 
97  $data = array(
98  'LastActionTime' => date('Y-m-d H:i:s',time()),
99  );
100  $where = $this->getAdapter()->quoteInto('Token=?',$token);
101 
102  $this->update($data, $where);
103  }
104  //==== End updateSession =========================================================================
105  //
106  //==== Start deleteSession =========================================================================
113  function deleteSession($token){
114 
115  $res=$this->delete($this->getAdapter()->quoteInto('Token=?',$token));
116 
117  if($res>0){
118  $return=array("resultId"=>0, "resultCode"=>"Sesiunea a fost incheiata.");
119  }else{
120  $return=array("resultId"=>$res, "resultCode"=>"Sesiunea nu a fost gasita.");
121  }
122  return $return;
123  }
124  //==== End deleteSession =========================================================================
125 
126  //==== Start updateAtemporal =====================================================================
137  function updateAtemporal($entity)
138  {
139  $resultId = 0;
140  $page="Session.php";
141  $method="updateAtemporal";
142  $result=json_encode($entity);
143  $message="";
144  $type="1";
145  $this->ErrorCode = new Application_Model_ErrorCode;
146  $this->Log = new Application_Model_Log;
147  $this->ControlSessionHistory = new Application_Model_ControlSessionHistory;
148  try{
149  $data=array();
150  if(!isset($entity['Token'])){
151  $resultId = 3104;
152  $r=$this->ErrorCode->getErrorCode($resultId,Zend_Registry::get('language'));
153  $resultCode=sprintf($r['Message'], "Token");
154  $message=sprintf($r['Message'], "Token");
155  $type=3;
156  }else{
157  $data['Token']=$entity['Token'];
158  }
159  if(isset($entity['Lat'])){
160  $data['Lat']=$entity['Lat'];
161 
162  }
163  if(isset($entity['Long'])){
164  $data['Long']=$entity['Long'];
165  }
166  if(isset($entity['Device'])){
167  $data['Device']=$entity['Device'];
168  }
169  if(isset($entity['Activity'])){
170  $data['Activity']=$entity['Activity'];
171  }
172  if(isset($entity['Speed'])){
173  $data['Speed']=$entity['Speed'];
174  }
175  if($resultId==0){
176  $lastData=$this->checkSession($data['Token']);
177  //echo "<pre>"; print_r($lastData); echo "</pre>";
178  $this->ControlSessionHistory->addControlSessionHistory(array("IdControlSession" =>$lastData['IdControlSession'],
179  "LastActionTime" =>$lastData['LastActionTime'],
180  "Token" =>$lastData['Token'],
181  "IdUserAccount" =>$lastData['IdUserAccount'],
182  "Lat" =>$lastData['Lat'],
183  "Long" =>$lastData['Long'],
184  "Device" =>$lastData['Device'],
185  "Activity" =>$lastData['Activity'],
186  "Speed" =>$lastData['Speed']));
187  $k=$this->update($data,$this->getAdapter()->quoteInto('Token=?',$data['Token']));
188 
189  $resultCode=$k;
190  }
191  }
192  catch(Exception $e){
193  $resultCode=$e->getMessage();
194  $resultId=101;
195  $type=3;
196  }
197  $this->Log->save($page,$method,$result,$resultId.'=>'.$resultCode,$type);
198  $response=array("resultId"=>$resultId,"resultCode"=>$resultCode);
199  return $response;
200 
201  }
202  //==== End updateAtemporal =======================================================================
203 }