Caldera_Forms_Field_JS

Prepares fieldjs config.

Defined (1)

The class is defined in the following location(s).

/classes/field/js.php  
  1. class Caldera_Forms_Field_JS implements JsonSerializable { 
  2.  
  3. /** 
  4. * Form config 
  5. * @since 1.5.0 
  6. * @var array 
  7. */ 
  8. protected $form; 
  9.  
  10. /** 
  11. * Prepared field data 
  12. * @since 1.5.0 
  13. * @var array 
  14. */ 
  15. protected $data; 
  16.  
  17. /** 
  18. * Form instance count 
  19. * @since 1.5.0 
  20. * @var int 
  21. */ 
  22. protected $form_count; 
  23.  
  24. /** 
  25. * Caldera_Forms_Render_FieldsJS constructor. 
  26. * @since 1.5.0 
  27. * @param array $form Form config 
  28. * @param int $form_count Form instance count 
  29. */ 
  30. public function __construct( array $form, $form_count ) { 
  31. $this->form = $form; 
  32. $this->form_count = $form_count; 
  33. $this->data = array(); 
  34.  
  35. /** 
  36. * Prepare data for each field 
  37. * @since 1.5.0 
  38. */ 
  39. public function prepare_data() { 
  40.  
  41. if( ! empty( $this->form[ 'fields' ] ) ) { 
  42. foreach( $this->form[ 'fields' ] as $field ) { 
  43. $type = Caldera_Forms_Field_Util::get_type( $field, $this->form ); 
  44. if( 'summary' == $type ) { 
  45. $type = 'html'; 
  46. //skip these types -- maybe add filter here later 
  47. $skip = array( 
  48. 'calculation',  
  49. 'star_rating',  
  50. ); 
  51. if( ! in_array( $type, $skip ) && method_exists( $this, $type ) ) { 
  52. call_user_func( array( $this, $type ), $field[ 'ID' ], $field ); 
  53.  
  54. /** 
  55. * @inheritdoc 
  56. */ 
  57. public function jsonSerialize() { 
  58. return $this->to_array(); 
  59.  
  60. /** 
  61. * Get array representation of this object 
  62. * @since 1.5.0 
  63. * @return array 
  64. */ 
  65. public function to_array() { 
  66. if( empty( $this->data ) ) { 
  67. $this->prepare_data(); 
  68.  
  69. return $this->get_data(); 
  70.  
  71. /** 
  72. * Get prepared data 
  73. * @since 1.5.0 
  74. * @return array 
  75. */ 
  76. public function get_data() { 
  77. return $this->data; 
  78.  
  79. /** 
  80. * Callback for processing button data 
  81. * @since 1.5.0 
  82. * @param string $field_id Field ID 
  83. */ 
  84. protected function button( $field_id ) { 
  85. $this->data[ $field_id ] = $this->create_config_array( $field_id, __FUNCTION__, array() ); 
  86.  
  87. protected function wysiwyg( $field_id ) { 
  88.  
  89.  
  90. $this->data[ $field_id ] = $this->create_config_array( $field_id, __FUNCTION__, array( 
  91. 'options' => $this->wysiqyg_options( $field_id ) 
  92. ) ); 
  93.  
  94.  
  95.  
  96. /** 
  97. * @param $field_id 
  98. * @return string 
  99. */ 
  100. protected function field_id( $field_id ) { 
  101. return Caldera_Forms_Field_Util::get_base_id( $field_id, $this->form_count, $this->form ); 
  102.  
  103. /** 
  104. * @param $field_id 
  105. * @return mixed|void 
  106. */ 
  107. protected function wysiqyg_options( $field_id ) { 
  108. $field = $this->form[ 'fields' ][ $field_id ]; 
  109. $options = array(); 
  110. if( ! empty( $field[ 'config' ]['language' ] ) ) { 
  111. $options[ 'lang' ] = strip_tags( $field[ 'config' ]['language' ] ); 
  112.  
  113. /** 
  114. * Filter options passed to Trumbowyg when initializing the WYSIWYG editor 
  115. * @since 1.5.0 
  116. * @see https://alex-d.github.io/Trumbowyg/documentation.html#general 
  117. * @param array $options Options will be empty unless language was set in UI 
  118. * @param array $field Field config 
  119. * @param array $form Form Config 
  120. */ 
  121. $options = apply_filters( 'caldera_forms_wysiwyg_options', $options, $field, $this->form ); 
  122.  
  123. return $options; 
  124.  
  125. /** 
  126. * Setup better_phone fields 
  127. * @since 1.5.0 
  128. * @param string $field_id Id of field 
  129. * @param array $field Field config 
  130. * @return void 
  131. */ 
  132. protected function phone_better( $field_id, $field ) { 
  133. $args = array( 
  134. 'options' => $this->better_phone_field_js_options( $field ),  
  135. 'messages' => array( 
  136. 'generic' => __( 'Invalid number', 'caldera-forms' ),  
  137. '1' => __( 'Invalid country code', 'caldera-forms' ),  
  138. '4' => __( 'Not a number', 'caldera-forms' ),  
  139. '3' => __( 'Number is too long', 'caldera-forms' ),  
  140. '2' => __( 'Number is too short', 'calera-forms' ) 
  141.  
  142. ); 
  143.  
  144. $this->data[ $field_id ] = $this->create_config_array( $field_id, __FUNCTION__, $args ); 
  145.  
  146.  
  147.  
  148.  
  149. /** 
  150. * Prepare options for better phone fields 
  151. * @since 1.5.0 
  152. * @param array $field Field config 
  153. * @return array 
  154. */ 
  155. protected function better_phone_field_js_options( array $field ) { 
  156. $options = array( 
  157. 'initialCountry' => 'us',  
  158. 'autoHideDialCode' => false,  
  159. 'utilsScript' => CFCORE_URL . 'fields/phone_better/assets/js/utils.js',  
  160. 'preferredCountries' => array( 'us' ) 
  161. ); 
  162.  
  163. if( empty( $field[ 'config' ][ 'nationalMode' ] ) ) { 
  164. $options[ 'nationalMode' ] = true; 
  165. $options[ 'autoHideDialCode' ] = true; 
  166. }else{ 
  167. $options[ 'nationalMode' ] = false; 
  168. $options[ 'autoHideDialCode' ] = false; 
  169.  
  170. /** 
  171. * Filter config options for better phone fields. 
  172. * This will be passed in JavaScript to constructor for the jQuery plugin that powers these fields. 
  173. * @see https://github.com/jackocnr/intl-tel-input 
  174. * @since 1.5.0 
  175. * @param array $options Options to use for this field 
  176. * @param array $field Field config 
  177. * @param array $form config 
  178. */ 
  179. return apply_filters( 'caldera_forms_phone_js_options', $options, $field, $this->form ); 
  180.  
  181.  
  182. /** 
  183. * Setup HTML fields 
  184. * @since 1.5.0 
  185. * @param string $field_id Id of field 
  186. * @param array $field Field config 
  187. * @return void 
  188. */ 
  189. protected function html( $field_id, $field ) { 
  190. $id_attr = $this->field_id( $field_id ); 
  191.  
  192. $this->data[ $field_id ] = $this->create_config_array( $field_id, Caldera_Forms_Field_Util::get_type( $field ), array( 
  193. 'sync' => false 
  194. ) ); 
  195.  
  196. /** @var Caldera_Forms_Sync_HTML $syncer */ 
  197. $syncer = Caldera_Forms_Sync_Factory::get_object( $this->form, $field, $id_attr ); 
  198.  
  199. if ( $syncer->can_sync() ) { 
  200. $this->data[ $field_id ] = array_merge( $this->data[ $field_id ], array( 
  201. 'binds' => $syncer->get_binds(),  
  202. 'sync' => true,  
  203. 'tmplId' => $syncer->template_id(),  
  204. 'contentId' => $syncer->content_id(),  
  205. 'bindFields' => array(),  
  206. ) ); 
  207.  
  208. foreach ( $syncer->get_binds() as $bind ) { 
  209. $this->data[ $field_id ][ 'bindFields' ][] = $bind . '_' . $this->form_count; 
  210.  
  211.  
  212. /** 
  213. * Setup range slider fields 
  214. * @since 1.5.0 
  215. * @param string $field_id Id of field 
  216. * @param array $field Field config 
  217. * @return void 
  218. */ 
  219. public function range_slider( $field_id, $field ) { 
  220.  
  221.  
  222. $this->data[ $field_id ] = $this->create_config_array( $field_id, __FUNCTION__, array( 
  223. 'value' => 0,  
  224. ) ); 
  225.  
  226. foreach( array( 
  227. 'handleborder',  
  228. 'trackcolor',  
  229. 'color',  
  230. 'handle',  
  231. ) as $setting ) { 
  232. if( isset( $field[ 'config'][ $setting ] ) ) { 
  233. $value = $field[ 'config'][ $setting ]; 
  234. }else{ 
  235. $value = ''; 
  236.  
  237. $this->data[ $field_id ][ $setting ] = $value; 
  238.  
  239. if( false !== strpos( $field['config']['step'], '.' ) ) { 
  240. $part = explode( '.', $field[ 'config' ][ 'step' ] ); 
  241. $this->data[ $field_id ][ 'value' ] = strlen( $part[1] ); 
  242.  
  243.  
  244.  
  245.  
  246. /** 
  247. * Setup star rate fields 
  248. * @since 1.5.0 
  249. * @param string $field_id Id of field 
  250. * @param array $field Field config 
  251. * @return void 
  252. */ 
  253. protected function star_rating( $field_id, $field ) { 
  254. $type = $field['config']['type']; 
  255. if ( ! isset( $field[ 'config' ][ 'track_color' ] ) ) { 
  256. $field[ 'config' ][ 'track_color' ] = '#AFAFAF'; 
  257. if ( ! isset( $field[ 'config' ][ 'type' ] ) ) { 
  258. $field[ 'config' ][ 'type' ] = 'star'; 
  259.  
  260. $args = array( 
  261. 'starFieldId' => Caldera_Forms_Field_Util::star_target( Caldera_Forms_Field_Util::get_base_id( $field, $this->form_count, $this->form ) ),  
  262. 'options' => array( 
  263. 'starOn' => 'raty-'. $type . '-on',  
  264. 'starOff' => 'raty-'. $type . '-off',  
  265. 'spaceWidth' => $field['config']['space'],  
  266. 'number' => $field['config']['number'],  
  267. 'color' => $field['config']['color'],  
  268. 'cancel' => false,  
  269. 'single' => false,  
  270. 'targetScore' => '#' . $this->field_id( $field_id ),  
  271. 'targetKeep' => true,  
  272. 'targetType' => 'score',  
  273. 'score' => 0,  
  274. 'hints' => array( 1, 2, 3, 4, 5),  
  275. 'starType' => 'f',  
  276. 'starColor' => $field['config']['color'],  
  277. 'numberMax' => 100,  
  278. ); 
  279.  
  280. if( ! empty( $field[ 'config' ][ 'default' ] ) && is_numeric( $field[ 'config' ][ 'default' ] ) ) { 
  281. $args[ 'options' ][ 'score' ] = $field[ 'config' ][ 'default' ]; 
  282.  
  283. $this->data[ $field_id ] = $this->create_config_array( $field_id, __FUNCTION__, $args ); 
  284.  
  285. if(!empty($field['config']['cancel']) ) { 
  286. $this->data[ $field_id ][ 'options' ][ 'cancel' ] = true; 
  287.  
  288. if( !empty($field['config']['single'] ) ) { 
  289. $this->data[ $field_id ][ 'options' ][ 'single' ] = true; 
  290.  
  291. protected function toggle_switch( $field_id, $field ) { 
  292. $selectedClassName = 'btn-success'; 
  293. if ( ! empty( $field[ 'config' ][ 'selected_class' ] ) ) { 
  294. $selectedClassName = $field[ 'config' ][ 'selected_class' ]; 
  295.  
  296. $defaultClassName = 'btn-default'; 
  297. if ( ! empty( $field[ 'config' ][ 'default_class' ] ) ) { 
  298. $defaultClassName = $field[ 'config' ][ 'default_class' ]; 
  299.  
  300. $options = array(); 
  301. foreach ( $field[ 'config' ][ 'option' ] as $option_key => $option ) { 
  302. $options[] = $this->field_id( $field_id ) . '_' . $option_key; 
  303.  
  304. $args = array( 
  305. 'selectedClassName' => $selectedClassName,  
  306. 'defaultClassName' => $defaultClassName,  
  307. 'options' => $options 
  308. ); 
  309.  
  310. $this->data[ $field_id ] = $this->create_config_array( $field_id, __FUNCTION__, $args ); 
  311.  
  312.  
  313. /** 
  314. * For calculation fields 
  315. * NOTE: NOT USED AS OF 1.5 
  316. * @since 1.5.0 
  317. * @param $field_id 
  318. * @param $field 
  319. */ 
  320. protected function calculation( $field_id, $field ) { 
  321. if( !isset( $field['config']['thousand_separator'] ) ) { 
  322. $field['config']['thousand_separator'] = ', '; 
  323.  
  324. if( !isset( $field['config']['decimal_separator'] ) ) { 
  325. $field['config']['decimal_separator'] = '.'; 
  326.  
  327. $thousand_separator = $field['config']['thousand_separator']; 
  328. $decimal_separator = $field['config']['decimal_separator']; 
  329. /** @var Caldera_Forms_Sync_Calc $syncer */ 
  330. $syncer = Caldera_Forms_Sync_Factory::get_object( $this->form, $field, Caldera_Forms_Field_Util::get_base_id( $field, null, $this->form ) ); 
  331.  
  332. //this creates binds array BTW 
  333. $syncer->can_sync(); 
  334. $formula = $syncer->get_formula( true ); 
  335. $args = array( 
  336. 'formula' => $formula,  
  337. 'binds' => $syncer->get_binds(),  
  338. 'decimalSeparator' => $decimal_separator,  
  339. 'thousandSeparator' => $thousand_separator,  
  340. 'fixed' => false,  
  341. 'fieldBinds' => $syncer->get_bind_fields(),  
  342. ); 
  343.  
  344. $this->data[ $field_id ] = $this->create_config_array( $field_id, __FUNCTION__, $args ); 
  345.  
  346. if(!empty($field['config']['fixed'])) { 
  347. $this->data[ $field_id ][ 'fixed' ] = true; 
  348.  
  349. /** 
  350. * Config for credit card number fields 
  351. * @since 1.5.0 
  352. * @param string $field_id The field ID 
  353. * @param array $field The field config 
  354. */ 
  355. protected function credit_card_number( $field_id, $field ) { 
  356.  
  357. $this->data[ $field_id ] = $this->create_config_array( $field_id, __FUNCTION__, array( 
  358. 'invalid' => __( 'Invalid Card Number', 'caldera-forms' ),  
  359. 'cvc' => false,  
  360. 'exp' => false,  
  361. ) ); 
  362.  
  363. if( Caldera_Forms_Render_Assets::should_minify( false ) ) { 
  364. $this->data[ $field_id ][ 'imgPath' ] = CFCORE_URL . 'assets/build/images/'; 
  365. }else{ 
  366. $this->data[ $field_id ][ 'imgPath' ] = CFCORE_URL . 'assets/images/'; 
  367.  
  368. if( ! empty( $field[ 'config' ][ 'cvc'] ) ) { 
  369. $this->data[ $field_id ][ 'cvc' ] = $this->field_id( $field[ 'config' ][ 'cvc' ] ); 
  370.  
  371. if( ! empty( $field[ 'config' ][ 'exp'] ) ) { 
  372. $this->data[ $field_id ][ 'exp' ] = $this->field_id( $field[ 'config' ][ 'exp' ] ); 
  373.  
  374.  
  375. /** 
  376. * Config for credit card expiration fields 
  377. * @since 1.5.0 
  378. * @param string $field_id The field ID 
  379. * @param array $field The field config 
  380. */ 
  381. protected function credit_card_exp( $field_id, $field ) { 
  382.  
  383. $this->data[ $field_id ] = $this->create_config_array( $field_id, __FUNCTION__, array( 
  384. 'invalid' => __( 'Invalid Expiration Date', 'caldera-forms' ),  
  385. 'ccField' => false 
  386. ) ); 
  387.  
  388. if( ! empty( $field[ 'config' ][ 'credit_card_field' ] ) ) { 
  389. $this->data[ $field_id ][ 'ccField' ] = $this->field_id( $field[ 'config' ][ 'credit_card_field' ] ); 
  390.  
  391.  
  392.  
  393. /** 
  394. * Config for credit card cvv fields 
  395. * @since 1.5.0 
  396. * @param string $field_id The field ID 
  397. * @param array $field The field config 
  398. */ 
  399. protected function credit_card_cvc( $field_id, $field ) { 
  400.  
  401. $this->data[ $field_id ] = $this->create_config_array( $field_id, __FUNCTION__, array( 
  402. 'invalid' => __( 'Invalid Security Code', 'caldera-forms' ),  
  403. 'ccField' => false 
  404. ) ); 
  405.  
  406. if( ! empty( $field[ 'config' ][ 'credit_card_field' ] ) ) { 
  407. $this->data[ $field_id ][ 'ccField' ] = $this->field_id( $field[ 'config' ][ 'credit_card_field' ] ); 
  408.  
  409.  
  410.  
  411. /** 
  412. * Config for color picker fields 
  413. * @since 1.5.0 
  414. * @param string $field_id The field ID 
  415. * @param array $field The field config 
  416. */ 
  417. public function color_picker( $field_id, $field ) { 
  418. /** 
  419. * Change settings for color picker fields passed to JavaScript 
  420. * @since 1.5.0 
  421. * @see http://labs.abeautifulsite.net/jquery-minicolors/#settings 
  422. * @param array $settings The settings 
  423. * @param array $field Field config 
  424. * @param array $form Form config 
  425. */ 
  426. $settings = apply_filters( 'caldera_forms_field_js_color_picker_settings', array( 
  427. 'animationSpeed' => 50,  
  428. 'animationEasing' => 'swing',  
  429. 'change' => null,  
  430. 'changeDelay' => 0,  
  431. 'control' => 'hue',  
  432. 'dataUris' =>true,  
  433. 'defaultValue' => '',  
  434. 'format' => 'hex',  
  435. 'hide' => null,  
  436. 'hideSpeed' => 100,  
  437. 'inline' => false,  
  438. 'keywords' => '',  
  439. 'letterCase' => 'lowercase',  
  440. 'opacity' => false,  
  441. 'position' => 'bottom left',  
  442. 'show' => null,  
  443. 'showSpeed' => 100,  
  444. 'theme' => 'default',  
  445. 'swatches' => array() 
  446. ), $field, $this->form ); 
  447.  
  448. $this->data[ $field_id ] = $this->create_config_array( $field_id, __FUNCTION__, array( 
  449. 'settings' => $settings 
  450. ) ); 
  451.  
  452.  
  453.  
  454. /** 
  455. * Create config array 
  456. * @since 1.5.0 
  457. * @param string $field_id Field ID 
  458. * @param string $type Field type 
  459. * @param array $args Additional data. 
  460. * @return array 
  461. */ 
  462. protected function create_config_array( $field_id, $type, $args ) { 
  463. $basic = array( 
  464. 'type' => $type,  
  465. 'id' => $this->field_id( $field_id ),  
  466. 'default' => Caldera_Forms_Field_Util::get_default( $field_id, $this->form ) 
  467. ); 
  468.  
  469.  
  470. return array_merge( $basic, wp_parse_args( $args, $this->default_config_args() ) ); 
  471.  
  472. protected function default_config_args() { 
  473. /** 
  474. * Default values passed to field configs to be printed in DOM for field types 
  475. * Useful for customizing field setups in bulk 
  476. * @since 1.5.0 
  477. * @param array $args 
  478. */ 
  479. return apply_filters( 'caldera_forms_field_js_config_defaults', array( 
  480. 'form_id' => $this->form[ 'ID' ],  
  481. 'form_id_attr' => Caldera_Forms_Render_Util::form_id_attr( $this->form_count ) 
  482.  
  483. ));