Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
5 | | CiviCRM version 2.2 | | |
6 | +--------------------------------------------------------------------+ | |
7 | | Copyright CiviCRM LLC (c) 2004-2009 | | |
8 | +--------------------------------------------------------------------+ | |
9 | | This file is a part of CiviCRM. | | |
10 | | | | |
11 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
12 | | under the terms of the GNU Affero General Public License | | |
13 | | Version 3, 19 November 2007. | | |
14 | | | | |
15 | | CiviCRM is distributed in the hope that it will be useful, but | | |
16 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
17 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
18 | | See the GNU Affero General Public License for more details. | | |
19 | | | | |
20 | | You should have received a copy of the GNU Affero General Public | | |
21 | | License along with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2009 | |
32 | * $Id$ | |
33 | * | |
34 | */ | |
35 | ||
36 | define( 'MULTICURRENCY_EVENT_ID', 4 ); | |
37 | define( 'DISCOUNT_EVENT_ID', 4 ); | |
38 | ||
39 | function multicurrency_civicrm_buildForm( $formName, | |
31037a42 | 40 | &$form ) { |
6a488035 | 41 | if ( ( strpos( $formName, 'CRM_Event_Form_Registration_' ) !== false ) && |
31037a42 | 42 | ( $form->getVar( '_eventId' ) == MULTICURRENCY_EVENT_ID ) ) { |
6a488035 TO |
43 | multicurrency_set_currency( $form ); |
44 | } | |
45 | ||
46 | //------- Coupon field --------// | |
47 | if ( $formName == 'CRM_Event_Form_Registration_Register' && | |
31037a42 | 48 | $form->getVar( '_eventId' ) == DISCOUNT_EVENT_ID ) { |
6a488035 TO |
49 | $form->addElement( 'text', 'discountCode', ts( 'Discount Code' ) ); |
50 | ||
51 | // also assign to template | |
52 | $template =& CRM_Core_Smarty::singleton( ); | |
53 | $beginHookFormElements = $template->get_template_vars( 'beginHookFormElements' ); | |
54 | if ( ! $beginHookFormElements ) { | |
55 | $beginHookFormElements = array( ); | |
56 | } | |
57 | $beginHookFormElements[] = 'discountCode'; | |
58 | $form->assign( 'beginHookFormElements', $beginHookFormElements ); | |
59 | ||
60 | $discountCode = CRM_Utils_Request::retrieve( 'discountCode', 'String', $form, false, null, $_REQUEST ); | |
61 | if ( $discountCode ) { | |
62 | $defaults = array( 'discountCode' => $discountCode ); | |
63 | $form->setDefaults( $defaults ); | |
64 | } | |
65 | } | |
66 | } | |
67 | ||
68 | function multicurrency_set_currency( &$form ) { | |
69 | static $processed = false; | |
70 | ||
71 | if ( $processed ) { | |
72 | return; | |
73 | } | |
74 | ||
75 | $processed = true; | |
76 | $currency = CRM_Utils_Request::retrieve( 'currency', 'String', $form, false, 'GBP' ); | |
77 | $config =& CRM_Core_Config::singleton( ); | |
78 | if ( strtoupper( $currency ) == 'EUR' ) { | |
79 | $config->defaultCurrency = 'EUR'; | |
80 | } else { | |
81 | $config->defaultCurrency = 'GBP'; | |
82 | } | |
83 | ||
84 | return $config->defaultCurrency; | |
85 | } | |
86 | ||
87 | function multicurrency_civicrm_buildAmount( $pageType, | |
31037a42 EM |
88 | &$form, |
89 | &$amount ) { | |
6a488035 TO |
90 | |
91 | // only modify the event pages for the UK event | |
92 | if ( $form->getVar( '_eventId' ) == MULTICURRENCY_EVENT_ID ) { | |
93 | $currency = multicurrency_set_currency( $form ); | |
31037a42 | 94 | |
6a488035 TO |
95 | // as of may 5th: 1 USD = 0.75 EUR, 1 USD = 0.667 GBP |
96 | $ratio = ( $currency == 'EUR' ) ? 0.75 : ( 2.0 / 3.0 ); | |
31037a42 | 97 | |
6a488035 TO |
98 | foreach ( $amount as $amountID =>& $amountInfo ) { |
99 | $amountInfo['value'] = ceil( $amountInfo['value'] * $ratio ); | |
100 | } | |
101 | } | |
102 | ||
103 | //---- DISCOUNT Code ----// | |
104 | $eventID = $form->getVar( '_eventId' ); | |
105 | if ( $pageType != 'event' || | |
106 | $eventID != DISCOUNT_EVENT_ID ) { | |
107 | return; | |
108 | } | |
109 | ||
110 | $discountCode = CRM_Utils_Request::retrieve( 'discountCode', 'String', $form, false, null, $_REQUEST ); | |
111 | if ( ! $discountCode ) { | |
112 | return; | |
113 | } | |
114 | ||
115 | list( $discountID, $discountPercent, $discountNumber ) = _multicurrency_discountHelper( $eventID, $discountCode ); | |
116 | if ( $discountNumber <= 0 ) { | |
117 | // no more discount left | |
118 | return; | |
119 | } | |
120 | ||
121 | foreach ( $amount as $amountId => $amountInfo ) { | |
122 | $amount[$amountId]['value'] = $amount[$amountId]['value'] - | |
123 | ceil($amount[$amountId]['value'] * $discountPercent / 100); | |
124 | $amount[$amountId]['label'] = $amount[$amountId]['label'] . | |
125 | "\t - with {$discountPercent}% discount"; | |
126 | } | |
127 | } | |
128 | ||
129 | function multicurrency_civicrm_pageRun( &$page ) { | |
130 | ||
131 | if ( $page->getVar( '_name' ) == 'CRM_Event_Page_EventInfo' && | |
132 | $page->getVar( '_id' ) == MULTICURRENCY_EVENT_ID ) { | |
133 | multicurrency_set_currency( $page ); | |
134 | } | |
135 | ||
136 | } | |
137 | ||
138 | //---- Discount using codes ------// | |
139 | function _multicurrency_discountHelper( $eventID, $discountCode ) { | |
140 | $sql = " | |
141 | SELECT v.id as id, v.value as value, v.weight as weight | |
142 | FROM civicrm_option_value v, | |
143 | civicrm_option_group g | |
144 | WHERE v.option_group_id = g.id | |
145 | AND v.name = %1 | |
146 | AND g.name = %2 | |
147 | ||
148 | "; | |
149 | $params = array( 1 => array( $discountCode , 'String' ), | |
150 | 2 => array( "event_discount_{$eventID}", 'String' ) ); | |
151 | $dao = CRM_Core_DAO::executeQuery( $sql, $params ); | |
152 | if ( $dao->fetch( ) ) { | |
153 | // ensure discountPercent is a valid numeric number <= 100 | |
154 | if ( $dao->value && | |
155 | is_numeric( $dao->value ) && | |
156 | $dao->value >= 0 && | |
157 | $dao->value <= 100 && | |
158 | is_numeric( $dao->weight ) ) { | |
159 | return array( $dao->id, $dao->value, $dao->weight ); | |
160 | } | |
161 | } | |
162 | return array( null, null, null ); | |
31037a42 | 163 | |
6a488035 TO |
164 | } |
165 | ||
166 | /* | |
167 | * The hook updates the random code used with event signup. | |
168 | */ | |
169 | function multicurrency_civicrm_postProcess( $class, &$form ) { | |
170 | $eventID = $form->getVar( '_eventId' ); | |
171 | if ( ! is_a($form, 'CRM_Event_Form_Registration_Confirm') || | |
172 | $eventID != DISCOUNT_EVENT_ID ) { | |
173 | return; | |
174 | } | |
31037a42 | 175 | |
6a488035 TO |
176 | $discountCode = CRM_Utils_Request::retrieve( 'discountCode', 'String', $form, false, null, $_REQUEST ); |
177 | if ( ! $discountCode ) { | |
178 | return; | |
179 | } | |
180 | ||
181 | list( $discountID, $discountPercent, $discountNumber ) = _multicurrency_discountHelper( $eventID, $discountCode ); | |
182 | if ( ! $discountID || | |
183 | $discountNumber <= 0 || | |
184 | $discountNumber == 123456789 ) { | |
185 | return; | |
186 | } | |
187 | ||
188 | $query = " | |
189 | UPDATE civicrm_option_value v | |
190 | SET v.weight = v.weight - 1 | |
191 | WHERE v.id = %1 | |
192 | AND v.weight > 0 | |
193 | "; | |
194 | $params = array( 1 => array( $discountID, 'Integer' ) ); | |
195 | ||
196 | CRM_Core_DAO::executeQuery( $query, $params ); | |
197 | } |