eZ Publish  [4.0]
ezcontentobjectassignmenthandler.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZContentObjectAssignmentHandler class
00004 //
00005 // Created on: <06-Mar-2003 13:32:27 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezcontentobjectassignmenthandler.php
00032 */
00033 
00034 /*!
00035   \class eZContentObjectAssignmentHandler ezcontentobjectassignmenthandler.php
00036   \brief Handles default assignments for content objects
00037 
00038 */
00039 
00040 //include_once( 'kernel/classes/ezcontentobject.php' );
00041 //include_once( 'kernel/classes/ezcontentobjectversion.php' );
00042 //include_once( 'kernel/classes/ezcontentobjecttreenode.php' );
00043 //include_once( 'kernel/classes/eznodeassignment.php' );
00044 
00045 class eZContentObjectAssignmentHandler
00046 {
00047     /*!
00048      Constructor
00049     */
00050     function eZContentObjectAssignmentHandler( $contentObject, $contentVersion )
00051     {
00052         $this->CurrentObject = $contentObject;
00053         $this->CurrentVersion = $contentVersion;
00054     }
00055 
00056     function nodeIDList( $selectionText )
00057     {
00058         $nodeList = array();
00059         $items = explode( ',', trim( $selectionText ) );
00060         foreach ( $items as $item )
00061         {
00062             $item = trim( $item );
00063             if ( $item != '' )
00064             {
00065                 $nodeID = $this->nodeID( $item );
00066                 if ( $nodeID !== false )
00067                 {
00068                     $nodeList[] = $nodeID;
00069                 }
00070             }
00071         }
00072         return $nodeList;
00073     }
00074 
00075     function nodeID( $name )
00076     {
00077         if ( is_numeric( $name ) )
00078         {
00079             $nodeID = false;
00080             $node = eZContentObjectTreeNode::fetch( $name, false, false );
00081             if ( $node )
00082                 $nodeID = $node['node_id'];
00083             return $nodeID;
00084         }
00085         $contentINI = eZINI::instance( 'content.ini' );
00086         switch ( $name )
00087         {
00088             case 'root':
00089             {
00090                 return $contentINI->variable( 'NodeSettings', 'RootNode' );
00091             }
00092             case 'users':
00093             {
00094                 return $contentINI->variable( 'NodeSettings', 'UserRootNode' );
00095             }
00096             case 'none':
00097             {
00098                 return false;
00099             }
00100             default:
00101             {
00102                 eZDebug::writeError( "Unknown node type '$name'", 'eZContentObjectAssignmentHandler::nodeID' );
00103             } break;
00104         }
00105         return false;
00106     }
00107 
00108     /*!
00109      \note Transaction unsafe. If you call several transaction unsafe methods you must enclose
00110      the calls within a db transaction; thus within db->begin and db->commit.
00111      */
00112     function setupAssignments( $parameters )
00113     {
00114         $parameters = array_merge( array( 'group-name' => false,
00115                                           'default-variable-name' => false,
00116                                           'specific-variable-name' => false,
00117                                           'fallback-node-id' => false,
00118                                           'section-id-wanted' => false ),
00119                                    $parameters );
00120         if ( !$parameters['group-name'] and
00121              !$parameters['default-variable-name'] and
00122              !$parameters['specific-variable-name'] )
00123              return false;
00124         $contentINI = eZINI::instance( 'content.ini' );
00125         $defaultAssignment = $contentINI->variable( $parameters['group-name'], $parameters['default-variable-name'] );
00126         $specificAssignments = $contentINI->variable( $parameters['group-name'], $parameters['specific-variable-name'] );
00127         $hasAssignment = false;
00128         $assignments = false;
00129         $sectionIDWanted = $parameters['section-id-wanted'];
00130         $sectionID = 0;
00131         $contentClass = $this->CurrentObject->attribute( 'content_class' );
00132         $contentClassIdentifier = $contentClass->attribute( 'identifier' );
00133         $contentClassID = $contentClass->attribute( 'id' );
00134         foreach ( $specificAssignments as $specificAssignment )
00135         {
00136             $assignmentRules = explode( ';', $specificAssignment );
00137             $classMatches = $assignmentRules[0];
00138             $assignments = $assignmentRules[1];
00139             $mainID = false;
00140             if ( isset( $assignmentRules[2] ) )
00141                 $mainID = $assignmentRules[2];
00142             $classMatchArray = explode( ',', $classMatches );
00143             foreach ( $classMatchArray as $classMatch )
00144             {
00145                 $classMatch = trim( $classMatch );
00146                 if ( preg_match( "#^group_([0-9]+)$#", $classMatch, $matches ) )
00147                 {
00148                     $classGroupID = $matches[1];
00149                     if ( $contentClass->inGroup( $classGroupID ) )
00150                     {
00151                         $hasAssignment = true;
00152                         break;
00153                     }
00154                 }
00155                 else if ( $classMatch == $contentClassIdentifier )
00156                 {
00157                     $hasAssignment = true;
00158                     break;
00159                 }
00160                 else if ( $classMatch == $contentClassID )
00161                 {
00162                     $hasAssignment = true;
00163                     break;
00164                 }
00165             }
00166             if ( $hasAssignment )
00167                 break;
00168         }
00169         if ( !$hasAssignment )
00170         {
00171             $assignmentRules = explode( ';', $defaultAssignment );
00172             $assignments = $assignmentRules[0];
00173             $mainID = false;
00174             if ( isset( $assignmentRules[1] ) )
00175                 $mainID = $assignmentRules[1];
00176         }
00177         eZDebug::writeDebug( $assignments, 'assignments' );
00178         if ( $assignments )
00179         {
00180             if ( $mainID )
00181                 $mainID = $this->nodeID( $mainID );
00182             $nodeList = $this->nodeIDList( $assignments );
00183             eZDebug::writeDebug( $nodeList, 'nodeList' );
00184             $assignmentCount = 0;
00185             eZDebug::writeDebug( $this->CurrentObject->attribute( 'id' ), 'current object' );
00186             eZDebug::writeDebug( $this->CurrentVersion->attribute( 'version' ), 'current version' );
00187             foreach ( $nodeList as $nodeID )
00188             {
00189                 $node = eZContentObjectTreeNode::fetch( $nodeID );
00190                 if ( !$node )
00191                     continue;
00192                 $parentContentObject = $node->attribute( 'object' );
00193 
00194                 eZDebug::writeDebug( "Checking for '$nodeID'" );
00195                 if ( $parentContentObject->checkAccess( 'create',
00196                                                         $contentClassID,
00197                                                         $parentContentObject->attribute( 'contentclass_id' ) ) == '1' )
00198                 {
00199                     eZDebug::writeDebug( "Adding to '$nodeID' and main = '$mainID'" );
00200                     if ( $mainID === false )
00201                     {
00202                         $isMain = ( $assignmentCount == 0 );
00203                     }
00204                     else
00205                         $isMain = ( $mainID == $nodeID );
00206 
00207                     /* Here we figure out the section ID in case it is needed
00208                      * to assign a newly created object to. */
00209                     if ( $sectionIDWanted and $isMain )
00210                     {
00211                         $db = eZDB::instance();
00212                         $query = "SELECT section_id
00213                                   FROM ezcontentobject c, ezcontentobject_tree t
00214                                   WHERE t.node_id = 109
00215                                       AND t.contentobject_id = c.id";
00216                         $result = $db->arrayQuery( $query );
00217                         $sectionID = $result[0]['section_id'];
00218                     }
00219 
00220                     $nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $this->CurrentObject->attribute( 'id' ),
00221                                                                        'contentobject_version' => $this->CurrentVersion->attribute( 'version' ),
00222                                                                        'parent_node' => $node->attribute( 'node_id' ),
00223                                                                        'is_main' => $isMain ) );
00224                     $nodeAssignment->store();
00225                     ++$assignmentCount;
00226                 }
00227                 return $sectionID;
00228             }
00229 
00230             if ( $assignmentCount == 0 &&
00231                  $parameters['fallback-node-id'] )
00232             {
00233                 $nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $this->CurrentObject->attribute( 'id' ),
00234                                                                    'contentobject_version' => $this->CurrentVersion->attribute( 'version' ),
00235                                                                    'parent_node' => $parameters['fallback-node-id'],
00236                                                                    'is_main' => true ) );
00237                 $nodeAssignment->store();
00238                 ++$assignmentCount;
00239             }
00240         }
00241         return true;
00242     }
00243 
00244     /// \privatesection
00245     public $CurrentObject;
00246     public $ContentVersion;
00247 }
00248 
00249 ?>