PageRenderTime 72ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Bvb/Grid/Source/Zend/Table.php

https://bitbucket.org/sader/domset
PHP | 344 lines | 191 code | 84 blank | 69 comment | 46 complexity | acd45cff61f03a38e5601e1e00399f84 MD5 | raw file
  1. <?php
  2. /**
  3. * LICENSE
  4. *
  5. * This source file is subject to the new BSD license
  6. * It is available through the world-wide-web at this URL:
  7. * http://www.petala-azul.com/bsd.txt
  8. * If you did not receive a copy of the license and are unable to
  9. * obtain it through the world-wide-web, please send an email
  10. * to geral@petala-azul.com so we can send you a copy immediately.
  11. *
  12. * @package Bvb_Grid
  13. * @author Bento Vilas Boas <geral@petala-azul.com>
  14. * @copyright 2010 ZFDatagrid
  15. * @license http://www.petala-azul.com/bsd.txt New BSD License
  16. * @version $Id: Table.php 1791 2011-05-31 03:19:53Z bento.vilas.boas@gmail.com $
  17. * @link http://zfdatagrid.com
  18. */
  19. class Bvb_Grid_Source_Zend_Table extends Bvb_Grid_Source_Zend_Select
  20. {
  21. private $_model;
  22. /**
  23. *
  24. * @var array
  25. */
  26. protected $_relationMap = array();
  27. /**
  28. * Returns current model
  29. *
  30. * @return Zend_Db_Table
  31. */
  32. public function getModel ()
  33. {
  34. return $this->_model;
  35. }
  36. /**
  37. * Returns current relationmap
  38. *
  39. * @return array
  40. */
  41. public function getRelationMap()
  42. {
  43. return $this->_relationMap;
  44. }
  45. /**
  46. * Builds form input types
  47. *
  48. * @param array $inputsType Elements input types
  49. *
  50. * @return array
  51. */
  52. public function buildForm ($inputsType = array())
  53. {
  54. $info = $this->getModel()->info();
  55. $cols = $info['metadata'];
  56. $form = $this->buildFormElements($cols, $info, $inputsType, $this->_relationMap);
  57. return $form;
  58. }
  59. /**
  60. * Creating a query using a Model.
  61. *
  62. * @param Zend_Db_Table_Abstract $model
  63. * @param array $relationmap Relation map for joins
  64. *
  65. * @return $this
  66. */
  67. public function __construct (Zend_Db_Table_Abstract $model,array $relationMap = array())
  68. {
  69. $this->_model = $model;
  70. $this->_relationMap = $relationMap;
  71. $info = $model->info();
  72. $select = $model->select();
  73. $map = $info['referenceMap'];
  74. $map = array_merge_recursive($map,$this->_relationMap);
  75. $this->_relationMap = $map;
  76. if ( is_array($map) && count($map) > 0 ) {
  77. $select->setIntegrityCheck(false);
  78. /**
  79. $columnsToRemove = array();
  80. foreach ( $map as $sel ) {
  81. if ( is_array($sel['columns']) ) {
  82. $columnsToRemove = array_merge($columnsToRemove, $sel['columns']);
  83. } else {
  84. $columnsToRemove[] = $sel['columns'];
  85. }
  86. }
  87. $columnsMainTable = array_diff($info['cols'], $columnsToRemove);*/
  88. $columnsMainTable = $info['cols'];
  89. $select->from($info['name'], $columnsMainTable, $info['schema']);
  90. $tAlias = array($info['name'] => 1);
  91. $this->_setJoins($info['name'], $map, $select, $tAlias);
  92. }else{
  93. $select->from($info['name'], $info['cols'], $info['schema']);
  94. }
  95. parent::__construct($select);
  96. return $this;
  97. }
  98. private function _setJoins ($tName, array $map, &$select, array &$tAlias = array())
  99. {
  100. foreach ( $map as $sel ) {
  101. $class = new $sel['refTableClass']();
  102. $info = $class->info();
  103. if ( ! isset($tAlias[$info['name']]) ) {
  104. $tAlias[$info['name']] = 0;
  105. }
  106. $alias = $tAlias[$info['name']] > 0 ? '_' . $tAlias[$info['name']] : null;
  107. if ( is_array($sel['columns']) ) {
  108. if ( ! is_array($sel['refColumns']) || (count($sel['columns']) != count($sel['refColumns'])) ) {
  109. throw new Bvb_Grid_Exception('Mapping of ' . $sel['refTableClass'] . ' is wrong: columns and refColumns must have same type. In case of arrays, they must have same length.');
  110. }
  111. if ( ! array_key_exists('refBvbColumns', $sel) ) {
  112. $cols = null;
  113. } else {
  114. if ( ! is_array($sel['refBvbColumns']) ) {
  115. $cols = array($sel['columns'][0] => $sel['refBvbColumns']);
  116. } else {
  117. $cols = $sel['refBvbColumns'];
  118. }
  119. }
  120. $tFields = array_combine($sel['columns'], $sel['refColumns']);
  121. $join = null;
  122. foreach ( $tFields as $key => $value ) {
  123. if ( ! is_null($join) ) {
  124. $join .= ' AND ';
  125. }
  126. $join .= $info['name'] . $alias . '.' . $value . ' = ' . $tName . '.' . $key;
  127. }
  128. $select->joinLeft(array($info['name'] . $alias => $info['name']), $join, $cols, $info['schema']);
  129. $tAlias[$info['name']] ++;
  130. } else {
  131. if ( is_array($sel['refColumns']) ) {
  132. throw new Bvb_Grid_Exception('Mapping of ' . $sel['refTableClass'] . ' is wrong: columns and refColumns must have same type.');
  133. }
  134. if ( array_key_exists('refBvbColumns', $sel) ) {
  135. if ( is_array($sel['refBvbColumns']) ) {
  136. $cols = $sel['refBvbColumns'];
  137. } else {
  138. $cols = array_combine((array) $sel['columns'], (array) $sel['refBvbColumns']);
  139. }
  140. } else {
  141. $cols = null;
  142. }
  143. $select->joinLeft(array($info['name'] . $alias => $info['name']), $info['name'] . $alias . '.' . array_shift($info['primary']) . ' = ' . $tName . '.' . $sel['columns'], $cols, $info['schema']);
  144. }
  145. $tAlias[$info['name']] ++;
  146. if ( ! array_key_exists('refBvbFollow', $sel) ) {
  147. $sel['refBvbFollow'] = false;
  148. }
  149. if ( is_array($info['referenceMap']) && count($info['referenceMap']) > 0 && $sel['refBvbFollow'] ) {
  150. $this->_setJoins($info['name'], $info['referenceMap'], $select, $tAlias);
  151. }
  152. }
  153. }
  154. public function getRecord ($table, array $condition)
  155. {
  156. if ( $this->_cache['enable'] == 1 ) {
  157. $hash = 'Bvb_Grid_Model' . md5($this->buildWhereCondition($condition));
  158. if ( ! $result = $this->_cache['instance']->load($hash) ) {
  159. $result = call_user_func_array(array($this->getModel(),'find'), $condition);
  160. $this->_cache['instance']->save($result, $hash, array($this->_cache['tag']));
  161. }
  162. } else {
  163. $result = call_user_func_array(array($this->getModel(),'find'), $condition);
  164. }
  165. if($result->current() === null)
  166. {
  167. return array();
  168. }
  169. return $result->current()->toArray();
  170. }
  171. /**
  172. * Executes the current query and returns an associative array of results
  173. *
  174. * @return array
  175. */
  176. public function execute()
  177. {
  178. $this->_prepareExecute();
  179. if ($this->_cache['enable'] == 1) {
  180. $hash = 'Bvb_Grid' . md5($this->_select->__toString());
  181. if (!$result = $this->_cache['instance']->load($hash)) {
  182. $result = $this->getModel()->fetchAll($this->_select);
  183. $this->_cache['instance']->save($result, $hash, array($this->_cache['tag']));
  184. }
  185. } else {
  186. $result = $this->getModel()->fetchAll($this->_select);
  187. if ($this->_server == 'mysql') {
  188. $this->_totalRecords = $this->_select->getAdapter()->fetchOne('select FOUND_ROWS()');
  189. }
  190. }
  191. return $result;
  192. }
  193. public function fetchDetail ( array $where)
  194. {
  195. if ( $this->_cache['enable'] == 1 ) {
  196. $hash = 'Bvb_Grid_Model' . md5($this->buildWhereCondition($where));
  197. if ( ! $result = $this->_cache['instance']->load($hash) ) {
  198. $result = call_user_func_array(array($this->getModel(),'find'), $where);
  199. $this->_cache['instance']->save($result, $hash, array($this->_cache['tag']));
  200. }
  201. } else {
  202. $result = call_user_func_array(array($this->getModel(),'find'), $where);
  203. }
  204. if ( $result === null ) {
  205. return false;
  206. }
  207. $result = $result->toArray();
  208. if (isset($result[0]['ZFG_GHOST'])) {
  209. unset($result[0]['ZFG_GHOST']);
  210. }
  211. return $result[0];
  212. }
  213. public function delete ($table, array $condition)
  214. {
  215. if ( $this->_cache['enable'] == 1 ) {
  216. $this->_cache['instance']->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array($this->_cache['tag']));
  217. }
  218. $result = call_user_func_array(array($this->getModel(),'find'), $condition);
  219. if($result->current() === null)
  220. {
  221. return array();
  222. }
  223. $return = $result->current()->delete();
  224. return $return;
  225. }
  226. public function update ($table, array $post, array $condition)
  227. {
  228. if ( $this->_cache['enable'] == 1 ) {
  229. $this->_cache['instance']->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array($this->_cache['tag']));
  230. }
  231. $result = call_user_func_array(array($this->getModel(),'find'), $condition);
  232. $return = $result->current()->setFromArray($post)->save();
  233. return $return;
  234. }
  235. public function insert ($table, array $post)
  236. {
  237. if ( $this->_cache['enable'] == 1 ) {
  238. $this->_cache['instance']->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array($this->_cache['tag']));
  239. }
  240. $return = $this->getModel()->createRow($post)->save();
  241. return $return;
  242. }
  243. /**
  244. * Get the primary table key
  245. * This is important because we only allow edit, add or remove records
  246. * From tables that have on primary key
  247. *
  248. * @return array
  249. */
  250. public function getIdentifierColumns ($table)
  251. {
  252. $info = $this->_model->info();
  253. $keys = array();
  254. foreach ( $info['primary'] as $pk ) {
  255. $keys[] = $info['metadata'][$pk]['TABLE_NAME'] . '.' . $pk;
  256. }
  257. return $keys;
  258. }
  259. }