| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* with thanks to Travis Smith's excellent tutorial: |
| 5 |
* http://wpsmith.net/2011/plugins/how-to-create-a-custom-form-field-in-gravity-forms-with-a-terms-of-service-form-field-example/ |
| 6 |
*/ |
| 7 |
class GFEwayRecurringField { |
| 8 |
|
| 9 |
protected $plugin; |
| 10 |
|
| 11 |
protected static $defaults = array ( |
| 12 |
'gfeway_initial_amount_label' => 'Initial Amount', |
| 13 |
'gfeway_recurring_amount_label' => 'Recurring Amount', |
| 14 |
'gfeway_initial_date_label' => 'Initial Date', |
| 15 |
'gfeway_start_date_label' => 'Start Date', |
| 16 |
'gfeway_end_date_label' => 'End Date', |
| 17 |
'gfeway_interval_type_label' => 'Interval Type', |
| 18 |
); |
| 19 |
|
| 20 |
/** |
| 21 |
* @param GFEwayPlugin $plugin |
| 22 |
*/ |
| 23 |
public function __construct($plugin) { |
| 24 |
$this->plugin = $plugin; |
| 25 |
|
| 26 |
// WordPress script hooks -- NB: must happen after Gravity Forms registers scripts |
| 27 |
add_action('wp_enqueue_scripts', array($this, 'registerScripts'), 20); |
| 28 |
add_action('admin_enqueue_scripts', array($this, 'registerScripts'), 20); |
| 29 |
|
| 30 |
// add Gravity Forms hooks |
| 31 |
add_action('gform_enqueue_scripts', array($this, 'gformEnqueueScripts'), 20, 2); |
| 32 |
add_action('gform_editor_js', array($this, 'gformEditorJS')); |
| 33 |
add_action('gform_field_standard_settings', array($this, 'gformFieldStandardSettings'), 10, 2); |
| 34 |
add_filter('gform_add_field_buttons', array($this, 'gformAddFieldButtons')); |
| 35 |
add_filter('gform_field_type_title', array($this, 'gformFieldTypeTitle'), 10, 2); |
| 36 |
add_filter('gform_field_input', array($this, 'gformFieldInput'), 10, 5); |
| 37 |
add_filter('gform_pre_validation', array($this, 'gformPreValidation')); |
| 38 |
add_filter('gform_field_validation', array($this, 'gformFieldValidation'), 10, 4); |
| 39 |
add_filter('gform_tooltips', array($this, 'gformTooltips')); |
| 40 |
add_filter('gform_pre_submission', array($this, 'gformPreSubmit')); |
| 41 |
|
| 42 |
if (is_admin()) { |
| 43 |
add_filter('gform_field_css_class', array($this, 'watchFieldType'), 10, 2); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* register and enqueue required scripts |
| 49 |
* NB: must happen after Gravity Forms registers scripts |
| 50 |
*/ |
| 51 |
public function registerScripts() { |
| 52 |
// recurring payments field has datepickers; register required scripts / stylesheets |
| 53 |
if (version_compare(GFCommon::$version, '1.7.6.99999', '<')) { |
| 54 |
// pre-1.7.7 script registrations |
| 55 |
$gfBaseUrl = GFCommon::get_base_url(); |
| 56 |
wp_register_script('gforms_ui_datepicker', $gfBaseUrl . '/js/jquery-ui/ui.datepicker.js', array('jquery'), GFCommon::$version, true); |
| 57 |
wp_register_script('gforms_datepicker', $gfBaseUrl . '/js/datepicker.js', array('gforms_ui_datepicker'), GFCommon::$version, true); |
| 58 |
$reqs = array('gforms_datepicker'); |
| 59 |
} |
| 60 |
else { |
| 61 |
// post-1.7.7 |
| 62 |
$reqs = array('gform_datepicker_init'); |
| 63 |
} |
| 64 |
|
| 65 |
$min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
| 66 |
$ver = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? time() : GFEWAY_PLUGIN_VERSION; |
| 67 |
|
| 68 |
wp_register_script('gfeway_recurring', "{$this->plugin->urlBase}js/recurring$min.js", $reqs, $ver, true); |
| 69 |
|
| 70 |
wp_register_style('gfeway', $this->plugin->urlBase . 'style.css', false, $ver); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* enqueue additional scripts if required by form |
| 75 |
* @param array $form |
| 76 |
* @param boolean $ajax |
| 77 |
*/ |
| 78 |
public function gformEnqueueScripts($form, $ajax) { |
| 79 |
if ($this->plugin->hasFieldType($form['fields'], GFEWAY_FIELD_RECURRING)) { |
| 80 |
// enqueue script for field |
| 81 |
wp_enqueue_script('gfeway_recurring'); |
| 82 |
|
| 83 |
// add datepicker style if Gravity Forms hasn't done so already -- since Gravity Forms v1.8.6 |
| 84 |
if (version_compare(GFCommon::$version, '1.8.6', '>=') && !wp_style_is('gforms_datepicker_css', 'done')) { |
| 85 |
wp_enqueue_style('gforms_datepicker_css', GFCommon::get_base_url() . '/css/datepicker.css', null, GFCommon::$version); |
| 86 |
wp_print_styles(array('gforms_datepicker_css')); |
| 87 |
} |
| 88 |
|
| 89 |
// enqueue default styling |
| 90 |
wp_enqueue_style('gfeway'); |
| 91 |
} |
| 92 |
|
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* load custom script for editor form |
| 97 |
*/ |
| 98 |
public function gformEditorJS() { |
| 99 |
$min = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '' : '.min'; |
| 100 |
$ver = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? time() : GFEWAY_PLUGIN_VERSION; |
| 101 |
echo "<script src=\"{$this->plugin->urlBase}js/admin-recurring$min.js?v=$ver\"></script>\n"; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* filter hook for modifying the field buttons on the forms editor |
| 106 |
* @param array $field_groups array of field groups; each element is an array of button definitions |
| 107 |
* @return array |
| 108 |
*/ |
| 109 |
public function gformAddFieldButtons($field_groups) { |
| 110 |
foreach ($field_groups as &$group) { |
| 111 |
if ($group['name'] == 'pricing_fields') { |
| 112 |
$group['fields'][] = array ( |
| 113 |
'class' => 'button', |
| 114 |
'value' => 'Recurring', |
| 115 |
'onclick' => "StartAddField('" . GFEWAY_FIELD_RECURRING . "');", |
| 116 |
); |
| 117 |
break; |
| 118 |
} |
| 119 |
} |
| 120 |
return $field_groups; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* filter hook for modifying the field title (e.g. on custom fields) |
| 125 |
* @param string $title |
| 126 |
* @param string $field_type |
| 127 |
* @return string |
| 128 |
*/ |
| 129 |
public function gformFieldTypeTitle($title, $field_type) { |
| 130 |
if ($field_type == GFEWAY_FIELD_RECURRING) { |
| 131 |
$title = 'Recurring Payments'; |
| 132 |
} |
| 133 |
|
| 134 |
return $title; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* add custom fields to form editor |
| 139 |
* @param integer $position |
| 140 |
* @param integer $form_id |
| 141 |
*/ |
| 142 |
public function gformFieldStandardSettings($position, $form_id) { |
| 143 |
// add inputs for labels right after the field label input |
| 144 |
if ($position == 25) { |
| 145 |
?> |
| 146 |
<li class="gfewayrecurring_setting field_setting"> |
| 147 |
|
| 148 |
<input type="checkbox" id="gfeway_initial_setting" onchange="GFEwayRecurring.ToggleInitialSetting(this)" /> |
| 149 |
<label for="gfeway_initial_setting" class="inline"> |
| 150 |
Show Initial Amount |
| 151 |
<?php gform_tooltip("gfeway_initial_setting") ?> |
| 152 |
<?php gform_tooltip("gfeway_initial_setting_html") ?> |
| 153 |
</label> |
| 154 |
<br /> |
| 155 |
<br /> |
| 156 |
|
| 157 |
<div id="gfeway_initial_fields"> |
| 158 |
|
| 159 |
<label for="gfeway_initial_date_label"> |
| 160 |
Initial Date Label |
| 161 |
<?php gform_tooltip("gfeway_initial_date_label") ?> |
| 162 |
<?php gform_tooltip("gfeway_initial_date_label_html") ?> |
| 163 |
</label> |
| 164 |
<input type="text" id="gfeway_initial_date_label" class="fieldwidth-3" size="35" |
| 165 |
onkeyup="GFEwayRecurring.SetFieldLabel(this, '<?php echo esc_attr(self::$defaults['gfeway_initial_date_label']) ?>')" /> |
| 166 |
|
| 167 |
<label for="gfeway_initial_amount_label"> |
| 168 |
Initial Amount Label |
| 169 |
<?php gform_tooltip("gfeway_initial_amount_label") ?> |
| 170 |
<?php gform_tooltip("gfeway_initial_amount_label_html") ?> |
| 171 |
</label> |
| 172 |
<input type="text" id="gfeway_initial_amount_label" class="fieldwidth-3" size="35" |
| 173 |
onkeyup="GFEwayRecurring.SetFieldLabel(this, '<?php echo esc_attr(self::$defaults['gfeway_initial_amount_label']) ?>')" /> |
| 174 |
|
| 175 |
</div> |
| 176 |
|
| 177 |
<label for="gfeway_recurring_amount_label"> |
| 178 |
Recurring Amount Label |
| 179 |
<?php gform_tooltip("gfeway_recurring_amount_label") ?> |
| 180 |
<?php gform_tooltip("gfeway_recurring_amount_label_html") ?> |
| 181 |
</label> |
| 182 |
<input type="text" id="gfeway_recurring_amount_label" class="fieldwidth-3" size="35" |
| 183 |
onkeyup="GFEwayRecurring.SetFieldLabel(this, '<?php echo esc_attr(self::$defaults['gfeway_recurring_amount_label']) ?>')" /> |
| 184 |
|
| 185 |
<br /> |
| 186 |
<br /> |
| 187 |
<input type="checkbox" id="gfeway_recurring_date_setting" onchange="GFEwayRecurring.ToggleRecurringDateSetting(this)" /> |
| 188 |
<label for="gfeway_recurring_date_setting" class="inline"> |
| 189 |
Show Start/End Dates |
| 190 |
<?php gform_tooltip("gfeway_recurring_date_setting") ?> |
| 191 |
<?php gform_tooltip("gfeway_recurring_date_setting_html") ?> |
| 192 |
</label> |
| 193 |
<br /> |
| 194 |
<br /> |
| 195 |
|
| 196 |
<div id="gfeway_recurring_date_fields"> |
| 197 |
|
| 198 |
<label for="gfeway_start_date_label"> |
| 199 |
Start Date Label |
| 200 |
<?php gform_tooltip("gfeway_start_date_label") ?> |
| 201 |
<?php gform_tooltip("gfeway_start_date_label_html") ?> |
| 202 |
</label> |
| 203 |
<input type="text" id="gfeway_start_date_label" class="fieldwidth-3" size="35" |
| 204 |
onkeyup="GFEwayRecurring.SetFieldLabel(this, '<?php echo esc_attr(self::$defaults['gfeway_start_date_label']) ?>')" /> |
| 205 |
|
| 206 |
<label for="gfeway_end_date_label"> |
| 207 |
End Date Label |
| 208 |
<?php gform_tooltip("gfeway_end_date_label") ?> |
| 209 |
<?php gform_tooltip("gfeway_end_date_label_html") ?> |
| 210 |
</label> |
| 211 |
<input type="text" id="gfeway_end_date_label" class="fieldwidth-3" size="35" |
| 212 |
onkeyup="GFEwayRecurring.SetFieldLabel(this, '<?php echo esc_attr(self::$defaults['gfeway_end_date_label']) ?>')" /> |
| 213 |
|
| 214 |
</div> |
| 215 |
|
| 216 |
<label for="gfeway_interval_type_label"> |
| 217 |
Interval Type Label |
| 218 |
<?php gform_tooltip("gfeway_interval_type_label") ?> |
| 219 |
<?php gform_tooltip("gfeway_interval_type_label_html") ?> |
| 220 |
</label> |
| 221 |
<input type="text" id="gfeway_interval_type_label" class="fieldwidth-3" size="35" |
| 222 |
onkeyup="GFEwayRecurring.SetFieldLabel(this, '<?php echo esc_attr(self::$defaults['gfeway_interval_type_label']) ?>')" /> |
| 223 |
|
| 224 |
</li> |
| 225 |
|
| 226 |
<?php |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* add custom tooltips for fields on form editor |
| 232 |
* @param array $tooltips |
| 233 |
* @return array |
| 234 |
*/ |
| 235 |
public function gformTooltips($tooltips) { |
| 236 |
$tooltips['gfeway_initial_setting'] = "<h6>Show Initial Amount</h6>Select this option to show Initial Amount and Initial Date fields."; |
| 237 |
$tooltips['gfeway_initial_amount_label'] = "<h6>Initial Amount</h6>The label shown for the Initial Amount field."; |
| 238 |
$tooltips['gfeway_initial_date_label'] = "<h6>Initial Date</h6>The label shown for the Initial Date field."; |
| 239 |
$tooltips['gfeway_recurring_amount_label'] = "<h6>Recurring Amount</h6>The label shown for the Recurring Amount field."; |
| 240 |
$tooltips['gfeway_recurring_date_setting'] = "<h6>Show Start/End Dates</h6>Select this option to show Start Date and End Date fields."; |
| 241 |
$tooltips['gfeway_start_date_label'] = "<h6>Start Date</h6>The label shown for the Start Date field."; |
| 242 |
$tooltips['gfeway_end_date_label'] = "<h6>End Date</h6>The label shown for the End Date field."; |
| 243 |
$tooltips['gfeway_interval_type_label'] = "<h6>Interval Type</h6>The label shown for the Interval Type field."; |
| 244 |
|
| 245 |
return $tooltips; |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* grab values and concatenate into a string before submission is accepted |
| 250 |
* @param array $form |
| 251 |
*/ |
| 252 |
public function gformPreSubmit($form) { |
| 253 |
foreach ($form['fields'] as $field) { |
| 254 |
if ($field['type'] == GFEWAY_FIELD_RECURRING && !RGFormsModel::is_field_hidden($form, $field, RGForms::post('gform_field_values'))) { |
| 255 |
$recurring = self::getPost($field['id']); |
| 256 |
$_POST["input_{$field['id']}"] = '$' . number_format($recurring['amountRecur'], 2) |
| 257 |
. " {$recurring['intervalTypeDesc']} from {$recurring['dateStart']->format('d M Y')}"; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* prime the inputs that will be checked by standard validation tests, |
| 264 |
* e.g. so that "required" fields don't fail |
| 265 |
* @param array $form |
| 266 |
* @return array |
| 267 |
*/ |
| 268 |
public function gformPreValidation($form) { |
| 269 |
foreach($form["fields"] as $field) { |
| 270 |
if ($field['type'] == GFEWAY_FIELD_RECURRING && !RGFormsModel::is_field_hidden($form, $field, RGForms::post('gform_field_values'))) { |
| 271 |
$recurring = self::getPost($field['id']); |
| 272 |
$_POST["input_{$field['id']}"] = sprintf('$%0.2d %s from %s', |
| 273 |
$recurring['amountRecur'], $recurring['intervalTypeDesc'], $recurring['dateStart']->format('d M Y')); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
return $form; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* validate inputs |
| 282 |
* @param array $validation_result an array with elements is_valid (boolean) and form (array of form elements) |
| 283 |
* @param string $value |
| 284 |
* @param array $form |
| 285 |
* @param array $field |
| 286 |
* @return array |
| 287 |
*/ |
| 288 |
public function gformFieldValidation($validation_result, $value, $form, $field) { |
| 289 |
if ($field['type'] == GFEWAY_FIELD_RECURRING) { |
| 290 |
if (!RGFormsModel::is_field_hidden($form, $field, RGForms::post('gform_field_values'))) { |
| 291 |
// get the real values |
| 292 |
$value = self::getPost($field['id']); |
| 293 |
|
| 294 |
if (!is_array($value)) { |
| 295 |
$validation_result['is_valid'] = false; |
| 296 |
$validation_result['message'] = __("This field is required.", "gravityforms"); |
| 297 |
} |
| 298 |
|
| 299 |
else { |
| 300 |
$messages = array(); |
| 301 |
|
| 302 |
if ($value['amountInit'] === false || $value['amountInit'] < 0) { |
| 303 |
$messages[] = "Please enter a valid initial amount."; |
| 304 |
} |
| 305 |
|
| 306 |
if (empty($value['dateInit'])) { |
| 307 |
$messages[] = "Please enter a valid initial date in the format dd/mm/yyyy."; |
| 308 |
} |
| 309 |
|
| 310 |
if (empty($value['amountRecur']) || $value['amountRecur'] < 0) { |
| 311 |
$messages[] = "Please enter a valid recurring amount."; |
| 312 |
} |
| 313 |
|
| 314 |
if (empty($value['dateStart'])) { |
| 315 |
$messages[] = "Please enter a valid start date in the format dd/mm/yyyy."; |
| 316 |
} |
| 317 |
|
| 318 |
if (empty($value['dateEnd'])) { |
| 319 |
$messages[] = "Please enter a valid end date in the format dd/mm/yyyy."; |
| 320 |
} |
| 321 |
|
| 322 |
if ($value['intervalType'] === -1) { |
| 323 |
$messages[] = "Please select a valid interval type."; |
| 324 |
} |
| 325 |
|
| 326 |
//~ echo "<pre>", print_r($messages,1), "</pre>"; |
| 327 |
//~ echo "<pre>", print_r($value,1), "</pre>"; exit; |
| 328 |
|
| 329 |
if (count($messages) > 0) { |
| 330 |
$validation_result['is_valid'] = false; |
| 331 |
$validation_result['message'] = implode("<br />\n", $messages); |
| 332 |
} |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
return $validation_result; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
/** |
| 342 |
* watch the field type so that we can use hooks that don't pass enough information |
| 343 |
* @param string $classes |
| 344 |
* @param array $field |
| 345 |
* @return string |
| 346 |
*/ |
| 347 |
public function watchFieldType($classes, $field) { |
| 348 |
// if field type matches, add filters that don't allow testing for field type |
| 349 |
if ($field['type'] == GFEWAY_FIELD_RECURRING) { |
| 350 |
add_filter('gform_duplicate_field_link', array($this, 'gformDuplicateFieldLink')); |
| 351 |
} |
| 352 |
|
| 353 |
return $classes; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* filter the field duplication link, we don't want one for this field type |
| 358 |
* @param string $duplicate_field_link |
| 359 |
* @return $duplicate_field_link |
| 360 |
*/ |
| 361 |
public function gformDuplicateFieldLink($duplicate_field_link) { |
| 362 |
// remove filter once called, only process current field |
| 363 |
remove_filter('gform_duplicate_field_link', array($this, __FUNCTION__)); |
| 364 |
|
| 365 |
// erase duplicate field link for this field |
| 366 |
return ''; |
| 367 |
} |
| 368 |
|
| 369 |
/** |
| 370 |
* filter hook for modifying a field's input tag (e.g. on custom fields) |
| 371 |
* @param string $input the input tag before modification |
| 372 |
* @param array $field |
| 373 |
* @param string $value |
| 374 |
* @param integer $lead_id |
| 375 |
* @param integer $form_id |
| 376 |
* @return string |
| 377 |
*/ |
| 378 |
public function gformFieldInput($input, $field, $value, $lead_id, $form_id) { |
| 379 |
if ($field['type'] == GFEWAY_FIELD_RECURRING) { |
| 380 |
|
| 381 |
// pick up the real value |
| 382 |
$value = rgpost('gfeway_' . $field['id']); |
| 383 |
|
| 384 |
$disabled_text = (IS_ADMIN && RG_CURRENT_VIEW != "entry") ? "disabled='disabled' " : ""; |
| 385 |
$css = isset($field['cssClass']) ? esc_attr($field['cssClass']) : ''; |
| 386 |
|
| 387 |
$today = date_create('now', timezone_open('Australia/Sydney')); |
| 388 |
$initial_amount = empty($value[1]) ? '0.00' : $value[1]; |
| 389 |
$initial_date = empty($value[2]) ? $today->format('d-m-Y') : $value[2]; |
| 390 |
$recurring_amount = empty($value[3]) ? '0.00' : $value[3]; |
| 391 |
$start_date = empty($value[4]) ? $today->format('d-m-Y') : $value[4]; |
| 392 |
$end_date = empty($value[5]) ? '31-12-2099' : $value[5]; |
| 393 |
$interval_type = empty($value[6]) ? 'monthly' : $value[6]; |
| 394 |
|
| 395 |
$input = "<div class='ginput_complex ginput_container gfeway_recurring_complex $css' id='input_{$field['id']}'>"; |
| 396 |
|
| 397 |
// initial amount |
| 398 |
$sub_field = array ( |
| 399 |
'type' => 'donation', |
| 400 |
'id' => $field['id'], |
| 401 |
'sub_id' => '1', |
| 402 |
'label' => empty($field['gfeway_initial_amount_label']) ? self::$defaults['gfeway_initial_amount_label'] : $field['gfeway_initial_amount_label'], |
| 403 |
'isRequired' => false, |
| 404 |
'size' => 'medium', |
| 405 |
'label_class' => 'gfeway_initial_amount_label', |
| 406 |
'hidden' => (isset($field['gfeway_initial_setting']) ? !$field['gfeway_initial_setting'] : false), |
| 407 |
); |
| 408 |
$input .= $this->fieldDonation($sub_field, $initial_amount, $lead_id, $form_id); |
| 409 |
|
| 410 |
// initial date |
| 411 |
$sub_field = array ( |
| 412 |
'type' => 'date', |
| 413 |
'id' => $field['id'], |
| 414 |
'sub_id' => '2', |
| 415 |
'label' => empty($field['gfeway_initial_date_label']) ? self::$defaults['gfeway_initial_date_label'] : $field['gfeway_initial_date_label'], |
| 416 |
'dateFormat' => 'dmy', |
| 417 |
'dateType' => 'datepicker', |
| 418 |
'dateMin' => '+0', |
| 419 |
'dateMax' => '+2Y', |
| 420 |
'calendarIconType' => 'calendar', |
| 421 |
'isRequired' => false, |
| 422 |
'size' => 'medium', |
| 423 |
'label_class' => 'gfeway_initial_date_label', |
| 424 |
'hidden' => (isset($field['gfeway_initial_setting']) ? !$field['gfeway_initial_setting'] : false), |
| 425 |
); |
| 426 |
$input .= $this->fieldDate($sub_field, $initial_date, $lead_id, $form_id); |
| 427 |
|
| 428 |
$input .= '<br />'; |
| 429 |
|
| 430 |
// recurring amount |
| 431 |
$sub_field = array ( |
| 432 |
'type' => 'donation', |
| 433 |
'id' => $field['id'], |
| 434 |
'sub_id' => '3', |
| 435 |
'label' => empty($field['gfeway_recurring_amount_label']) ? self::$defaults['gfeway_recurring_amount_label'] : $field['gfeway_recurring_amount_label'], |
| 436 |
'isRequired' => true, |
| 437 |
'size' => 'medium', |
| 438 |
'label_class' => 'gfeway_recurring_amount_label', |
| 439 |
); |
| 440 |
$input .= $this->fieldDonation($sub_field, $recurring_amount, $lead_id, $form_id); |
| 441 |
|
| 442 |
// start date |
| 443 |
$sub_field = array ( |
| 444 |
'type' => 'date', |
| 445 |
'id' => $field['id'], |
| 446 |
'sub_id' => '4', |
| 447 |
'label' => empty($field['gfeway_start_date_label']) ? self::$defaults['gfeway_start_date_label'] : $field['gfeway_start_date_label'], |
| 448 |
'dateFormat' => 'dmy', |
| 449 |
'dateType' => 'datepicker', |
| 450 |
'dateMin' => '+0', |
| 451 |
'dateMax' => '+2Y', |
| 452 |
'calendarIconType' => 'calendar', |
| 453 |
'isRequired' => true, |
| 454 |
'size' => 'medium', |
| 455 |
'label_class' => 'gfeway_start_date_label', |
| 456 |
'hidden' => (isset($field['gfeway_recurring_date_setting']) ? !$field['gfeway_recurring_date_setting'] : false), |
| 457 |
); |
| 458 |
$input .= $this->fieldDate($sub_field, $start_date, $lead_id, $form_id); |
| 459 |
|
| 460 |
// end date |
| 461 |
$sub_field = array ( |
| 462 |
'type' => 'date', |
| 463 |
'id' => $field['id'], |
| 464 |
'sub_id' => '5', |
| 465 |
'label' => empty($field['gfeway_end_date_label']) ? self::$defaults['gfeway_end_date_label'] : $field['gfeway_end_date_label'], |
| 466 |
'dateFormat' => 'dmy', |
| 467 |
'dateType' => 'datepicker', |
| 468 |
'dateMin' => '+0', |
| 469 |
'dateMax' => '2099-12-31', |
| 470 |
'calendarIconType' => 'calendar', |
| 471 |
'isRequired' => true, |
| 472 |
'size' => 'medium', |
| 473 |
'label_class' => 'gfeway_end_date_label', |
| 474 |
'hidden' => (isset($field['gfeway_recurring_date_setting']) ? !$field['gfeway_recurring_date_setting'] : false), |
| 475 |
); |
| 476 |
$input .= $this->fieldDate($sub_field, $end_date, $lead_id, $form_id); |
| 477 |
|
| 478 |
$input .= '<br />'; |
| 479 |
|
| 480 |
// recurrance interval type drop-down |
| 481 |
$sub_field = array ( |
| 482 |
'type' => 'number', |
| 483 |
'id' => $field['id'], |
| 484 |
'sub_id' => '6', |
| 485 |
'label' => empty($field['gfeway_interval_type_label']) ? self::$defaults['gfeway_interval_type_label'] : $field['gfeway_interval_type_label'], |
| 486 |
'isRequired' => true, |
| 487 |
'size' => 'medium', |
| 488 |
'label_class' => 'gfeway_interval_type_label', |
| 489 |
); |
| 490 |
$input .= $this->fieldIntervalType($sub_field, $interval_type, $lead_id, $form_id); |
| 491 |
|
| 492 |
// concatenated value added to database |
| 493 |
$sub_field = array ( |
| 494 |
'type' => 'hidden', |
| 495 |
'id' => $field['id'], |
| 496 |
'isRequired' => true, |
| 497 |
); |
| 498 |
$input .= $this->fieldConcatenated($sub_field, $interval_type, $lead_id, $form_id); |
| 499 |
|
| 500 |
$input .= "</div>"; |
| 501 |
} |
| 502 |
|
| 503 |
return $input; |
| 504 |
} |
| 505 |
|
| 506 |
/** |
| 507 |
* get HTML for input and label for date field (as date picker) |
| 508 |
* @param array $field |
| 509 |
* @param string $value |
| 510 |
* @param integer $lead_id |
| 511 |
* @param integer $form_id |
| 512 |
* @return string |
| 513 |
*/ |
| 514 |
protected function fieldDate($field, $value="", $lead_id=0, $form_id=0) { |
| 515 |
$id = $field['id']; |
| 516 |
$sub_id = $field['sub_id']; |
| 517 |
$field_id = IS_ADMIN || $form_id == 0 ? "gfeway_{$id}_{$sub_id}" : "gfeway_{$form_id}_{$id}_{$sub_id}"; |
| 518 |
$form_id = IS_ADMIN && empty($form_id) ? rgget('id') : $form_id; |
| 519 |
|
| 520 |
$format = empty($field['dateFormat']) ? 'dmy' : esc_attr($field['dateFormat']); |
| 521 |
$size = rgar($field, 'size'); |
| 522 |
$disabled_text = (IS_ADMIN && RG_CURRENT_VIEW != "entry") ? "disabled='disabled'" : ""; |
| 523 |
$class_suffix = RG_CURRENT_VIEW == 'entry' ? '_admin' : ''; |
| 524 |
|
| 525 |
$value = GFCommon::date_display($value, $format); |
| 526 |
$icon_class = $field["calendarIconType"] == "none" ? "datepicker_no_icon" : "datepicker_with_icon"; |
| 527 |
$icon_url = empty($field["calendarIconUrl"]) ? GFCommon::get_base_url() . "/images/calendar.png" : $field["calendarIconUrl"]; |
| 528 |
$tabindex = GFCommon::get_tabindex(); |
| 529 |
|
| 530 |
$inputClass = array($size . $class_suffix, $format, $icon_class); |
| 531 |
$spanClass = array('gfeway_recurring_left', 'gfeway_recurring_date'); |
| 532 |
|
| 533 |
if (empty($field['hidden'])) { |
| 534 |
$inputClass[] = 'datepicker'; |
| 535 |
} |
| 536 |
else { |
| 537 |
$spanClass[] = 'gf_hidden'; |
| 538 |
} |
| 539 |
|
| 540 |
$dataMin = ''; |
| 541 |
if (!empty($field['dateMin'])) { |
| 542 |
$dataMin = "data-gfeway-minDate='" . esc_attr($field['dateMin']) . "'"; |
| 543 |
} |
| 544 |
|
| 545 |
$dataMax = ''; |
| 546 |
if (!empty($field['dateMax'])) { |
| 547 |
$dataMax = "data-gfeway-maxDate='" . esc_attr($field['dateMax']) . "'"; |
| 548 |
} |
| 549 |
|
| 550 |
$value = esc_attr($value); |
| 551 |
$spanClass = esc_attr(implode(' ', $spanClass)); |
| 552 |
$inputClass = esc_attr(implode(' ', $inputClass)); |
| 553 |
|
| 554 |
$label = htmlspecialchars($field['label']); |
| 555 |
|
| 556 |
$input = "<span class='$spanClass'>"; |
| 557 |
$input .= "<input name='gfeway_{$id}[{$sub_id}]' id='$field_id' type='text' value='$value' $dataMin $dataMax class='$inputClass' $tabindex $disabled_text />"; |
| 558 |
$input .= "<input type='hidden' id='gforms_calendar_icon_$field_id' class='gform_hidden' value='$icon_url'/>"; |
| 559 |
$input .= "<label class='{$field['label_class']}' for='$field_id' id='{$field_id}_label'>$label</label>"; |
| 560 |
$input .= "</span>"; |
| 561 |
|
| 562 |
return $input; |
| 563 |
} |
| 564 |
|
| 565 |
/** |
| 566 |
* get HTML for input and label for donation (amount) field |
| 567 |
* @param array $field |
| 568 |
* @param string $value |
| 569 |
* @param integer $lead_id |
| 570 |
* @param integer $form_id |
| 571 |
* @return string |
| 572 |
*/ |
| 573 |
protected function fieldDonation($field, $value="", $lead_id=0, $form_id=0) { |
| 574 |
$id = $field["id"]; |
| 575 |
$sub_id = $field["sub_id"]; |
| 576 |
$field_id = IS_ADMIN || $form_id == 0 ? "gfeway_{$id}_{$sub_id}" : "gfeway_{$form_id}_{$id}_{$sub_id}"; |
| 577 |
$form_id = IS_ADMIN && empty($form_id) ? rgget("id") : $form_id; |
| 578 |
|
| 579 |
$size = rgar($field, "size"); |
| 580 |
$disabled_text = (IS_ADMIN && RG_CURRENT_VIEW != "entry") ? "disabled='disabled'" : ""; |
| 581 |
$class_suffix = RG_CURRENT_VIEW == "entry" ? "_admin" : ""; |
| 582 |
$class = $size . $class_suffix; |
| 583 |
|
| 584 |
$tabindex = GFCommon::get_tabindex(); |
| 585 |
//~ $logic_event = GFCommon::get_logic_event($field, "keyup"); |
| 586 |
|
| 587 |
$spanClass = ''; |
| 588 |
if (!empty($field['hidden'])) { |
| 589 |
$spanClass = 'gf_hidden'; |
| 590 |
} |
| 591 |
|
| 592 |
$value = esc_attr($value); |
| 593 |
$class = esc_attr($class); |
| 594 |
|
| 595 |
$label = htmlspecialchars($field['label']); |
| 596 |
|
| 597 |
$input = "<span class='gfeway_recurring_left $spanClass'>"; |
| 598 |
$input .= "<input name='gfeway_{$id}[{$sub_id}]' id='$field_id' type='text' value='$value' class='ginput_amount $class' $tabindex $disabled_text />"; |
| 599 |
$input .= "<label class='{$field['label_class']}' for='$field_id' id='{$field_id}_label'>$label</label>"; |
| 600 |
$input .= "</span>"; |
| 601 |
|
| 602 |
return $input; |
| 603 |
} |
| 604 |
|
| 605 |
/** |
| 606 |
* get HTML for input and label for Interval Type field |
| 607 |
* @param array $field |
| 608 |
* @param string $value |
| 609 |
* @param integer $lead_id |
| 610 |
* @param integer $form_id |
| 611 |
* @return string |
| 612 |
*/ |
| 613 |
protected function fieldIntervalType($field, $value="", $lead_id=0, $form_id=0) { |
| 614 |
$id = $field["id"]; |
| 615 |
$sub_id = $field["sub_id"]; |
| 616 |
$field_id = IS_ADMIN || $form_id == 0 ? "gfeway_{$id}_{$sub_id}" : "gfeway_{$form_id}_{$id}_{$sub_id}"; |
| 617 |
$form_id = IS_ADMIN && empty($form_id) ? rgget("id") : $form_id; |
| 618 |
|
| 619 |
$size = rgar($field, "size"); |
| 620 |
$disabled_text = (IS_ADMIN && RG_CURRENT_VIEW != "entry") ? "disabled='disabled'" : ""; |
| 621 |
$class_suffix = RG_CURRENT_VIEW == "entry" ? "_admin" : ""; |
| 622 |
$class = $size . $class_suffix; |
| 623 |
|
| 624 |
$tabindex = GFCommon::get_tabindex(); |
| 625 |
|
| 626 |
$spanClass = ''; |
| 627 |
if (!empty($field['hidden'])) { |
| 628 |
$spanClass = 'gf_hidden'; |
| 629 |
} |
| 630 |
|
| 631 |
$class = esc_attr($class); |
| 632 |
|
| 633 |
$label = htmlspecialchars($field['label']); |
| 634 |
|
| 635 |
$periods = apply_filters('gfeway_recurring_periods', array('weekly', 'fortnightly', 'monthly', 'quarterly', 'yearly'), $form_id, $field); |
| 636 |
if (count($periods) == 1) { |
| 637 |
// build a hidden field and label |
| 638 |
$input = "<span class='gfeway_recurring_left $spanClass'>"; |
| 639 |
$input .= "<input type='hidden' name='gfeway_{$id}[{$sub_id}]' value='{$periods[0]}' />"; |
| 640 |
$input .= "<label class='{$field['label_class']}' for='$field_id' id='{$field_id}_label'>$label: {$periods[0]}</label>"; |
| 641 |
$input .= "</span>"; |
| 642 |
} |
| 643 |
else { |
| 644 |
// build a drop-down list |
| 645 |
$opts = ''; |
| 646 |
foreach ($periods as $period) { |
| 647 |
$opts .= "<option value='$period'"; |
| 648 |
if ($period == $value) |
| 649 |
$opts .= " selected='selected'"; |
| 650 |
$opts .= ">$period</option>"; |
| 651 |
} |
| 652 |
|
| 653 |
$input = "<span class='gfeway_recurring_left $spanClass'>"; |
| 654 |
$input .= "<select size='1' name='gfeway_{$id}[{$sub_id}]' id='$field_id' $tabindex class='gfield_select $class' $disabled_text>$opts</select>"; |
| 655 |
$input .= "<label class='{$field['label_class']}' for='$field_id' id='{$field_id}_label'>$label</label>"; |
| 656 |
$input .= "</span>"; |
| 657 |
} |
| 658 |
|
| 659 |
return $input; |
| 660 |
} |
| 661 |
|
| 662 |
/** |
| 663 |
* get HTML for hidden input with concatenated value for complex field |
| 664 |
* @param array $field |
| 665 |
* @param string $value |
| 666 |
* @param integer $lead_id |
| 667 |
* @param integer $form_id |
| 668 |
* @return string |
| 669 |
*/ |
| 670 |
protected function fieldConcatenated($field, $value="", $lead_id=0, $form_id=0) { |
| 671 |
$id = $field["id"]; |
| 672 |
$field_id = IS_ADMIN || $form_id == 0 ? "input_{$id}" : "input_{$form_id}_{$id}"; |
| 673 |
$form_id = IS_ADMIN && empty($form_id) ? rgget("id") : $form_id; |
| 674 |
|
| 675 |
$input = "<input type='hidden' name='input_{$id}' id='$field_id' />"; |
| 676 |
|
| 677 |
return $input; |
| 678 |
} |
| 679 |
|
| 680 |
/** |
| 681 |
* safe checkdate function that verifies each component as numeric and not empty, before calling PHP's function |
| 682 |
* @param string $month |
| 683 |
* @param string $day |
| 684 |
* @param string $year |
| 685 |
* @return boolean |
| 686 |
*/ |
| 687 |
protected static function checkdate($month, $day, $year) { |
| 688 |
if (empty($month) || !is_numeric($month) || empty($day) || !is_numeric($day) || empty($year) || !is_numeric($year) || strlen($year) != 4) |
| 689 |
return false; |
| 690 |
|
| 691 |
return checkdate($month, $day, $year); |
| 692 |
} |
| 693 |
|
| 694 |
/** |
| 695 |
* get input values for recurring payments field |
| 696 |
* @param integer $field_id |
| 697 |
* @return array |
| 698 |
*/ |
| 699 |
public static function getPost($field_id) { |
| 700 |
$recurring = rgpost('gfeway_' . $field_id); |
| 701 |
|
| 702 |
//~ echo "<pre>'gfeway_$field_id: ", print_r($recurring,1), "</pre>\n"; exit; |
| 703 |
|
| 704 |
if (is_array($recurring)) { |
| 705 |
$intervalSize = 1; |
| 706 |
|
| 707 |
switch ($recurring[6]) { |
| 708 |
case 'weekly': |
| 709 |
$intervalType = GFEwayRecurringPayment::WEEKS; |
| 710 |
break; |
| 711 |
|
| 712 |
case 'fortnightly': |
| 713 |
$intervalType = GFEwayRecurringPayment::WEEKS; |
| 714 |
$intervalSize = 2; |
| 715 |
break; |
| 716 |
|
| 717 |
case 'monthly': |
| 718 |
$intervalType = GFEwayRecurringPayment::MONTHS; |
| 719 |
break; |
| 720 |
|
| 721 |
case 'quarterly': |
| 722 |
$intervalType = GFEwayRecurringPayment::MONTHS; |
| 723 |
$intervalSize = 3; |
| 724 |
break; |
| 725 |
|
| 726 |
case 'yearly': |
| 727 |
$intervalType = GFEwayRecurringPayment::YEARS; |
| 728 |
break; |
| 729 |
|
| 730 |
default: |
| 731 |
// invalid or not selected |
| 732 |
$intervalType = -1; |
| 733 |
break; |
| 734 |
} |
| 735 |
|
| 736 |
$recurring = array ( |
| 737 |
'amountInit' => GFCommon::to_number($recurring[1]), |
| 738 |
'dateInit' => self::parseDate($recurring[2]), |
| 739 |
'amountRecur' => GFCommon::to_number($recurring[3]), |
| 740 |
'dateStart' => self::parseDate($recurring[4]), |
| 741 |
'dateEnd' => self::parseDate($recurring[5]), |
| 742 |
'intervalSize' => $intervalSize, |
| 743 |
'intervalType' => $intervalType, |
| 744 |
'intervalTypeDesc' => $recurring[6], |
| 745 |
); |
| 746 |
} |
| 747 |
else { |
| 748 |
$recurring = false; |
| 749 |
} |
| 750 |
|
| 751 |
return $recurring; |
| 752 |
} |
| 753 |
|
| 754 |
/** |
| 755 |
* no date_create_from_format before PHP 5.3, so roll-your-own |
| 756 |
* @param string $value date value in dd/mm/yyyy format |
| 757 |
* @return DateTime |
| 758 |
*/ |
| 759 |
protected static function parseDate($value) { |
| 760 |
if (preg_match('#(\d{1,2})/(\d{1,2})/(\d{4})#', $value, $matches)) { |
| 761 |
$date = date_create(); |
| 762 |
$date->setDate($matches[3], $matches[2], $matches[1]); |
| 763 |
return $date; |
| 764 |
} |
| 765 |
|
| 766 |
return false; |
| 767 |
} |
| 768 |
} |
| 769 |
|