PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_roksprocket/lib/RokSprocket/Provider/AbstractJoomlaPlatformFilter.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 388 lines | 237 code | 49 blank | 102 comment | 27 complexity | f2ee91ad938ec77ac079ba1b6f002fe0 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - 2013 RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. abstract class RokSprocket_Provider_AbstractJoomlaPlatformFilter implements RokSprocket_Provider_Filter_IProcessor
  9. {
  10. /**
  11. * @var SimpleXMLElement
  12. */
  13. protected $xml;
  14. /**
  15. * @var JDatabaseQuery
  16. */
  17. protected $query;
  18. /**
  19. * @var JDatabase
  20. */
  21. protected $db;
  22. /**
  23. * @var array
  24. */
  25. protected $article_where = array();
  26. /**
  27. * @var array
  28. */
  29. protected $filter_where = array();
  30. /**
  31. * @var array
  32. */
  33. protected $access_where = array();
  34. /**
  35. * @var array
  36. */
  37. protected $displayed_where = array();
  38. /**
  39. * @var array
  40. */
  41. protected $sort_order = array();
  42. /** @var bool */
  43. protected $showUnpublished = false;
  44. /**
  45. * @var int
  46. */
  47. protected $moduleId;
  48. /**
  49. * @var array
  50. */
  51. protected $displayedIds = array();
  52. /**
  53. * @var bool
  54. */
  55. protected $manualSort = false;
  56. /**
  57. * @var string
  58. */
  59. protected $manualAppend = 'after';
  60. /**
  61. */
  62. public function __construct()
  63. {
  64. $this->db = JFactory::getDbo();
  65. $this->query = $this->db->getQuery(true);
  66. $this->setBaseQuery();
  67. }
  68. /**
  69. * @abstract
  70. * @return mixed
  71. */
  72. abstract protected function setBaseQuery();
  73. /**
  74. * @abstract
  75. * @return mixed
  76. */
  77. abstract protected function setAccessWhere();
  78. /**
  79. * @abstract
  80. * @return mixed
  81. */
  82. abstract protected function setDisplayedWhere();
  83. /**
  84. * @param array $filters
  85. * @param array $sort_filters
  86. */
  87. public function process(array $filters, array $sort_filters = array(), $showUnpublished = false)
  88. {
  89. $this->showUnpublished = $showUnpublished;
  90. $this->setAccessWhere();
  91. $this->setDisplayedWhere();
  92. foreach ($filters as $filtertype => $fitlerdata) {
  93. if (!method_exists($this, $filtertype)) {
  94. //throw new RokSprocket_Exception(rc__('Unknown Filter %s', $filtertype));
  95. } else {
  96. $this->$filtertype($fitlerdata);
  97. }
  98. }
  99. foreach ($sort_filters as $sort_filtertype => $sort_fitlerdata) {
  100. $sort_function = 'sort_' . $sort_filtertype;
  101. if (!method_exists($this, 'sort_' . $sort_filtertype)) {
  102. //throw new RokSprocket_Exception(rc__('Unknown Filter %s', $filtertype));
  103. } else {
  104. $this->$sort_function($sort_fitlerdata);
  105. }
  106. }
  107. if (!empty($this->access_where)) {
  108. $access_where = sprintf('(%s)', implode(' AND ', $this->access_where));
  109. $article_where_parts[] = $access_where;
  110. $filter_where_parts[] = $access_where;
  111. }
  112. if (!empty($this->displayed_where)) {
  113. $displayed_where = sprintf('(%s)', implode(' AND ', $this->displayed_where));
  114. $article_where_parts[] = $displayed_where;
  115. $filter_where_parts[] = $displayed_where;
  116. }
  117. if (!empty($this->article_where)) {
  118. $article_where_parts[] = implode(' AND ', $this->article_where);
  119. $this->query->where(sprintf('(%s)', implode(' AND ', $article_where_parts)), 'OR');
  120. }
  121. if (!empty($this->filter_where)) {
  122. $filter_where_parts[] = implode(' AND ', $this->filter_where);
  123. $this->query->where(sprintf('(%s)', implode(' AND ', $filter_where_parts)), 'OR');
  124. }
  125. if (empty($this->article_where) && empty($this->filter_where)) {
  126. $this->query->where('0=1');
  127. }
  128. if ($this->manualSort) {
  129. $this->query->join('LEFT OUTER', sprintf('(select rsi.provider_id, rsi.order from #__roksprocket_items as rsi where module_id = %d) rsi on a.id = rsi.provider_id', $this->moduleId));
  130. array_unshift($this->sort_order, 'rsi.order');
  131. if ($this->manualAppend == 'after') {
  132. array_unshift($this->sort_order, 'IF(ISNULL(rsi.order),1,0)');
  133. }
  134. }
  135. foreach ($this->sort_order as $sort) {
  136. $this->query->order($sort);
  137. }
  138. }
  139. /**
  140. * @return \JDatabaseQuery
  141. */
  142. public function getQuery()
  143. {
  144. return $this->query;
  145. }
  146. /**
  147. * @param $field
  148. * @param $data
  149. */
  150. protected function dateMatch($field, $data)
  151. {
  152. $wheres = array();
  153. foreach ($data as $options) {
  154. foreach ($options as $type => $match) {
  155. if (!empty($match)) {
  156. switch ($type) {
  157. case 'withinlast':
  158. switch ($match['range']) {
  159. case 'weeks':
  160. $range = 'WEEK';
  161. break;
  162. case 'months':
  163. $range = 'MONTH';
  164. break;
  165. case 'years':
  166. $range = 'YEAR';
  167. break;
  168. case 'days':
  169. default:
  170. $range = 'DAY';
  171. break;
  172. }
  173. $wheres[] = 'DATEDIFF(NOW(),' . $field . ') < DATEDIFF(NOW(), DATE_SUB(NOW(),INTERVAL ' . (int)$match['value'] . ' ' . $range . '))';
  174. break;
  175. case 'exactly':
  176. $wheres[] = 'DATEDIFF(' . $this->db->quote($this->db->escape($match, true)) . ',' . $field . ') = 0';
  177. break;
  178. case 'before':
  179. $wheres[] = 'DATEDIFF(' . $this->db->quote($this->db->escape($match, true)) . ',' . $field . ') > 0';
  180. break;
  181. case 'after':
  182. $wheres[] = 'DATEDIFF(' . $this->db->quote($this->db->escape($match, true)) . ',' . $field . ') < 0';
  183. break;
  184. case 'today':
  185. $wheres[] = 'DATE(' . $field . ') = DATE(NOW())';
  186. break;
  187. case 'yesterday':
  188. $wheres[] = 'DATE(' . $field . ') = DATE_SUB(CURDATE(), INTERVAL -1 DAY)';
  189. break;
  190. case 'thisweek':
  191. $wheres[] = '(YEAR(' . $field . ') = YEAR(CURDATE()) AND WEEK(' . $field . ') = WEEK(CURDATE()))';
  192. break;
  193. case 'thismonth':
  194. $wheres[] = '(YEAR(' . $field . ') = YEAR(CURDATE()) AND MONTH(' . $field . ') = MONTH(CURDATE()))';
  195. break;
  196. case 'thisyear':
  197. $wheres[] = 'YEAR(' . $field . ') = YEAR(CURDATE())';
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. if (!empty($wheres)) {
  204. $this->filter_where[] = '(' . implode(' OR ', $wheres) . ')';
  205. }
  206. }
  207. /**
  208. * @param $field
  209. * @param $data
  210. */
  211. protected function numberMatch($field, $data)
  212. {
  213. $wheres = array();
  214. foreach ($data as $options) {
  215. foreach ($options as $type => $match) {
  216. if (!empty($match)) {
  217. switch ($type) {
  218. case 'equals':
  219. $wheres[] = $field . '=' . (float)$this->db->escape($match, true);
  220. break;
  221. case 'greaterthan':
  222. $wheres[] = $field . ' > ' . (float)$this->db->escape($match, true);
  223. break;
  224. case 'lessthan':
  225. $wheres[] = $field . ' < ' . (float)$this->db->escape($match, true);
  226. break;
  227. case 'isnot':
  228. $wheres[] = $field . ' != ' . (float)$this->db->escape($match, true);
  229. break;
  230. }
  231. }
  232. }
  233. }
  234. if (!empty($wheres)) {
  235. $this->filter_where[] = '(' . implode(' OR ', $wheres) . ')';
  236. }
  237. }
  238. /**
  239. * @param $field
  240. * @param $data
  241. */
  242. protected function textMatch($field, $data)
  243. {
  244. $wheres = array();
  245. foreach ($data as $options) {
  246. foreach ($options as $type => $match) {
  247. $match = trim($match);
  248. if (!empty($match)) {
  249. switch ($type) {
  250. case 'matches':
  251. break;
  252. case 'contains':
  253. $wheres[] = $field . ' like ' . $this->db->quote('%' . $this->db->escape($match, true) . '%');
  254. break;
  255. case 'beginswith':
  256. $wheres[] = $field . ' like ' . $this->db->quote($this->db->escape($match, true) . '%');
  257. break;
  258. case 'endswith':
  259. $wheres[] = $field . ' like ' . $this->db->quote('%' . $this->db->escape($match, true));
  260. break;
  261. case 'is':
  262. $wheres[] = $field . ' = ' . $this->db->quote($this->db->escape($match, true));
  263. break;
  264. }
  265. }
  266. }
  267. }
  268. if (!empty($wheres)) {
  269. $this->filter_where[] = '(' . implode(' OR ', $wheres) . ')';
  270. }
  271. }
  272. /**
  273. * @param $field
  274. * @param $data
  275. */
  276. protected function booleanMatch($field, $data)
  277. {
  278. $options = array();
  279. foreach ($data as $entry) {
  280. $options[$entry] = ($entry == 'yes') ? 1 : 0;
  281. }
  282. $this->filter_where[] = $field . ' IN (' . implode(',', $options) . ')';
  283. }
  284. /**
  285. * @param $field
  286. * @param $data
  287. */
  288. protected function stringMatch($field, $data)
  289. {
  290. $wheres = array();
  291. foreach ($data as $match) {
  292. $match = trim($match);
  293. if (!empty($match)) {
  294. $wheres[] = $field . ' LIKE ' . $this->db->quote('%' . $this->db->escape($match, true) . '%');
  295. }
  296. }
  297. if (!empty($wheres)) {
  298. $this->filter_where[] = '(' . implode(' OR ', $wheres) . ')';
  299. }
  300. }
  301. /**
  302. * @param $field
  303. * @param $data
  304. */
  305. protected function normalSortBy($field, $data)
  306. {
  307. $sort = $field;
  308. $sort .= ($data[0] == 'descending') ? ' DESC' : ' ASC';
  309. $this->sort_order[] = $sort;
  310. }
  311. /**
  312. * @param boolean $manualSort
  313. */
  314. public function setManualSort($manualSort)
  315. {
  316. $this->manualSort = $manualSort;
  317. }
  318. /**
  319. * @param int $moduleId
  320. */
  321. public function setModuleId($moduleId)
  322. {
  323. $this->moduleId = $moduleId;
  324. }
  325. /**
  326. * @param $displayedIds
  327. */
  328. public function setDisplayedIds($displayedIds)
  329. {
  330. $this->displayedIds = $displayedIds;
  331. }
  332. /**
  333. * @param string $manualAppend
  334. */
  335. public function setManualAppend($manualAppend)
  336. {
  337. $this->manualAppend = $manualAppend;
  338. }
  339. }