/** * Encode a value from the widget to native data of the data provider via event. * * @param string $property The property. * * @param mixed $value The value of the property. * * @return mixed */ public function encodeValue($property, $value) { $environment = $this->getEnvironment(); $event = new EncodePropertyValueFromWidgetEvent($environment, $this->model); $event->setProperty($property)->setValue($value); $environment->getEventPropagator()->propagate($event::NAME, $event, array($environment->getDataDefinition()->getName(), $property)); return $event->getValue(); }
/** * Encode a value from the widget to native data of the data provider via event. * * @param string $property The property. * * @param mixed $value The value of the property. * * @param PropertyValueBag $propertyValues The property value bag the property value originates from. * * @return mixed */ public function encodeValue($property, $value, PropertyValueBag $propertyValues) { $environment = $this->getEnvironment(); $event = new EncodePropertyValueFromWidgetEvent($environment, $this->model, $propertyValues); $event->setProperty($property)->setValue($value); $environment->getEventDispatcher()->dispatch(EncodePropertyValueFromWidgetEvent::NAME, $event); return $event->getValue(); }
/** * Convert file source to uuid before save the model. * After convert this is an insert tag {{file::uuid}}. * * @param EncodePropertyValueFromWidgetEvent $event The event to handle. * * @return void */ public function convertFileSourceToUuid(EncodePropertyValueFromWidgetEvent $event) { $environment = $event->getEnvironment(); $dataDefinition = $environment->getDataDefinition(); $propertiesDefinition = $dataDefinition->getPropertiesDefinition(); $property = $propertiesDefinition->getProperty($event->getProperty()); if (!array_key_exists('rte', $property->getExtra()) || strpos($property->getExtra()['rte'], 'tiny') !== 0) { return; } $event->setValue(StringUtil::srcToInsertTag($event->getValue())); }
public function checkCsvColumnEmail(EncodePropertyValueFromWidgetEvent $event) { $value = $event->getValue(); if (!is_array($value)) { $value = deserialize($value, true); } foreach ($value as $item) { if ($item['field'] == 'email') { return; } } throw new \RuntimeException($GLOBALS['TL_LANG']['orm_avisota_recipient_source']['missing_email_column']); }
/** * Check that the parsing of the callbacks is working. * * @return void */ public function testCallbackParsing() { $this->aliasContaoClass('Session'); $this->aliasContaoClass('System'); $this->aliasContaoClass('Controller'); $this->aliasContaoClass('Backend'); $this->aliasContaoClass('DataContainer'); $dispatcher = new EventDispatcher(); $container = new DefaultContainer('tl_test'); $event = new BuildDataDefinitionEvent($container); $builder = $this->mockBuilderWithDca(array('fields' => array('testProperty' => array('save_callback' => array(function () { return 'executed'; })))), $event::NAME, $dispatcher); $builder->build($event->getContainer(), $event); $environment = new DefaultEnvironment(); $environment->setDataDefinition($container); $event = new EncodePropertyValueFromWidgetEvent($environment, new DefaultModel(), new PropertyValueBag()); $event->setProperty('testProperty'); $this->assertEquals(1, count($dispatcher->getListeners(EncodePropertyValueFromWidgetEvent::NAME))); foreach ($dispatcher->getListeners(EncodePropertyValueFromWidgetEvent::NAME) as $listener) { /** @var AbstractCallbackListener $listener */ $this->assertTrue($listener->wantToExecute($event)); $event->setValue('testvalue'); $listener($event); $this->assertEquals('executed', $event->getValue()); } $event->setProperty('testProperty2'); foreach ($dispatcher->getListeners(EncodePropertyValueFromWidgetEvent::NAME) as $listener) { /** @var AbstractCallbackListener $listener */ $this->assertFalse($listener->wantToExecute($event)); } }
/** * Update the value in the event. * * @param EncodePropertyValueFromWidgetEvent $event The event being emitted. * * @param mixed $value The encoded value. * * @return void */ public function update($event, $value) { $event->setValue($value); }
/** * Encode the values into a serialized array. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void */ public function saveValues(EncodePropertyValueFromWidgetEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_attribute' || $event->getProperty() !== 'translatedtabletext_cols') { return; } $objMetaModel = $this->getMetaModelById($event->getModel()->getProperty('pid')); $varValue = $event->getValue(); // Not translated, make it a plain string. if (!$objMetaModel->isTranslated()) { $event->setValue(serialize($varValue)); return; } $arrLangValues = deserialize($varValue); $arrOutput = array(); foreach ($arrLangValues as $varSubValue) { $strLangCode = $varSubValue['langcode']; unset($varSubValue['langcode']); if (count($varSubValue) > 1) { $arrOutput[$strLangCode] = $varSubValue; } else { $arrKeys = array_keys($varSubValue); $arrOutput[$strLangCode] = $varSubValue[$arrKeys[0]]; } } $event->setValue(serialize($arrOutput)); }
/** * Encode the title value. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void */ public function encodeLegendTitleValue(EncodePropertyValueFromWidgetEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_dcasetting' || $event->getProperty() !== 'legendtitle') { return; } $metaModel = $this->getMetaModelFromModel($event->getModel()); $values = Helper::encodeLangArray($event->getValue(), $metaModel); $event->setValue($values); }
/** * The subscriber creates the date from a timestamp. * * @param string $format The given date format. * @param string $value The given date example * * @dataProvider dataProvider * @test */ public function it_parses_timestamp_for_widget($format, $value) { $valuesBag = $this->getMock('ContaoCommunityAlliance\\DcGeneral\\Data\\PropertyValueBagInterface', array(), array()); // Attribute will return timestamp, create it. $dateTime = \DateTime::createFromFormat($format, $value); $timestamp = $dateTime->getTimestamp(); $attribute = $this->mockAttribute($format); $attribute->expects($this->any())->method('valueToWidget')->will($this->returnValue($timestamp)); $model = $this->mockModelWithAttribute($attribute); $event = new EncodePropertyValueFromWidgetEvent($this->mockEnvironment(), $model, $valuesBag); $event->setProperty('date'); $event->setValue($value); $this->backendSubscriber->handleEncodePropertyValueFromWidget($event); $this->assertEquals($timestamp, $event->getValue()); }
/** * Translates an generated alias {@see getAttributeNames()} to the corresponding attribute id. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void */ public function encodeAttributeIdValue(EncodePropertyValueFromWidgetEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_filtersetting' || $event->getProperty() !== 'attr_id') { return; } $model = $event->getModel(); $metaModel = $this->getMetaModel($model); $value = $event->getValue(); if (!($metaModel && $value)) { return; } $value = substr($value, strlen($metaModel->getTableName() . '_')); $attribute = $metaModel->getAttribute($value); if ($attribute) { $event->setValue($attribute->get('id')); } }
/** * Check if the select_where value is valid by firing a test query. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void * * @throws \RuntimeException When the where condition is invalid. * * @SuppressWarnings(PHPMD.Superglobals) * @SuppressWarnings(PHPMD.CamelCaseVariableName) */ public function checkQuery(EncodePropertyValueFromWidgetEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_attribute' || $event->getProperty() !== 'tag_where') { return; } $where = $event->getValue(); $values = $event->getPropertyValueBag(); if ($where) { $objDB = \Database::getInstance(); $strTableName = $values->getPropertyValue('tag_table'); $strColNameId = $values->getPropertyValue('tag_id'); $strSortColumn = $values->getPropertyValue('tag_sorting') ?: $strColNameId; $query = sprintf('SELECT %1$s.* FROM %1$s%2$s ORDER BY %1$s.%3$s', $strTableName, $where ? ' WHERE (' . $where . ')' : false, $strSortColumn); try { $objDB->prepare($query)->execute(); } catch (\Exception $e) { throw new \RuntimeException(sprintf('%s %s', $GLOBALS['TL_LANG']['tl_metamodel_attribute']['sql_error'], $e->getMessage())); } } }
/** * Translates an generated alias to the corresponding attribute id. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void */ public function encodeAttrValue(EncodePropertyValueFromWidgetEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_dca_sortgroup' || $event->getProperty() !== 'rendergroupattr' && $event->getProperty() !== 'rendersortattr') { return; } $metaModel = self::getMetaModel($event->getEnvironment()); $value = $event->getValue(); if (!($metaModel && $value)) { return; } $value = substr($value, strlen($metaModel->getTableName() . '_')); $attribute = $metaModel->getAttribute($value); if ($attribute) { $event->setValue($attribute->get('id')); } }
/** * Get the supported attribute or null. * * @param EncodePropertyValueFromWidgetEvent|DecodePropertyValueForWidgetEvent $event The subscribed event. * * @return Timestamp|null */ private function getSupportedAttribute($event) { $model = $event->getModel(); // Not a metamodel model. if (!$model instanceof Model) { return null; } $property = $event->getProperty(); $attribute = $model->getItem()->getAttribute($property); if ($attribute instanceof Timestamp) { return $attribute; } return null; }
/** * Encode the given value from a real language array into a serialized language array. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void * * @throws \RuntimeException When the column name is illegal or duplicate. */ public function encodeColumnNameValue(EncodePropertyValueFromWidgetEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_attribute' || $event->getProperty() !== 'colname') { return; } $oldColumnName = $event->getModel()->getProperty($event->getProperty()); $columnName = $event->getValue(); $metaModel = $this->getMetaModelByModelPid($event->getModel()); if (!$columnName || $oldColumnName !== $columnName) { TableManipulation::checkColumnDoesNotExist($metaModel->getTableName(), $columnName); $colNames = array_keys($metaModel->getAttributes()); if (in_array($columnName, $colNames)) { throw new \RuntimeException(sprintf($event->getEnvironment()->getTranslator()->translate('columnExists', 'ERR'), $columnName, $metaModel->getTableName())); } } }
/** * Called by tl_metamodel.tableName onsave_callback. * * Prefixes the table name with mm_ if not provided by the user as such. * Checks if the table name is legal to the DB. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void * * @throws \RuntimeException When no table name has been given. */ public function ensureTableNamePrefix(EncodePropertyValueFromWidgetEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel' || $event->getProperty() !== 'tableName') { return; } // See #49. $tableName = strtolower($event->getValue()); if (!strlen($tableName)) { throw new \RuntimeException('Table name not given'); } // Force mm_ prefix. if (substr($tableName, 0, 3) !== 'mm_') { $tableName = 'mm_' . $tableName; } $dataProvider = $event->getEnvironment()->getDataProvider('tl_metamodel'); // New model, ensure the table does not exist. if (!$event->getModel()->getId()) { TableManipulation::checkTableDoesNotExist($tableName); } else { // Edited model, ensure the value is unique and then that the table does not exist. $oldVersion = $dataProvider->fetch($dataProvider->getEmptyConfig()->setId($event->getModel()->getId())); if ($oldVersion->getProperty('tableName') !== $event->getModel()->getProperty('tableName')) { TableManipulation::checkTableDoesNotExist($tableName); } } $event->setValue($tableName); }
/** * Translates the values of the jumpTo entries into the internal array. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void */ public function encodeJumpToValue(EncodePropertyValueFromWidgetEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_rendersettings' || $event->getProperty() !== 'jumpTo') { return; } $value = deserialize($event->getValue(), true); foreach ($value as $k => $v) { $value[$k]['value'] = str_replace(array('{{link_url::', '}}'), array('', ''), $v['value']); } $event->setValue(serialize($value)); }
/** * Translates an generated alias {@see getAttributeNames()} to the corresponding attribute id. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void */ public function encodeAttributeIdValue(EncodePropertyValueFromWidgetEvent $event) { $isAllowed = $this->isAllowedContext($event->getEnvironment()->getDataDefinition(), $event->getProperty(), $event->getModel()); if (!$isAllowed) { return; } $model = $event->getModel(); $metaModel = $this->getMetaModel($model); $value = $event->getValue(); if (!($metaModel && $value)) { return; } $value = substr($value, strlen($metaModel->getTableName() . '_')); $attribute = $metaModel->getAttribute($value); if ($attribute) { $event->setValue($attribute->get('id')); } }
/** * Check if column email exists in the csv file. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void */ public function checkCsvColumnEmail(EncodePropertyValueFromWidgetEvent $event) { $environment = $event->getEnvironment(); $dataProvider = $environment->getDataProvider(); if ($dataProvider !== 'orm_avisota_recipient_source' || $dataProvider === 'orm_avisota_recipient_source' && $event->getProperty() != 'csvColumnAssignment') { return; } $value = $event->getValue(); if (!is_array($value)) { $value = deserialize($value, true); } foreach ($value as $item) { if ($item['field'] == 'email') { return; } } $translator = $environment->getTranslator(); throw new \RuntimeException($translator->translate('missing_email_column', 'orm_avisota_recipient_source')); }
/** * Handle event to update the sorting for DCA combinations. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void */ public function fixSortingInRows(EncodePropertyValueFromWidgetEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_dca_combine' || $event->getProperty() !== 'rows') { return; } $values = $event->getValue(); $index = 0; $time = time(); foreach (array_keys($values) as $key) { $values[$key]['sorting'] = $index; $values[$key]['tstamp'] = $time; $index += 128; } $event->setValue($values); }
/** * Translates an generated alias to the corresponding value. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void */ public function encodeValueValue(EncodePropertyValueFromWidgetEvent $event) { if ($event->getEnvironment()->getDataDefinition()->getName() !== 'tl_metamodel_dcasetting_condition' || $event->getProperty() !== 'value') { return; } if (is_array($event->getValue())) { $values = array(); foreach ($event->getValue() as $value) { $values[] = substr($value, 6); } // Cut off the 'value_' prefix. $event->setValue($values); } else { // Cut off the 'value_' prefix. $event->setValue(substr($event->getValue(), 6)); } }