private function renamegroup($groupIdentifier, $newGroupIdentifier) { $groupObject = eZContentClassGroup::fetchByName($groupIdentifier, true); if ($groupObject == null) { echo "Invalid group identifier\n"; return; } $date_time = time(); $groupObject->setAttribute("modified", $date_time); $groupObject->setAttribute("modifier_id", false); $groupObject->setAttribute("name", $newGroupIdentifier); $groupObject->store(); eZContentClassClassGroup::update(null, $groupObject->ID, $newGroupIdentifier); echo "Successfully renamed group {$groupIdentifier} to {$newGroupIdentifier}\n"; }
function install($package, $installType, $parameters, $name, $os, $filename, $subdirectory, $content, &$installParameters, &$installData) { $serializedNameListNode = $content->getElementsByTagName('serialized-name-list')->item(0); $serializedNameList = $serializedNameListNode ? $serializedNameListNode->textContent : false; $classNameList = new eZContentClassNameList($serializedNameList); if ($classNameList->isEmpty()) { $classNameList->initFromString($content->getElementsByTagName('name')->item(0)->textContent); // for backward compatibility( <= 3.8 ) } $classNameList->validate(); $serializedDescriptionListNode = $content->getElementsByTagName('serialized-description-list')->item(0); $serializedDescriptionList = $serializedDescriptionListNode ? $serializedDescriptionListNode->textContent : false; $classDescriptionList = new eZSerializedObjectNameList($serializedDescriptionList); $classIdentifier = $content->getElementsByTagName('identifier')->item(0)->textContent; $classRemoteID = $content->getElementsByTagName('remote-id')->item(0)->textContent; $classObjectNamePattern = $content->getElementsByTagName('object-name-pattern')->item(0)->textContent; $classURLAliasPattern = is_object($content->getElementsByTagName('url-alias-pattern')->item(0)) ? $content->getElementsByTagName('url-alias-pattern')->item(0)->textContent : null; $classIsContainer = $content->getAttribute('is-container'); if ($classIsContainer !== false) { $classIsContainer = $classIsContainer == 'true' ? 1 : 0; } $classRemoteNode = $content->getElementsByTagName('remote')->item(0); $classID = $classRemoteNode->getElementsByTagName('id')->item(0)->textContent; $classGroupsNode = $classRemoteNode->getElementsByTagName('groups')->item(0); $classCreated = $classRemoteNode->getElementsByTagName('created')->item(0)->textContent; $classModified = $classRemoteNode->getElementsByTagName('modified')->item(0)->textContent; $classCreatorNode = $classRemoteNode->getElementsByTagName('creator')->item(0); $classModifierNode = $classRemoteNode->getElementsByTagName('modifier')->item(0); $classAttributesNode = $content->getElementsByTagName('attributes')->item(0); $dateTime = time(); $classCreated = $dateTime; $classModified = $dateTime; $userID = false; if (isset($installParameters['user_id'])) { $userID = $installParameters['user_id']; } $class = eZContentClass::fetchByRemoteID($classRemoteID); if ($class) { $className = $class->name(); $description = ezpI18n::tr('kernel/package', "Class '%classname' already exists.", false, array('%classname' => $className)); $choosenAction = $this->errorChoosenAction(self::ERROR_EXISTS, $installParameters, $description, $this->HandlerType); switch ($choosenAction) { case eZPackage::NON_INTERACTIVE: case self::ACTION_REPLACE: if (eZContentClassOperations::remove($class->attribute('id')) == false) { eZDebug::writeWarning("Unable to remove class '{$className}'."); return true; } eZDebug::writeNotice("Class '{$className}' will be replaced.", 'eZContentClassPackageHandler'); break; case self::ACTION_SKIP: return true; case self::ACTION_NEW: $class->setAttribute('remote_id', eZRemoteIdUtility::generate('class')); $class->store(); $classNameList->appendGroupName(" (imported)"); break; default: $installParameters['error'] = array('error_code' => self::ERROR_EXISTS, 'element_id' => $classRemoteID, 'description' => $description, 'actions' => array()); if ($class->isRemovable()) { $errorMsg = ezpI18n::tr('kernel/package', "Replace existing class"); $objectsCount = eZContentObject::fetchSameClassListCount($class->attribute('id')); if ($objectsCount) { $errorMsg .= ' ' . ezpI18n::tr('kernel/package', "(Warning! {$objectsCount} content object(s) and their sub-items will be removed)"); } $installParameters['error']['actions'][self::ACTION_REPLACE] = $errorMsg; } $installParameters['error']['actions'][self::ACTION_SKIP] = ezpI18n::tr('kernel/package', 'Skip installing this class'); $installParameters['error']['actions'][self::ACTION_NEW] = ezpI18n::tr('kernel/package', 'Keep existing and create a new one'); return false; } } unset($class); // Try to create a unique class identifier $currentClassIdentifier = $classIdentifier; $unique = false; while (!$unique) { $classList = eZContentClass::fetchByIdentifier($currentClassIdentifier); if ($classList) { // "increment" class identifier if (preg_match('/^(.*)_(\\d+)$/', $currentClassIdentifier, $matches)) { $currentClassIdentifier = $matches[1] . '_' . ($matches[2] + 1); } else { $currentClassIdentifier = $currentClassIdentifier . '_1'; } } else { $unique = true; } unset($classList); } $classIdentifier = $currentClassIdentifier; $values = array('version' => 0, 'serialized_name_list' => $classNameList->serializeNames(), 'serialized_description_list' => $classDescriptionList->serializeNames(), 'create_lang_if_not_exist' => true, 'identifier' => $classIdentifier, 'remote_id' => $classRemoteID, 'contentobject_name' => $classObjectNamePattern, 'url_alias_name' => $classURLAliasPattern, 'is_container' => $classIsContainer, 'created' => $classCreated, 'modified' => $classModified); if ($content->hasAttribute('sort-field')) { $values['sort_field'] = eZContentObjectTreeNode::sortFieldID($content->getAttribute('sort-field')); } else { eZDebug::writeNotice('The sort field was not specified in the content class package. ' . 'This property is exported and imported since eZ Publish 4.0.2', __METHOD__); } if ($content->hasAttribute('sort-order')) { $values['sort_order'] = $content->getAttribute('sort-order'); } else { eZDebug::writeNotice('The sort order was not specified in the content class package. ' . 'This property is exported and imported since eZ Publish 4.0.2', __METHOD__); } if ($content->hasAttribute('always-available')) { $values['always_available'] = $content->getAttribute('always-available') === 'true' ? 1 : 0; } else { eZDebug::writeNotice('The default object availability was not specified in the content class package. ' . 'This property is exported and imported since eZ Publish 4.0.2', __METHOD__); } // create class $class = eZContentClass::create($userID, $values); $class->store(); $classID = $class->attribute('id'); if (!isset($installData['classid_list'])) { $installData['classid_list'] = array(); } if (!isset($installData['classid_map'])) { $installData['classid_map'] = array(); } $installData['classid_list'][] = $class->attribute('id'); $installData['classid_map'][$classID] = $class->attribute('id'); // create class attributes $classAttributeList = $classAttributesNode->getElementsByTagName('attribute'); foreach ($classAttributeList as $classAttributeNode) { $isNotSupported = strtolower($classAttributeNode->getAttribute('unsupported')) == 'true'; if ($isNotSupported) { continue; } $attributeDatatype = $classAttributeNode->getAttribute('datatype'); $attributeIsRequired = strtolower($classAttributeNode->getAttribute('required')) == 'true'; $attributeIsSearchable = strtolower($classAttributeNode->getAttribute('searchable')) == 'true'; $attributeIsInformationCollector = strtolower($classAttributeNode->getAttribute('information-collector')) == 'true'; $attributeIsTranslatable = strtolower($classAttributeNode->getAttribute('translatable')) == 'true'; $attributeSerializedNameListNode = $classAttributeNode->getElementsByTagName('serialized-name-list')->item(0); $attributeSerializedNameListContent = $attributeSerializedNameListNode ? $attributeSerializedNameListNode->textContent : false; $attributeSerializedNameList = new eZSerializedObjectNameList($attributeSerializedNameListContent); if ($attributeSerializedNameList->isEmpty()) { $attributeSerializedNameList->initFromString($classAttributeNode->getElementsByTagName('name')->item(0)->textContent); } // for backward compatibility( <= 3.8 ) $attributeSerializedNameList->validate(); $attributeSerializedDescriptionListNode = $classAttributeNode->getElementsByTagName('serialized-description-list')->item(0); $attributeSerializedDescriptionListContent = $attributeSerializedDescriptionListNode ? $attributeSerializedDescriptionListNode->textContent : false; $attributeSerializedDescriptionList = new eZSerializedObjectNameList($attributeSerializedDescriptionListContent); $attributeCategoryNode = $classAttributeNode->getElementsByTagName('category')->item(0); $attributeCategory = $attributeCategoryNode ? $attributeCategoryNode->textContent : ''; $attributeSerializedDataTextNode = $classAttributeNode->getElementsByTagName('serialized-description-text')->item(0); $attributeSerializedDataTextContent = $attributeSerializedDataTextNode ? $attributeSerializedDataTextNode->textContent : false; $attributeSerializedDataText = new eZSerializedObjectNameList($attributeSerializedDataTextContent); $attributeIdentifier = $classAttributeNode->getElementsByTagName('identifier')->item(0)->textContent; $attributePlacement = $classAttributeNode->getElementsByTagName('placement')->item(0)->textContent; $attributeDatatypeParameterNode = $classAttributeNode->getElementsByTagName('datatype-parameters')->item(0); $classAttribute = $class->fetchAttributeByIdentifier($attributeIdentifier); if (!$classAttribute) { $classAttribute = eZContentClassAttribute::create($class->attribute('id'), $attributeDatatype, array('version' => 0, 'identifier' => $attributeIdentifier, 'serialized_name_list' => $attributeSerializedNameList->serializeNames(), 'serialized_description_list' => $attributeSerializedDescriptionList->serializeNames(), 'category' => $attributeCategory, 'serialized_data_text' => $attributeSerializedDataText->serializeNames(), 'is_required' => $attributeIsRequired, 'is_searchable' => $attributeIsSearchable, 'is_information_collector' => $attributeIsInformationCollector, 'can_translate' => $attributeIsTranslatable, 'placement' => $attributePlacement)); $dataType = $classAttribute->dataType(); $classAttribute->store(); $dataType->unserializeContentClassAttribute($classAttribute, $classAttributeNode, $attributeDatatypeParameterNode); $classAttribute->sync(); } } // add class to a class group $classGroupsList = $classGroupsNode->getElementsByTagName('group'); foreach ($classGroupsList as $classGroupNode) { $classGroupName = $classGroupNode->getAttribute('name'); $classGroup = eZContentClassGroup::fetchByName($classGroupName); if (!$classGroup) { $classGroup = eZContentClassGroup::create(); $classGroup->setAttribute('name', $classGroupName); $classGroup->store(); } $classGroup->appendClass($class); } return true; }
public function run($argv, $additional) { $command = @$argv[2]; $param1 = @$argv[3]; $param2 = @$argv[4]; if (!in_array($command, $this->availableCommands)) { throw new Exception("Command '" . $command . "' not recognized."); } $eepCache = eepCache::getInstance(); switch ($command) { case "help": echo "\nAvailable commands:: " . implode(", ", $this->availableCommands) . "\n"; echo "\n" . $this->help . "\n"; break; case self::contentclass_listattributes: $classIdentifier = $eepCache->readFromCache(eepCache::use_key_contentclass); if ($param1) { $classIdentifier = $param1; } AttributeFunctions::listAttributes($classIdentifier); break; case self::contentclass_deleteclass: $classIdentifier = $eepCache->readFromCache(eepCache::use_key_contentclass); if ($param1) { $classIdentifier = $param1; } $this->deleteClass($classIdentifier); break; case self::contentclass_fetchallinstances: $classIdentifier = $eepCache->readFromCache(eepCache::use_key_contentclass); if ($param1) { $classIdentifier = $param1; } $this->fetchallinstances($classIdentifier, $additional); break; case self::contentclass_appendtogroup: $classIdentifier = $eepCache->readFromCache(eepCache::use_key_contentclass); if ($param1) { $classIdentifier = $param1; } if ($param2) { $groupIdentifier = $param2; } else { $groupIdentifier = null; } $this->appendToGroup($classIdentifier, $groupIdentifier); break; case self::contentclass_removefromgroup: $classIdentifier = $eepCache->readFromCache(eepCache::use_key_contentclass); if ($param1) { $classIdentifier = $param1; } if ($param2) { $groupIdentifier = $param2; } else { $groupIdentifier = null; } $this->removeFromGroup($classIdentifier, $groupIdentifier); break; // eep createclass <Display name> <Content class group identifier> // eep createclass <Display name> <Content class group identifier> case self::contentclass_createclass: $displayName = $param1; // convert the display name to lowercase and solo underscores $classIdentifier = strtolower(trim($displayName)); $classIdentifier = preg_replace("/[^a-z0-9]/", "_", $classIdentifier); $classIdentifier = preg_replace("/_[_]+/", "_", $classIdentifier); if (0 == strlen($classIdentifier)) { throw new Exception("Empty content class identifier"); } $classId = eZContentClass::classIDByIdentifier($classIdentifier); if ($classId) { throw new Exception("This content class identifier is already used: '" . $classIdentifier . "'"); } $groupIdentifier = $param2; $groupObject = eZContentClassGroup::fetchByName($groupIdentifier); if (!is_object($groupObject)) { throw new Exception("Failed to locate the content class group '" . $groupIdentifier . "'"); } $groupId = $groupObject->ID; $this->createClass($displayName, $classIdentifier, $groupIdentifier, $groupId); echo "created " . $classIdentifier . " ok\n"; break; // eep contentclass setclassobjectidentifier <class identifier> <object naming string or pattern> // eep contentclass setclassobjectidentifier <class identifier> <object naming string or pattern> case self::contentclass_setclassobjectidentifier: $classIdentifier = $param1; $classId = eZContentClass::classIDByIdentifier($classIdentifier); $contentClass = eZContentClass::fetch($classId); if (!is_object($contentClass)) { throw new Exception("Failed to instantiate content class. [" . $classIdentifier . "]"); } $contentClass->setAttribute('contentobject_name', $param2); $contentClass->store(); break; case self::contentclass_setiscontainer: $classIdentifier = $param1; $classId = eZContentClass::classIDByIdentifier($classIdentifier); $contentClass = eZContentClass::fetch($classId); if (!is_object($contentClass)) { throw new Exception("Failed to instantiate content class. [" . $classIdentifier . "]"); } $newSetting = 0; if (0 != $param2) { $newSetting = 1; } $contentClass->setAttribute('is_container', $newSetting); $contentClass->store(); break; } }
function execute($xml) { $classList = $xml->getElementsByTagName('ContentClass'); $refArray = array(); $availableLanguageList = eZContentLanguage::fetchLocaleList(); foreach ($classList as $class) { $this->adjustAttributesPlacement = false; $user = eZUser::currentUser(); $userID = $user->attribute('contentobject_id'); $classIdentifier = $class->getAttribute('identifier'); $classRemoteID = $class->getAttribute('remoteID'); $classObjectNamePattern = $class->getAttribute('objectNamePattern'); $classExistAction = $class->getAttribute('classExistAction'); $referenceID = $class->getAttribute('referenceID'); $this->writeMessage("\tClass '{$classIdentifier}' will be updated.", 'notice'); $classURLAliasPattern = $class->getAttribute('urlAliasPattern') ? $class->getAttribute('urlAliasPattern') : null; $classIsContainer = $class->getAttribute('isContainer'); if ($classIsContainer !== false) { $classIsContainer = $classIsContainer == 'true' ? 1 : 0; } $classGroupsNode = $class->getElementsByTagName('Groups')->item(0); $classAttributesNode = $class->getElementsByTagName('Attributes')->item(0); $nameList = array(); $nameListObject = $class->getElementsByTagName('Names')->item(0); if ($nameListObject && $nameListObject->parentNode === $class && $nameListObject->hasAttributes()) { $attributes = $nameListObject->attributes; if (!is_null($attributes)) { foreach ($attributes as $index => $attr) { if (in_array($attr->name, $availableLanguageList)) { $nameList[$attr->name] = $attr->value; } } } } if (!empty($nameList)) { $classNameList = new eZContentClassNameList(serialize($nameList)); $classNameList->validate(); } else { $classNameList = null; } $dateTime = time(); $classCreated = $dateTime; $classModified = $dateTime; $class = eZContentClass::fetchByRemoteID($classRemoteID); if (!$class) { $class = eZContentClass::fetchByIdentifier($classIdentifier); } if ($class) { $className = $class->name(); switch ($classExistAction) { case 'replace': $this->writeMessage("\t\tClass '{$classIdentifier}' will be replaced.", 'notice'); foreach ($nameList as $lang => $name) { if (in_array($lang, $availableLanguageList)) { $class->setName($name, $lang); } } $class->setAttribute('contentobject_name', $classObjectNamePattern); $class->setAttribute('identifier', $classIdentifier); $class->setAttribute('is_container', $classIsContainer); $class->setAttribute('url_alias_name', $classURLAliasPattern); $class->store(); $class->removeAttributes(); break; case 'new': unset($class); $class = false; break; break; case 'extend': $this->writeMessage("\t\tClass '{$classIdentifier}' will be extended.", 'notice'); foreach ($nameList as $lang => $name) { if (in_array($lang, $availableLanguageList)) { $class->setName($name, $lang); } } $class->setAttribute('contentobject_name', $classObjectNamePattern); $class->setAttribute('identifier', $classIdentifier); $class->setAttribute('is_container', $classIsContainer); $class->setAttribute('url_alias_name', $classURLAliasPattern); $class->store(); break; case 'skip': default: continue; break; } } if (!$class) { // Try to create a unique class identifier $currentClassIdentifier = $classIdentifier; $unique = false; while (!$unique) { $classList = eZContentClass::fetchByIdentifier($currentClassIdentifier); if ($classList) { // "increment" class identifier if (preg_match('/^(.*)_(\\d+)$/', $currentClassIdentifier, $matches)) { $currentClassIdentifier = $matches[1] . '_' . ($matches[2] + 1); } else { $currentClassIdentifier = $currentClassIdentifier . '_1'; } } else { $unique = true; } unset($classList); } $classIdentifier = $currentClassIdentifier; // create class $class = eZContentClass::create($userID, array('version' => 1, 'serialized_name_list' => $classNameList->serializeNames(), 'create_lang_if_not_exist' => true, 'identifier' => $classIdentifier, 'remote_id' => $classRemoteID, 'contentobject_name' => $classObjectNamePattern, 'url_alias_name' => $classURLAliasPattern, 'is_container' => $classIsContainer, 'created' => $classCreated, 'modified' => $classModified)); $class->store(); $attributes = $class->fetchAttributes(); $class->storeDefined($attributes); $classID = $class->attribute('id'); $this->writeMessage("\t\tClass '{$classIdentifier}' will be newly created.", 'notice'); } // create class attributes $classAttributeList = $classAttributesNode->getElementsByTagName('Attribute'); $classDataMap = $class->attribute('data_map'); $updateAttributeList = array(); if ($classDataMap == NULL) { $classDataMap = array(); } foreach ($classAttributeList as $classAttributeNode) { $attributeDatatype = $classAttributeNode->getAttribute('datatype'); $attributeIsRequired = strtolower($classAttributeNode->getAttribute('required')) == 'true'; $attributeIsSearchable = strtolower($classAttributeNode->getAttribute('searchable')) == 'true'; $attributeIsInformationCollector = strtolower($classAttributeNode->getAttribute('informationCollector')) == 'true'; $attributeIsTranslatable = strtolower($classAttributeNode->getAttribute('translatable')) == 'false' ? 0 : 1; $attributeIdentifier = $classAttributeNode->getAttribute('identifier'); $attributePlacement = $classAttributeNode->getAttribute('placement'); $attributeNameListObject = $classAttributeNode->getElementsByTagName('Names')->item(0); if ($attributeNameListObject->hasAttributes()) { if ($attributeNameListObject->hasAttributes()) { $attributes = $attributeNameListObject->attributes; if (!is_null($attributes)) { $attributeNameList = array(); foreach ($attributes as $index => $attr) { $attributeNameList[$attr->name] = $attr->value; } } } } $classAttributeNameList = new eZContentClassNameList(serialize($attributeNameList)); $classAttributeNameList->validate(); $attributeDatatypeParameterNode = $classAttributeNode->getElementsByTagName('DatatypeParameters')->item(0); $classAttribute = $class->fetchAttributeByIdentifier($attributeIdentifier); $params = array(); $params['identifier'] = $attributeIdentifier; $params['name_list'] = $classAttributeNameList; $params['data_type_string'] = $attributeDatatype; $params['default_value'] = ''; $params['can_translate'] = $attributeIsTranslatable; $params['is_required'] = $attributeIsRequired; $params['is_searchable'] = $attributeIsSearchable; $params['content'] = ''; $params['placement'] = $attributePlacement; $params['is_information_collector'] = $attributeIsInformationCollector; $params['datatype-parameter'] = $this->parseAndReplaceNodeStringReferences($attributeDatatypeParameterNode); $params['attribute-node'] = $classAttributeNode; if (!array_key_exists($attributeIdentifier, $classDataMap)) { $this->writeMessage("\t\tClass '{$classIdentifier}' will get new Attribute '{$attributeIdentifier}'.", 'notice'); $updateAttributeList[] = $this->addClassAttribute($class, $params); } else { $this->writeMessage("\t\tClass '{$classIdentifier}' will get updated Attribute '{$attributeIdentifier}'.", 'notice'); $this->updateClassAttribute($class, $params); } } if ($this->adjustAttributesPlacement) { //once every attribute has been processed, we may reset placement $this->writeMessage("\t\tAdjusting attributes placement.", 'notice'); $this->adjustClassAttributesPlacement($class); } if (count($updateAttributeList)) { $this->writeMessage("\t\tUpdating content object attributes.", 'notice'); $classID = $class->attribute('id'); // update object attributes $objects = eZContentObject::fetchSameClassList($classID, false); foreach ($objects as $objectID) { $object = eZContentObject::fetch($objectID['id']); if ($object) { $contentobjectID = $object->attribute('id'); $objectVersions = $object->versions(); foreach ($objectVersions as $objectVersion) { $translations = $objectVersion->translations(false); $version = $objectVersion->attribute('version'); foreach ($translations as $translation) { foreach ($updateAttributeList as $classAttributeID) { $objectAttribute = eZContentObjectAttribute::create($classAttributeID, $contentobjectID, $version); $objectAttribute->setAttribute('language_code', $translation); $objectAttribute->initialize(); $objectAttribute->store(); $objectAttribute->postInitialize(); } } } } unset($object); } } if ($classNameList) { $classNameList->store($class); } // add class to a class group $classGroupsList = $classGroupsNode->getElementsByTagName('Group'); foreach ($classGroupsList as $classGroupNode) { $classGroupName = $classGroupNode->getAttribute('name'); $classGroup = eZContentClassGroup::fetchByName($classGroupName); if (!$classGroup) { $classGroup = eZContentClassGroup::create(); $classGroup->setAttribute('name', $classGroupName); $classGroup->store(); } $classGroup->appendClass($class); } if ($referenceID) { $refArray[$referenceID] = $class->attribute('id'); } } $this->addReference($refArray); eZContentCacheManager::clearAllContentCache(); }
protected function syncGroups() { $remote = $this->getRemote(); if ($remote === null) { throw new Exception("Classe remota non trovata"); } $this->syncAllGroups($remote); $locale = $this->currentClass; /** @var eZContentClassClassGroup[] $localGroups */ $localGroups = $locale->fetchGroupList(); //$localGroupsNames = array(); //$remoteGroupsNames = array(); foreach ($localGroups as $group) { /** @var eZContentClassGroup $classGroup */ $classGroup = eZContentClassGroup::fetchByName($group->attribute('group_name')); if ($classGroup) { eZContentClassClassGroup::removeGroup($this->currentClass->attribute('id'), $this->currentClass->attribute('version'), $classGroup->attribute('id')); } } foreach ($remote->InGroups as $group) { /** @var eZContentClassGroup $classGroup */ $classGroup = eZContentClassGroup::fetchByName($group->GroupName); if ($classGroup) { $ingroup = eZContentClassClassGroup::create($this->currentClass->attribute('id'), $this->currentClass->attribute('version'), $classGroup->attribute('id'), $classGroup->attribute('name')); $ingroup->store(); } } //$groups = $this->currentClass->attribute( 'ingroup_list' ); //if ( count( $groups ) == 0 ) //{ // //@todo //} }