PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Adapto/Handler/AbstractSearch.php

http://github.com/egeniq/adapto
PHP | 279 lines | 120 code | 37 blank | 122 comment | 16 complexity | 1ce74eacefdc5bb4b05656c72b26dcbc MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Adapto Toolkit.
  4. * Detailed copyright and licensing information can be found
  5. * in the doc/COPYRIGHT and doc/LICENSE files which should be
  6. * included in the distribution.
  7. *
  8. * @package adapto
  9. * @subpackage handlers
  10. *
  11. * @copyright (c)2000-2008 Ibuildings.nl BV
  12. * @license http://www.achievo.org/atk/licensing ATK Open Source License
  13. *
  14. */
  15. /**
  16. * Abstract class for implementing an atkSearchHandler
  17. *
  18. * @package adapto
  19. * @subpackage testcases
  20. */
  21. abstract class Adapto_Handler_AbstractSearch extends Adapto_ActionHandler
  22. {
  23. /**
  24. * Holds the table name of the searchcriteria
  25. * table. Due some BC issues of the atkSmartSearchHandler
  26. * this value can be overwritten by the checkTable function
  27. *
  28. * @var string
  29. */
  30. protected $m_table = 'Adapto_searchcriteria';
  31. /**
  32. * Indicates if the table
  33. * Adapto_searchcriteria exists
  34. * use the function tableExists
  35. *
  36. * @var boolean
  37. */
  38. protected $m_table_exists = null;
  39. /**
  40. * Return the criteria based on the postvarse
  41. * used for storing
  42. *
  43. * @return array
  44. */
  45. abstract function fetchCriteria();
  46. /**
  47. * Return the type of the atkSmartSearchHandler
  48. *
  49. * @return string
  50. */
  51. function getSearchHandlerType()
  52. {
  53. return strtolower(get_class($this));
  54. }
  55. /**
  56. * check if database table exists
  57. *
  58. * @return boolean
  59. */
  60. protected function tableExist()
  61. {
  62. if ($this->m_table_exists !== null)
  63. return $this->m_table_exists;
  64. $db = $this->m_entity->getDb();
  65. $this->m_table_exists = $db->tableExists($this->m_table);
  66. Adapto_Util_Debugger::debug('tableExists checking table: ' . $this->m_table . ' exists : ' . print_r($this->m_table_exists, true));
  67. return $this->m_table_exists;
  68. }
  69. /**
  70. * List criteria.
  71. *
  72. * @return Array criteria list
  73. */
  74. function listCriteria()
  75. {
  76. if (!$this->tableExist())
  77. return array();
  78. $db = &$this->m_entity->getDb();
  79. $query = "SELECT c.name FROM {$this->m_table} c WHERE c.entitytype = '%s' ORDER BY UPPER(c.name) AND handlertype = '%s'";
  80. $rows = $db->getRows(sprintf($query, $this->m_entity->atkEntityType(), $this->getSearchHandlerType()));
  81. $result = array();
  82. foreach ($rows as $row)
  83. $result[] = $row['name'];
  84. return $result;
  85. }
  86. /**
  87. * Remove search criteria.
  88. *
  89. * @param String $name name of the search criteria
  90. */
  91. function forgetCriteria($name)
  92. {
  93. if (!$this->tableExist())
  94. return false;
  95. $db = &$this->m_entity->getDb();
  96. $query = "DELETE FROM {$this->m_table} WHERE entitytype = '%s' AND UPPER(name) = UPPER('%s') AND handlertype = '%s'";
  97. $db->query(sprintf($query, $this->m_entity->atkEntityType(), escapeSQL($name), $this->getSearchHandlerType()));
  98. $db->commit();
  99. }
  100. /**
  101. * Save search criteria.
  102. *
  103. * NOTE:
  104. * This method will overwrite existing criteria with the same name.
  105. *
  106. * @param String $name name for the search criteria
  107. * @param Array $criteria search criteria data
  108. */
  109. function saveCriteria($name, $criteria)
  110. {
  111. if (!$this->tableExist())
  112. return false;
  113. $this->forgetCriteria($name);
  114. $db = &$this->m_entity->getDb();
  115. $query = "INSERT INTO {$this->m_table} (entitytype, name, criteria, handlertype) VALUES('%s', '%s', '%s', '%s')";
  116. $db->query(sprintf($query, $this->m_entity->atkEntityType(), escapeSQL($name), escapeSQL(serialize($criteria)), $this->getSearchHandlerType()));
  117. $db->commit();
  118. }
  119. /**
  120. * Load search criteria.
  121. *
  122. * @param String $name name of the search criteria
  123. * @return Array search criteria
  124. */
  125. function loadCriteria($name)
  126. {
  127. if (!$this->tableExist())
  128. return array();
  129. $db = &$this->m_entity->getDb();
  130. $query = "SELECT c.criteria FROM {$this->m_table} c WHERE c.entitytype = '%s' AND UPPER(c.name) = UPPER('%s') AND handlertype = '%s'";
  131. Adapto_var_dump(sprintf($query, $this->m_entity->atkEntityType(), escapeSQL($name), $this->getSearchHandlerType()), 'loadCriteria query');
  132. list($row) = $db->getRows(sprintf($query, $this->m_entity->atkEntityType(), escapeSQL($name), $this->getSearchHandlerType()));
  133. $criteria = $row == NULL ? NULL : unserialize($row['criteria']);
  134. Adapto_var_dump($criteria, 'loadCriteria criteria');
  135. return $criteria;
  136. }
  137. /**
  138. * Load base criteria.
  139. *
  140. * @return Array search criteria
  141. */
  142. function loadBaseCriteria()
  143. {
  144. return array(array('attrs' => array()));
  145. }
  146. /**
  147. * Returns a select list of loadable criteria which will on-selection
  148. * refresh the smart search page with the loaded criteria.
  149. *
  150. * @param String $current The current load criteria
  151. * @return String criteria load HTML
  152. */
  153. function getLoadCriteria($current)
  154. {
  155. $criteria = $this->listCriteria();
  156. if (count($criteria) == 0)
  157. return NULL;
  158. $result = '
  159. <select name="load_criteria" onchange="this.form.submit();">
  160. <option value=""></option>';
  161. foreach ($criteria as $name)
  162. $result .= '<option value="' . Adapto_htmlentities($name) . '"' . ($name == $current ? ' selected' : '') . '>' . Adapto_htmlentities($name)
  163. . '</option>';
  164. $result .= '</select>';
  165. return $result;
  166. }
  167. /**
  168. * Take the necessary 'saved criteria' actions based on the
  169. * posted variables.
  170. * Returns the name of the saved criteria
  171. *
  172. * @param array $criteria array with the current criteria
  173. * @return string name of the saved criteria
  174. */
  175. function handleSavedCriteria($criteria)
  176. {
  177. $name = array_key_exists('load_criteria', $this->m_postvars) ? $this->m_postvars['load_criteria'] : '';
  178. if (!empty($this->m_postvars['forget_criteria'])) {
  179. $forget = $this->m_postvars['forget_criteria'];
  180. $this->forgetCriteria($forget);
  181. $name = NULL;
  182. } else if (!empty($this->m_postvars['save_criteria'])) {
  183. $save = $this->m_postvars['save_criteria'];
  184. $this->saveCriteria($save, $criteria);
  185. $name = $save;
  186. }
  187. return $name;
  188. }
  189. /**
  190. * Returns an array with all the saved criteria
  191. * information. This information will be parsed
  192. * to the different
  193. *
  194. * @param string $current
  195. * @return array
  196. */
  197. function getSavedCriteria($current)
  198. {
  199. // check if table is present
  200. if (!$this->tableExist())
  201. return array();
  202. return array('load_criteria' => $this->getLoadCriteria($current), 'forget_criteria' => $this->getForgetCriteria($current),
  203. 'toggle_save_criteria' => $this->getToggleSaveCriteria(), 'save_criteria' => $this->getSaveCriteria($current),
  204. 'label_load_criteria' => Adapto_htmlentities(atktext('load_criteria', 'atk')),
  205. 'label_forget_criteria' => Adapto_htmlentities(atktext('forget_criteria', 'atk')),
  206. 'label_save_criteria' => '<label for="toggle_save_criteria">' . Adapto_htmlentities(atktext('save_criteria', 'atk')) . '</label>');
  207. }
  208. /**
  209. * Returns a link for removing the currently selected criteria. If
  210. * nothing (valid) is selected nothing is returned.
  211. *
  212. * @param String $current currently loaded criteria
  213. * @return String forget url
  214. */
  215. function getForgetCriteria($current)
  216. {
  217. if (empty($current) || $this->loadCriteria($current) == NULL)
  218. return NULL;
  219. else
  220. return session_url(dispatch_url($this->m_entity->atkEntityType(), $this->m_action, array('forget_criteria' => $current)), SESSION_REPLACE);
  221. }
  222. /**
  223. * Returns a checkbox for enabling/disabling the saving of criteria.
  224. *
  225. * @return checkbox HTML
  226. */
  227. function getToggleSaveCriteria()
  228. {
  229. return '<input id="toggle_save_criteria" type="checkbox" class="atkcheckbox" onclick="$(save_criteria).disabled = !$(save_criteria).disabled">';
  230. }
  231. /**
  232. * Returns a textfield for entering a name to save the search criteria as.
  233. *
  234. * @param String $current currently loaded criteria
  235. * @param String textfield HTML
  236. */
  237. function getSaveCriteria($current)
  238. {
  239. return '<input id="save_criteria" type="text" size="30" name="save_criteria" value="' . Adapto_htmlentities($current) . '" disabled="disabled">';
  240. }
  241. }