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