eZ Publish  [trunk]
ezcontentobjectassignmenthandler.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZContentObjectAssignmentHandler class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package kernel
00009  */
00010 
00011 /*!
00012   \class eZContentObjectAssignmentHandler ezcontentobjectassignmenthandler.php
00013   \brief Handles default assignments for content objects
00014 
00015 */
00016 
00017 class eZContentObjectAssignmentHandler
00018 {
00019     /*!
00020      Constructor
00021     */
00022     function eZContentObjectAssignmentHandler( $contentObject, $contentVersion )
00023     {
00024         $this->CurrentObject = $contentObject;
00025         $this->CurrentVersion = $contentVersion;
00026     }
00027 
00028     function nodeIDList( $selectionText )
00029     {
00030         $nodeList = array();
00031         $items = explode( ',', trim( $selectionText ) );
00032         foreach ( $items as $item )
00033         {
00034             $item = trim( $item );
00035             if ( $item != '' )
00036             {
00037                 $nodeID = $this->nodeID( $item );
00038                 if ( $nodeID !== false )
00039                 {
00040                     $nodeList[] = $nodeID;
00041                 }
00042             }
00043         }
00044         return $nodeList;
00045     }
00046 
00047     function nodeID( $name )
00048     {
00049         if ( is_numeric( $name ) )
00050         {
00051             $nodeID = false;
00052             $node = eZContentObjectTreeNode::fetch( $name, false, false );
00053             if ( $node )
00054                 $nodeID = $node['node_id'];
00055             return $nodeID;
00056         }
00057         $contentINI = eZINI::instance( 'content.ini' );
00058         switch ( $name )
00059         {
00060             case 'root':
00061             {
00062                 return $contentINI->variable( 'NodeSettings', 'RootNode' );
00063             }
00064             case 'users':
00065             {
00066                 return $contentINI->variable( 'NodeSettings', 'UserRootNode' );
00067             }
00068             case 'none':
00069             {
00070                 return false;
00071             }
00072             default:
00073             {
00074                 eZDebug::writeError( "Unknown node type '$name'", __METHOD__ );
00075             } break;
00076         }
00077         return false;
00078     }
00079 
00080     /*!
00081      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00082      the calls within a db transaction; thus within db->begin and db->commit.
00083      */
00084     function setupAssignments( $parameters )
00085     {
00086         $parameters = array_merge( array( 'group-name' => false,
00087                                           'default-variable-name' => false,
00088                                           'specific-variable-name' => false,
00089                                           'fallback-node-id' => false,
00090                                           'section-id-wanted' => false ),
00091                                    $parameters );
00092         if ( !$parameters['group-name'] and
00093              !$parameters['default-variable-name'] and
00094              !$parameters['specific-variable-name'] )
00095              return false;
00096         $contentINI = eZINI::instance( 'content.ini' );
00097         $defaultAssignment = $contentINI->variable( $parameters['group-name'], $parameters['default-variable-name'] );
00098         $specificAssignments = $contentINI->variable( $parameters['group-name'], $parameters['specific-variable-name'] );
00099         $hasAssignment = false;
00100         $assignments = false;
00101         $sectionIDWanted = $parameters['section-id-wanted'];
00102         $sectionID = 0;
00103         $contentClass = $this->CurrentObject->attribute( 'content_class' );
00104         $contentClassIdentifier = $contentClass->attribute( 'identifier' );
00105         $contentClassID = $contentClass->attribute( 'id' );
00106         foreach ( $specificAssignments as $specificAssignment )
00107         {
00108             $assignmentRules = explode( ';', $specificAssignment );
00109             $classMatches = $assignmentRules[0];
00110             $assignments = $assignmentRules[1];
00111             $mainID = false;
00112             if ( isset( $assignmentRules[2] ) )
00113                 $mainID = $assignmentRules[2];
00114             $classMatchArray = explode( ',', $classMatches );
00115             foreach ( $classMatchArray as $classMatch )
00116             {
00117                 $classMatch = trim( $classMatch );
00118                 if ( preg_match( "#^group_([0-9]+)$#", $classMatch, $matches ) )
00119                 {
00120                     $classGroupID = $matches[1];
00121                     if ( $contentClass->inGroup( $classGroupID ) )
00122                     {
00123                         $hasAssignment = true;
00124                         break;
00125                     }
00126                 }
00127                 else if ( $classMatch == $contentClassIdentifier )
00128                 {
00129                     $hasAssignment = true;
00130                     break;
00131                 }
00132                 else if ( $classMatch == $contentClassID )
00133                 {
00134                     $hasAssignment = true;
00135                     break;
00136                 }
00137             }
00138             if ( $hasAssignment )
00139                 break;
00140         }
00141         if ( !$hasAssignment )
00142         {
00143             $assignmentRules = explode( ';', $defaultAssignment );
00144             $assignments = $assignmentRules[0];
00145             $mainID = false;
00146             if ( isset( $assignmentRules[1] ) )
00147                 $mainID = $assignmentRules[1];
00148         }
00149         eZDebug::writeDebug( $assignments, 'assignments' );
00150         if ( $assignments )
00151         {
00152             if ( $mainID )
00153                 $mainID = $this->nodeID( $mainID );
00154             $nodeList = $this->nodeIDList( $assignments );
00155             eZDebug::writeDebug( $nodeList, 'nodeList' );
00156             $assignmentCount = 0;
00157             eZDebug::writeDebug( $this->CurrentObject->attribute( 'id' ), 'current object' );
00158             eZDebug::writeDebug( $this->CurrentVersion->attribute( 'version' ), 'current version' );
00159             foreach ( $nodeList as $nodeID )
00160             {
00161                 $node = eZContentObjectTreeNode::fetch( $nodeID );
00162                 if ( !$node )
00163                     continue;
00164                 $parentContentObject = $node->attribute( 'object' );
00165 
00166                 eZDebug::writeDebug( "Checking for '$nodeID'" );
00167                 if ( $parentContentObject->checkAccess( 'create',
00168                                                         $contentClassID,
00169                                                         $parentContentObject->attribute( 'contentclass_id' ) ) == '1' )
00170                 {
00171                     eZDebug::writeDebug( "Adding to '$nodeID' and main = '$mainID'" );
00172                     if ( $mainID === false )
00173                     {
00174                         $isMain = ( $assignmentCount == 0 );
00175                     }
00176                     else
00177                         $isMain = ( $mainID == $nodeID );
00178 
00179                     /* Here we figure out the section ID in case it is needed
00180                      * to assign a newly created object to. */
00181                     if ( $sectionIDWanted and $isMain )
00182                     {
00183                         $db = eZDB::instance();
00184                         $query = "SELECT section_id
00185                                   FROM ezcontentobject c, ezcontentobject_tree t
00186                                   WHERE t.node_id = 109
00187                                       AND t.contentobject_id = c.id";
00188                         $result = $db->arrayQuery( $query );
00189                         $sectionID = $result[0]['section_id'];
00190                     }
00191 
00192                     $nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $this->CurrentObject->attribute( 'id' ),
00193                                                                        'contentobject_version' => $this->CurrentVersion->attribute( 'version' ),
00194                                                                        'parent_node' => $node->attribute( 'node_id' ),
00195                                                                        'is_main' => $isMain ) );
00196                     $nodeAssignment->store();
00197                     ++$assignmentCount;
00198                 }
00199                 return $sectionID;
00200             }
00201 
00202             if ( $assignmentCount == 0 &&
00203                  $parameters['fallback-node-id'] )
00204             {
00205                 $nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $this->CurrentObject->attribute( 'id' ),
00206                                                                    'contentobject_version' => $this->CurrentVersion->attribute( 'version' ),
00207                                                                    'parent_node' => $parameters['fallback-node-id'],
00208                                                                    'is_main' => true ) );
00209                 $nodeAssignment->store();
00210                 ++$assignmentCount;
00211             }
00212         }
00213         return true;
00214     }
00215 
00216     /// \privatesection
00217     public $CurrentObject;
00218     public $ContentVersion;
00219 }
00220 
00221 ?>