/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
- <?php
- /**
- * Pimcore
- *
- * This source file is available under two different licenses:
- * - GNU General Public License version 3 (GPLv3)
- * - Pimcore Commercial License (PCL)
- * Full copyright and license information is available in
- * LICENSE.md which is distributed with this source code.
- *
- * @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
- * @license http://www.pimcore.org/license GPLv3 and PCL
- */
- namespace Pimcore\Model\DataObject\ClassDefinition\Data;
- use Pimcore\Model;
- use Pimcore\Model\DataObject;
- use Pimcore\Model\DataObject\ClassDefinition\Data;
- use Pimcore\Normalizer\NormalizerInterface;
- class Checkbox extends Data implements ResourcePersistenceAwareInterface, QueryResourcePersistenceAwareInterface, TypeDeclarationSupportInterface, EqualComparisonInterface, VarExporterInterface, NormalizerInterface
- {
- use DataObject\Traits\DefaultValueTrait;
- use DataObject\Traits\SimpleNormalizerTrait;
- use Extension\ColumnType;
- use Extension\QueryColumnType;
- /**
- * Static type of this element
- *
- * @internal
- *
- * @var string
- */
- public $fieldtype = 'checkbox';
- /**
- * @internal
- *
- * @var int|null
- */
- public $defaultValue;
- /**
- * Type for the column to query
- *
- * @internal
- *
- * @var string
- */
- public $queryColumnType = 'tinyint(1)';
- /**
- * Type for the column
- *
- * @internal
- *
- * @var string
- */
- public $columnType = 'tinyint(1)';
- /**
- * @return int|null
- */
- public function getDefaultValue()
- {
- return $this->defaultValue;
- }
- /**
- * @param mixed $defaultValue
- *
- * @return $this
- */
- public function setDefaultValue($defaultValue)
- {
- if (!is_numeric($defaultValue)) {
- $defaultValue = null;
- }
- $this->defaultValue = $defaultValue;
- return $this;
- }
- /**
- * @see ResourcePersistenceAwareInterface::getDataForResource
- *
- * @param bool $data
- * @param null|DataObject\Concrete $object
- * @param mixed $params
- *
- * @return int
- */
- public function getDataForResource($data, $object = null, $params = [])
- {
- $data = $this->handleDefaultValue($data, $object, $params);
- return is_null($data) ? null : (int)$data;
- }
- /**
- * @see ResourcePersistenceAwareInterface::getDataFromResource
- *
- * @param bool|null $data
- * @param null|DataObject\Concrete $object
- * @param mixed $params
- *
- * @return bool
- */
- public function getDataFromResource($data, $object = null, $params = [])
- {
- if (!is_null($data)) {
- $data = (bool) $data;
- }
- return $data;
- }
- /**
- * @see QueryResourcePersistenceAwareInterface::getDataForQueryResource
- *
- * @param bool $data
- * @param null|DataObject\Concrete $object
- * @param mixed $params
- *
- * @return int
- */
- public function getDataForQueryResource($data, $object = null, $params = [])
- {
- return $this->getDataForResource($data, $object, $params);
- }
- /**
- * @see Data::getDataForEditmode
- *
- * @param bool $data
- * @param null|DataObject\Concrete $object
- * @param mixed $params
- *
- * @return int
- */
- public function getDataForEditmode($data, $object = null, $params = [])
- {
- return $this->getDataForResource($data, $object, $params);
- }
- /**
- * @see Data::getDataFromEditmode
- *
- * @param bool $data
- * @param null|DataObject\Concrete $object
- * @param mixed $params
- *
- * @return bool
- */
- public function getDataFromEditmode($data, $object = null, $params = [])
- {
- return $this->getDataFromResource($data, $object, $params);
- }
- /**
- * @see Data::getVersionPreview
- *
- * @param bool $data
- * @param DataObject\Concrete|null $object
- * @param mixed $params
- *
- * @return string
- */
- public function getVersionPreview($data, $object = null, $params = [])
- {
- return (string)$data;
- }
- /**
- * {@inheritdoc}
- */
- public function checkValidity($data, $omitMandatoryCheck = false, $params = [])
- {
- if (!$omitMandatoryCheck && $this->getMandatory() && $data === null) {
- throw new Model\Element\ValidationException('Empty mandatory field [ ' . $this->getName() . ' ]');
- }
- /* @todo seems to cause problems with old installations
- if(!is_bool($data) and $data !== 1 and $data !== 0){
- throw new \Exception(get_class($this).": invalid data");
- }*/
- }
- /**
- * {@inheritdoc}
- */
- public function getForCsvExport($object, $params = [])
- {
- $data = $this->getDataFromObjectParam($object, $params);
- return (string)$data;
- }
- /**
- * {@inheritdoc}
- */
- public function isDiffChangeAllowed($object, $params = [])
- {
- return true;
- }
- /**
- * @param DataObject\ClassDefinition\Data\Checkbox $masterDefinition
- */
- public function synchronizeWithMasterDefinition(DataObject\ClassDefinition\Data $masterDefinition)
- {
- $this->defaultValue = $masterDefinition->defaultValue;
- }
- /**
- * returns sql query statement to filter according to this data types value(s)
- *
- * @param string $value
- * @param string $operator
- * @param array $params
- *
- * @return string
- *
- */
- public function getFilterCondition($value, $operator, $params = [])
- {
- $params['name'] = $this->name;
- return $this->getFilterConditionExt(
- $value,
- $operator,
- $params
- );
- }
- /**
- * returns sql query statement to filter according to this data types value(s)
- *
- * @param string $value
- * @param string $operator
- * @param array $params optional params used to change the behavior
- *
- * @return string
- */
- public function getFilterConditionExt($value, $operator, $params = [])
- {
- $db = \Pimcore\Db::get();
- $value = $db->quote($value);
- $key = $db->quoteIdentifier($this->name);
- $brickPrefix = $params['brickPrefix'] ? $db->quoteIdentifier($params['brickPrefix']) . '.' : '';
- return 'IFNULL(' . $brickPrefix . $key . ', 0) = ' . $value . ' ';
- }
- /**
- * {@inheritdoc}
- */
- public function getDataForSearchIndex($object, $params = [])
- {
- return '';
- }
- /**
- * {@inheritdoc}
- */
- public function isEmpty($data)
- {
- return $data === null;
- }
- /**
- * {@inheritdoc}
- */
- public function isFilterable(): bool
- {
- return true;
- }
- /**
- * {@inheritdoc}
- */
- protected function doGetDefaultValue($object, $context = [])
- {
- return $this->getDefaultValue() ?? null;
- }
- /**
- * @param bool|null $oldValue
- * @param bool|null $newValue
- *
- * @return bool
- */
- public function isEqual($oldValue, $newValue): bool
- {
- return $oldValue === $newValue;
- }
- /**
- * {@inheritdoc}
- */
- public function getParameterTypeDeclaration(): ?string
- {
- return '?bool';
- }
- /**
- * {@inheritdoc}
- */
- public function getReturnTypeDeclaration(): ?string
- {
- return '?bool';
- }
- /**
- * {@inheritdoc}
- */
- public function getPhpdocInputType(): ?string
- {
- return 'bool|null';
- }
- /**
- * {@inheritdoc}
- */
- public function getPhpdocReturnType(): ?string
- {
- return 'bool|null';
- }
- }