Subversion Repositories autorss

[/] [trunk/] [extension/] [autorss/] [eventtypes/] [event/] [autorss/] [autorsstype.php] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1 cyberwolf
<?php
2
//
3
// ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
4 4 Cyberwolf
// SOFTWARE NAME: eZ Publish Auto RSS extension
5 5 Cyberwolf
// SOFTWARE RELEASE: 2.x
6 4 Cyberwolf
// COPYRIGHT NOTICE: Copyright (C) 2007-2008 Kristof Coomans <http://blog.kristofcoomans.be>
7 1 cyberwolf
// SOFTWARE LICENSE: GNU General Public License v2.0
8
// NOTICE: >
9
//   This program is free software; you can redistribute it and/or
10
//   modify it under the terms of version 2.0  of the GNU General
11
//   Public License as published by the Free Software Foundation.
12
//
13
//   This program is distributed in the hope that it will be useful,
14
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
//   GNU General Public License for more details.
17
//
18
//   You should have received a copy of version 2.0 of the GNU General
19
//   Public License along with this program; if not, write to the Free
20
//   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21
//   MA 02110-1301, USA.
22
//
23
//
24
// ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
25
//
26
 
27
class AutoRSSType extends eZWorkflowEventType
28
{
29
    function AutoRSSType()
30
    {
31 9 nfrp
        $this->eZWorkflowEventType( 'autorss', ezpI18n::tr( 'extension/autorss', 'Auto RSS' ) );
32 1 cyberwolf
        $this->setTriggerTypes( array( 'content' => array( 'publish' => array( 'after' ) ) ) );
33
    }
34
 
35 5 Cyberwolf
    function attributeDecoder( $event, $attr )
36 1 cyberwolf
    {
37
        $retValue = null;
38
        switch( $attr )
39
        {
40
            case 'path_offset':
41
            {
42
                $retValue = $event->attribute( 'data_int1' );
43
            } break;
44
 
45
            case 'defer':
46
            {
47
                $retValue = $event->attribute( 'data_int2' ) == 1 ? true : false;
48
            } break;
49
 
50
            case 'attribute_mappings':
51
            {
52
                $retValue = explode( ';', $event->attribute( 'data_text1' ) );
53
            } break;
54
 
55
            default:
56
            {
57
                eZDebug::writeNotice( 'unknown attribute: ' . $attr, 'AutoRSSType' );
58
            }
59
        }
60
        return $retValue;
61
    }
62
 
63
    function typeFunctionalAttributes()
64
    {
65
        return array( 'path_offset', 'attribute_mappings', 'defer' );
66
    }
67
 
68 5 Cyberwolf
    function fetchHTTPInput( $http, $base, $event )
69 1 cyberwolf
    {
70
        // this condition can be removed when this issue if fixed: http://issues.ez.no/10685
71
        if ( count( $_POST ) > 0 )
72
        {
73
            $offset = false;
74
            $offsetPostVarName = 'PathOffset_' . $event->attribute( 'id' );
75
            if ( $http->hasPostVariable( $offsetPostVarName ) )
76
            {
77
                $offset = $http->postVariable( $offsetPostVarName );
78
            }
79
 
80
            if ( is_numeric( $offset ) )
81
            {
82
                $event->setAttribute( 'data_int1', $offset );
83
            }
84
 
85
            $deferPostVarName = 'Defer_' . $event->attribute( 'id' );
86
            $defer = false;
87
            if ( $http->hasPostVariable( $deferPostVarName ) )
88
            {
89
                $defer = true;
90
            }
91
            $event->setAttribute( 'data_int2', $defer );
92
 
93
            $mappingsPostVarName = 'AttributeMappings_' . $event->attribute( 'id' );
94
            if ( $http->hasPostVariable( $mappingsPostVarName ) )
95
            {
96
                $attributeMappings = $http->postVariable( $mappingsPostVarName );
97
            }
98
            else
99
            {
100
                $attributeMappings = array();
101
            }
102
 
103
            $event->setAttribute( 'data_text1', implode( ';', $attributeMappings ) );
104
        }
105
    }
106
 
107 5 Cyberwolf
    function execute( $process, $event )
108 1 cyberwolf
    {
109
        $parameters = $process->attribute( 'parameter_list' );
110 5 Cyberwolf
        $object = eZContentObject::fetch( $parameters['object_id'] );
111 1 cyberwolf
 
112
        if ( $this->attributeDecoder( $event, 'defer' ) )
113
        {
114
            if ( eZSys::isShellExecution() == false )
115
            {
116 8 Cyberwolf
                return eZWorkflowType::STATUS_DEFERRED_TO_CRON_REPEAT;
117 1 cyberwolf
            }
118
        }
119
 
120 5 Cyberwolf
        $mainNode = $object->attribute( 'main_node' );
121 1 cyberwolf
        $attributeMappings = $this->attributeDecoder( $event, 'attribute_mappings' );
122
        $pathOffset = $this->attributeDecoder( $event, 'path_offset' );
123
        $this->createFeedIfNeeded( $mainNode, $attributeMappings, $pathOffset );
124
 
125 8 Cyberwolf
        return eZWorkflowType::STATUS_ACCEPTED;
126 1 cyberwolf
    }
127
 
128 5 Cyberwolf
    static function createFeedIfNeeded( $node, $attributeMappings, $pathOffset )
129 1 cyberwolf
    {
130
        $name = $node->attribute( 'node_id' );
131
        $rssExport = eZRSSExport::fetchByName( $name );
132
 
133
        if ( is_object( $rssExport ) )
134
        {
135
            return false;
136
        }
137
 
138
        $rssExport = eZRSSExport::create( 14 );
139
        $rssExport->store();
140
 
141
        $rssExportID = $rssExport->attribute( 'id' );
142
 
143 5 Cyberwolf
        $ini = eZINI::instance( 'autorss.ini' );
144 1 cyberwolf
 
145
        foreach ( $attributeMappings as $mappingIdentifier )
146
        {
147
            $mappingGroup = 'Mapping_' . $mappingIdentifier;
148
            if ( !$ini->hasGroup( $mappingGroup ) )
149
            {
150
                eZDebug::writeError( 'No RSS attribute mapping with identifier ' . $mappingIdentifier . ' in autorss.ini', 'AutoRSSType::execute' );
151
                continue;
152
            }
153
 
154
            $classID = $ini->variable( $mappingGroup, 'ClassID' );
155
            $titleIdentifier = $ini->variable( $mappingGroup, 'TitleIdentifier' );
156
            $descriptionIdentifier = $ini->variable( $mappingGroup, 'DescriptionIdentifier' );
157
 
158
            $rssExportItem = eZRSSExportItem::create( $rssExportID );
159
            $rssExportItem->setAttribute( 'subnodes', 1 );
160
            $rssExportItem->setAttribute( 'source_node_id', $node->attribute( 'node_id' ) );
161
            $rssExportItem->setAttribute( 'class_id', $classID );
162
            $rssExportItem->setAttribute( 'title', $titleIdentifier );
163
            $rssExportItem->setAttribute( 'description', $descriptionIdentifier );
164
            $rssExportItem->setAttribute( 'status', 1 );
165
            $rssExportItem->store();
166
 
167
            // delete draft version
168
            $rssExportItem->setAttribute( 'status', 0 );
169
            $rssExportItem->remove();
170
 
171
            unset( $rssExportItem );
172
        }
173
 
174
        $path = $node->fetchPath();
175
 
176
        $titleParts = array();
177
        foreach ( $path as $pathNode )
178
        {
179
            $titleParts[] = $pathNode->attribute( 'name' );
180
        }
181
 
182
        if ( $pathOffset > 0 )
183
        {
184
            $titleParts = array_slice( $titleParts, $pathOffset );
185
        }
186
 
187
        $rssExport->setAttribute( 'title', implode( ' / ', $titleParts ) . ' / ' . $node->attribute( 'name' ) );
188
        $rssExport->setAttribute( 'url', $ini->variable( 'GeneralSettings', 'SiteURL' ) );
189
        $rssExport->setAttribute( 'description', '' );
190
        $rssExport->setAttribute( 'rss_version', '2.0' );
191
        $rssExport->setAttribute( 'number_of_objects', 50 );
192
        $rssExport->setAttribute( 'active', 1 );
193
 
194
        $rssExport->setAttribute( 'access_url', $name );
195
        $rssExport->setAttribute( 'main_node_only', 1 );
196
 
197
        // argument true will store it with status valid instead of draft
198
        $rssExport->store( true );
199
        // remove draft
200
        $rssExport->remove();
201 2 Cyberwolf
 
202
        return true;
203 1 cyberwolf
    }
204
}
205
 
206 5 Cyberwolf
eZWorkflowEventType::registerEventType( 'autorss', 'AutoRSSType' );
207 1 cyberwolf
 
208 5 Cyberwolf
?>