eZ Publish  [4.0]
ezmultiplexertype.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZMultiplexerType class
00004 //
00005 // Created on: <01-ξΟΡ-2002 15:34:23 sp>
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 ezmultiplexertype.php
00032 */
00033 
00034 /*!
00035   \class eZMultiplexerType ezmultiplexertype.php
00036   \brief The class eZMultiplexerType does
00037 
00038   WorkflowEvent storage fields : data_text1 - selected_sections
00039                                  data_text2 - selected_usergroups
00040                                  data_text3 - selected_classes
00041                                  data_int1  - selected_workflow
00042                                  data_int2  - language_list
00043                                  data_int3  - content object version option
00044 */
00045 
00046 class eZMultiplexerType extends eZWorkflowEventType
00047 {
00048     const WORKFLOW_TYPE_STRING = "ezmultiplexer";
00049     const VERSION_OPTION_FIRST_ONLY = 1;
00050     const VERSION_OPTION_EXCEPT_FIRST = 2;
00051     const VERSION_OPTION_ALL = 3;
00052 
00053     /*!
00054      Constructor
00055     */
00056     function eZMultiplexerType()
00057     {
00058         $this->eZWorkflowEventType( eZMultiplexerType::WORKFLOW_TYPE_STRING, ezi18n( 'kernel/workflow/event', 'Multiplexer' ) );
00059     }
00060 
00061     function attributeDecoder( $event, $attr )
00062     {
00063         switch ( $attr )
00064         {
00065             case 'selected_sections':
00066             {
00067                 $attributeValue = trim( $event->attribute( 'data_text1' ) );
00068                 $returnValue = empty( $attributeValue ) ? array( -1 ) : explode( ',', $attributeValue );
00069             }break;
00070 
00071             case 'selected_classes':
00072             {
00073                 $attributeValue = trim( $event->attribute( 'data_text3' ) );
00074                 $returnValue = empty( $attributeValue ) ? array( -1 ) : explode( ',', $attributeValue );
00075             }break;
00076 
00077             case 'selected_usergroups':
00078             {
00079                 $attributeValue = trim( $event->attribute('data_text2') );
00080                 $returnValue = empty( $attributeValue ) ? array() : explode( ',', $attributeValue );
00081             }break;
00082 
00083             case 'selected_workflow':
00084             {
00085                 $returnValue = $event->attribute( 'data_int1' );
00086             }break;
00087 
00088             case 'language_list':
00089             {
00090                 $returnValue = array();
00091                 $attributeValue = $event->attribute( 'data_int2' );
00092                 if ( $attributeValue != 0 )
00093                 {
00094                     //include_once( 'kernel/classes/ezcontentlanguage.php' );
00095                     $languages = eZContentLanguage::languagesByMask( $attributeValue );
00096                     foreach ( $languages as $language )
00097                     {
00098                         $returnValue[$language->attribute( 'id' )] = $language->attribute( 'name' );
00099                     }
00100                 }
00101             }break;
00102 
00103             case 'version_option':
00104             {
00105                 $returnValue = eZMultiplexerType::VERSION_OPTION_ALL & $event->attribute( 'data_int3' );
00106             }break;
00107 
00108             default:
00109                 $returnValue = null;
00110         }
00111         return $returnValue;
00112     }
00113 
00114     function typeFunctionalAttributes()
00115     {
00116         return array( 'selected_sections',
00117                       'selected_usergroups',
00118                       'selected_classes',
00119                       'selected_workflow',
00120                       'language_list',
00121                       'version_option' );
00122     }
00123 
00124     function attributes()
00125     {
00126         return array_merge( array( 'sections',
00127                                    'languages',
00128                                    'contentclass_list',
00129                                    'workflow_list',
00130                                    'usergroups' ),
00131                             eZWorkflowEventType::attributes() );
00132     }
00133 
00134     function hasAttribute( $attr )
00135     {
00136         return in_array( $attr, $this->attributes() );
00137     }
00138 
00139     function attribute( $attr )
00140     {
00141         switch( $attr )
00142         {
00143             case 'sections':
00144             {
00145                 //include_once( 'kernel/classes/ezsection.php' );
00146                 $sections = eZSection::fetchList( false );
00147                 foreach ( $sections as $key => $section )
00148                 {
00149                     $sections[$key]['Name'] = $section['name'];
00150                     $sections[$key]['value'] = $section['id'];
00151                 }
00152                 return $sections;
00153             }
00154             break;
00155 
00156             case 'languages':
00157             {
00158                 //include_once( 'kernel/classes/ezcontentlanguage.php' );
00159                 return eZContentLanguage::fetchList();
00160             }break;
00161 
00162             case 'usergroups':
00163             {
00164                 $groups = eZPersistentObject::fetchObjectList( eZContentObject::definition(), array( 'id', 'name' ),
00165                                                                 array( 'contentclass_id' => 3 ), null, null, false );
00166                 foreach ( $groups as $key => $group )
00167                 {
00168                     $groups[$key]['Name'] = $group['name'];
00169                     $groups[$key]['value'] = $group['id'];
00170                 }
00171                 return $groups;
00172             }
00173             break;
00174 
00175             case 'contentclass_list':
00176             {
00177                 $classes = eZContentClass::fetchList( eZContentClass::VERSION_STATUS_DEFINED, true, false, array( 'name' => 'asc' ) );
00178                 $classList = array();
00179                 for ( $i = 0; $i < count( $classes ); $i++ )
00180                 {
00181                     $classList[$i]['Name'] = $classes[$i]->attribute( 'name' );
00182                     $classList[$i]['value'] = $classes[$i]->attribute( 'id' );
00183                 }
00184                 return $classList;
00185             }
00186             break;
00187 
00188             case 'workflow_list':
00189             {
00190                 $workflows = eZWorkflow::fetchList();
00191                 $workflowList = array();
00192                 for ( $i = 0; $i < count( $workflows ); $i++ )
00193                 {
00194                     $workflowList[$i]['Name'] = $workflows[$i]->attribute( 'name' );
00195                     $workflowList[$i]['value'] = $workflows[$i]->attribute( 'id' );
00196                 }
00197                 return $workflowList;
00198             }
00199             break;
00200         }
00201         return eZWorkflowEventType::attribute( $attr );
00202     }
00203 
00204     function execute( $process, $event )
00205     {
00206         $processParameters = $process->attribute( 'parameter_list' );
00207         $storeProcessParameters = false;
00208         $classID = false;
00209         $objectID = false;
00210         $sectionID = false;
00211         $languageID = 0;
00212 
00213         if ( isset( $processParameters['object_id'] ) )
00214         {
00215             $objectID = $processParameters['object_id'];
00216             $object = eZContentObject::fetch( $objectID );
00217             if ( $object )
00218             {
00219                 // Examine if the published version contains one of the languages we
00220                 // match for.
00221                 if ( isset( $processParameters['version'] ) )
00222                 {
00223                     $versionID = $processParameters['version'];
00224                     $version = $object->version( $versionID );
00225 
00226                     if ( is_object( $version ) )
00227                     {
00228                         $version_option = $event->attribute( 'version_option' );
00229                         if ( ( $version_option == eZMultiplexerType::VERSION_OPTION_FIRST_ONLY and $processParameters['version'] > 1 ) or
00230                              ( $version_option == eZMultiplexerType::VERSION_OPTION_EXCEPT_FIRST and $processParameters['version'] == 1 ) )
00231                         {
00232                             return eZWorkflowType::STATUS_ACCEPTED;
00233                         }
00234 
00235                         // If the language ID is part of the mask the result is non-zero.
00236                         $languageID = (int)$version->attribute( 'initial_language_id' );
00237                     }
00238                 }
00239                 $sectionID = $object->attribute( 'section_id' );
00240                 $class = $object->attribute( 'content_class' );
00241                 if ( $class )
00242                 {
00243                     $classID = $class->attribute( 'id' );
00244                 }
00245             }
00246         }
00247 
00248         $userArray = explode( ',', $event->attribute( 'data_text2' ) );
00249         $classArray = explode( ',', $event->attribute( 'data_text3' ) );
00250         $languageMask = $event->attribute( 'data_int2' );
00251 
00252         if ( !isset( $processParameters['user_id'] ) )
00253         {
00254             $user = eZUser::currentUser();
00255             $userID = $user->id();
00256             $processParameters['user_id'] = $userID;
00257             $storeProcessParameters = true;
00258         }
00259         else
00260         {
00261             $userID = $processParameters['user_id'];
00262             $user = eZUser::fetch( $userID );
00263             if ( !( $user instanceof eZUser ) )
00264             {
00265                 $user = eZUser::currentUser();
00266                 $userID = $user->id();
00267                 $processParameters['user_id'] = $userID;
00268                 $storeProcessParameters = true;
00269             }
00270         }
00271         $userGroups = $user->attribute( 'groups' );
00272         $inExcludeGroups = count( array_intersect( $userGroups, $userArray ) ) != 0;
00273 
00274         if ( $storeProcessParameters )
00275         {
00276             $process->setParameters( $processParameters );
00277             $process->store();
00278         }
00279 
00280         // All languages match by default
00281         $hasLanguageMatch = true;
00282         if ( $languageMask != 0 )
00283         {
00284             // Match ID with mask.
00285             $hasLanguageMatch = (bool)( $languageMask & $languageID );
00286         }
00287 
00288         if ( $hasLanguageMatch &&
00289              ( !$inExcludeGroups ) &&
00290              ( in_array( -1, $classArray ) ||
00291                in_array( $classID, $classArray ) ) )
00292         {
00293             $sectionArray = explode( ',', $event->attribute( 'data_text1' ) );
00294 
00295             if ( in_array( $sectionID, $sectionArray ) ||
00296                  in_array( -1, $sectionArray ) )
00297             {
00298                 $workflowToRun = $event->attribute( 'data_int1' );
00299 
00300                 $childParameters = array_merge( $processParameters,
00301                                                 array( 'workflow_id' => $workflowToRun,
00302                                                        'user_id' => $userID,
00303                                                        'parent_process_id' => $process->attribute( 'id' )
00304                                                        ) );
00305 
00306                 $childProcessKey = eZWorkflowProcess::createKey( $childParameters );
00307 
00308                 $childProcessArray = eZWorkflowProcess::fetchListByKey( $childProcessKey );
00309                 $childProcess =& $childProcessArray[0];
00310                 if ( $childProcess == null )
00311                 {
00312                     $childProcess = eZWorkflowProcess::create( $childProcessKey, $childParameters );
00313                     $childProcess->store();
00314                 }
00315 
00316                 $workflow = eZWorkflow::fetch( $childProcess->attribute( "workflow_id" ) );
00317                 $workflowEvent = null;
00318 
00319                 if ( $childProcess->attribute( "event_id" ) != 0 )
00320                     $workflowEvent = eZWorkflowEvent::fetch( $childProcess->attribute( "event_id" ) );
00321 
00322                 $childStatus = $childProcess->run( $workflow, $workflowEvent, $eventLog );
00323                 $childProcess->store();
00324 
00325                 if ( $childStatus ==  eZWorkflow::STATUS_DEFERRED_TO_CRON )
00326                 {
00327                     $this->setActivationDate( $childProcess->attribute( 'activation_date' ) );
00328                     $childProcess->setAttribute( "status", eZWorkflow::STATUS_WAITING_PARENT );
00329                     $childProcess->store();
00330                     return eZWorkflowType::STATUS_DEFERRED_TO_CRON_REPEAT;
00331                 }
00332                 else if ( $childStatus == eZWorkflow::STATUS_FETCH_TEMPLATE or $childStatus == eZWorkflow::STATUS_FETCH_TEMPLATE_REPEAT )
00333                 {
00334                     $process->Template =& $childProcess->Template;
00335                     return eZWorkflowType::STATUS_FETCH_TEMPLATE_REPEAT;
00336                 }
00337                 else if ( $childStatus ==  eZWorkflow::STATUS_REDIRECT )
00338                 {
00339                     $process->RedirectUrl =& $childProcess->RedirectUrl;
00340                     return eZWorkflowType::STATUS_REDIRECT_REPEAT;
00341                 }
00342                 else if ( $childStatus ==  eZWorkflow::STATUS_DONE  )
00343                 {
00344                     $childProcess->removeThis();
00345                     return eZWorkflowType::STATUS_ACCEPTED;
00346                 }
00347                 else if ( $childStatus == eZWorkflow::STATUS_CANCELLED || $childStatus == eZWorkflow::STATUS_FAILED )
00348                 {
00349                     $childProcess->removeThis();
00350                     return eZWorkflowType::STATUS_REJECTED;
00351                 }
00352                 return $childProcess->attribute( 'event_status' );
00353             }
00354         }
00355         return eZWorkflowType::STATUS_ACCEPTED;
00356     }
00357 
00358     function initializeEvent( $event )
00359     {
00360     }
00361 
00362 
00363     function fetchHTTPInput( $http, $base, $event )
00364     {
00365         $sectionsVar = $base . "_event_ezmultiplexer_section_ids_" . $event->attribute( "id" );
00366         if ( $http->hasPostVariable( $sectionsVar ) )
00367         {
00368             $sectionsArray = $http->postVariable( $sectionsVar );
00369             if ( in_array( '-1', $sectionsArray ) )
00370             {
00371                 $sectionsArray = array( -1 );
00372             }
00373             $sectionsString = implode( ',', $sectionsArray );
00374             $event->setAttribute( "data_text1", $sectionsString );
00375         }
00376 
00377         $languageVar = $base . "_event_ezmultiplexer_languages_" . $event->attribute( "id" );
00378         if ( $http->hasPostVariable( $languageVar ) )
00379         {
00380             $languageArray = $http->postVariable( $languageVar );
00381             if ( in_array( '-1', $languageArray ) )
00382             {
00383                 $languageArray = array();
00384             }
00385             $languageMask = 0;
00386             foreach ( $languageArray as $languageID )
00387             {
00388                 $languageMask |= $languageID;
00389             }
00390             $event->setAttribute( "data_int2", $languageMask );
00391         }
00392 
00393         $usersVar = $base . "_event_ezmultiplexer_not_run_ids_" . $event->attribute( "id" );
00394         if ( $http->hasPostVariable( $usersVar ) )
00395         {
00396             $usersArray = $http->postVariable( $usersVar );
00397             if ( in_array( '-1', $usersArray ) )
00398             {
00399                 $usersArray = array( -1 );
00400             }
00401             $usersString = implode( ',', $usersArray );
00402             $event->setAttribute( "data_text2", $usersString );
00403         }
00404 
00405         $classesVar = $base . "_event_ezmultiplexer_class_ids_" . $event->attribute( "id" );
00406         if ( $http->hasPostVariable( $classesVar ) )
00407         {
00408             $classesArray = $http->postVariable( $classesVar );
00409             if ( in_array( '-1', $classesArray ) )
00410             {
00411                 $classesArray = array( -1 );
00412             }
00413             $classesString = implode( ',', $classesArray );
00414             $event->setAttribute( "data_text3", $classesString );
00415         }
00416 
00417         $workflowVar = $base . "_event_ezmultiplexer_workflow_id_" . $event->attribute( "id" );
00418         if ( $http->hasPostVariable( $workflowVar ) )
00419         {
00420             $workflowID = $http->postVariable( $workflowVar );
00421             $event->setAttribute( "data_int1", $workflowID );
00422         }
00423 
00424         $versionOptionVar = $base . "_event_ezmultiplexer_version_option_" . $event->attribute( "id" );
00425         if ( $http->hasPostVariable( $versionOptionVar ) )
00426         {
00427             $versionOptionArray = $http->postVariable( $versionOptionVar );
00428             $versionOption = 0;
00429             if ( is_array( $versionOptionArray ) )
00430             {
00431                 foreach ( $versionOptionArray as $vv )
00432                 {
00433                     $versionOption = $versionOption | $vv;
00434                 }
00435             }
00436             $versionOption = $versionOption & eZMultiplexerType::VERSION_OPTION_ALL;
00437             $event->setAttribute( 'data_int3', $versionOption );
00438         }
00439     }
00440 }
00441 
00442 eZWorkflowEventType::registerEventType( eZMultiplexerType::WORKFLOW_TYPE_STRING, 'eZMultiplexerType' );
00443 
00444 ?>