- Fixed #16160: Call to undefined function ezi18n()
[tinyz:tinyz.git] / kernel / classes / workflowtypes / event / ezapprove / ezapprovetype.php
1 <?php
2 //
3 // Definition of eZApproveType class
4 //
5 // Created on: <16-Apr-2002 11:08:14 amos>
6 //
7 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
8 // SOFTWARE NAME: eZ Publish
9 // SOFTWARE RELEASE: 4.1.x
10 // COPYRIGHT NOTICE: Copyright (C) 1999-2010 eZ Systems AS
11 // SOFTWARE LICENSE: GNU General Public License v2.0
12 // NOTICE: >
13 //   This program is free software; you can redistribute it and/or
14 //   modify it under the terms of version 2.0  of the GNU General
15 //   Public License as published by the Free Software Foundation.
16 //
17 //   This program is distributed in the hope that it will be useful,
18 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 //   GNU General Public License for more details.
21 //
22 //   You should have received a copy of version 2.0 of the GNU General
23 //   Public License along with this program; if not, write to the Free
24 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 //   MA 02110-1301, USA.
26 //
27 //
28 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
29 //
30
31 /*!
32   \class eZApproveType ezapprovetype.php
33   \brief Event type for user approvals
34
35   WorkflowEvent storage fields : data_text1 - selected_sections
36                                  data_text2 - selected_usergroups
37                                  data_text3 - approve_users
38                                  data_text4 - approve_groups
39                                  data_int2  - language_list
40                                  data_int3  - content object version option
41 */
42
43 class eZApproveType extends eZWorkflowEventType
44 {
45     const WORKFLOW_TYPE_STRING = "ezapprove";
46
47     const COLLABORATION_NOT_CREATED = 0;
48     const COLLABORATION_CREATED = 1;
49
50     const VERSION_OPTION_FIRST_ONLY = 1;
51     const VERSION_OPTION_EXCEPT_FIRST = 2;
52     const VERSION_OPTION_ALL = 3;
53
54     function eZApproveType()
55     {
56         $this->eZWorkflowEventType( eZApproveType::WORKFLOW_TYPE_STRING, ezpI18n::translate( 'kernel/workflow/event', "Approve" ) );
57         $this->setTriggerTypes( array( 'content' => array( 'publish' => array( 'before' ) ) ) );
58     }
59
60     function attributeDecoder( $event, $attr )
61     {
62         switch ( $attr )
63         {
64             case 'selected_sections':
65             {
66                 $attributeValue = trim( $event->attribute( 'data_text1' ) );
67                 $returnValue = empty( $attributeValue ) ? array( -1 ) : explode( ',', $attributeValue );
68             }break;
69
70             case 'approve_users':
71             {
72                 $attributeValue = trim( $event->attribute( 'data_text3' ) );
73                 $returnValue = empty( $attributeValue ) ? array() : explode( ',', $attributeValue );
74             }break;
75
76             case 'approve_groups':
77             {
78                 $attributeValue = trim( $event->attribute( 'data_text4' ) );
79                 $returnValue = empty( $attributeValue ) ? array() : explode( ',', $attributeValue );
80             }break;
81
82             case 'selected_usergroups':
83             {
84                 $attributeValue = trim( $event->attribute( 'data_text2' ) );
85                 $returnValue = empty( $attributeValue ) ? array() : explode( ',', $attributeValue );
86             }break;
87
88             case 'language_list':
89             {
90                 $returnValue = array();
91                 $attributeValue = $event->attribute( 'data_int2' );
92                 if ( $attributeValue != 0 )
93                 {
94                     $languages = eZContentLanguage::languagesByMask( $attributeValue );
95                     foreach ( $languages as $language )
96                     {
97                         $returnValue[$language->attribute( 'id' )] = $language->attribute( 'name' );
98                     }
99                 }
100             }break;
101
102             case 'version_option':
103             {
104                 $returnValue = eZApproveType::VERSION_OPTION_ALL & $event->attribute( 'data_int3' );
105             }break;
106
107             default:
108                 $returnValue = null;
109         }
110         return $returnValue;
111     }
112
113     function typeFunctionalAttributes( )
114     {
115         return array( 'selected_sections',
116                       'approve_users',
117                       'approve_groups',
118                       'selected_usergroups',
119                       'language_list',
120                       'version_option' );
121     }
122
123     function attributes()
124     {
125         return array_merge( array( 'sections',
126                                    'languages',
127                                    'users',
128                                    'usergroups' ),
129                             eZWorkflowEventType::attributes() );
130
131     }
132
133     function hasAttribute( $attr )
134     {
135         return in_array( $attr, $this->attributes() );
136     }
137
138     function attribute( $attr )
139     {
140         switch( $attr )
141         {
142             case 'sections':
143             {
144                 $sections = eZSection::fetchList( false );
145                 foreach ( $sections as $key => $section )
146                 {
147                     $sections[$key]['Name'] = $section['name'];
148                     $sections[$key]['value'] = $section['id'];
149                 }
150                 return $sections;
151             }break;
152             case 'languages':
153             {
154                 return eZContentLanguage::fetchList();
155             }break;
156         }
157         return eZWorkflowEventType::attribute( $attr );
158     }
159
160     function execute( $process, $event )
161     {
162         eZDebugSetting::writeDebug( 'kernel-workflow-approve', $process, 'eZApproveType::execute' );
163         eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, 'eZApproveType::execute' );
164         $parameters = $process->attribute( 'parameter_list' );
165         $versionID = $parameters['version'];
166         $objectID = $parameters['object_id'];
167         $object = eZContentObject::fetch( $objectID );
168
169         if ( !$object )
170         {
171             eZDebugSetting::writeError( 'kernel-workflow-approve', "No object with ID $objectID", 'eZApproveType::execute' );
172             return eZWorkflowType::STATUS_WORKFLOW_CANCELLED;
173         }
174
175         $version = $object->version( $versionID );
176
177         if ( !$version )
178         {
179             eZDebugSetting::writeError( 'kernel-workflow-approve', "No version $versionID for object with ID $objectID", 'eZApproveType::execute' );
180             return eZWorkflowType::STATUS_WORKFLOW_CANCELLED;
181         }
182
183         // version option checking
184         $version_option = $event->attribute( 'version_option' );
185         if ( ( $version_option == eZApproveType::VERSION_OPTION_FIRST_ONLY and $parameters['version'] > 1 ) or
186              ( $version_option == eZApproveType::VERSION_OPTION_EXCEPT_FIRST and $parameters['version'] == 1 ) )
187         {
188             return eZWorkflowType::STATUS_ACCEPTED;
189         }
190
191         /*
192           If we run event first time ( when we click publish in admin ) we do not have user_id set in workflow process,
193           so we take current user and store it in workflow process, so next time when we run event from cronjob we fetch
194           user_id from there.
195          */
196         if ( $process->attribute( 'user_id' ) == 0 )
197         {
198             $user = eZUser::currentUser();
199             $process->setAttribute( 'user_id', $user->id() );
200         }
201         else
202         {
203             $user = eZUser::instance( $process->attribute( 'user_id' ) );
204         }
205
206         $userGroups = array_merge( $user->attribute( 'groups' ), array( $user->attribute( 'contentobject_id' ) ) );
207         $workflowSections = explode( ',', $event->attribute( 'data_text1' ) );
208         $workflowGroups =   $event->attribute( 'data_text2' ) == '' ? array() : explode( ',', $event->attribute( 'data_text2' ) );
209         $editors =          $event->attribute( 'data_text3' ) == '' ? array() : explode( ',', $event->attribute( 'data_text3' ) );
210         $approveGroups =    $event->attribute( 'data_text4' ) == '' ? array() : explode( ',', $event->attribute( 'data_text4' ) );
211         $languageMask = $event->attribute( 'data_int2' );
212
213         eZDebugSetting::writeDebug( 'kernel-workflow-approve', $user, 'eZApproveType::execute::user' );
214         eZDebugSetting::writeDebug( 'kernel-workflow-approve', $userGroups, 'eZApproveType::execute::userGroups' );
215         eZDebugSetting::writeDebug( 'kernel-workflow-approve', $editors, 'eZApproveType::execute::editor' );
216         eZDebugSetting::writeDebug( 'kernel-workflow-approve', $approveGroups, 'eZApproveType::execute::approveGroups' );
217         eZDebugSetting::writeDebug( 'kernel-workflow-approve', $workflowSections, 'eZApproveType::execute::workflowSections' );
218         eZDebugSetting::writeDebug( 'kernel-workflow-approve', $workflowGroups, 'eZApproveType::execute::workflowGroups' );
219         eZDebugSetting::writeDebug( 'kernel-workflow-approve', $languageMask, 'eZApproveType::execute::languageMask' );
220         eZDebugSetting::writeDebug( 'kernel-workflow-approve', $object->attribute( 'section_id'), 'eZApproveType::execute::section_id' );
221
222         $section = $object->attribute( 'section_id' );
223         $correctSection = false;
224
225         if ( !in_array( $section, $workflowSections ) && !in_array( -1, $workflowSections ) )
226         {
227             $assignedNodes = $object->attribute( 'assigned_nodes' );
228             if ( $assignedNodes )
229             {
230                 foreach( $assignedNodes as $assignedNode )
231                 {
232                     $parent = $assignedNode->attribute( 'parent' );
233                     $parentObject = $parent->object();
234                     $section = $parentObject->attribute( 'section_id');
235
236                     if ( in_array( $section, $workflowSections ) )
237                     {
238                         $correctSection = true;
239                         break;
240                     }
241                 }
242             }
243         }
244         else
245             $correctSection = true;
246
247         $inExcludeGroups = count( array_intersect( $userGroups, $workflowGroups ) ) != 0;
248
249         $userIsEditor = ( in_array( $user->id(), $editors ) ||
250                           count( array_intersect( $userGroups, $approveGroups ) ) != 0 );
251
252         // All languages match by default
253         $hasLanguageMatch = true;
254         if ( $languageMask != 0 )
255         {
256             // Examine if the published version contains one of the languages we
257             // match for.
258             // If the language ID is part of the mask the result is non-zero.
259             $languageID = (int)$version->attribute( 'initial_language_id' );
260             $hasLanguageMatch = (bool)( $languageMask & $languageID );
261         }
262
263         if ( $hasLanguageMatch and
264              !$userIsEditor and
265              !$inExcludeGroups and
266              $correctSection )
267         {
268
269             /* Get user IDs from approve user groups */
270             $userClassIDArray = eZUser::contentClassIDs();
271             $approveUserIDArray = array();
272             foreach ( $approveGroups as $approveUserGroupID )
273             {
274                 if (  $approveUserGroupID != false )
275                 {
276                     $approveUserGroup = eZContentObject::fetch( $approveUserGroupID );
277                     if ( isset( $approveUserGroup ) )
278                     {
279                         foreach ( $approveUserGroup->attribute( 'assigned_nodes' ) as $assignedNode )
280                         {
281                             $userNodeArray = $assignedNode->subTree( array( 'ClassFilterType' => 'include',
282                                                                             'ClassFilterArray' => $userClassIDArray,
283                                                                             'Limitation' => array() ) );
284                             foreach ( $userNodeArray as $userNode )
285                             {
286                                 $approveUserIDArray[] = $userNode->attribute( 'contentobject_id' );
287                             }
288                         }
289                     }
290                 }
291             }
292             $approveUserIDArray = array_merge( $approveUserIDArray, $editors );
293             $approveUserIDArray = array_unique( $approveUserIDArray );
294
295             $collaborationID = false;
296             $db = eZDb::instance();
297             $taskResult = $db->arrayQuery( 'select workflow_process_id, collaboration_id from ezapprove_items where workflow_process_id = ' . $process->attribute( 'id' )  );
298             if ( count( $taskResult ) > 0 )
299                 $collaborationID = $taskResult[0]['collaboration_id'];
300
301             eZDebugSetting::writeDebug( 'kernel-workflow-approve', $collaborationID, 'approve collaborationID' );
302             eZDebugSetting::writeDebug( 'kernel-workflow-approve', $process->attribute( 'event_state'), 'approve $process->attribute( \'event_state\')' );
303             if ( $collaborationID === false )
304             {
305                 $this->createApproveCollaboration( $process, $event, $user->id(), $object->attribute( 'id' ), $versionID, $approveUserIDArray );
306                 $this->setInformation( "We are going to create approval" );
307                 $process->setAttribute( 'event_state', eZApproveType::COLLABORATION_CREATED );
308                 $process->store();
309                 eZDebugSetting::writeDebug( 'kernel-workflow-approve', $this, 'approve execute' );
310                 return eZWorkflowType::STATUS_DEFERRED_TO_CRON_REPEAT;
311             }
312             else if ( $process->attribute( 'event_state') == eZApproveType::COLLABORATION_NOT_CREATED )
313             {
314                 eZApproveCollaborationHandler::activateApproval( $collaborationID );
315                 $process->setAttribute( 'event_state', eZApproveType::COLLABORATION_CREATED );
316                 $process->store();
317                 eZDebugSetting::writeDebug( 'kernel-workflow-approve', $this, 'approve re-execute' );
318                 return eZWorkflowType::STATUS_DEFERRED_TO_CRON_REPEAT;
319             }
320             else //eZApproveType::COLLABORATION_CREATED
321             {
322                 $this->setInformation( "we are checking approval now" );
323                 eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, 'check approval' );
324                 return $this->checkApproveCollaboration(  $process, $event );
325             }
326         }
327         else
328         {
329             eZDebugSetting::writeDebug( 'kernel-workflow-approve', $workflowSections , "we are not going to create approval " . $object->attribute( 'section_id') );
330             eZDebugSetting::writeDebug( 'kernel-workflow-approve', $userGroups, "we are not going to create approval" );
331             eZDebugSetting::writeDebug( 'kernel-workflow-approve', $workflowGroups,  "we are not going to create approval" );
332             eZDebugSetting::writeDebug( 'kernel-workflow-approve', $user->id(), "we are not going to create approval "  );
333             return eZWorkflowType::STATUS_ACCEPTED;
334         }
335     }
336
337     function initializeEvent( $event )
338     {
339     }
340
341     function validateUserIDList( $userIDList, &$reason )
342     {
343         $returnState = eZInputValidator::STATE_ACCEPTED;
344         foreach ( $userIDList as $userID )
345         {
346             if ( !is_numeric( $userID ) or
347                  !eZUser::isUserObject( eZContentObject::fetch( $userID ) ) )
348             {
349                 $returnState = eZInputValidator::STATE_INVALID;
350                 $reason[ 'list' ][] = $userID;
351             }
352         }
353         $reason[ 'text' ] = "Some of passed user IDs are not valid, must be IDs of existing users only.";
354         return $returnState;
355     }
356
357     function validateGroupIDList( $userGroupIDList, &$reason )
358     {
359         $returnState = eZInputValidator::STATE_ACCEPTED;
360         $groupClassNames = eZUser::fetchUserGroupClassNames();
361         if ( count( $groupClassNames ) > 0 )
362         {
363             foreach( $userGroupIDList as $userGroupID )
364             {
365                 if ( !is_numeric( $userGroupID ) or
366                      !is_object( $userGroup = eZContentObject::fetch( $userGroupID ) ) or
367                      !in_array( $userGroup->attribute( 'class_identifier' ), $groupClassNames ) )
368                 {
369                     $returnState = eZInputValidator::STATE_INVALID;
370                     $reason[ 'list' ][] = $userGroupID;
371                 }
372             }
373             $reason[ 'text' ] = "Some of passed user-group IDs are not valid, must be IDs of existing user groups only.";
374         }
375         else
376         {
377             $returnState = eZInputValidator::STATE_INVALID;
378             $reason[ 'text' ] = "There is no one user-group classes among the user accounts, please choose standalone users.";
379         }
380         return $returnState;
381     }
382
383     function validateHTTPInput( $http, $base, $workflowEvent, &$validation )
384     {
385         $returnState = eZInputValidator::STATE_ACCEPTED;
386         $reason = array();
387
388         if ( !$http->hasSessionVariable( 'BrowseParameters' ) )
389         {
390             // check approve-users
391             $approversIDs = array_unique( $this->attributeDecoder( $workflowEvent, 'approve_users' ) );
392             if ( is_array( $approversIDs ) and
393                  count( $approversIDs ) > 0 )
394             {
395                 $returnState = eZApproveType::validateUserIDList( $approversIDs, $reason );
396             }
397             else
398                 $returnState = false;
399
400             if ( $returnState != eZInputValidator::STATE_INVALID )
401             {
402                 // check approve-groups
403                 $userGroupIDList = array_unique( $this->attributeDecoder( $workflowEvent, 'approve_groups' ) );
404                 if ( is_array( $userGroupIDList ) and
405                      count( $userGroupIDList ) > 0 )
406                 {
407                     $returnState = eZApproveType::validateGroupIDList( $userGroupIDList, $reason );
408                 }
409                 else if ( $returnState === false )
410                 {
411                     // if no one user or user-group was passed as approvers
412                     $returnState = eZInputValidator::STATE_INVALID;
413                     $reason[ 'text' ] = "There must be passed at least one valid user or user group who approves content for the event.";
414                 }
415
416                 // check excluded-users
417                 /*
418                 if ( $returnState != eZInputValidator::STATE_INVALID )
419                 {
420                     // TODO:
421                     // ....
422                 }
423                 */
424
425                 // check excluded-groups
426                 if ( $returnState != eZInputValidator::STATE_INVALID )
427                 {
428                     $userGroupIDList = array_unique( $this->attributeDecoder( $workflowEvent, 'selected_usergroups' ) );
429                     if ( is_array( $userGroupIDList ) and
430                          count( $userGroupIDList ) > 0 )
431                     {
432                         $returnState = eZApproveType::validateGroupIDList( $userGroupIDList, $reason );
433                     }
434                 }
435             }
436         }
437         else
438         {
439             $browseParameters = $http->sessionVariable( 'BrowseParameters' );
440             if ( isset( $browseParameters['custom_action_data'] ) )
441             {
442                 $customData = $browseParameters['custom_action_data'];
443                 if ( isset( $customData['event_id'] ) and
444                      $customData['event_id'] == $workflowEvent->attribute( 'id' ) )
445                 {
446                     if ( !$http->hasPostVariable( 'BrowseCancelButton' ) and
447                          $http->hasPostVariable( 'SelectedObjectIDArray' ) )
448                     {
449                         $objectIDArray = $http->postVariable( 'SelectedObjectIDArray' );
450                         if ( is_array( $objectIDArray ) and
451                              count( $objectIDArray ) > 0 )
452                         {
453                             switch( $customData['browse_action'] )
454                             {
455                             case "AddApproveUsers":
456                                 {
457                                     $returnState = eZApproveType::validateUserIDList( $objectIDArray, $reason );
458                                 } break;
459                             case 'AddApproveGroups':
460                             case 'AddExcludeUser':
461                                 {
462                                     $returnState = eZApproveType::validateGroupIDList( $objectIDArray, $reason );
463                                 } break;
464                             case 'AddExcludedGroups':
465                                 {
466                                     // TODO:
467                                     // .....
468                                 } break;
469                             }
470                         }
471                     }
472                 }
473             }
474         }
475
476         if ( $returnState == eZInputValidator::STATE_INVALID )
477         {
478             $validation[ 'processed' ] = true;
479             $validation[ 'events' ][] = array( 'id' => $workflowEvent->attribute( 'id' ),
480                                                'placement' => $workflowEvent->attribute( 'placement' ),
481                                                'workflow_type' => &$this,
482                                                'reason' => $reason );
483         }
484         return $returnState;
485     }
486
487
488     function fetchHTTPInput( $http, $base, $event )
489     {
490         $sectionsVar = $base . "_event_ezapprove_section_" . $event->attribute( "id" );
491         if ( $http->hasPostVariable( $sectionsVar ) )
492         {
493             $sectionsArray = $http->postVariable( $sectionsVar );
494             if ( in_array( '-1', $sectionsArray ) )
495             {
496                 $sectionsArray = array( -1 );
497             }
498             $sectionsString = implode( ',', $sectionsArray );
499             $event->setAttribute( "data_text1", $sectionsString );
500         }
501
502         $languageVar = $base . "_event_ezapprove_languages_" . $event->attribute( "id" );
503         if ( $http->hasPostVariable( $languageVar ) )
504         {
505             $languageArray = $http->postVariable( $languageVar );
506             if ( in_array( '-1', $languageArray ) )
507             {
508                 $languageArray = array();
509             }
510             $languageMask = 0;
511             foreach ( $languageArray as $languageID )
512             {
513                 $languageMask |= $languageID;
514             }
515             $event->setAttribute( "data_int2", $languageMask );
516         }
517
518         $versionOptionVar = $base . "_event_ezapprove_version_option_" . $event->attribute( "id" );
519         if ( $http->hasPostVariable( $versionOptionVar ) )
520         {
521             $versionOptionArray = $http->postVariable( $versionOptionVar );
522             $versionOption = 0;
523             if ( is_array( $versionOptionArray ) )
524             {
525                 foreach ( $versionOptionArray as $vv )
526                 {
527                     $versionOption = $versionOption | $vv;
528                 }
529             }
530             $versionOption = $versionOption & eZApproveType::VERSION_OPTION_ALL;
531             $event->setAttribute( 'data_int3', $versionOption );
532         }
533
534         if ( $http->hasSessionVariable( 'BrowseParameters' ) )
535         {
536             $browseParameters = $http->sessionVariable( 'BrowseParameters' );
537             if ( isset( $browseParameters['custom_action_data'] ) )
538             {
539                 $customData = $browseParameters['custom_action_data'];
540                 if ( isset( $customData['event_id'] ) &&
541                      $customData['event_id'] == $event->attribute( 'id' ) )
542                 {
543                     if ( !$http->hasPostVariable( 'BrowseCancelButton' ) and
544                          $http->hasPostVariable( 'SelectedObjectIDArray' ) )
545                     {
546                         $objectIDArray = $http->postVariable( 'SelectedObjectIDArray' );
547                         if ( is_array( $objectIDArray ) and
548                              count( $objectIDArray ) > 0 )
549                         {
550
551                             switch( $customData['browse_action'] )
552                             {
553                             case 'AddApproveUsers':
554                                 {
555                                     foreach( $objectIDArray as $key => $userID )
556                                     {
557                                         if ( !eZUser::isUserObject( eZContentObject::fetch( $userID ) ) )
558                                         {
559                                             unset( $objectIDArray[$key] );
560                                         }
561                                     }
562                                     $event->setAttribute( 'data_text3', implode( ',',
563                                                                                  array_unique( array_merge( $this->attributeDecoder( $event, 'approve_users' ),
564                                                                                                             $objectIDArray ) ) ) );
565                                 } break;
566
567                             case 'AddApproveGroups':
568                                 {
569                                     $event->setAttribute( 'data_text4', implode( ',',
570                                                                                  array_unique( array_merge( $this->attributeDecoder( $event, 'approve_groups' ),
571                                                                                                             $objectIDArray ) ) ) );
572                                 } break;
573
574                             case 'AddExcludeUser':
575                                 {
576                                     $event->setAttribute( 'data_text2', implode( ',',
577                                                                                  array_unique( array_merge( $this->attributeDecoder( $event, 'selected_usergroups' ),
578                                                                                                             $objectIDArray ) ) ) );
579                                 } break;
580
581                             case 'AddExcludedGroups':
582                                 {
583                                     // TODO:
584                                     // .....
585                                 } break;
586                             }
587                         }
588                         $http->removeSessionVariable( 'BrowseParameters' );
589                     }
590                 }
591             }
592         }
593     }
594
595     function createApproveCollaboration( $process, $event, $userID, $contentobjectID, $contentobjectVersion, $editors )
596     {
597         if ( $editors === null )
598             return false;
599         $authorID = $userID;
600         $collaborationItem = eZApproveCollaborationHandler::createApproval( $contentobjectID, $contentobjectVersion,
601                                                                             $authorID, $editors );
602         $db = eZDb::instance();
603         $db->query( 'INSERT INTO ezapprove_items( workflow_process_id, collaboration_id )
604                        VALUES(' . $process->attribute( 'id' ) . ',' . $collaborationItem->attribute( 'id' ) . ' ) ' );
605     }
606
607     /*
608     */
609     function customWorkflowEventHTTPAction( $http, $action, $workflowEvent )
610     {
611         $eventID = $workflowEvent->attribute( "id" );
612         $module =& $GLOBALS['eZRequestedModule'];
613         //$siteIni = eZINI::instance();
614         switch ( $action )
615         {
616             case 'AddApproveUsers' :
617             {
618                 $userClassNames = eZUser::fetchUserClassNames();
619                 if ( count( $userClassNames ) > 0 )
620                 {
621                     eZContentBrowse::browse( array( 'action_name' => 'SelectMultipleUsers',
622                                                     'from_page' => '/workflow/edit/' . $workflowEvent->attribute( 'workflow_id' ),
623                                                     'custom_action_data' => array( 'event_id' => $eventID,
624                                                                                    'browse_action' => $action ),
625                                                     'class_array' => $userClassNames ),
626                                              $module );
627                 }
628             } break;
629
630             case 'RemoveApproveUsers' :
631             {
632                 if ( $http->hasPostVariable( 'DeleteApproveUserIDArray_' . $eventID ) )
633                 {
634                     $workflowEvent->setAttribute( 'data_text3', implode( ',', array_diff( $this->attributeDecoder( $workflowEvent, 'approve_users' ),
635                                                                                           $http->postVariable( 'DeleteApproveUserIDArray_' . $eventID ) ) ) );
636                 }
637             } break;
638
639             case 'AddApproveGroups' :
640             case 'AddExcludeUser' :
641             {
642                 $groupClassNames = eZUser::fetchUserGroupClassNames();
643                 if ( count( $groupClassNames ) > 0 )
644                 {
645                     eZContentBrowse::browse( array( 'action_name' => 'SelectMultipleUsers',
646                                                     'from_page' => '/workflow/edit/' . $workflowEvent->attribute( 'workflow_id' ),
647                                                     'custom_action_data' => array( 'event_id' => $eventID,
648                                                                                    'browse_action' => $action ),
649                                                     'class_array' => $groupClassNames ),
650                                              $module );
651                 }
652             } break;
653
654             case 'RemoveApproveGroups' :
655             {
656                 if ( $http->hasPostVariable( 'DeleteApproveGroupIDArray_' . $eventID ) )
657                 {
658                     $workflowEvent->setAttribute( 'data_text4', implode( ',', array_diff( $this->attributeDecoder( $workflowEvent, 'approve_groups' ),
659                                                                                           $http->postVariable( 'DeleteApproveGroupIDArray_' . $eventID ) ) ) );
660                 }
661             } break;
662
663             case 'RemoveExcludeUser' :
664             {
665                 if ( $http->hasPostVariable( 'DeleteExcludeUserIDArray_' . $eventID ) )
666                 {
667                     $workflowEvent->setAttribute( 'data_text2', implode( ',', array_diff( $this->attributeDecoder( $workflowEvent, 'selected_usergroups' ),
668                                                                                           $http->postVariable( 'DeleteExcludeUserIDArray_' . $eventID ) ) ) );
669                 }
670             } break;
671
672             case 'AddExcludedGroups' :
673             {
674                 // TODO:
675                 // .....
676             } break;
677
678             case 'RemoveExcludedGroups' :
679             {
680                 // TODO:
681                 // .....
682             } break;
683         }
684     }
685
686     /*
687     */
688     function cleanupAfterRemoving( $attr = array() )
689     {
690         foreach ( array_keys( $attr ) as $attrKey )
691         {
692           switch ( $attrKey )
693           {
694               case 'DeleteContentObject':
695               {
696                      $contentObjectID = (int)$attr[ $attrKey ];
697                      $db = eZDb::instance();
698                      // Cleanup "User who approves content"
699                      $db->query( "UPDATE ezworkflow_event
700                                   SET    data_int1 = '0'
701                                   WHERE  workflow_type_string = '{$this->TypeString}' AND
702                                          data_int1 = $contentObjectID" );
703                      // Cleanup "Excluded user groups"
704                      $excludedGroupsID = $db->arrayQuery( "SELECT data_text2, id
705                                                            FROM   ezworkflow_event
706                                                            WHERE  workflow_type_string = '{$this->TypeString}' AND
707                                                                   data_text2 like '%$contentObjectID%'" );
708
709                      if ( is_array( $excludedGroupsID ) )
710                      {
711                          foreach ( $excludedGroupsID as $groupID )
712                          {
713                              // $IDArray will contain IDs of "Excluded user groups"
714                              $IDArray = explode( ',', $groupID[ 'data_text2' ] );
715                              // $newIDArray will contain  array without $contentObjectID
716                              $newIDArray = array_filter( $IDArray, create_function( '$v', 'return ( $v != ' . $contentObjectID .' );' ) );
717                              $newValues = $db->escapeString( implode( ',', $newIDArray ) );
718                              $db->query( "UPDATE ezworkflow_event
719                                           SET    data_text2 = '$newValues'
720                                           WHERE  id = {$groupID['id']}" );
721                          }
722                      }
723               } break;
724           }
725         }
726     }
727
728     function checkApproveCollaboration( $process, $event )
729     {
730         $db = eZDb::instance();
731         $taskResult = $db->arrayQuery( 'select workflow_process_id, collaboration_id from ezapprove_items where workflow_process_id = ' . $process->attribute( 'id' )  );
732         $collaborationID = $taskResult[0]['collaboration_id'];
733         $collaborationItem = eZCollaborationItem::fetch( $collaborationID );
734         $contentObjectVersion = eZApproveCollaborationHandler::contentObjectVersion( $collaborationItem );
735         $approvalStatus = eZApproveCollaborationHandler::checkApproval( $collaborationID );
736         if ( $approvalStatus == eZApproveCollaborationHandler::STATUS_WAITING )
737         {
738             eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, 'approval still waiting' );
739             return eZWorkflowType::STATUS_DEFERRED_TO_CRON_REPEAT;
740         }
741         else if ( $approvalStatus == eZApproveCollaborationHandler::STATUS_ACCEPTED )
742         {
743             eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, 'approval was accepted' );
744             $status = eZWorkflowType::STATUS_ACCEPTED;
745         }
746         else if ( $approvalStatus == eZApproveCollaborationHandler::STATUS_DENIED or
747                   $approvalStatus == eZApproveCollaborationHandler::STATUS_DEFERRED )
748         {
749             eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, 'approval was denied' );
750             $contentObjectVersion->setAttribute( 'status', eZContentObjectVersion::STATUS_DRAFT );
751             $status = eZWorkflowType::STATUS_WORKFLOW_CANCELLED;
752         }
753         else
754         {
755             eZDebugSetting::writeDebug( 'kernel-workflow-approve', $event, "approval unknown status '$approvalStatus'" );
756             $contentObjectVersion->setAttribute( 'status', eZContentObjectVersion::STATUS_REJECTED );
757             $status = eZWorkflowType::STATUS_WORKFLOW_CANCELLED;
758         }
759         $contentObjectVersion->sync();
760         if ( $approvalStatus != eZApproveCollaborationHandler::STATUS_DEFERRED )
761             $db->query( 'DELETE FROM ezapprove_items WHERE workflow_process_id = ' . $process->attribute( 'id' )  );
762         return $status;
763     }
764 }
765
766 eZWorkflowEventType::registerEventType( eZApproveType::WORKFLOW_TYPE_STRING, "eZApproveType" );
767
768 ?>