- Fixed #16160: Call to undefined function ezi18n()
[tinyz:tinyz.git] / kernel / classes / workflowtypes / event / ezpaymentgateway / ezpaymentgatewaytype.php
1 <?php
2 //
3 // Definition of eZPaymentGatewayType class
4 //
5 // Created on: <18-Jul-2004 14:18:58 dl>
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 /*! \file
32 */
33
34 /*!
35   \class eZPaymentGatewayType ezpaymentgatewaytype.php
36   \brief Interface for different types of payment gateways.
37
38   Allows use multiple payment gateways in workflow.
39   Allows user to choose necessary gateway type 'on the fly'.
40 */
41
42 class eZPaymentGatewayType extends eZWorkflowEventType
43 {
44     const WORKFLOW_TYPE_STRING = 'ezpaymentgateway';
45     const GATEWAY_NOT_SELECTED = 0;
46     const GATEWAY_SELECTED = 1;
47
48     /*!
49     Constructor.
50     */
51
52     function eZPaymentGatewayType()
53     {
54         $this->logger   = eZPaymentLogger::CreateForAdd( "var/log/eZPaymentGatewayType.log" );
55
56         $this->eZWorkflowEventType( eZPaymentGatewayType::WORKFLOW_TYPE_STRING, ezpI18n::translate( 'kernel/workflow/event', "Payment Gateway" ) );
57         $this->loadAndRegisterGateways();
58     }
59
60     /*!
61     Creates necessary gateway and delegate execution to it.
62     If there are multiple gateways in eZPaymentGatewayType, fetches
63     template with list of 'selected'(see. 'attributes' section)
64     gateways and asks user to choose one.
65     */
66
67     function execute( $process, $event )
68     {
69         $this->logger->writeTimedString( 'execute' );
70
71         if( $process->attribute( 'event_state' ) == eZPaymentGatewayType::GATEWAY_NOT_SELECTED )
72         {
73             $this->logger->writeTimedString( 'execute: eZPaymentGatewayType::GATEWAY_NOT_SELECTED' );
74
75             $process->setAttribute( 'event_state', eZPaymentGatewayType::GATEWAY_SELECTED );
76             if ( !$this->selectGateway( $event ) )
77             {
78                 $process->Template = array();
79                 $process->Template['templateName'] = 'design:workflow/selectgateway.tpl';
80                 $process->Template['templateVars'] = array ( 'event' => $event );
81
82                 return eZWorkflowType::STATUS_FETCH_TEMPLATE_REPEAT;
83             }
84         }
85
86         $theGateway = $this->getCurrentGateway( $event );
87         if( $theGateway != null )
88         {
89             return $theGateway->execute( $process, $event );
90         }
91
92         $this->logger->writeTimedString( 'execute: something wrong' );
93         return eZWorkflowType::STATUS_REJECTED;
94     }
95
96     /*!
97     Attributes. There are three types of gateways in eZPaymentGatewayType.
98     'Available' gateways - gateways that were installed in the eZPublish
99                    (as extensions, build-in);
100     'Selected' gateways  - gateways that were selected for this instance of
101                    eZPaymentGatewayType;
102     'Current' gateway    - through this gateway payment will be made.
103     */
104
105     function attributeDecoder( $event, $attr )
106     {
107         switch ( $attr )
108         {
109             case 'selected_gateways_types':
110             {
111                 return explode( ',', $event->attribute( 'data_text1' ) );
112             }
113             break;
114
115             case 'selected_gateways':
116             {
117                 $selectedGatewaysTypes  = explode( ',', $event->attribute( 'data_text1' ) );
118                 return $this->getGateways( $selectedGatewaysTypes );
119             }break;
120
121             case 'current_gateway':
122             {
123                 return $event->attribute( 'data_text2' );
124             }
125             break;
126         }
127         return null;
128     }
129
130     function typeFunctionalAttributes( )
131     {
132         return array( 'selected_gateways_types', 'selected_gateways', 'current_gateway' );
133     }
134
135     function attributes()
136     {
137         return array_merge( array( 'available_gateways' ),
138                             eZWorkflowEventType::attributes() );
139     }
140
141     function hasAttribute( $attr )
142     {
143         return in_array( $attr, $this->attributes() );
144     }
145
146     function attribute( $attr )
147     {
148         switch( $attr )
149         {
150             case 'available_gateways':
151             {
152                 return $this->getGateways( array( -1 ) );
153             }break;
154         }
155         return eZWorkflowEventType::attribute( $attr );
156     }
157
158     /*!
159      \static
160     Searches 'available' gateways( built-in or as extensions ).
161     */
162
163     function loadAndRegisterGateways()
164     {
165         eZPaymentGatewayType::loadAndRegisterBuiltInGateways();
166         eZPaymentGatewayType::loadAndRegisterExtensionGateways();
167     }
168
169     /*!
170       \static
171     */
172     function loadAndRegisterBuiltInGateways()
173     {
174         $gatewaysINI        = eZINI::instance( 'paymentgateways.ini' );
175         $gatewaysTypes      = $gatewaysINI->variable( 'GatewaysSettings', 'AvailableGateways' );
176         $gatewaysDir        = false;
177
178         // GatewaysDirectories was spelt as GatewaysDerictories, which is
179         // confusing for people writing ini files - it's a typo.
180         if ( $gatewaysINI->hasVariable( 'GatewaysSettings', 'GatewaysDerictories' ) )
181             $gatewaysDir = $gatewaysINI->variable( 'GatewaysSettings', 'GatewaysDerictories' );
182         else
183             $gatewaysDir = $gatewaysINI->variable( 'GatewaysSettings', 'GatewaysDirectories' );
184
185         if ( is_array( $gatewaysDir ) && is_array( $gatewaysTypes ) )
186         {
187             foreach( $gatewaysDir as $dir )
188             {
189                 foreach( $gatewaysTypes as $gateway )
190                 {
191                     $gatewayPath = "$dir/$gateway/classes/" . $gateway . 'gateway.php';
192                     if( file_exists( $gatewayPath ) )
193                     {
194                         include_once( $gatewayPath );
195                     }
196                 }
197             }
198         }
199     }
200
201     /*!
202       \static
203     */
204     function loadAndRegisterExtensionGateways()
205     {
206         $gatewaysINI        = eZINI::instance( 'paymentgateways.ini' );
207         $siteINI            = eZINI::instance( 'site.ini' );
208         $extensionDirectory = $siteINI->variable( 'ExtensionSettings', 'ExtensionDirectory' );
209         $activeExtensions   = eZExtension::activeExtensions();
210
211         foreach ( $activeExtensions as $extension )
212         {
213             $gatewayPath = "$extensionDirectory/$extension/classes/" . $extension . 'gateway.php';
214             if ( file_exists( $gatewayPath ) )
215             {
216                 include_once( $gatewayPath );
217             }
218         }
219     }
220
221     /*!
222     Each gateway must call this function to become 'available'.
223     */
224
225     function registerGateway( $gateway, $class_name, $description )
226     {
227         $gateways =& $GLOBALS["eZPaymentGateways"];
228         if ( !is_array( $gateways ) )
229         {
230             $gateways = array();
231         }
232
233         if ( isset( $gateways[$gateway] ) )
234         {
235             eZDebug::writeError( "Gateway already registered: $gateway", "eZPaymentGatewayType::registerGateway" );
236         }
237         else
238         {
239             $gateways[$gateway] = array( "class_name" => $class_name, "description" => $description );
240         }
241     }
242
243     /*!
244     Returns an array of gateways difinitions( class_name, description ) by
245     'gatewaysTypes'( array of 'gateway' values that were passed to
246     'registerGateway' function).
247     */
248     function getGateways( $gatewaysTypes )
249     {
250         $gateways           = array();
251         $availableGateways  = $GLOBALS[ 'eZPaymentGateways' ];
252         if ( !is_array( $availableGateways ) ){
253             return $gateways;
254         }
255
256         if ( in_array( '-1', $gatewaysTypes ) )
257         {
258             $gatewaysTypes  = array_keys( $availableGateways );
259         }
260
261         foreach ( $gatewaysTypes as $key )
262         {
263             $gateway = $availableGateways[$key];
264
265             $gateway['Name']    = $gateway['description'];
266             $gateway['value']   = $key;
267             $gateways[] = $gateway;
268         }
269
270         return $gateways;
271     }
272
273     /*!
274     Creates and returns object of eZPaymentGateway subclass.
275     */
276
277     function createGateway( $inGatewayType )
278     {
279         $gateway_difinition = $GLOBALS[ 'eZPaymentGateways' ][ $inGatewayType ];
280
281         $this->logger->writeTimedString( $gateway_difinition, "createGateway. gateway_difinition" );
282
283         if( $gateway_difinition )
284         {
285             $class_name = $gateway_difinition[ 'class_name' ];
286             return new $class_name();
287         }
288
289         return null;
290     }
291
292     /*!
293     Returns 'current' gateway.
294     */
295
296     function getCurrentGateway( $event )
297     {
298         $theGateway  = null;
299         $gatewayType = $this->getCurrentGatewayType( $event );
300
301         if( $gatewayType != null )
302         {
303             $theGateway = $this->createGateway( $gatewayType );
304         }
305
306         return $theGateway;
307     }
308
309     /*!
310     Returns 'current' gatewaytype.
311     */
312
313     function getCurrentGatewayType( $event )
314     {
315         $gateway =  null;
316         $http    = eZHTTPTool::instance();
317
318         if ( $http->hasPostVariable( 'SelectButton' ) && $http->hasPostVariable( 'SelectedGateway' ) )
319         {
320             $gateway = $http->postVariable( 'SelectedGateway' );
321             $event->setAttribute( 'data_text2', $gateway );
322             $event->store();
323         }
324         else if ( $http->hasPostVariable( 'CancelButton' ) )
325         {
326             $gateway = null;
327         }
328         else
329         {
330             $gateway = $event->attribute( 'current_gateway' );
331         }
332
333         return $gateway;
334     }
335
336     /*!
337     Sets 'current' gateway from 'selected' gateways. If 'selected' is just one,
338     it becomes 'current'. Else user have to choose some( appropriate template
339     will be shown).
340     */
341
342     function selectGateway( $event )
343     {
344         $selectedGatewaysTypes  = explode( ',', $event->attribute( 'data_text1' ) );
345
346         if ( count( $selectedGatewaysTypes ) == 1 && $selectedGatewaysTypes[0] != -1 )
347         {
348             $event->setAttribute( 'data_text2', $selectedGatewaysTypes[0] );
349             $event->store();
350
351             $this->logger->writeTimedString( $selectedGatewaysTypes[0], 'selectGateway' );
352             return true;
353         }
354
355         $this->logger->writeTimedString( 'selectGateways. multiple gateways, let user choose.' );
356         return false;
357     }
358
359     function needCleanup()
360     {
361         return true;
362     }
363
364     /*!
365     Delegate to eZPaymentGateway subclass.
366     */
367
368     function cleanup( $process, $event )
369     {
370         $theGateway = $this->getCurrentGateway( $event );
371         if( $theGateway != null and $theGateway->needCleanup() )
372         {
373             $theGateway->cleanup( $process, $event );
374         }
375     }
376
377     function initializeEvent( $event )
378     {
379     }
380
381     /*!
382     Sets 'selected' gateways. -1 means 'Any' - all 'available' gateways
383     becomes 'selected'.
384     */
385
386     function fetchHTTPInput( $http, $base, $event )
387     {
388         $gatewaysVar = $base . "_event_ezpaymentgateway_gateways_" . $event->attribute( "id" );
389         if ( $http->hasPostVariable( $gatewaysVar ) )
390         {
391             $gatewaysArray = $http->postVariable( $gatewaysVar );
392             if ( in_array( '-1', $gatewaysArray ) )
393             {
394                 $gatewaysArray = array( -1 );
395             }
396
397             $gatewaysString = implode( ',', $gatewaysArray );
398             $event->setAttribute( "data_text1", $gatewaysString );
399         }
400     }
401
402     public $logger;
403 }
404
405 eZWorkflowEventType::registerEventType( eZPaymentGatewayType::WORKFLOW_TYPE_STRING, 'ezpaymentgatewaytype' );
406 ?>