コード例 #1
0
    /**
     * Initialize the attribute in the existing objects.
     *
     * @param mixed $objects not used, the existing objects are fetched if
     *        necessary (depending on the datatype of the attribute).
     */
    function initializeObjectAttributes( &$objects = null )
    {
        $classAttributeID = $this->ID;
        $classID = $this->ContentClassID;
        $dataType = $this->attribute( 'data_type' );
        if ( $dataType->supportsBatchInitializeObjectAttribute() )
        {
            $db = eZDB::instance();

            $data = array( 'contentobject_id'         => 'a.contentobject_id',
                           'version'                  => 'a.version',
                           'contentclassattribute_id' => $classAttributeID,
                           'data_type_string'         => "'" . $db->escapeString( $this->DataTypeString ) . "'",
                           'language_code'            => 'a.language_code',
                           'language_id'              => 'MAX(a.language_id)' );

            $datatypeData = $dataType->batchInitializeObjectAttributeData( $this );
            $data = array_merge( $data, $datatypeData );

            $cols = implode( ', ', array_keys( $data ) );
            $values = implode( ', ', $data );

            $sql = "INSERT INTO ezcontentobject_attribute( $cols )
            SELECT $values
            FROM ezcontentobject_attribute a, ezcontentobject o
            WHERE o.id = a.contentobject_id AND
                  o.contentclass_id=$classID
            GROUP BY contentobject_id,
                     version,
                     language_code";

            $db->query( $sql );
            // update ids to keep them same with one attribute for different versions
            if( $db->databaseName() == 'mysql' )
            {
                $updateSql = "UPDATE ezcontentobject_attribute,
                                 ( SELECT contentobject_id, language_code, version, contentclassattribute_id, MIN( id ) AS minid
                                 FROM ezcontentobject_attribute WHERE contentclassattribute_id = $classAttributeID
                              GROUP BY contentobject_id, language_code, version, contentclassattribute_id ) t
                              SET ezcontentobject_attribute.id = t.minid
                              WHERE ezcontentobject_attribute.contentobject_id = t.contentobject_id
                              AND ezcontentobject_attribute.language_code = t.language_code
                              AND ezcontentobject_attribute.contentclassattribute_id = $classAttributeID";
            }
            else if( $db->databaseName() == 'postgresql' )
            {
                $updateSql = "UPDATE ezcontentobject_attribute
                              SET id=t.minid FROM
                                ( SELECT contentobject_id, language_code, version, contentclassattribute_id, MIN( id ) AS minid
                                 FROM ezcontentobject_attribute WHERE contentclassattribute_id = $classAttributeID
                                 GROUP BY contentobject_id, language_code, version, contentclassattribute_id ) t
                              WHERE ezcontentobject_attribute.contentobject_id = t.contentobject_id
                              AND ezcontentobject_attribute.language_code = t.language_code
                              AND ezcontentobject_attribute.contentclassattribute_id = $classAttributeID";
            }
            else if( $db->databaseName() == 'oracle' )
            {
                $updateSql = "UPDATE ezcontentobject_attribute a SET a.id = (
                                 SELECT MIN( id ) FROM ezcontentobject_attribute b
                                 WHERE b.contentclassattribute_id = $classAttributeID
                                       AND b.contentobject_id = a.contentobject_id
                                       AND b.language_code = a.language_code )
                                WHERE a.contentclassattribute_id = $classAttributeID";
            }
            else
            {
                $updateSql = "";
            }
            $db->query( $updateSql );
        }
        else
        {
            $limit = 1000;
            $offset = 0;
            while ( true )
            {
                $contentObjects = eZContentObject::fetchSameClassList( $classID, true, $offset, $limit );
                if ( empty( $contentObjects ) )
                {
                    break;
                }

                foreach ( $contentObjects as $object )
                {
                    $contentobjectID = $object->attribute( 'id' );
                    $objectVersions = $object->versions();
                    // the start version ID, to make sure one attribute in different version has same id.
                    $startAttributeID = array();
                    foreach ( $objectVersions as $objectVersion )
                    {
                        $translations = $objectVersion->translations( false );
                        $version = $objectVersion->attribute( 'version' );
                        foreach ( $translations as $translation )
                        {
                            $objectAttribute = eZContentObjectAttribute::create( $classAttributeID, $contentobjectID, $version, $translation );
                            if( array_key_exists( $translation, $startAttributeID ) )
                            {
                                $objectAttribute->setAttribute( 'id', $startAttributeID[$translation] );
                            }
                            $objectAttribute->setAttribute( 'language_code', $translation );
                            $objectAttribute->initialize();
                            $objectAttribute->store();
                            if( !array_key_exists( $translation, $startAttributeID ) )
                            {
                                $startAttributeID[$translation] = $objectAttribute->attribute( 'id' );
                            }
                            $objectAttribute->postInitialize();
                        }
                    }
                }

                $offset += $limit;
                eZContentObject::clearCache();
            }
        }
    }
コード例 #2
0
ファイル: ezsiteinstaller.php プロジェクト: nottavi/ezpublish
    function addClassAttributes( $params )
    {
        $classInfo = $params['class'];
        $attributesInfo = $params['attributes'];

        $classID = isset( $classInfo['id'] ) ? $classInfo['id'] : false;
        if( $classID )
        {
            $class = eZContentClass::fetch( $classID );
        }
        else
        {
            if( isset( $classInfo['identifier'] ) )
            {
                $class = eZSiteInstaller::classByIdentifier( $classInfo );
            }
            else
            {
                $this->reportError( "neither 'id' nor 'identifier' is set for content class" ,
                                    'eZSiteInstaller::addClassAttribute' );
            }
        }

        if( !is_object( $class ) )
        {
            $this->reportError( "Can't fetch content class" ,
                                'eZSiteInstaller::addClassAttribute' );
            return;
        }

        $classID = $class->attribute( 'id' );

        foreach( $attributesInfo as $attributeInfo )
        {
            $classAttributeIdentifier = $attributeInfo['identifier'];
            $classAttributeName = $attributeInfo['name'];
            $datatype = $attributeInfo['data_type_string'];
            $defaultValue = isset( $attributeInfo['default_value'] ) ? $attributeInfo['default_value'] : false;
            $canTranslate = isset( $attributeInfo['can_translate'] ) ? $attributeInfo['can_translate'] : 1;
            $isRequired   = isset( $attributeInfo['is_required']   ) ? $attributeInfo['is_required'] : 0;
            $isSearchable = isset( $attributeInfo['is_searchable'] ) ? $attributeInfo['is_searchable'] : 0;
            $attrContent  = isset( $attributeInfo['content'] ) ? $attributeInfo['content'] : false;

            $attrCreateInfo = array( 'identifier' => $classAttributeIdentifier,
                                     'name' => $classAttributeName,
                                     'can_translate' => $canTranslate,
                                     'is_required' => $isRequired,
                                     'is_searchable' => $isSearchable );
            $newAttribute = eZContentClassAttribute::create( $classID, $datatype, $attrCreateInfo  );

            $dataType = $newAttribute->dataType();
            $dataType->initializeClassAttribute( $newAttribute );

            // not all datatype can have 'default_value'. do check here.
            if( $defaultValue !== false  )
            {
                switch( $datatype )
                {
                    case 'ezboolean':
                    {
                        $newAttribute->setAttribute( 'data_int3', $defaultValue );
                    }
                    break;

                    default:
                        break;
                }
            }

            if( $attrContent )
                $newAttribute->setContent( $attrContent );

            // store attribute, update placement, etc...
            $attributes = $class->fetchAttributes();
            $attributes[] = $newAttribute;

            // remove temporary version
            if ( $newAttribute->attribute( 'id' ) !== null )
            {
                $newAttribute->remove();
            }

            $newAttribute->setAttribute( 'version', eZContentClass::VERSION_STATUS_DEFINED );
            $newAttribute->setAttribute( 'placement', count( $attributes ) );

            $class->adjustAttributePlacements( $attributes );
            foreach( $attributes as $attribute )
            {
                $attribute->storeDefined();
            }

            // update objects
            $classAttributeID = $newAttribute->attribute( 'id' );
            $objects = eZContentObject::fetchSameClassList( $classID );
            foreach( $objects as $object )
            {
                $contentobjectID = $object->attribute( 'id' );
                $objectVersions = $object->versions();
                foreach( $objectVersions as $objectVersion )
                {
                    $translations = $objectVersion->translations( false );
                    $version = $objectVersion->attribute( 'version' );
                    foreach( $translations as $translation )
                    {
                        $objectAttribute = eZContentObjectAttribute::create( $classAttributeID, $contentobjectID, $version );
                        $objectAttribute->setAttribute( 'language_code', $translation );
                        $objectAttribute->initialize();
                        $objectAttribute->store();
                        $objectAttribute->postInitialize();
                    }
                }
            }
        }
    }
コード例 #3
0
 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();
 }
コード例 #4
0
function updateClass($classId)
{
    global $cli, $script, $db, $scheduledScript;
    // If the class is not stored yet, store it now
    $class = eZContentClass::fetch($classId, true, eZContentClass::VERSION_STATUS_TEMPORARY);
    if ($class) {
        $cli->output("Storing class");
        $class->storeDefined($class->fetchAttributes());
    }
    // Fetch the stored class
    $class = eZContentClass::fetch($classId);
    if (!$class) {
        $cli->error('Could not fetch class with ID: ' . $classId);
        return;
    }
    $classAttributes = $class->fetchAttributes();
    $classAttributeIDs = array();
    foreach ($classAttributes as $classAttribute) {
        $classAttributeIDs[] = $classAttribute->attribute('id');
    }
    $objectCount = eZContentObject::fetchSameClassListCount($classId);
    $cli->output('Number of objects to be processed: ' . $objectCount);
    $counter = 0;
    $offset = 0;
    $limit = 100;
    $objects = eZContentObject::fetchSameClassList($classId, true, $offset, $limit);
    // Add and/or remove attributes for all versions and translations of all objects of this class
    while (count($objects) > 0) {
        // Run a transaction per $limit objects
        $db->begin();
        foreach ($objects as $object) {
            $contentObjectID = $object->attribute('id');
            $objectVersions = $object->versions();
            foreach ($objectVersions as $objectVersion) {
                $versionID = $objectVersion->attribute('version');
                $translations = $objectVersion->translations();
                foreach ($translations as $translation) {
                    $translationName = $translation->attribute('language_code');
                    // Class attribute IDs of object attributes (not necessarily the same as those in the class, hence the manual sql)
                    $objectClassAttributeIDs = array();
                    $rows = $db->arrayQuery("SELECT id,contentclassattribute_id, data_type_string\n                                              FROM ezcontentobject_attribute\n                                              WHERE contentobject_id = '{$contentObjectID}' AND\n                                                    version = '{$versionID}' AND\n                                                    language_code='{$translationName}'");
                    foreach ($rows as $row) {
                        $objectClassAttributeIDs[$row['id']] = $row['contentclassattribute_id'];
                    }
                    // Quick array diffs
                    $attributesToRemove = array_diff($objectClassAttributeIDs, $classAttributeIDs);
                    // Present in the object, not in the class
                    $attributesToAdd = array_diff($classAttributeIDs, $objectClassAttributeIDs);
                    // Present in the class, not in the object
                    // Remove old attributes
                    foreach ($attributesToRemove as $objectAttributeID => $classAttributeID) {
                        $objectAttribute = eZContentObjectAttribute::fetch($objectAttributeID, $versionID);
                        if (!is_object($objectAttribute)) {
                            continue;
                        }
                        $objectAttribute->remove($objectAttributeID);
                    }
                    // Add new attributes
                    foreach ($attributesToAdd as $classAttributeID) {
                        $objectAttribute = eZContentObjectAttribute::create($classAttributeID, $contentObjectID, $versionID, $translationName);
                        if (!is_object($objectAttribute)) {
                            continue;
                        }
                        $objectAttribute->setAttribute('language_code', $translationName);
                        $objectAttribute->initialize();
                        $objectAttribute->store();
                        $objectAttribute->postInitialize();
                    }
                }
            }
            // Progress bar and Script Monitor progress
            $cli->output('.', false);
            $counter++;
            if ($counter % 70 == 0 or $counter >= $objectCount) {
                $progressPercentage = $counter / $objectCount * 100;
                $cli->output(sprintf(' %01.1f %%', $progressPercentage));
                if ($scheduledScript) {
                    $scheduledScript->updateProgress($progressPercentage);
                }
            }
        }
        $db->commit();
        $offset += $limit;
        $objects = eZContentObject::fetchSameClassList($classId, true, $offset, $limit);
    }
    // Set the object name to the first attribute, if not set
    $classAttributes = $class->fetchAttributes();
    // Fetch the first attribute
    if (count($classAttributes) > 0 && trim($class->attribute('contentobject_name')) == '') {
        $db->begin();
        $identifier = $classAttributes[0]->attribute('identifier');
        $identifier = '<' . $identifier . '>';
        $class->setAttribute('contentobject_name', $identifier);
        $class->store();
        $db->commit();
    }
}
コード例 #5
0
 function initializeObjectAttributes(&$objects = null)
 {
     $classAttributeID = $this->ID;
     $classID = $this->ContentClassID;
     $dataType = $this->attribute('data_type');
     if ($dataType->supportsBatchInitializeObjectAttribute()) {
         $db = eZDB::instance();
         $data = array('contentobject_id' => 'a.contentobject_id', 'version' => 'a.version', 'contentclassattribute_id' => $classAttributeID, 'data_type_string' => "'" . $db->escapeString($this->DataTypeString) . "'", 'language_code' => 'a.language_code', 'language_id' => 'MAX(a.language_id)');
         $datatypeData = $dataType->batchInitializeObjectAttributeData($this);
         $data = array_merge($data, $datatypeData);
         $cols = implode(', ', array_keys($data));
         $values = implode(', ', $data);
         $sql = "INSERT INTO ezcontentobject_attribute( {$cols} )\n            SELECT {$values}\n            FROM ezcontentobject_attribute a, ezcontentobject o\n            WHERE o.id = a.contentobject_id AND\n                  o.contentclass_id={$classID}\n            GROUP BY contentobject_id,\n                     version,\n                     language_code";
         $db->query($sql);
     } else {
         if (!is_array($objects)) {
             $objects = eZContentObject::fetchSameClassList($classID);
         }
         foreach ($objects as $object) {
             $contentobjectID = $object->attribute('id');
             $objectVersions = $object->versions();
             foreach ($objectVersions as $objectVersion) {
                 $translations = $objectVersion->translations(false);
                 $version = $objectVersion->attribute('version');
                 foreach ($translations as $translation) {
                     $objectAttribute = eZContentObjectAttribute::create($classAttributeID, $contentobjectID, $version, $translation);
                     $objectAttribute->setAttribute('language_code', $translation);
                     $objectAttribute->initialize();
                     $objectAttribute->store();
                     $objectAttribute->postInitialize();
                 }
             }
         }
     }
 }
コード例 #6
0
ファイル: AttributeFunctions.php プロジェクト: truffo/eep
 /**
  * update all the objects with the new attribute info
  * todo; this might have to support processing in batches
  */
 static function updateContentObjectAttributes($contentClass, $classAttributeID, $identifier = false)
 {
     $classId = $contentClass->attribute("id");
     // update object attributes
     $countProcessed = 0;
     $batchSize = 200;
     $totalObjectCount = eZContentObject::fetchSameClassListCount($classId);
     for ($offset = 0; $offset < $totalObjectCount; $offset += $batchSize) {
         $objects = eZContentObject::fetchSameClassList($classId, false, $offset, $batchSize);
         foreach ($objects as $num => $object) {
             $object = eZContentObject::fetch($object["id"]);
             if ($object) {
                 $contentobjectID = $object->attribute("id");
                 $objectVersions = $object->versions();
                 foreach ($objectVersions as $objectVersion) {
                     $translations = $objectVersion->translations(false);
                     $version = $objectVersion->attribute("version");
                     $dataMap = $objectVersion->attribute("data_map");
                     if ($identifier && isset($dataMap[$identifier])) {
                         // Attribute already exists for this object version
                     } else {
                         foreach ($translations as $translation) {
                             $objectAttribute = eZContentObjectAttribute::create($classAttributeID, $contentobjectID, $version);
                             $objectAttribute->setAttribute("language_code", $translation);
                             $objectAttribute->initialize();
                             $objectAttribute->store();
                             $objectAttribute->postInitialize();
                         }
                     }
                 }
                 $countProcessed += 1;
             }
             echo "Percent complete: " . sprintf("% 3.2f", $countProcessed / $totalObjectCount * 100.0) . "%\r";
         }
         unset($GLOBALS["eZContentObjectContentObjectCache"]);
         unset($GLOBALS["eZContentObjectDataMapCache"]);
         unset($GLOBALS["eZContentObjectVersionCache"]);
         unset($objects);
     }
     echo "\n";
 }
コード例 #7
0
 public static function addClassAttributes($class_identifier, $attributesInfo)
 {
     if (isset($class_identifier)) {
         $class = eZContentClass::fetchByIdentifier($class_identifier);
     }
     if (!is_object($class)) {
         return;
     }
     $classID = $class->attribute('id');
     foreach ($attributesInfo as $attributeInfo) {
         $classAttributeIdentifier = $attributeInfo['identifier'];
         $classAttributeName = $attributeInfo['name'];
         $datatype = $attributeInfo['data_type_string'];
         $defaultValue = isset($attributeInfo['default_value']) ? $attributeInfo['default_value'] : false;
         $canTranslate = isset($attributeInfo['can_translate']) ? $attributeInfo['can_translate'] : 1;
         $isRequired = isset($attributeInfo['is_required']) ? $attributeInfo['is_required'] : 0;
         $isSearchable = isset($attributeInfo['is_searchable']) ? $attributeInfo['is_searchable'] : 1;
         $attrContent = isset($attributeInfo['content']) ? $attributeInfo['content'] : false;
         $attrCreateInfo = array('identifier' => $classAttributeIdentifier, 'name' => $classAttributeName, 'can_translate' => $canTranslate, 'is_required' => $isRequired, 'is_searchable' => $isSearchable);
         $newAttribute = eZContentClassAttribute::create($classID, $datatype, $attrCreateInfo);
         $dataType = $newAttribute->dataType();
         $dataType->initializeClassAttribute($newAttribute);
         // not all datatype can have 'default_value'. do check here.
         if ($defaultValue !== false) {
             switch ($datatype) {
                 case 'ezboolean':
                     $newAttribute->setAttribute('data_int3', $defaultValue);
                     break;
                 default:
                     break;
             }
         }
         if ($attrContent) {
             $newAttribute->setContent($attrContent);
         }
         // store attribute, update placement, etc...
         $attributes = $class->fetchAttributes();
         $attributes[] = $newAttribute;
         // remove temporary version
         if ($newAttribute->attribute('id') !== null) {
             $newAttribute->remove();
         }
         $newAttribute->setAttribute('version', eZContentClass::VERSION_STATUS_DEFINED);
         $newAttribute->setAttribute('placement', count($attributes));
         $class->adjustAttributePlacements($attributes);
         foreach ($attributes as $attribute) {
             $attribute->storeDefined();
         }
         // update objects
         $classAttributeID = $newAttribute->attribute('id');
         $count = eZContentObject::fetchSameClassListCount($class->ID);
         $output = new ezcConsoleOutput();
         $bar = new ezcConsoleProgressbar($output, (int) $count);
         $offset = 0;
         $limit = 50;
         while (true) {
             if ($offset > $count) {
                 break;
             }
             $objects = eZContentObject::fetchSameClassList($classID, true, $offset, $limit);
             foreach ($objects as $object) {
                 $contentobjectID = $object->attribute('id');
                 $objectVersions = $object->versions();
                 foreach ($objectVersions as $objectVersion) {
                     $translations = $objectVersion->translations(false);
                     $version = $objectVersion->attribute('version');
                     foreach ($translations as $translation) {
                         $objectAttribute = eZContentObjectAttribute::create($classAttributeID, $contentobjectID, $version);
                         $objectAttribute->setAttribute('language_code', $translation);
                         $objectAttribute->initialize();
                         $objectAttribute->store();
                         $objectAttribute->postInitialize();
                     }
                 }
                 $bar->advance();
             }
             eZContentObject::clearCache();
             $offset += $limit;
         }
         $bar->finish();
     }
 }