PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/estadisticas/estadisticas/public/xStreamOld/application/models/catalogs/UserLogCatalog.php

https://bitbucket.org/xsngroup/estadisticas
PHP | 370 lines | 301 code | 11 blank | 58 comment | 5 complexity | 081d2e1f4077e3cdaa4e3ece33aad0a6 MD5 | raw file
  1. <?php
  2. /**
  3. * Bender Modeler
  4. *
  5. * Our Simple Models
  6. *
  7. * @category lib
  8. * @package lib_models
  9. * @copyright Copyright (c) 2008-2010 Bender Modeler (http://www.ctrl-zetta.com/#code)
  10. * @author <zetta> <chentepixtol>, $LastChangedBy$
  11. * @version 1.0.0 SVN: $Id$
  12. */
  13. /**
  14. * Dependences
  15. */
  16. require_once "lib/db/Catalog.php";
  17. require_once "application/models/beans/UserLog.php";
  18. require_once "application/models/exceptions/UserLogException.php";
  19. require_once "application/models/collections/UserLogCollection.php";
  20. require_once "application/models/factories/UserLogFactory.php";
  21. /**
  22. * Singleton UserLogCatalog Class
  23. *
  24. * @category lib
  25. * @package lib_models
  26. * @subpackage lib_models_catalogs
  27. * @copyright Copyright (c) 2008-2010 Bender Modeler (http://www.ctrl-zetta.com/#code)
  28. * @copyright This File as been proudly generated by Bender (http://code.google.com/p/bender-modeler/). <chentepixtol> <zetta>
  29. * @author zetta & chentepixtol
  30. * @version 1.0.0 SVN: $Revision$
  31. */
  32. class UserLogCatalog extends Catalog
  33. {
  34. /**
  35. * Singleton Instance
  36. * @var UserLogCatalog
  37. */
  38. static protected $instance = null;
  39. /**
  40. * Método para obtener la instancia del catálogo
  41. * @return UserLogCatalog
  42. */
  43. public static function getInstance()
  44. {
  45. if (!isset(self::$instance))
  46. {
  47. self::$instance = new self();
  48. }
  49. return self::$instance;
  50. }
  51. /**
  52. * Constructor de la clase UserLogCatalog
  53. * @return UserLogCatalog
  54. */
  55. protected function UserLogCatalog()
  56. {
  57. parent::Catalog();
  58. }
  59. /**
  60. * Metodo para agregar un UserLog a la base de datos
  61. * @param UserLog $userLog Objeto UserLog
  62. */
  63. public function create($userLog)
  64. {
  65. if(!($userLog instanceof UserLog))
  66. throw new UserLogException("passed parameter isn't a UserLog instance");
  67. try
  68. {
  69. $data = array(
  70. 'id_user' => $userLog->getIdUser(),
  71. 'event_type' => $userLog->getEventType(),
  72. 'ip' => $userLog->getIp(),
  73. 'id_responsible' => $userLog->getIdResponsible(),
  74. 'timestamp' => $userLog->getTimestamp(),
  75. 'note' => $userLog->getTimestamp(),
  76. );
  77. $data = array_filter($data, 'Catalog::notNull');
  78. $this->db->insert(UserLog::TABLENAME, $data);
  79. $userLog->setIdUserLog($this->db->lastInsertId());
  80. }
  81. catch(Exception $e)
  82. {
  83. throw new UserLogException("The UserLog can't be saved \n" . $e->getMessage());
  84. }
  85. }
  86. /**
  87. * Metodo para Obtener los datos de un objeto por su llave primaria
  88. * @param int $idUserLog
  89. * @return UserLog|null
  90. */
  91. public function getById($idUserLog)
  92. {
  93. try
  94. {
  95. $criteria = new Criteria();
  96. $criteria->add(UserLog::ID_USER_LOG, $idUserLog, Criteria::EQUAL);
  97. $newUserLog = $this->getByCriteria($criteria)->getOne();
  98. }
  99. catch(Exception $e)
  100. {
  101. throw new UserLogException("Can't obtain the UserLog \n" . $e->getMessage());
  102. }
  103. return $newUserLog;
  104. }
  105. /**
  106. * Metodo para Obtener una colección de objetos por varios ids
  107. * @param array $ids
  108. * @return UserLogCollection
  109. */
  110. public function getByIds(array $ids)
  111. {
  112. if(null == $ids) return new UserLogCollection();
  113. try
  114. {
  115. $criteria = new Criteria();
  116. $criteria->add(UserLog::ID_USER_LOG, $ids, Criteria::IN);
  117. $userLogCollection = $this->getByCriteria($criteria);
  118. }
  119. catch(Exception $e)
  120. {
  121. throw new UserLogException("UserLogCollection can't be populated\n" . $e->getMessage());
  122. }
  123. return $userLogCollection;
  124. }
  125. /**
  126. * Metodo para Obtener todos los ids en un arreglo
  127. * @return array
  128. */
  129. public function retrieveAllIds()
  130. {
  131. try
  132. {
  133. $result = $this->db->fetchCol('SELECT id_user_log FROM '.UserLog::TABLENAME);
  134. }
  135. catch(Exception $e)
  136. {
  137. throw new UserLogException("Can't obtain the ids\n" . $e->getMessage());
  138. }
  139. return $result;
  140. }
  141. /**
  142. * Metodo para actualizar un UserLog
  143. * @param UserLog $userLog
  144. */
  145. public function update($userLog)
  146. {
  147. if(!($userLog instanceof UserLog))
  148. throw new UserLogException("passed parameter isn't a UserLog instance");
  149. try
  150. {
  151. $where[] = "id_user_log = '{$userLog->getIdUserLog()}'";
  152. $data = array(
  153. 'id_user' => $userLog->getIdUser(),
  154. 'event_type' => $userLog->getEventType(),
  155. 'ip' => $userLog->getIp(),
  156. 'id_responsible' => $userLog->getIdResponsible(),
  157. 'timestamp' => $userLog->getTimestamp(),
  158. 'note' => $userLog->getTimestamp(),
  159. );
  160. $data = array_filter($data, 'Catalog::notNull');
  161. $this->db->update(UserLog::TABLENAME, $data, $where);
  162. }
  163. catch(Exception $e)
  164. {
  165. throw new UserLogException("The UserLog can't be updated \n" . $e->getMessage());
  166. }
  167. }
  168. /**
  169. * Metodo para guardar un userLog
  170. * @param UserLog $userLog
  171. */
  172. public function save($userLog)
  173. {
  174. if(!($userLog instanceof UserLog))
  175. throw new UserLogException("passed parameter isn't a UserLog instance");
  176. if(null != $userLog->getIdUserLog())
  177. $this->update($userLog);
  178. else
  179. $this->create($userLog);
  180. }
  181. /**
  182. * Metodo para eliminar un userLog
  183. * @param UserLog $userLog
  184. */
  185. public function delete($userLog)
  186. {
  187. if(!($userLog instanceof UserLog))
  188. throw new UserLogException("passed parameter isn't a UserLog instance");
  189. $this->deleteById($userLog->getIdUserLog());
  190. }
  191. /**
  192. * Metodo para eliminar un UserLog a partir de su Id
  193. * @param int $idUserLog
  194. */
  195. public function deleteById($idUserLog)
  196. {
  197. try
  198. {
  199. $where = array($this->db->quoteInto('id_user_log = ?', $idUserLog));
  200. $this->db->delete(UserLog::TABLENAME, $where);
  201. }
  202. catch(Exception $e)
  203. {
  204. throw new UserLogException("The UserLog can't be deleted\n" . $e->getMessage());
  205. }
  206. }
  207. /**
  208. * Metodo para eliminar varios UserLog a partir de su Id
  209. * @param array $ids
  210. */
  211. public function deleteByIds(array $ids)
  212. {
  213. try
  214. {
  215. $criteria = new Criteria();
  216. $criteria->add(UserLog::ID_USER_LOG, $ids, Criteria::IN);
  217. $this->db->delete(UserLog::TABLENAME, array($criteria->createSql()));
  218. }
  219. catch(Exception $e)
  220. {
  221. throw new UserLogException("Can't delete that\n" . $e->getMessage());
  222. }
  223. }
  224. /**
  225. * Metodo para obtener todos los id de UserLog por criterio
  226. * @param Criteria $criteria
  227. * @return array Array con todos los id de UserLog que encajen en la busqueda
  228. */
  229. public function getIdsByCriteria(Criteria $criteria = null)
  230. {
  231. $criteria = (null === $criteria) ? new Criteria() : $criteria;
  232. try
  233. {
  234. $sql = "SELECT id_user_log
  235. FROM ".UserLog::TABLENAME."
  236. WHERE " . $criteria->createSql();
  237. $ids = $this->db->fetchCol($sql);
  238. } catch(Exception $e)
  239. {
  240. throw new UserLogException("Can't obtain UserLog's id\n" . $e->getMessage());
  241. }
  242. return $ids;
  243. }
  244. /**
  245. * Metodo para obtener un campo en particular de un UserLog dado un criterio
  246. * @param string $field
  247. * @param Criteria $criteria
  248. * @return array Array con el campo de los objetos UserLog que encajen en la busqueda
  249. */
  250. public function getCustomFieldByCriteria($field, Criteria $criteria = null)
  251. {
  252. $criteria = (null === $criteria) ? new Criteria() : $criteria;
  253. try
  254. {
  255. $sql = "SELECT {$field}
  256. FROM ".UserLog::TABLENAME."
  257. WHERE " . $criteria->createSql();
  258. $result = $this->db->fetchCol($sql);
  259. } catch(Zend_Db_Exception $e)
  260. {
  261. throw new UserLogException("No se pudieron obtener los ids de objetos {$Bean}\n" . $e->getMessage());
  262. }
  263. return $result;
  264. }
  265. /**
  266. * Metodo que regresa una coleccion de objetos UserLog
  267. * dependiendo del criterio establecido
  268. * @param Criteria $criteria
  269. * @return UserLogCollection $userLogCollection
  270. */
  271. public function getByCriteria(Criteria $criteria = null)
  272. {
  273. $criteria = (null === $criteria) ? new Criteria() : $criteria;
  274. $this->db->setFetchMode(Zend_Db::FETCH_ASSOC);
  275. try
  276. {
  277. $sql = "SELECT * FROM ".UserLog::TABLENAME."
  278. WHERE " . $criteria->createSql();
  279. $userLogCollection = new UserLogCollection();
  280. foreach ($this->db->fetchAll($sql) as $result){
  281. $userLogCollection->append($this->getUserLogInstance($result));
  282. }
  283. }
  284. catch(Zend_Db_Exception $e)
  285. {
  286. throw new UserLogException("Cant obtain UserLogCollection\n" . $e->getMessage());
  287. }
  288. return $userLogCollection;
  289. }
  290. /**
  291. * Metodo que cuenta UserLog
  292. * dependiendo del criterio establecido
  293. * @param Criteria $criteria
  294. * @param string $field
  295. * @return int $count
  296. */
  297. public function countByCriteria(Criteria $criteria = null, $field = 'id_user_log')
  298. {
  299. $criteria = (null === $criteria) ? new Criteria() : $criteria;
  300. try
  301. {
  302. $sql = "SELECT COUNT( $field ) FROM ".UserLog::TABLENAME."
  303. WHERE " . $criteria->createSql();
  304. $count = $this->db->fetchOne($sql);
  305. }
  306. catch(Zend_Db_Exception $e)
  307. {
  308. throw new UserLogException("Cant obtain the count \n" . $e->getMessage());
  309. }
  310. return $count;
  311. }
  312. /**
  313. * Método que construye un objeto UserLog y lo rellena con la información del rowset
  314. * @param array $result El arreglo que devolvió el objeto Zend_Db despues del fetch
  315. * @return UserLog
  316. */
  317. private function getUserLogInstance($result)
  318. {
  319. return UserLogFactory::createFromArray($result);
  320. }
  321. /**
  322. * Obtiene un UserLogCollection dependiendo del idUser
  323. * @param int $idUser
  324. * @return UserLogCollection
  325. */
  326. public function getByIdUser($idUser)
  327. {
  328. $criteria = new Criteria();
  329. $criteria->add(UserLog::ID_USER, $idUser, Criteria::EQUAL);
  330. $userLogCollection = $this->getByCriteria($criteria);
  331. return $userLogCollection;
  332. }
  333. /**
  334. * Obtiene un UserLogCollection dependiendo del idResponsible
  335. * @param int $idResponsible
  336. * @return UserLogCollection
  337. */
  338. public function getByIdResponsible($idResponsible)
  339. {
  340. $criteria = new Criteria();
  341. $criteria->add(UserLog::ID_RESPONSIBLE, $idResponsible, Criteria::EQUAL);
  342. $userLogCollection = $this->getByCriteria($criteria);
  343. return $userLogCollection;
  344. }
  345. }