/** * build a config for the target fields of the converter * * @param $intConverter */ public static function createTargetFields($intConverter) { $objConverter = Converter::findByPk($intConverter); $objConverterfields = Converterfield::findBy('pid', $intConverter); // ----------------------------------------- // check if we already have some field definitions, only add new if ($objConverterfields) { while ($objConverterfields->next()) { $arrFieldsSource = $arrFieldsSource ? $arrFieldsSource : unserialize($objConverter->fieldsSource); $arrFieldsTarget = $arrFieldsTarget ? $arrFieldsTarget : unserialize($objConverter->fieldsTarget); $BlnAllowInsert = $BlnAllowInsert ? $BlnAllowInsert : $objConverter->allowInsert; $BlnAllowUpdate = $BlnAllowUpdate ? $BlnAllowUpdate : $objConverter->allowUpdate; foreach ($arrFieldsTarget as $k => $v) { if ($v['name'] == $objConverterfields->fieldname) { unset($arrFieldsTarget[$k]); $sorting[] = $objConverterfields->sorting; } } } rsort($sorting); $sorting = $sorting[0]; } else { $arrFieldsSource = unserialize($objConverter->fieldsSource); $arrFieldsTarget = unserialize($objConverter->fieldsTarget); $BlnAllowInsert = $objConverter->allowInsert; $BlnAllowUpdate = $objConverter->allowUpdate; } // source fields, collect fieldnames foreach ($arrFieldsSource as $fieldSource) { $arrFieldnamesSource[] = $fieldSource['name']; } // pre-define converterdefinitions for existing target fields, only 1:1 foreach ($arrFieldsTarget as $fieldTarget) { $sorting = $sorting + 32; $arrNew = array('pid' => $intConverter, 'sorting' => $sorting, 'tstamp' => time(), 'published' => '', 'fieldname' => $fieldTarget['name'], 'allowInsert' => $BlnAllowInsert ? $BlnAllowInsert : '', 'typeInsert' => 'Insertfromfield', 'fieldInsert' => in_array($fieldTarget['name'], $arrFieldnamesSource) ? $fieldTarget['name'] : '', 'modeInsert' => 'Addnew', 'allowUpdate' => $BlnAllowUpdate ? $BlnAllowUpdate : '', 'typeUpdate' => 'Updatefromfield', 'fieldUpdate' => in_array($fieldTarget['name'], $arrFieldnamesSource) ? $fieldTarget['name'] : '', 'modeUpdate' => 'Replace'); $objNew = new Converterfield(); $objNew->setRow($arrNew); $objNew->save(); } }
/** * Create target field definitions */ public function autoFill() { if (Input::get('key') == 'fill') { ConverterfieldModel::createTargetFields(Input::get('id')); Controller::redirect(str_replace('&key=fill', '', Environment::get('request'))); } return; }
/** * get the converted data * * @param $strType * @param $objRun * @param $objConverter * @param $objSource * @param $strTarget * @return array */ protected static function getData($strType, $objRun, $objConverter, $objSource, $strTarget) { // in case of update the new data may be added to the old, so we need the existent data if ($strType == 'update') { $strKeySource = $objConverter->keySource; $objExistentData = Database::getInstance()->prepare("SELECT * FROM " . $strTarget . " WHERE " . $objConverter->keyTarget . "=?")->limit(1)->execute($objSource->{$strKeySource}); if ($objExistentData->numRows) { $arrExistentData = $objExistentData->fetchAllAssoc(); } } $arrTargetData = array(); // see which fields have to be converted. on update the others aren't touched, on inserts they're cleared (set to db default) $objConverterfields = ConverterfieldModel::findBy('pid', $objConverter->id, array('order' => 'sorting')); // no fields defined if (!$objConverterfields) { return $arrTargetData; } // set the id-field of an internal tmp table if ($objConverter->targetType == 'InternalTable') { $strIdField = 'id'; } else { $strIdField = $objConverter->idField; } while ($objConverterfields->next()) { switch ($strType) { case 'update': if (!$objConverterfields->published || !$objConverterfields->allowUpdate || $objConverterfields->fieldname == $strIdField) { // field is not allowed to be updated - keep the data // on update hands off the id field } else { // determine the class of the conversion $strClass = ($GLOBALS['convertx']['classpath'][$objConverterfields->typeUpdate] ? $GLOBALS['convertx']['classpath'][$objConverterfields->typeUpdate] : 'Delahaye\\ConvertX\\Conversion') . '\\' . $objConverterfields->typeUpdate; // build target data out of source and existent data for this line $arrTargetData[$objConverterfields->fieldname] = $strClass::convertData($objConverterfields, $objSource, $strTarget, $arrExistentData); } break; default: // is disabled or not allowed to be inserted if (!$objConverterfields->published || !$objConverterfields->allowInsert) { // just do nothing with this field } else { // fields maybe processed more than once in a step, so perform an update with temporary data instead if ($objConverterfields->allowUpdate && $objConverterfields->typeUpdate == 'Updatefromfield' && $objConverterfields->modeUpdate == 'Tags' && $arrTargetData[$objConverterfields->fieldname]) { // tags maybe added or removed // determine the class of the conversion $strClass = ($GLOBALS['convertx']['classpath'][$objConverterfields->typeUpdate] ? $GLOBALS['convertx']['classpath'][$objConverterfields->typeUpdate] : 'Delahaye\\ConvertX\\Conversion') . '\\' . $objConverterfields->typeUpdate; // build target data out of source data for this line $strData = $strClass::convertData($objConverterfields, $objSource, $strTarget, array($arrTargetData)); } elseif (!$objConverterfields->fieldInsert && !$objConverterfields->modeInsert) { // field data is built of modified existing target data, field is processed twice or more (but not as tags) $objConverterfields->fieldUpdate = 'ConvertXTmpField'; $objSource->ConvertXTmpField = $arrTargetData[$objConverterfields->fieldname]; // determine the class of the conversion $strClass = ($GLOBALS['convertx']['classpath'][$objConverterfields->typeUpdate] ? $GLOBALS['convertx']['classpath'][$objConverterfields->typeUpdate] : 'Delahaye\\ConvertX\\Conversion') . '\\' . $objConverterfields->typeUpdate; // build target data out of source data for this line $strData = $strClass::convertData($objConverterfields, $objSource, $strTarget, array($arrTargetData)); } else { // normal insert operation // determine the class of the conversion $strClass = ($GLOBALS['convertx']['classpath'][$objConverterfields->typeInsert] ? $GLOBALS['convertx']['classpath'][$objConverterfields->typeInsert] : 'Delahaye\\ConvertX\\Conversion') . '\\' . $objConverterfields->typeInsert; // build target data out of source data for this line $strData = $strClass::convertData($objConverterfields, $objSource, $strTarget); } // converter class is missing if (strpos($strData, 'ConvertX-missingConverterclass') !== false) { return $strData; } // if is id-field check if given id is already present -> set new one if ($objConverterfields->fieldname == $strIdField) { $tmp = Database::getInstance()->prepare("select id from " . $strTarget . " where id=?")->execute($strData); if ($tmp->numRows > 0) { $strData = ''; } } $arrTargetData[$objConverterfields->fieldname] = $strData; } break; } } return $arrTargetData; }
/** * Save field lists for target and source * * @param DataContainer $dc */ public function setFieldLists(DataContainer $dc) { $objData = ConverterModel::findByPk(Input::get('id')); $classTarget = $dc->activeRecord->targetType ? ($GLOBALS['convertx']['classpath'][$dc->activeRecord->targetType] ? $GLOBALS['convertx']['classpath'][$dc->activeRecord->targetType] : 'Delahaye\\ConvertX\\Container') . '\\' . $dc->activeRecord->targetType : false; $classSource = $dc->activeRecord->sourceType ? ($GLOBALS['convertx']['classpath'][$dc->activeRecord->sourceType] ? $GLOBALS['convertx']['classpath'][$dc->activeRecord->sourceType] : 'Delahaye\\ConvertX\\Container') . '\\' . $dc->activeRecord->sourceType : false; // check if target fields have to be cleared if ($classTarget) { if ($classTarget::checkTargetClear($dc, $objData)) { ConverterfieldModel::clearTargetFields(Input::get('id')); } } // use actual data foreach ($GLOBALS['TL_DCA']['tl_convertx_converter']['fields'] as $k => $v) { $objData->{$k} = $dc->activeRecord->{$k}; } // get field lists for source and/or target if ($classTarget) { $objData->fieldsTarget = $dc->activeRecord->targetType ? serialize($classTarget::getFieldList($dc->activeRecord, 'target')) : false; } if ($classSource) { $objData->fieldsSource = $dc->activeRecord->sourceType ? serialize($classSource::getFieldList($dc->activeRecord, 'source')) : false; } // save the model $objData->save(); return; }