Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Storing eZ Tags
<?php
/**
 * Created by JetBrains PhpStorm.
 * User: tyler
 * Date: 1/6/12
 * Time: 4:33 PM
 * To change this template use File | Settings | File Templates.
 */
class AddeZTag
{
    public static function store($keyword, $contentObjectId, $contentObjectAttributeId, $attributeVersion)
    {
        $blkeyword = strtolower($keyword);
        $blacklisted = false;
        $eZTagsINI = eZINI::instance( 'eztags.ini' );
        $blacklist = $eZTagsINI->variable( 'BlacklistFile', 'blacklist' );
        $blacklist = file($blacklist);
        if (in_array($blkeyword,$blacklist)){
            $blacklisted = true;
        }
        if ($blacklisted == false){
            foreach ($blacklist as $bl) {
                if ($blkeyword == trim(strtolower($bl)) ){
                    $blacklisted = true;
                    break;
                }
            }
        }
        if ($blacklisted == true){
            return false;
        }
        //rebuilding the add module file here with necessary modifications
        $error = '';
        $relations = FALSE;
        $parentTag = FALSE;
        $newKeyword = trim( $keyword );
        //echo "<pre>"; print_r($keyword); echo "</pre>";
        if ( strlen( trim( $newKeyword ) ) == 0 )
        {
            $error = ezpI18n::tr( 'extension/eztags/errors', 'Name cannot be empty.' );
        }
 
        if ( empty( $error ) && eZTagsObject::exists( 0, $newKeyword, ( $parentTag instanceof eZTagsObject ) ? $parentTag->ID : 0 ) )
        {
            $db = eZDB::instance();
            $linkExists = FALSE;
            $error = ezpI18n::tr( 'extension/eztags/errors', 'Tag/synonym with that name already exists in selected location.' );
            $currTag = $db->arrayQuery("SELECT DISTINCT id FROM eztags WHERE keyword = '" . $newKeyword . "'");
            $relations = eZTagsAttributeLinkObject::fetchByTagID($currTag[0]['id']);
 
            if ($relations)
            {
                foreach ($relations as $relation) {
                    if ($relation->ObjectID == $contentObjectId){
                        $linkExists = TRUE;
                        break;
                    }
                }
 
                if (!$linkExists)
                {
                    //if no links exist, build a new link object, then store and commit it to the DB
                    $db->begin();
                    $link = new eZTagsAttributeLinkObject( array( 'keyword_id'              => $currTag[0]['id'],
                                                                  'objectattribute_id'      => $contentObjectAttributeId,
                                                                  'objectattribute_version' => $attributeVersion,
                                                                  'object_id'               => $contentObjectId
                    ) );
                    $link->store();
                    $db->commit();
                }
            }
            else
            {
                $db->begin();
                $link = new eZTagsAttributeLinkObject( array( 'keyword_id'              => $currTag[0]['id'],
                                                              'objectattribute_id'      => $contentObjectAttributeId,
                                                              'objectattribute_version' => $attributeVersion,
                                                              'object_id'               => $contentObjectId
                ) );
                $link->store();
                $db->commit();
            }
 
        }
        if ( empty( $error ) )
        {
            $db = eZDB::instance();
            $db->begin();
 
            //build the $tag object
            $tag = new eZTagsObject( array( 'parent_id'   =>  0,
                                            'main_tag_id' => 0,
                                            'keyword'     => $newKeyword,
                                            'depth'       =>  1,
                                            'path_string' =>  '/' ) );
            //store the tag, ID is auto-generated
            $tag->store();
            //get the id and set it to the path string
            $tag->PathString = $tag->PathString . $tag->ID . '/';
            $tagId = $tag->ID;
            //re-store the tag with the path string
            $tag->store();
            $tag->updateModified();
 
            //$build the $link object
            $link = new eZTagsAttributeLinkObject( array( 'keyword_id'              => $tagId,
                                                          'objectattribute_id'      => $contentObjectAttributeId,
                                                          'objectattribute_version' => $attributeVersion,
                                                          'object_id'               => $contentObjectId
            ) );
            //store the $link object
            $link->store();
 
 
            //commit Database Changes
            $db->commit();
        }
        eZContentCacheManager::clearViewCache($contentObjectId);
         
        return;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment