/application/modules/Core/models/User/MapperDbTable.php

https://github.com/ArchiCroc/Tower-Web-Platform · PHP · 156 lines · 74 code · 21 blank · 61 comment · 11 complexity · 1def7b63c501a6fd5b838e63e1a9c6b0 MD5 · raw file

  1. <?php
  2. /*
  3. * ApikeyMapperDbTable.php
  4. *
  5. * Copyright (c) 2011 Matthew Doll <mdoll at homenet.me>.
  6. *
  7. * This file is part of HomeNet.
  8. *
  9. * HomeNet is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * HomeNet is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with HomeNet. If not, see <http ://www.gnu.org/licenses/>.
  21. */
  22. require_once "MapperInterface.php";
  23. /**
  24. * @package Core
  25. * @subpackage User
  26. * @copyright Copyright (c) 2011 Matthew Doll <mdoll at homenet.me>.
  27. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
  28. */
  29. class Core_Model_User_MapperDbTable implements Core_Model_User_MapperInterface {
  30. /**
  31. * @var Zend_Db_Table_Abstract
  32. */
  33. protected $_table = null;
  34. /**
  35. * @return Core_Model_DbTable_User;
  36. */
  37. public function getTable() {
  38. if ($this->_table === null) {
  39. $this->_table = new Zend_Db_Table('users');
  40. $this->_table->setRowClass('Core_Model_User_DbTableRow');
  41. }
  42. return $this->_table;
  43. }
  44. /**
  45. * @param Zend_Db_Table_Abstract $table
  46. */
  47. public function setTable(Zend_Db_Table_Abstract $table) {
  48. $this->_table = $table;
  49. }
  50. /**
  51. * @return int
  52. */
  53. public function fetchCount() {
  54. $select = $this->getTable()->select();
  55. $select->from($this->getTable(), array('count(*) as num_of_items'));
  56. $result = $this->getTable()->fetchRow($select);
  57. return($result->num_of_items);
  58. }
  59. /**
  60. * @return Zend_Db_RowSet
  61. */
  62. public function fetchObjects() {
  63. $select = $this->getTable()->select()->order('id ASC');
  64. return $this->getTable()->fetchAll($select);
  65. }
  66. /**
  67. * @param int $id
  68. * @return Core_Model_User_DbTableRow
  69. */
  70. public function fetchObjectById($id) {
  71. return $this->getTable()->find($id)->current();
  72. }
  73. /**
  74. * @param int $group
  75. * @return Core_Model_User_DbTableRow
  76. */
  77. public function fetchObjectsByPrimaryGroup($group) {
  78. $select = $this->getTable()->select()->where('primary_group = ?', $group);
  79. return $this->getTable()->fetchAll($select);
  80. }
  81. /**
  82. * @param Core_Model_User_Interface $object
  83. * @return Core_Model_User_DbTableRow
  84. */
  85. public function save(Core_Model_User_Interface $object) {
  86. if (($object instanceof Core_Model_User_DbTableRow) && ($object->isConnected())) {
  87. return $object->save();
  88. } elseif ($object->id !== null) {
  89. $row = $this->getTable()->find($object->id)->current();
  90. if (empty($row)) {
  91. $row = $this->getTable()->createRow();
  92. }
  93. } else {
  94. $row = $this->getTable()->createRow();
  95. }
  96. $row->fromArray($object->toArray());
  97. // die(debugArray($row));
  98. try {
  99. $row->save();
  100. } catch (Exception $e) {
  101. if (strstr($e->getMessage(), '1062 Duplicate')) {
  102. throw new DuplicateEntryException("URL Already Exists");
  103. } elseif (strstr($e->getMessage(), '1048 Column')) {
  104. throw new InvalidArgumentException("Invalid Column");
  105. } else {
  106. throw new Exception($e->getMessage());
  107. }
  108. };
  109. return $row;
  110. }
  111. /**
  112. * @param Core_Model_User_Interface $object
  113. * @return bool Success
  114. */
  115. public function delete(Core_Model_User_Interface $object) {
  116. if (($object instanceof Core_Model_User_DbTableRow) && ($object->isConnected())) {
  117. $object->delete();
  118. return true;
  119. } elseif ($object->id !== null) {
  120. $row = $this->getTable()->find($object->id)->current()->delete();
  121. return true;
  122. }
  123. throw new Exception('Invalid User Object');
  124. }
  125. /**
  126. * @return bool Success
  127. */
  128. public function deleteAll() {
  129. if (APPLICATION_ENV != 'production') {
  130. $this->getTable()->getAdapter()->query('TRUNCATE TABLE `' . $this->getTable()->info('name') . '`');
  131. return true;
  132. }
  133. return false;
  134. }
  135. }