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

https://github.com/HomeNet/HomeNet.me-Website · PHP · 191 lines · 116 code · 40 blank · 35 comment · 9 complexity · 45be4bafdbf9bc786b1b960ade92eb57 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 Acl_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_Acl_User_MapperDbTable implements Core_Model_Acl_User_MapperInterface {
  30. protected $_table = null;
  31. /**
  32. *
  33. * @return Core_Model_DbTable_Acl_User;
  34. */
  35. public function getTable() {
  36. if ($this->_table === null) {
  37. $this->_table = new Zend_Db_Table('user_acls');
  38. $this->_table->setRowClass('Core_Model_Acl_User_DbTableRow');
  39. }
  40. return $this->_table;
  41. }
  42. public function setTable(Zend_Db_Table_Abstract $table) {
  43. $this->_table = $table;
  44. }
  45. /*
  46. *
  47. */
  48. public function fetchObjectById($id){
  49. return $this->getTable()->find($id)->current();
  50. }
  51. public function fetchObjectsByUser($user){
  52. $select = $this->getTable()->select()->where('user = ?',$user)
  53. ->where('collection is NULL')
  54. ->where('object is NULL');
  55. return $this->getTable()->fetchAll($select);
  56. }
  57. public function fetchObjectsByUserModule($user, $module) {
  58. $select = $this->getTable()->select()
  59. ->where('user = ?', $user)
  60. ->where('(module = ? OR module is NULL)', $module) //->orWhere('module is NULL')
  61. ->where('collection is NULL')
  62. ->where('object is NULL')
  63. ->order(array('controller','action'));
  64. return $this->getTable()->fetchAll($select);
  65. }
  66. public function fetchObjectsByUserModuleCollection($user, $module, $collection) {
  67. $select = $this->getTable()->select()
  68. ->where('user = ?', $user)
  69. ->where('module = ?', $module)
  70. ->where('collection = ?', $collection)
  71. ->order(array('controller','object','action'));
  72. return $this->getTable()->fetchAll($select);
  73. }
  74. public function fetchObjectsByUserModuleControllerObject($user, $module, $controller, $object) {
  75. $select = $this->getTable()->select()
  76. ->where('user = ?', $user)
  77. ->where('module = ?', $module)
  78. ->where('controller = ?', $controller)
  79. ->where('object = ?', $object)
  80. ->order(array('controller','object','action'));
  81. return $this->getTable()->fetchAll($select);
  82. }
  83. public function fetchObjectsByUserModuleControllerObjects($user, $module, $controller, $objects) {
  84. $select = $this->getTable()->select()
  85. ->where('user = ?', $user)
  86. ->where('module = ?', $module)
  87. ->where('controller = ?', $controller)
  88. ->where('object in (?) ', $objects)
  89. ->order(array('controller','object','action'));
  90. return $this->getTable()->fetchAll($select);
  91. }
  92. public function save(Core_Model_Acl_User_Interface $object) {
  93. if (($object instanceof Core_Model_DbTableRow_UserAcl) && ($object->isConnected())) {
  94. return $object->save();;
  95. } elseif ($object->id !== null) {
  96. $row = $this->getTable()->find($object->id)->current();
  97. if(empty($row)){
  98. $row = $this->getTable()->createRow();
  99. }
  100. } else {
  101. $row = $this->getTable()->createRow();
  102. }
  103. $row->fromArray($object->toArray());
  104. // die(debugArray($row));
  105. return $row->save();
  106. //return $row;
  107. }
  108. public function delete(Core_Model_Acl_User_Interface $object) {
  109. if (($object instanceof Core_Model_DbTableRow_UserAcl) && ($object->isConnected())) {
  110. $object->delete();
  111. return true;
  112. } elseif ($object->id !== null) {
  113. $row = $this->getTable()->find($object->id)->current()->delete();
  114. return;
  115. }
  116. throw new Exception('Invalid User Object');
  117. }
  118. public function deleteByUser($user){
  119. $where = $this->getTable()->getAdapter()->quoteInto('user = ?',$user);
  120. return $this->getTable()->delete($where);
  121. }
  122. public function deleteByModule($module) {
  123. $where = $this->getTable()->getAdapter()->quoteInto('module = ?',$module);
  124. return $this->getTable()->delete($where);
  125. }
  126. public function deleteByModuleCollection($module, $collection) {
  127. $where = array();
  128. $where[] = $this->getTable()->getAdapter()->quoteInto('module = ?',$module);
  129. $where[] = $this->getTable()->getAdapter()->quoteInto('collection = ?',$collection);
  130. return $this->getTable()->delete($where);
  131. }
  132. public function deleteByUserModuleCollection($user, $module, $collection) {
  133. $where = array();
  134. $where[] = $this->getTable()->getAdapter()->quoteInto('user = ?',$user);
  135. $where[] = $this->getTable()->getAdapter()->quoteInto('module = ?',$module);
  136. $where[] = $this->getTable()->getAdapter()->quoteInto('collection = ?',$collection);
  137. return $this->getTable()->delete($where);
  138. }
  139. public function deleteByModuleControllerObject($module, $controller, $object) {
  140. $where = array();
  141. $where[] = $this->getTable()->getAdapter()->quoteInto('module = ?',$module);
  142. $where[] = $this->getTable()->getAdapter()->quoteInto('controller = ?',$controller);
  143. $where[] = $this->getTable()->getAdapter()->quoteInto('object = ?',$object);
  144. return $this->getTable()->delete($where);
  145. }
  146. public function deleteAll(){
  147. if(APPLICATION_ENV != 'production'){
  148. $this->getTable()->getAdapter()->query('TRUNCATE TABLE `'. $this->getTable()->info('name').'`');
  149. }
  150. }
  151. }