Subversion Repositories dbi_notifications

[/] [eventtypes/] [event/] [dbiaddnotificationrule/] [dbiaddnotificationruletype.php] - Blame information for rev 1

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1 Bedlore
<?php
2
 
3
include_once( 'kernel/classes/ezworkflowtype.php' );
4
 
5
class DBIAddNotificationRuleType extends eZWorkflowEventType
6
{
7
    function DBIAddNotificationRuleType()
8
    {
9
        $this->eZWorkflowEventType( 'dbiaddnotificationrule', ezi18n( 'kernel/workflow/event', 'DBI add notification rule' ) );
10
        $this->setTriggerTypes( array( 'content' => array( 'publish' => array( 'after' ) ) ) );
11
    }
12
 
13
    function &attributeDecoder( &$event, $attr )
14
    {
15
        $retValue = null;
16
        switch( $attr )
17
        {
18
            case 'selected_attributes':
19
            {
20
                $implodedAttributeList = $event->attribute( 'data_text1' );
21
 
22
                $attributeList = array();
23
                if ( $implodedAttributeList != '' )
24
                {
25
                    $attributeList = explode( ';', $implodedAttributeList );
26
                }
27
                return $attributeList;
28
            }
29
 
30
            default:
31
            {
32
                eZDebug::writeNotice( 'unknown attribute:' . $attr, 'DBIAddNotificationRuleType' );
33
            }
34
        }
35
        return $retValue;
36
    }
37
 
38
    function typeFunctionalAttributes()
39
    {
40
        return array( 'selected_attributes' );
41
    }
42
 
43
    function customWorkflowEventHTTPAction( &$http, $action, &$workflowEvent )
44
    {
45
        $eventID = $workflowEvent->attribute( 'id' );
46
 
47
        switch ( $action )
48
        {
49
            case 'AddAttribute':
50
            {
51
                if ( $http->hasPostVariable( 'AttributeSelection_' . $eventID ) )
52
                {
53
                    $attributeID = $http->postVariable( 'AttributeSelection_' . $eventID );
54
                    $workflowEvent->setAttribute( 'data_text1', implode( ';', array_unique( array_merge( $this->attributeDecoder( $workflowEvent, 'selected_attributes' ), array( $attributeID ) ) ) ) );
55
                }
56
            } break;
57
 
58
            case 'RemoveAttributes':
59
            {
60
                if ( $http->hasPostVariable( 'DeleteAttributeIDArray_' . $eventID ) )
61
                {
62
                    $deleteList = $http->postVariable( 'DeleteAttributeIDArray_' . $eventID );
63
                    $currentList = $this->attributeDecoder( $workflowEvent, 'selected_attributes' );
64
 
65
                    if ( is_array( $deleteList ) )
66
                    {
67
                        $dif = array_diff( $currentList, $deleteList );
68
                        $workflowEvent->setAttribute( 'data_text1', implode( ';', $dif ) );
69
                    }
70
                }
71
            } break;
72
 
73
            default:
74
            {
75
                eZDebug::writeNotice( 'unknown custom action: ' . $action, 'DBIAddNotificationRuleType' );
76
            }
77
        }
78
    }
79
 
80
    /*!
81
     \reimp
82
    */
83
    function execute( &$process, &$event )
84
    {
85
        $parameters = $process->attribute( 'parameter_list' );
86
        include_once( 'kernel/classes/ezcontentobject.php' );
87
        $object =& eZContentObject::fetch( $parameters['object_id'] );
88
 
89
        $datamap = $object->attribute( 'data_map' );
90
        $attributeIDList = $event->attribute( 'selected_attributes' );
91
 
92
        $mainNodeID = $object->attribute( 'main_node_id' );
93
 
94
        foreach ( $datamap as $attribute )
95
        {
96
            if ( in_array( $attribute->attribute('contentclassattribute_id'), $attributeIDList ) )
97
            {
98
                eZDebug::writeDebug( 'found matching attribute: ' . $attribute->attribute('contentclassattribute_id'), 'DBIAddNotificationRuleType' );
99
 
100
                $dataTypeString = $attribute->attribute( 'data_type_string' );
101
                if ( $dataTypeString != 'ezselection' )
102
                {
103
                    $attributeName = $attribute->attribute( 'contentclass_attribute_name' );
104
                    eZDebug::writeError( "attribute '$attributeName' has datatype '$dataTypeString', expected: ezselection", 'DBIAddNotificationRuleType::execute' );
105
                    continue;
106
                }
107
 
108
                $selectedOptions = $attribute->attribute( 'content' );
109
                $classContent = $attribute->attribute( 'class_content' );
110
                $possibleOptions = $classContent['options'];
111
                foreach ( $possibleOptions as $possibleOption )
112
                {
113
                    if ( in_array( $possibleOption['id'], $selectedOptions ) )
114
                    {
115
                        DBIAddNotificationRuleType::createNotificationRulesByOptionName( $object->attribute( 'id' ), $possibleOption['name'] );
116
                    }
117
                }
118
            }
119
        }
120
 
121
        return EZ_WORKFLOW_TYPE_STATUS_ACCEPTED;
122
    }
123
 
124
    /*!
125
     \static
126
    */
127
    function createNotificationRulesByOptionName( $userID, $optionName )
128
    {
129
        $ini =& eZINI::instance( 'dbi_notifications.ini' );
130
        $nodeIDList = $ini->hasGroup( $optionName ) && $ini->hasVariable( $optionName, 'SubtreeNotifications' ) ? $ini->variable( $optionName, 'SubtreeNotifications' ) : array();
131
 
132
        if ( is_array( $nodeIDList ) && count( $nodeIDList ) > 0 )
133
        {
134
            foreach ( $nodeIDList as $nodeID )
135
            {
136
                DBIAddNotificationRuleType::createNotificationRuleIfNeeded( $userID, $nodeID );
137
            }
138
        }
139
        else
140
        {
141
            eZDebug::writeWarning( "no notifications set in dbi_notification.ini for option $optionName", 'DBIAddNotificationRuleType::execute' );
142
        }
143
    }
144
 
145
    /*!
146
     \static
147
    */
148
    function createNotificationRuleIfNeeded( $userID, $nodeID )
149
    {
150
        include_once( 'kernel/classes/notification/handler/ezsubtree/ezsubtreenotificationrule.php' );
151
 
152
        $nodeIDList =& eZSubtreeNotificationRule::fetchNodesForUserID( $userID, false );
153
 
154
        if ( !in_array( $nodeID, $nodeIDList ) )
155
        {
156
            $rule =& eZSubtreeNotificationRule::create( $nodeID, $userID );
157
            $rule->store();
158
        }
159
    }
160
}
161
 
162
eZWorkflowEventType::registerType( 'dbiaddnotificationrule', 'dbiaddnotificationruletype' );
163
 
164
?>