PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/src/PrestaShopBundle/Entity/AdminFilter.php

https://bitbucket.org/tippycracker/autokraitis
PHP | 347 lines | 150 code | 39 blank | 158 comment | 7 complexity | 7fe9bd6d7c90c5960b1123bf7142bc3c MD5 | raw file
Possible License(s): BSD-2-Clause, GPL-2.0, GPL-3.0, BSD-3-Clause, Apache-2.0
  1. <?php
  2. /**
  3. * 2007-2017 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * https://opensource.org/licenses/OSL-3.0
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2017 PrestaShop SA
  23. * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. namespace PrestaShopBundle\Entity;
  27. use Doctrine\ORM\Mapping as ORM;
  28. /**
  29. * AdminFilter.
  30. *
  31. * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(name="admin_filter_search_idx", columns={"employee", "shop", "controller", "action"})})
  32. * @ORM\Entity
  33. */
  34. class AdminFilter
  35. {
  36. /**
  37. * @var int
  38. *
  39. * @ORM\Id
  40. * @ORM\Column(name="id", type="integer")
  41. * @ORM\GeneratedValue(strategy="AUTO")
  42. */
  43. private $id;
  44. /**
  45. * @var int
  46. *
  47. * @ORM\Column(name="employee", type="integer")
  48. */
  49. private $employee;
  50. /**
  51. * @var int
  52. *
  53. * @ORM\Column(name="shop", type="integer")
  54. */
  55. private $shop;
  56. /**
  57. * @var string
  58. *
  59. * @ORM\Column(name="controller", type="string", length=60)
  60. */
  61. private $controller;
  62. /**
  63. * @var string
  64. *
  65. * @ORM\Column(name="action", type="string", length=100)
  66. */
  67. private $action;
  68. /**
  69. * @var string
  70. *
  71. * @ORM\Column(name="filter", type="text")
  72. */
  73. private $filter;
  74. /**
  75. * Get id.
  76. *
  77. * @return int
  78. */
  79. public function getId()
  80. {
  81. return $this->id;
  82. }
  83. /**
  84. * Set employee.
  85. *
  86. * @param int $employee
  87. *
  88. * @return AdminFilter
  89. */
  90. public function setEmployee($employee)
  91. {
  92. $this->employee = $employee;
  93. return $this;
  94. }
  95. /**
  96. * Get employee.
  97. *
  98. * @return int
  99. */
  100. public function getEmployee()
  101. {
  102. return $this->employee;
  103. }
  104. /**
  105. * Set shop.
  106. *
  107. * @param int $shop
  108. *
  109. * @return AdminFilter
  110. */
  111. public function setShop($shop)
  112. {
  113. $this->shop = $shop;
  114. return $this;
  115. }
  116. /**
  117. * Get shop.
  118. *
  119. * @return int
  120. */
  121. public function getShop()
  122. {
  123. return $this->shop;
  124. }
  125. /**
  126. * Set controller.
  127. *
  128. * @param string $controller
  129. *
  130. * @return AdminFilter
  131. */
  132. public function setController($controller)
  133. {
  134. $this->controller = $controller;
  135. return $this;
  136. }
  137. /**
  138. * Get controller.
  139. *
  140. * @return string
  141. */
  142. public function getController()
  143. {
  144. return $this->controller;
  145. }
  146. /**
  147. * Set action.
  148. *
  149. * @param string $action
  150. *
  151. * @return AdminFilter
  152. */
  153. public function setAction($action)
  154. {
  155. $this->action = $action;
  156. return $this;
  157. }
  158. /**
  159. * Get action.
  160. *
  161. * @return string
  162. */
  163. public function getAction()
  164. {
  165. return $this->action;
  166. }
  167. /**
  168. * Set filter.
  169. *
  170. * @param string $filter
  171. *
  172. * @return AdminFilter
  173. */
  174. public function setFilter($filter)
  175. {
  176. $this->filter = $filter;
  177. return $this;
  178. }
  179. /**
  180. * Get filter.
  181. *
  182. * @return string
  183. */
  184. public function getFilter()
  185. {
  186. return $this->filter;
  187. }
  188. /**
  189. * Gets an array with each filter key needed by Product catalog page.
  190. *
  191. * Values are filled with empty strings.
  192. *
  193. * @return array
  194. */
  195. public static function getProductCatalogEmptyFilter()
  196. {
  197. return array(
  198. 'filter_category' => '',
  199. 'filter_column_id_product' => '',
  200. 'filter_column_name' => '',
  201. 'filter_column_reference' => '',
  202. 'filter_column_name_category' => '',
  203. 'filter_column_price' => '',
  204. 'filter_column_sav_quantity' => '',
  205. 'filter_column_active' => '',
  206. 'last_offset' => 0,
  207. 'last_limit' => 20,
  208. 'last_orderBy' => 'id_product',
  209. 'last_sortOrder' => 'desc',
  210. );
  211. }
  212. /**
  213. * Gets an array with filters needed by Product catalog page.
  214. *
  215. * The data is decoded and filled with empty strings if there is no value on each entry
  216. * .
  217. *
  218. * @return array
  219. */
  220. public function getProductCatalogFilter()
  221. {
  222. $decoded = json_decode($this->getFilter(), true);
  223. return array_merge(
  224. $this->getProductCatalogEmptyFilter(),
  225. $decoded
  226. );
  227. }
  228. /**
  229. * Set the filters for Product catalog page into $this->filter.
  230. *
  231. * Filters input data to keep only Product catalog filters, and encode it.
  232. *
  233. * @param $filter
  234. *
  235. * @return AdminFilter tis object for fluent chaining
  236. */
  237. public function setProductCatalogFilter($filter)
  238. {
  239. $filter = array_intersect_key(
  240. $filter,
  241. $this->getProductCatalogEmptyFilter()
  242. );
  243. $filter = self::sanitizeFilterParameters($filter);
  244. return $this->setFilter(json_encode($filter));
  245. }
  246. /**
  247. * Sanitize filter parameters.
  248. *
  249. * @param $filter
  250. *
  251. * @return mixed
  252. */
  253. public static function sanitizeFilterParameters(array $filter)
  254. {
  255. $filterMinMax = function ($filter) {
  256. return function ($subject) use ($filter) {
  257. $operator = null;
  258. if (false !== strpos($subject, '<=')) {
  259. $operator = '<=';
  260. }
  261. if (false !== strpos($subject, '>=')) {
  262. $operator = '>=';
  263. }
  264. if (is_null($operator)) {
  265. $pattern = '#BETWEEN (?P<min>\d+\.?\d*) AND (?P<max>\d+\.?\d*)#';
  266. if (0 === preg_match($pattern, $subject, $matches)) {
  267. return '';
  268. }
  269. return sprintf('BETWEEN %f AND %f', $matches['min'], $matches['max']);
  270. } else {
  271. $subjectWithoutOperator = str_replace($operator, '', $subject);
  272. $flag = FILTER_DEFAULT;
  273. if ($filter === FILTER_SANITIZE_NUMBER_FLOAT) {
  274. $flag = FILTER_FLAG_ALLOW_FRACTION;
  275. }
  276. $filteredSubjectWithoutOperator = filter_var($subjectWithoutOperator, $filter, $flag);
  277. if (!$filteredSubjectWithoutOperator) {
  278. $filteredSubjectWithoutOperator = 0;
  279. }
  280. return $operator.$filteredSubjectWithoutOperator;
  281. }
  282. };
  283. };
  284. return filter_var_array($filter, array(
  285. 'filter_category' => FILTER_SANITIZE_NUMBER_INT,
  286. 'filter_column_id_product' => array(
  287. 'filter' => FILTER_CALLBACK,
  288. 'options' => $filterMinMax(FILTER_SANITIZE_NUMBER_INT),
  289. ),
  290. 'filter_column_name' => FILTER_SANITIZE_STRING,
  291. 'filter_column_reference' => FILTER_SANITIZE_STRING,
  292. 'filter_column_name_category' => FILTER_SANITIZE_STRING,
  293. 'filter_column_price' => array(
  294. 'filter' => FILTER_CALLBACK,
  295. 'options' => $filterMinMax(FILTER_SANITIZE_NUMBER_FLOAT),
  296. ),
  297. 'filter_column_sav_quantity' => array(
  298. 'filter' => FILTER_CALLBACK,
  299. 'options' => $filterMinMax(FILTER_SANITIZE_NUMBER_INT),
  300. ),
  301. 'filter_column_active' => FILTER_SANITIZE_NUMBER_INT,
  302. 'last_offset' => FILTER_SANITIZE_NUMBER_INT,
  303. 'last_limit' => FILTER_SANITIZE_NUMBER_INT,
  304. 'last_orderBy' => FILTER_SANITIZE_STRING,
  305. 'last_sortOrder' => FILTER_SANITIZE_STRING,
  306. ));
  307. }
  308. }