Initialize contentobject attribute content

@param eZContentObjectAttribute $contentObjectAttribute
@param integer $currentVersion
@param eZContentObjectAttribute $originalContentObjectAttribute
349705
function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
    {
        if ( $currentVersion != false )
        {
            $contentObjectID = $contentObjectAttribute->attribute( 'contentobject_id' );
            $originalContentObjectID = $originalContentObjectAttribute->attribute( 'contentobject_id' );
            $languageMask = $contentObjectAttribute->attribute( 'language_id' ) & ~1;
            $originalLanguageMask = $originalContentObjectAttribute->attribute( 'language_id' ) & ~1;

            // Case when content object was copied or when new translation has been added to the existing one
            if ( ( $contentObjectID != $originalContentObjectID )
                    || ( ( $contentObjectID == $originalContentObjectID )
                       && ( $languageMask != $originalLanguageMask )
                            && ( $contentObjectAttribute->attribute( 'can_translate' ) ) ) )
            {
                $page = $originalContentObjectAttribute->content();
                $clonedPage = clone $page;
                $contentObjectAttribute->setContent( $clonedPage );
                $contentObjectAttribute->store();
            }
            else
            {
                $dataText = $originalContentObjectAttribute->attribute( 'data_text' );
                $contentObjectAttribute->setAttribute( 'data_text', $dataText );
            }
        }
        else
        {
            $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
            $defaultLayout = $contentClassAttribute->attribute( self::DEFAULT_ZONE_LAYOUT_FIELD );
            $zoneINI = eZINI::instance( 'zone.ini' );
            $page = new eZPage();
            $zones = array();
            if ( $defaultLayout !== '' )
            {
                if ( $zoneINI->hasVariable( $defaultLayout, 'Zones' ) )
                    $zones = $zoneINI->variable( $defaultLayout, 'Zones' );

                $page->setAttribute( 'zone_layout', $defaultLayout );
                foreach ( $zones as $zoneIdentifier )
                {
                    $newZone = $page->addZone( new eZPageZone() );
                    $newZone->setAttribute( 'id', md5( mt_rand() . microtime() . $page->getZoneCount() ) );
                    $newZone->setAttribute( 'zone_identifier', $zoneIdentifier );
                    $newZone->setAttribute( 'action', 'add' );
                }
            }
            else
            {
                $allowedZones = array();
                if ( $zoneINI->hasGroup( 'General' ) && $zoneINI->hasVariable( 'General', 'AllowedTypes' ) )
                    $allowedZones = $zoneINI->variable( 'General', 'AllowedTypes' );

                $class = eZContentClass::fetch( $contentClassAttribute->attribute( 'contentclass_id' ) );

                foreach ( $allowedZones as $allowedZone )
                {
                    $availableForClasses = array();
                    if ( $zoneINI->hasVariable( $allowedZone, 'AvailableForClasses' ) )
                        $availableForClasses = $zoneINI->variable( $allowedZone, 'AvailableForClasses' );

                    if ( in_array( $class->attribute( 'identifier' ), $availableForClasses ) )
                    {
                        if ( $zoneINI->hasVariable( $allowedZone, 'Zones' ) )
                            $zones = $zoneINI->variable( $allowedZone, 'Zones' );

                        $page->setAttribute( 'zone_layout', $allowedZone );
                        foreach ( $zones as $zoneIdentifier )
                        {
                            $newZone = $page->addZone( new eZPageZone() );
                            $newZone->setAttribute( 'id', md5( mt_rand() . microtime() . $page->getZoneCount() ) );
                            $newZone->setAttribute( 'zone_identifier', $zoneIdentifier );
                            $newZone->setAttribute( 'action', 'add' );
                        }

                        break;
                    }
                    else
                        continue;
                }
            }
            $contentObjectAttribute->setContent( $page );
        }
    }
297113
public function loadContentInVersion($contentId, $versionNumber, Request $request)
    {
        $languages = Language::ALL;
        if ($request->query->has('languages')) {
            $languages = explode(',', $request->query->get('languages'));
        }

        $content = $this->repository->getContentService()->loadContent(
            $contentId,
            $languages,
            $versionNumber
        );
        $contentType = $this->repository->getContentTypeService()->loadContentType(
            $content->getVersionInfo()->getContentInfo()->contentTypeId
        );

        $versionValue = new Values\Version(
            $content,
            $contentType,
            $this->repository->getContentService()->loadRelations($content->getVersionInfo()),
            $request->getPathInfo()
        );

        if ($content->contentInfo->mainLocationId === null || $content->versionInfo->status === VersionInfo::STATUS_DRAFT) {
            return $versionValue;
        }

        return new Values\CachedValue(
            $versionValue,
            array('locationId' => $content->contentInfo->mainLocationId)
        );
    }
1331957
function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
    {
        if ( $currentVersion != false )
        {
            $xmlText = eZXMLTextType::rawXMLText( $originalContentObjectAttribute );
            $contentObjectAttribute->setAttribute( "data_text", $xmlText );
        }
        else
        {
            $parser = new eZXMLInputParser();
            $doc = $parser->createRootNode();
            $xmlText = eZXMLTextType::domString( $doc );
            $contentObjectAttribute->setAttribute( "data_text", $xmlText );
        }
    }
638684
public static function fromContentObject( eZContentObject $contentObject )
    {
        $set = new ezpContentFieldSet();
        $languages = $contentObject->availableLanguages();
        foreach( $languages as $language )
        {
            $set->childrenFieldSets[$language] =
                ezpContentFieldSet::fromDataMap( $contentObject->fetchDataMap( false, $language ) );
        }

        // this sequence is REALLY ugly
        reset( $languages );
        $set->setActiveLanguage( current( $languages ) );

        return $set;
    }
766740
function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
    {
        if ( $currentVersion != false )
        {
            $dataText = $originalContentObjectAttribute->attribute( "data_text" );
            $contentObjectAttribute->setAttribute( "data_text", $dataText );
        }
    }
489
public function createDraftFromCurrentVersion($contentId)
    {
        $contentInfo = $this->repository->getContentService()->loadContentInfo($contentId);
        $contentType = $this->repository->getContentTypeService()->loadContentType($contentInfo->contentTypeId);
        $versionInfo = $this->repository->getContentService()->loadVersionInfo(
            $contentInfo
        );

        if ($versionInfo->isDraft()) {
            throw new ForbiddenException('Current version is already in status DRAFT');
        }

        $contentDraft = $this->repository->getContentService()->createContentDraft($contentInfo);

        return new Values\CreatedVersion(
            array(
                'version' => new Values\Version(
                    $contentDraft,
                    $contentType,
                    $this->repository->getContentService()->loadRelations($contentDraft->getVersionInfo())
                ),
            )
        );
    }
1150986
function createContentObjectAttributeDOMNode( $objectAttribute )
    {
        $dom = new DOMDocument( '1.0', 'utf-8' );

        $node = $dom->createElementNS( 'http://ez.no/object/', 'ezobject:attribute' );

        $node->setAttributeNS( 'http://ez.no/ezobject', 'ezremote:id', $objectAttribute->attribute( 'id' ) );
        $node->setAttributeNS( 'http://ez.no/ezobject', 'ezremote:identifier', $objectAttribute->contentClassAttributeIdentifier() );
        $node->setAttribute( 'name', $objectAttribute->contentClassAttributeName() );
        $node->setAttribute( 'type', $this->isA() );

        return $node;
    }
831973
public function contentVersion($contentId, $versionNo)
    {
        if ($this->allCleared === true || $this->isSwitchedOff()) {
            return;
        }

        // todo Once we can require 2.2.2+, simplify to just invalidate "content-{$contentId}-version-{$versionNo}"
        $this->cache->deleteItem("ez-content-version-info-${contentId}-${versionNo}");
        $this->cache->invalidateTags(["content-${contentId}-version-list", "content-{$contentId}-version-{$versionNo}"]);
    }
1065550
public function postProcessContentObjectInitialization(ContentObjectRenderer &$parentObject)
    {
        if (!empty($parentObject->currentRecord)) {
            list($table) = explode(':', $parentObject->currentRecord);

            if (!empty($table) && $table != 'pages') {
                $this->findFrontendGroups($parentObject->data, $table);
            }
        }
    }
321013
public function updateVersion($contentId, $versionNumber, Request $request)
    {
        if (!$this->isUserContent($contentId)) {
            return parent::updateVersion($contentId, $versionNumber, $request);
        }

        $updatedUser = $this->repository->getUserService()->updateUser(
            $this->repository->getUserService()->loadUser($contentId),
            $this->mapRequestToUserUpdateStruct($request)
        );

        return $this->mapUserToVersion($updatedUser, $request);
    }
1193898
function downloadFileObject( $contentObject, $contentObjectAttribute )
    {
        $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
        $version = $contentObject->attribute( 'current_version' );
        $fileObject = eZBinaryFile::fetch( $contentObjectAttributeID, $version );
        if ( $fileObject )
            return $fileObject;
        $fileObject = eZMedia::fetch( $contentObjectAttributeID, $version );
        return $fileObject;
    }
95718
function objectAttributeContent( $contentObjectAttribute )
    {
        $contentObjectAttributeID = $contentObjectAttribute->attribute( 'id' );
        $contentObjectAttributeVersion = $contentObjectAttribute->attribute( 'version' );
        $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
        $id = $contentClassAttribute->attribute( 'id' );
        $version = $contentClassAttribute->attribute( 'version' );
        $ismultiple = $contentClassAttribute->attribute( 'data_int1' );
        $isoption = $contentClassAttribute->attribute( 'data_int2' );
        $enum = new eZEnum( $id, $version );
        $enum->setIsmultipleValue( $ismultiple );
        $enum->setIsoptionValue( $isoption );
        $enum->setObjectEnumValue( $contentObjectAttributeID, $contentObjectAttributeVersion );
        return $enum;
    }
17733
function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
    {
        if ( $currentVersion != false )
        {
            $dataInt = $originalContentObjectAttribute->attribute( 'data_int' );
            $contentObjectAttribute->setAttribute( 'data_int', $dataInt );
        }
        else
        {
            $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
            $defaultType = $contentClassAttribute->attribute( self::DEFAULT_FIELD );

            if ( $defaultType == 1 )
            {
                $time = new eZTime();
                $contentObjectAttribute->setAttribute( 'data_int', $time->timeOfDay() );
            }
        }
    }
74353
function initializeObjectAttribute( $contentObjectAttribute, $currentVersion, $originalContentObjectAttribute )
    {

        if ( $currentVersion != false )
        {
            $matrix = $originalContentObjectAttribute->content();
            $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
            // make sure that $matrix contains right columns
            $matrix->adjustColumnsToDefinition( $contentClassAttribute->attribute( 'content' ) );

            $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
            $contentObjectAttribute->setContent( $matrix );
        }
        else
        {
            $contentClassAttribute = $contentObjectAttribute->contentClassAttribute();
            $numRows = $contentClassAttribute->attribute( 'data_int1' );
            $matrix = new eZMatrix( '', $numRows, $contentClassAttribute->attribute( 'content' ) );
            // 'default name' is never used => just a stub
            // $matrix->setName( $contentClassAttribute->attribute( 'data_text1' ) );
            $contentObjectAttribute->setAttribute( 'data_text', $matrix->xmlString() );
            $contentObjectAttribute->setContent( $matrix );
        }

    }
1268907
public function copyRelations($originalContentId, $copiedContentId, $versionNo = null)
    {
        // Given we can retain all columns, we just create copies with new `from_contentobject_id` using INSERT INTO SELECT
        $sql = 'INSERT INTO ezcontentobject_link ( contentclassattribute_id, from_contentobject_id, from_contentobject_version, relation_type, to_contentobject_id )
                SELECT  L2.contentclassattribute_id, :copied_id, L2.from_contentobject_version, L2.relation_type, L2.to_contentobject_id
                FROM    ezcontentobject_link AS L2
                WHERE   L2.from_contentobject_id = :original_id';

        if ($versionNo) {
            $stmt = $this->connection->prepare($sql . ' AND L2.from_contentobject_version = :version');
            $stmt->bindValue('version', $versionNo, PDO::PARAM_INT);
        } else {
            $stmt = $this->connection->prepare($sql);
        }

        $stmt->bindValue('original_id', $originalContentId, PDO::PARAM_INT);
        $stmt->bindValue('copied_id', $copiedContentId, PDO::PARAM_INT);

        $stmt->execute();
    }
978066
function objectAttributeContent( $contentObjectAttribute )
    {
        $value = $contentObjectAttribute->attribute( 'data_text' );

        $countryList = explode( ',', $value );
        $resultList = array();
        foreach ( $countryList as $alpha2 )
        {
            $eZCountry = eZCountryType::fetchCountry( $alpha2, 'Alpha2' );
            $resultList[$alpha2] = $eZCountry ? $eZCountry : '';
        }
        // Supporting of previous version format.
        // For backwards compatibility.
        if ( count( $resultList ) == 1 and $resultList[$value] == '' )
            $resultList = $value;

        $content = array( 'value' => $resultList );
        return $content;
    }
895636
public function updateVersion($contentId, $versionNo, UpdateStruct $struct)
    {
        $q = $this->dbHandler->createUpdateQuery();
        $q->update(
            $this->dbHandler->quoteTable('ezcontentobject_version')
        )->set(
            $this->dbHandler->quoteColumn('creator_id'),
            $q->bindValue($struct->creatorId, null, \PDO::PARAM_INT)
        )->set(
            $this->dbHandler->quoteColumn('modified'),
            $q->bindValue($struct->modificationDate, null, \PDO::PARAM_INT)
        )->set(
            $this->dbHandler->quoteColumn('initial_language_id'),
            $q->bindValue($struct->initialLanguageId, null, \PDO::PARAM_INT)
        )->set(
            $this->dbHandler->quoteColumn('language_mask'),
            $q->expr->bitOr(
                $this->dbHandler->quoteColumn('language_mask'),
                $q->bindValue(
                    $this->generateLanguageMask(
                        $struct->fields,
                        $this->languageHandler->load($struct->initialLanguageId)->languageCode,
                        false
                    ),
                    null,
                    \PDO::PARAM_INT
                )
            )
        )->where(
            $q->expr->lAnd(
                $q->expr->eq(
                    $this->dbHandler->quoteColumn('contentobject_id'),
                    $q->bindValue($contentId, null, \PDO::PARAM_INT)
                ),
                $q->expr->eq(
                    $this->dbHandler->quoteColumn('version'),
                    $q->bindValue($versionNo, null, \PDO::PARAM_INT)
                )
            )
        );
        $q->prepare()->execute();
    }