PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/Tinebase/Model/Filter/Abstract.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 414 lines | 186 code | 50 blank | 178 comment | 24 complexity | 9a99b2324b50b8caf4df099e27ac072d MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0
  4. *
  5. * @package Tinebase
  6. * @subpackage Filter
  7. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  8. * @copyright Copyright (c) 2007-2012 Metaways Infosystems GmbH (http://www.metaways.de)
  9. * @author Cornelius Weiss <c.weiss@metaways.de>
  10. */
  11. /**
  12. * Tinebase_Model_Filter_Abstract
  13. *
  14. * @package Tinebase
  15. * @subpackage Filter
  16. *
  17. * Abstract filter
  18. *
  19. * @todo validate value!
  20. */
  21. abstract class Tinebase_Model_Filter_Abstract
  22. {
  23. /**
  24. * @var array list of allowed operators
  25. */
  26. protected $_operators = array();
  27. /**
  28. * @var string property this filter is applied to
  29. */
  30. protected $_field = NULL;
  31. /**
  32. * @var string operator
  33. */
  34. protected $_operator = NULL;
  35. /**
  36. * @var mixed value to filter with
  37. */
  38. protected $_value = NULL;
  39. /**
  40. * @var string filter id [optional]
  41. */
  42. protected $_id = NULL;
  43. /**
  44. * @var string filter label [optional]
  45. */
  46. protected $_label = NULL;
  47. /**
  48. * @var array special options
  49. */
  50. protected $_options = NULL;
  51. /**
  52. * @var Tinebase_Backend_Sql_Command_Interface
  53. */
  54. protected $_dbCommand;
  55. /**
  56. * filter is implicit, this is returned in toArray
  57. * - this is only needed to detect acl filters that have been added by a controller
  58. *
  59. * @var boolean
  60. * @todo move this to acl filter?
  61. */
  62. protected $_isImplicit = FALSE;
  63. /**
  64. * get a new single filter action
  65. *
  66. * @param string|array $_fieldOrData
  67. * @param string $_operator
  68. * @param mixed $_value
  69. * @param array $_options
  70. *
  71. * @todo remove legacy code + obsolete params sometimes
  72. */
  73. public function __construct($_fieldOrData, $_operator = NULL, $_value = NULL, array $_options = array())
  74. {
  75. $this->_db = Tinebase_Core::getDb();
  76. $this->_dbCommand = Tinebase_Backend_Sql_Command::factory($this->_db);
  77. if (is_array($_fieldOrData)) {
  78. $data = $_fieldOrData;
  79. } else {
  80. if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__
  81. . ' Using deprecated constructor syntax. Please pass all filter data in one array (filter field: ' . $_fieldOrData . ').');
  82. $data = array(
  83. 'field' => $_fieldOrData,
  84. 'operator' => $_operator,
  85. 'value' => $_value,
  86. 'options' => $_options,
  87. );
  88. }
  89. foreach (array('field', 'operator', 'value') as $requiredKey) {
  90. if (! array_key_exists($requiredKey, $data)) {
  91. throw new Tinebase_Exception_InvalidArgument('Filter object needs ' . $requiredKey);
  92. }
  93. }
  94. $this->_setOptions((isset($data['options'])) ? $data['options'] : array());
  95. $this->setField($data['field']);
  96. $this->setOperator($data['operator']);
  97. $this->setValue($data['value']);
  98. if (isset($data['id'])) {
  99. $this->setId($data['id']);
  100. }
  101. if (isset($data['label'])) {
  102. $this->setLabel($data['label']);
  103. }
  104. }
  105. /**
  106. * returns the id of the filter
  107. *
  108. * @return string
  109. */
  110. public function getId()
  111. {
  112. return $this->_id;
  113. }
  114. /**
  115. * returns operators of this filter model
  116. * @return array
  117. */
  118. public function getOperators()
  119. {
  120. return $this->_operators;
  121. }
  122. /**
  123. * returns operator sql mapping
  124. * @return array
  125. */
  126. public function getOpSqlMap()
  127. {
  128. if($this->_opSqlMap) {
  129. return $this->_opSqlMap;
  130. }
  131. return NULL;
  132. }
  133. /**
  134. * set options
  135. *
  136. * @param array $_options
  137. */
  138. protected function _setOptions(array $_options)
  139. {
  140. if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' '
  141. . ' ' . print_r($_options, TRUE));
  142. $this->_options = $_options;
  143. }
  144. /**
  145. * set field
  146. *
  147. * @param string $_field
  148. */
  149. public function setField($_field)
  150. {
  151. $this->_field = $_field;
  152. }
  153. /**
  154. * returns fieldname of this filter
  155. *
  156. * @return string
  157. */
  158. public function getField()
  159. {
  160. return $this->_field;
  161. }
  162. /**
  163. * sets operator
  164. *
  165. * @param string $_operator
  166. */
  167. public function setOperator($_operator)
  168. {
  169. if (empty($_operator) && isset($this->_operators[0])) {
  170. // try to use default/first operator
  171. $_operator = $this->_operators[0];
  172. }
  173. if (! in_array($_operator, $this->_operators)) {
  174. if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' '
  175. . ' Allowed operators: ' . print_r($this->_operators, TRUE));
  176. throw new Tinebase_Exception_UnexpectedValue("operator $_operator is not defined");
  177. }
  178. $this->_operator = $_operator;
  179. }
  180. /**
  181. * gets operator
  182. *
  183. * @return string
  184. */
  185. public function getOperator()
  186. {
  187. return $this->_operator;
  188. }
  189. /**
  190. * sets value
  191. *
  192. * @param string $_value
  193. */
  194. public function setValue($_value)
  195. {
  196. // cope with resolved records
  197. if (is_array($_value) && array_key_exists('id', $_value)) {
  198. $_value = $_value['id'];
  199. }
  200. //@todo validate value before setting it!
  201. $this->_value = $_value;
  202. }
  203. /**
  204. * sets id
  205. *
  206. * @param string $_id
  207. */
  208. public function setId($_id)
  209. {
  210. $this->_id = $_id;
  211. }
  212. /**
  213. * remove id of filter object
  214. */
  215. public function removeId()
  216. {
  217. $this->_id = NULL;
  218. }
  219. /**
  220. * set label
  221. *
  222. * @param string $_label
  223. */
  224. public function setLabel($_label)
  225. {
  226. $this->_label = $_label;
  227. }
  228. /**
  229. * gets value
  230. *
  231. * @return mixed
  232. */
  233. public function getValue()
  234. {
  235. return $this->_value;
  236. }
  237. /**
  238. * set implicit
  239. * @deprecated use isImplicit()
  240. *
  241. * @param boolean $_isImplicit
  242. */
  243. public function setIsImplicit($_isImplicit)
  244. {
  245. $this->_isImplicit = ($_isImplicit === TRUE);
  246. }
  247. /**
  248. * set implicit
  249. *
  250. * @param boolean optional
  251. * @return boolean
  252. */
  253. public function isImplicit()
  254. {
  255. $value = (func_num_args() === 1) ? (bool) func_get_arg(0) : NULL;
  256. $currValue = $this->_isImplicit;
  257. if ($value !== NULL) {
  258. $this->_isImplicit = $value;
  259. }
  260. return $currValue;
  261. }
  262. /**
  263. * appends sql to given select statement
  264. *
  265. * @param Zend_Db_Select $_select
  266. * @param Tinebase_Backend_Sql_Abstract $_backend
  267. *
  268. * @todo to be removed once we split filter model / backend
  269. */
  270. abstract public function appendFilterSql($_select, $_backend);
  271. /**
  272. * returns quoted column name for sql backend
  273. *
  274. * @param Tinebase_Backend_Sql_Interface $_backend
  275. * @return string
  276. *
  277. * @todo to be removed once we split filter model / backend
  278. */
  279. protected function _getQuotedFieldName($_backend) {
  280. $tablename = (isset($this->_options['tablename'])) ? $this->_options['tablename'] : $_backend->getTableName();
  281. if (Tinebase_Core::isLogLevel(Zend_Log::TRACE)) Tinebase_Core::getLogger()->trace(__METHOD__ . '::' . __LINE__ . ' '
  282. . 'Using tablename: ' . $tablename);
  283. return $_backend->getAdapter()->quoteIdentifier(
  284. $tablename . '.' . $this->_field
  285. );
  286. }
  287. /**
  288. * returns array with the filter settings of this filter
  289. *
  290. * @param bool $_valueToJson resolve value for json api?
  291. * @return array
  292. */
  293. public function toArray($_valueToJson = false)
  294. {
  295. $result = array(
  296. 'field' => $this->_field,
  297. 'operator' => $this->_operator,
  298. 'value' => $this->_value
  299. );
  300. if ($this->_isImplicit) {
  301. $result['implicit'] = TRUE;
  302. }
  303. if ($this->_id) {
  304. $result['id'] = $this->_id;
  305. }
  306. if ($this->_label) {
  307. $result['label'] = $this->_label;
  308. }
  309. return $result;
  310. }
  311. /**
  312. * convert string in user time to UTC
  313. *
  314. * @param string $_string
  315. * @return string
  316. */
  317. protected function _convertStringToUTC($_string)
  318. {
  319. if (empty($_string)) {
  320. $date = new Tinebase_DateTime();
  321. $result = $date->toString(Tinebase_Record_Abstract::ISO8601LONG);
  322. } elseif (isset($this->_options['timezone']) && $this->_options['timezone'] !== 'UTC') {
  323. $date = new Tinebase_DateTime($_string, $this->_options['timezone']);
  324. $date->setTimezone('UTC');
  325. $result = $date->toString(Tinebase_Record_Abstract::ISO8601LONG);
  326. } else {
  327. $result = $_string;
  328. }
  329. return $result;
  330. }
  331. /**
  332. * replaces wildcards
  333. *
  334. * @param string $value
  335. * @return string
  336. */
  337. protected function _replaceWildcards($value)
  338. {
  339. if (is_array($value)) {
  340. $returnValue = array();
  341. foreach ($value as $idx => $val) {
  342. $returnValue[$idx] = $this->_replaceWildcardsSingleValue($val);
  343. }
  344. } else {
  345. $returnValue = $this->_replaceWildcardsSingleValue($value);
  346. }
  347. return $returnValue;
  348. }
  349. /**
  350. * replaces wildcards of a single value
  351. *
  352. * @param string $value
  353. * @return string
  354. */
  355. protected function _replaceWildcardsSingleValue($value)
  356. {
  357. $action = $this->_opSqlMap[$this->_operator];
  358. // replace wildcards from user ()
  359. $returnValue = str_replace(array('*', '_'), $this->_dbCommand->setDatabaseJokerCharacters(), $value);
  360. // add wildcard to value according to operator
  361. $returnValue = str_replace('?', $returnValue, $action['wildcards']);
  362. return $returnValue;
  363. }
  364. }