/** * Import module objects from given array datas * * @param array $data The module datas to import * @param array $params The import parameters. * array( * module => false|true : the module to create categories (required) * create => false|true : create missing objects (default : true) * update => false|true : update existing objects (default : true) * files => false|true : use files from PATH_TMP_FS (default : true) * ) * @param CMS_language $cms_language The CMS_langage to use * @param array $idsRelation : Reference : The relations between import datas ids and real imported ids * @param string $infos : Reference : The import infos returned * @return boolean : true on success, false on failure * @access public */ static function fromArray($data, $params, $cms_language, &$idsRelation, &$infos) { if (!isset($params['module'])) { $infos .= 'Error : missing module codename for objects importation ...' . "\n"; return false; } $module = CMS_modulesCatalog::getByCodename($params['module']); if ($module->hasError()) { $infos .= 'Error : invalid module for objects importation : ' . $params['module'] . "\n"; return false; } $return = true; //first create missing objects to get relation ids foreach ($data as $objectDatas) { if (!isset($objectDatas['uuid']) || !CMS_poly_object_catalog::objectExists($params['module'], $objectDatas['uuid'])) { //create new object if we can if (!isset($params['create']) || $params['create'] == true) { //create object $object = new CMS_poly_object_definition(); //set module $object->setValue('module', $params['module']); //set uuid $object->setUuid($objectDatas['uuid']); //write object to persistence to get relations ids $object->writeToPersistence(); //set id translation if (isset($objectDatas['id']) && $objectDatas['id']) { // && $object->getID() != $objectDatas['id']) { // Fix for bug #3157 : in some cases the imported object will have the same id has the newly created, // we still need the relation table otherwise it will fail to link to the new object $idsRelation['objects'][$objectDatas['id']] = $object->getID(); } //set uuid translation if (isset($objectDatas['uuid']) && $objectDatas['uuid'] && $object->getValue('uuid') != $objectDatas['uuid']) { $idsRelation['objects-uuid'][$objectDatas['uuid']] = $object->getValue('uuid'); } } } elseif (isset($objectDatas['uuid']) && isset($objectDatas['id'])) { //get relation between imported object id and local id $id = CMS_poly_object_catalog::objectExists($params['module'], $objectDatas['uuid']); if (io::isPositiveInteger($id)) { $idsRelation['objects'][$objectDatas['id']] = $id; } } } //then import objects datas foreach ($data as $objectDatas) { $importType = ''; if (isset($objectDatas['uuid']) && ($id = CMS_poly_object_catalog::objectExists($params['module'], $objectDatas['uuid']))) { //object already exist : load it if we can update it if (!isset($params['update']) || $params['update'] == true) { $object = CMS_poly_object_catalog::getObjectDefinition($id); $importType = ' (Update)'; //set id translation $idsRelation['objects'][$objectDatas['id']] = $id; } } else { //check for translated id if (isset($objectDatas['id']) && isset($idsRelation['objects'][$objectDatas['id']])) { //object exists with a translated id $objectDatas['id'] = $idsRelation['objects'][$objectDatas['id']]; //load translated object $object = CMS_poly_object_catalog::getObjectDefinition($objectDatas['id']); $importType = ' (Creation)'; } //check for translated uuid if (isset($objectDatas['uuid']) && isset($idsRelation['objects-uuid'][$objectDatas['uuid']])) { //object exists with a translated uuid $objectDatas['uuid'] = $idsRelation['objects-uuid'][$objectDatas['uuid']]; //load translated object if ($id = CMS_poly_object_catalog::objectExists($params['module'], $objectDatas['uuid'])) { $object = CMS_poly_object_catalog::getObjectDefinition($id); $importType = ' (Creation)'; } } } if (isset($object)) { if ($object->fromArray($objectDatas, $params, $cms_language, $idsRelation, $infos)) { $return &= true; $infos .= 'Object "' . $object->getLabel($cms_language) . '" successfully imported' . $importType . "\n"; } else { $return = false; $infos .= 'Error during import of object ' . $objectDatas['id'] . $importType . "\n"; } } } return $return; }
switch ($_POST["cms_action"]) { case "validate": case "switchexplanation": //checks and assignments $cms_message = ""; $object->setDebug(false); if (!$_POST["label" . $availableLanguagesCodes[0]] || !$_POST["description" . $availableLanguagesCodes[0]]) { $cms_message .= $cms_language->getMessage(MESSAGE_FORM_ERROR_MANDATORY_FIELDS); } foreach ($availableLanguagesCodes as $aLanguageCode) { $label->setValue($aLanguageCode, $_POST["label" . $aLanguageCode]); $description->setValue($aLanguageCode, $_POST["description" . $aLanguageCode]); } $label->writeToPersistence(); $description->writeToPersistence(); if (!$object->setValue("labelID", $label->getID())) { $cms_message .= "\n" . $cms_language->getMessage(MESSAGE_FORM_ERROR_MALFORMED_FIELD, array($cms_language->getMessage(MESSAGE_PAGE_FIELD_TITLE))); } if (!$object->setValue("descriptionID", $description->getID())) { $cms_message .= "\n" . $cms_language->getMessage(MESSAGE_FORM_ERROR_MALFORMED_FIELD, array($cms_language->getMessage(MESSAGE_PAGE_FIELD_DESCRIPTION))); } if (!$object->setValue("admineditable", $_POST["admineditable"])) { $cms_message .= "\n" . $cms_language->getMessage(MESSAGE_FORM_ERROR_MALFORMED_FIELD, array($cms_language->getMessage(MESSAGE_PAGE_FIELD_EDITABLE))); } if (!$object->setValue("resourceUsage", $_POST["resourceUsage"])) { $cms_message .= "\n" . $cms_language->getMessage(MESSAGE_FORM_ERROR_MALFORMED_FIELD, array($cms_language->getMessage(MESSAGE_PAGE_FIELD_RESOURCE))); } //composed label $composedLabel = $_POST["composedLabel"] ? $polymod->convertDefinitionString($_POST["composedLabel"], false) : ''; if (!$object->setValue("composedLabel", $composedLabel)) { $cms_message .= "\n" . $cms_language->getMessage(MESSAGE_FORM_ERROR_MALFORMED_FIELD, array($cms_language->getMessage(MESSAGE_PAGE_FIELD_COMPOSED_LABEL)));