/models/DataObject/ClassDefinition/Data/Checkbox.php

https://github.com/pimcore/pimcore · PHP · 333 lines · 129 code · 39 blank · 165 comment · 5 complexity · a7b2a7eb17937fb0709b8c80a1e4b028 MD5 · raw file

  1. <?php
  2. /**
  3. * Pimcore
  4. *
  5. * This source file is available under two different licenses:
  6. * - GNU General Public License version 3 (GPLv3)
  7. * - Pimcore Commercial License (PCL)
  8. * Full copyright and license information is available in
  9. * LICENSE.md which is distributed with this source code.
  10. *
  11. * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12. * @license http://www.pimcore.org/license GPLv3 and PCL
  13. */
  14. namespace Pimcore\Model\DataObject\ClassDefinition\Data;
  15. use Pimcore\Model;
  16. use Pimcore\Model\DataObject;
  17. use Pimcore\Model\DataObject\ClassDefinition\Data;
  18. use Pimcore\Normalizer\NormalizerInterface;
  19. class Checkbox extends Data implements ResourcePersistenceAwareInterface, QueryResourcePersistenceAwareInterface, TypeDeclarationSupportInterface, EqualComparisonInterface, VarExporterInterface, NormalizerInterface
  20. {
  21. use DataObject\Traits\DefaultValueTrait;
  22. use DataObject\Traits\SimpleNormalizerTrait;
  23. use Extension\ColumnType;
  24. use Extension\QueryColumnType;
  25. /**
  26. * Static type of this element
  27. *
  28. * @internal
  29. *
  30. * @var string
  31. */
  32. public $fieldtype = 'checkbox';
  33. /**
  34. * @internal
  35. *
  36. * @var int|null
  37. */
  38. public $defaultValue;
  39. /**
  40. * Type for the column to query
  41. *
  42. * @internal
  43. *
  44. * @var string
  45. */
  46. public $queryColumnType = 'tinyint(1)';
  47. /**
  48. * Type for the column
  49. *
  50. * @internal
  51. *
  52. * @var string
  53. */
  54. public $columnType = 'tinyint(1)';
  55. /**
  56. * @return int|null
  57. */
  58. public function getDefaultValue()
  59. {
  60. return $this->defaultValue;
  61. }
  62. /**
  63. * @param mixed $defaultValue
  64. *
  65. * @return $this
  66. */
  67. public function setDefaultValue($defaultValue)
  68. {
  69. if (!is_numeric($defaultValue)) {
  70. $defaultValue = null;
  71. }
  72. $this->defaultValue = $defaultValue;
  73. return $this;
  74. }
  75. /**
  76. * @see ResourcePersistenceAwareInterface::getDataForResource
  77. *
  78. * @param bool $data
  79. * @param null|DataObject\Concrete $object
  80. * @param mixed $params
  81. *
  82. * @return int
  83. */
  84. public function getDataForResource($data, $object = null, $params = [])
  85. {
  86. $data = $this->handleDefaultValue($data, $object, $params);
  87. return is_null($data) ? null : (int)$data;
  88. }
  89. /**
  90. * @see ResourcePersistenceAwareInterface::getDataFromResource
  91. *
  92. * @param bool|null $data
  93. * @param null|DataObject\Concrete $object
  94. * @param mixed $params
  95. *
  96. * @return bool
  97. */
  98. public function getDataFromResource($data, $object = null, $params = [])
  99. {
  100. if (!is_null($data)) {
  101. $data = (bool) $data;
  102. }
  103. return $data;
  104. }
  105. /**
  106. * @see QueryResourcePersistenceAwareInterface::getDataForQueryResource
  107. *
  108. * @param bool $data
  109. * @param null|DataObject\Concrete $object
  110. * @param mixed $params
  111. *
  112. * @return int
  113. */
  114. public function getDataForQueryResource($data, $object = null, $params = [])
  115. {
  116. return $this->getDataForResource($data, $object, $params);
  117. }
  118. /**
  119. * @see Data::getDataForEditmode
  120. *
  121. * @param bool $data
  122. * @param null|DataObject\Concrete $object
  123. * @param mixed $params
  124. *
  125. * @return int
  126. */
  127. public function getDataForEditmode($data, $object = null, $params = [])
  128. {
  129. return $this->getDataForResource($data, $object, $params);
  130. }
  131. /**
  132. * @see Data::getDataFromEditmode
  133. *
  134. * @param bool $data
  135. * @param null|DataObject\Concrete $object
  136. * @param mixed $params
  137. *
  138. * @return bool
  139. */
  140. public function getDataFromEditmode($data, $object = null, $params = [])
  141. {
  142. return $this->getDataFromResource($data, $object, $params);
  143. }
  144. /**
  145. * @see Data::getVersionPreview
  146. *
  147. * @param bool $data
  148. * @param DataObject\Concrete|null $object
  149. * @param mixed $params
  150. *
  151. * @return string
  152. */
  153. public function getVersionPreview($data, $object = null, $params = [])
  154. {
  155. return (string)$data;
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function checkValidity($data, $omitMandatoryCheck = false, $params = [])
  161. {
  162. if (!$omitMandatoryCheck && $this->getMandatory() && $data === null) {
  163. throw new Model\Element\ValidationException('Empty mandatory field [ ' . $this->getName() . ' ]');
  164. }
  165. /* @todo seems to cause problems with old installations
  166. if(!is_bool($data) and $data !== 1 and $data !== 0){
  167. throw new \Exception(get_class($this).": invalid data");
  168. }*/
  169. }
  170. /**
  171. * {@inheritdoc}
  172. */
  173. public function getForCsvExport($object, $params = [])
  174. {
  175. $data = $this->getDataFromObjectParam($object, $params);
  176. return (string)$data;
  177. }
  178. /**
  179. * {@inheritdoc}
  180. */
  181. public function isDiffChangeAllowed($object, $params = [])
  182. {
  183. return true;
  184. }
  185. /**
  186. * @param DataObject\ClassDefinition\Data\Checkbox $masterDefinition
  187. */
  188. public function synchronizeWithMasterDefinition(DataObject\ClassDefinition\Data $masterDefinition)
  189. {
  190. $this->defaultValue = $masterDefinition->defaultValue;
  191. }
  192. /**
  193. * returns sql query statement to filter according to this data types value(s)
  194. *
  195. * @param string $value
  196. * @param string $operator
  197. * @param array $params
  198. *
  199. * @return string
  200. *
  201. */
  202. public function getFilterCondition($value, $operator, $params = [])
  203. {
  204. $params['name'] = $this->name;
  205. return $this->getFilterConditionExt(
  206. $value,
  207. $operator,
  208. $params
  209. );
  210. }
  211. /**
  212. * returns sql query statement to filter according to this data types value(s)
  213. *
  214. * @param string $value
  215. * @param string $operator
  216. * @param array $params optional params used to change the behavior
  217. *
  218. * @return string
  219. */
  220. public function getFilterConditionExt($value, $operator, $params = [])
  221. {
  222. $db = \Pimcore\Db::get();
  223. $value = $db->quote($value);
  224. $key = $db->quoteIdentifier($this->name);
  225. $brickPrefix = $params['brickPrefix'] ? $db->quoteIdentifier($params['brickPrefix']) . '.' : '';
  226. return 'IFNULL(' . $brickPrefix . $key . ', 0) = ' . $value . ' ';
  227. }
  228. /**
  229. * {@inheritdoc}
  230. */
  231. public function getDataForSearchIndex($object, $params = [])
  232. {
  233. return '';
  234. }
  235. /**
  236. * {@inheritdoc}
  237. */
  238. public function isEmpty($data)
  239. {
  240. return $data === null;
  241. }
  242. /**
  243. * {@inheritdoc}
  244. */
  245. public function isFilterable(): bool
  246. {
  247. return true;
  248. }
  249. /**
  250. * {@inheritdoc}
  251. */
  252. protected function doGetDefaultValue($object, $context = [])
  253. {
  254. return $this->getDefaultValue() ?? null;
  255. }
  256. /**
  257. * @param bool|null $oldValue
  258. * @param bool|null $newValue
  259. *
  260. * @return bool
  261. */
  262. public function isEqual($oldValue, $newValue): bool
  263. {
  264. return $oldValue === $newValue;
  265. }
  266. /**
  267. * {@inheritdoc}
  268. */
  269. public function getParameterTypeDeclaration(): ?string
  270. {
  271. return '?bool';
  272. }
  273. /**
  274. * {@inheritdoc}
  275. */
  276. public function getReturnTypeDeclaration(): ?string
  277. {
  278. return '?bool';
  279. }
  280. /**
  281. * {@inheritdoc}
  282. */
  283. public function getPhpdocInputType(): ?string
  284. {
  285. return 'bool|null';
  286. }
  287. /**
  288. * {@inheritdoc}
  289. */
  290. public function getPhpdocReturnType(): ?string
  291. {
  292. return 'bool|null';
  293. }
  294. }