/** * 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())); }
/** * 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 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')); } }
/** * 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)); }
/** * 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 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); }
/** * 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')); } }
/** * 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)); } }
/** * Encode the given value from a real language array into a serialized language array. * * @param EncodePropertyValueFromWidgetEvent $event The event. * * @return void */ protected function encodeValue(EncodePropertyValueFromWidgetEvent $event) { $metaModel = $this->getMetaModelByModelPid($event->getModel()); $values = Helper::encodeLangArray($event->getValue(), $metaModel); $event->setValue($values); }
/** * 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')); } }
/** * 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()); }