list.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. function ninja_forms_register_field_list(){
  3. $args = array(
  4. 'name' => __( 'List', 'ninja-forms' ),
  5. 'edit_function' => 'ninja_forms_field_list_edit',
  6. 'edit_options' => array(
  7. array(
  8. 'type' => 'hidden',
  9. 'name' => 'user_info_field_group',
  10. ),
  11. ),
  12. 'edit_settings' => array(
  13. 'advanced' => array(
  14. array(
  15. 'type' => 'checkbox',
  16. 'name' => 'user_state',
  17. 'label' => __( 'This is the user\'s state', 'ninja-forms' ),
  18. ),
  19. ),
  20. ),
  21. 'display_function' => 'ninja_forms_field_list_display',
  22. 'group' => 'standard_fields',
  23. 'edit_label' => true,
  24. 'edit_label_pos' => true,
  25. 'edit_req' => true,
  26. 'edit_custom_class' => true,
  27. 'edit_help' => true,
  28. 'edit_desc' => true,
  29. 'edit_meta' => false,
  30. 'sidebar' => 'template_fields',
  31. 'edit_conditional' => true,
  32. 'conditional' => array(
  33. 'action' => array(
  34. 'show' => array(
  35. 'name' => __( 'Show This', 'ninja-forms' ),
  36. 'js_function' => 'show',
  37. 'output' => 'hide',
  38. ),
  39. 'hide' => array(
  40. 'name' => __( 'Hide This', 'ninja-forms' ),
  41. 'js_function' => 'hide',
  42. 'output' => 'hide',
  43. ),
  44. 'change_value' => array(
  45. 'name' => __( 'Selected Value', 'ninja-forms' ),
  46. 'js_function' => 'change_value',
  47. 'output' => 'list',
  48. ),
  49. 'add_value' => array(
  50. 'name' => __( 'Add Value', 'ninja-forms' ),
  51. 'js_function' => 'add_value',
  52. 'output' => 'ninja_forms_field_list_add_value',
  53. ),
  54. 'remove_value' => array(
  55. 'name' => __( 'Remove Value', 'ninja-forms' ),
  56. 'js_function' => 'remove_value',
  57. 'output' => 'list',
  58. ),
  59. ),
  60. 'value' => array(
  61. 'type' => 'list',
  62. ),
  63. ),
  64. 'edit_sub_value' => 'nf_field_list_edit_sub_value',
  65. 'sub_table_value' => 'nf_field_list_sub_table_value',
  66. );
  67. ninja_forms_register_field('_list', $args);
  68. add_filter( 'ninja_forms_field_wrap_class', 'ninja_forms_field_filter_list_wrap_class', 10, 2 );
  69. add_action('ninja_forms_display_after_opening_field_wrap', 'ninja_forms_display_list_type', 10, 2);
  70. }
  71. add_action('init', 'ninja_forms_register_field_list');
  72. function ninja_forms_display_list_type( $field_id, $data ){
  73. $field_row = ninja_forms_get_field_by_id( $field_id );
  74. $field_type = $field_row['type'];
  75. if( $field_type == '_list' ){
  76. if ( isset( $data['list_type'] ) ){
  77. $list_type = $data['list_type'];
  78. } else{
  79. $list_type = '';
  80. }
  81. ?>
  82. <input type="hidden" id="ninja_forms_field_<?php echo $field_id;?>_list_type" value="<?php echo $list_type;?>">
  83. <?php
  84. }
  85. }
  86. function ninja_forms_field_list_add_value( $field_id, $x, $conditional, $name, $id, $current = '', $field_data = '' ){
  87. $current_value = isset( $current['value']['value'] ) ? $current['value']['value'] : '';
  88. $current_label = isset( $current['value']['label'] ) ? $current['value']['label'] : '';
  89. $current_calc = isset ( $current['value']['calc'] ) ? $current['value']['calc'] : '';
  90. $list_show_value = isset( $field_data['list_show_value'] ) ? $field_data['list_show_value'] : '';
  91. _e( 'Label', 'ninja-forms' );
  92. ?>
  93. <input type="text" name="<?php echo $name;?>[label]" id="<?php echo $id;?>" class="" value="<?php echo $current_label;?>">
  94. <?php
  95. if( $list_show_value == 1 ){
  96. _e( 'Value', 'ninja-forms' );
  97. ?>
  98. <input type="text" name="<?php echo $name;?>[value]" id="<?php echo $id;?>" class="ninja-forms-field-<?php echo $field_id;?>-list-option-value" value="<?php echo $current_value;?>">
  99. <?php
  100. }else{
  101. ?>
  102. <input type="hidden" name="<?php echo $name;?>[value]" value="_ninja_forms_no_value">
  103. <?php
  104. }
  105. _e( 'Calc', 'ninja-forms' );
  106. ?>
  107. <input type="text" name="<?php echo $name;?>[calc]" id="<?php echo $id;?>" class="" value="<?php echo $current_calc;?>">
  108. <?php
  109. }
  110. function ninja_forms_field_list_edit( $field_id, $data ) {
  111. global $wpdb;
  112. $list_type = isset( $data['list_type'] ) ? $data['list_type'] : '';
  113. $hidden = isset( $data['list_show_value'] ) ? $data['list_show_value'] : 0;
  114. $multi_size = isset( $data['multi_size'] ) ? $data['multi_size'] : 5;
  115. $default_options = array(
  116. array( 'label' => 'Option 1', 'value' => '', 'calc' => '', 'selected' => 0 ),
  117. array( 'label' => 'Option 2', 'value' => '', 'calc' => '', 'selected' => 0 ),
  118. array( 'label' => 'Option 3', 'value' => '', 'calc' => '', 'selected' => 0 ),
  119. );
  120. $list_options = isset ( $data['list']['options'] ) ? $data['list']['options'] : $default_options;
  121. $list_type_options = array(
  122. array('name' => __( 'Dropdown', 'ninja-forms' ), 'value' => 'dropdown'),
  123. array('name' => __( 'Radio', 'ninja-forms' ), 'value' => 'radio'),
  124. array('name' => __( 'Checkboxes', 'ninja-forms' ), 'value' => 'checkbox'),
  125. array('name' => __( 'Multi-Select', 'ninja-forms' ), 'value' => 'multi'),
  126. );
  127. ninja_forms_edit_field_el_output( $field_id, 'select', __( 'List Type', 'ninja-forms' ), 'list_type', $list_type, 'wide', $list_type_options, 'widefat' );
  128. ?>
  129. <p id="ninja_forms_field_<?php echo $field_id;?>_multi_size_p" class="description description-wide" style="<?php if($list_type != 'multi'){ echo 'display:none;';}?>">
  130. <?php _e( 'Multi-Select Box Size', 'ninja-forms' );?>: <input type="text" id="" name="ninja_forms_field_<?php echo $field_id;?>[multi_size]" value="<?php echo $multi_size;?>">
  131. </p>
  132. <span id="ninja_forms_field_<?php echo $field_id;?>_list_span" class="ninja-forms-list-span">
  133. <!-- <p class="description description-wide"> -->
  134. <a href="#" id="ninja_forms_field_<?php echo $field_id;?>_list_add_option" class="ninja-forms-field-add-list-option button-secondary"><?php _e( 'Add New', 'ninja-forms' );?></a>
  135. <a href="#TB_inline?width=640&height=530&inlineId=ninja_forms_field_<?php echo $field_id;?>_import_options_div" class="thickbox button-secondary" title="<?php _e( 'Import List Items', 'ninja-forms' ); ?>" id=""><?php _e( 'Import List Items', 'ninja-forms' );?></a>
  136. <!-- </p> -->
  137. <p class="description description-wide">
  138. <input type="hidden" id="" name="ninja_forms_field_<?php echo $field_id;?>[list_show_value]" value="0">
  139. <label for="ninja_forms_field_<?php echo $field_id;?>_list_show_value"><input type="checkbox" value="1" id="ninja_forms_field_<?php echo $field_id;?>_list_show_value" name="ninja_forms_field_<?php echo $field_id;?>[list_show_value]" class="ninja-forms-field-list-show-value" <?php if(isset($data['list_show_value']) AND $data['list_show_value'] == 1){ echo "checked='checked'";}?>>
  140. <?php _e( 'Show list item values', 'ninja-forms' );?> </label>
  141. </p>
  142. <div id="ninja_forms_field_<?php echo $field_id;?>_list_options" class="ninja-forms-field-list-options description description-wide">
  143. <input type="hidden" name="ninja_forms_field_<?php echo $field_id;?>[list][options]" value="">
  144. <?php
  145. if( isset( $list_options ) AND is_array( $list_options ) AND $list_options != '' ){
  146. $x = 0;
  147. foreach( $list_options as $option ) {
  148. ninja_forms_field_list_option_output( $field_id, $x, $option, $hidden );
  149. $x++;
  150. }
  151. }
  152. ?>
  153. </div>
  154. </span>
  155. <?php add_thickbox(); ?>
  156. <div id="ninja_forms_field_<?php echo $field_id;?>_import_options_div" style="display:none;">
  157. <textarea id="test" class="list-import-textarea"></textarea>
  158. <input type="button" class="save-list-import button-secondary" value="<?php _e( 'Import', 'ninja-forms' ); ?>" rel="<?php echo $field_id;?>">
  159. <input type="button" class="cancel-list-import button-secondary" value="<?php _e( 'Cancel', 'ninja-forms' ); ?>">
  160. <p><?php _e( 'To use this feature, you can paste your CSV into the textarea above.', 'ninja-forms' );?></p>
  161. <p><?php _e( 'The format should look like the following:', 'ninja-forms' );?></p>
  162. <pre>
  163. <?php
  164. $example1 = _x( 'Label,Value,Calc', 'Example for list importing. Leave puncation in place.', 'ninja-forms' );
  165. echo $example1;
  166. echo '<br />';
  167. echo $example1;
  168. echo '<br />';
  169. echo $example1;
  170. ?>
  171. </pre>
  172. <p><?php _e( "If you want to send an empty value or calc, you should use '' for those.", 'ninja-forms' );?></p>
  173. <pre>
  174. <?php
  175. $example2 = __( 'Label', 'ninja-forms' ) . ",'',''";
  176. echo $example2;
  177. echo '<br />';
  178. echo $example2;
  179. echo '<br />';
  180. echo $example2;
  181. ?>
  182. </pre>
  183. </div>
  184. <?php
  185. }
  186. function ninja_forms_field_list_display( $field_id, $data, $form_id = '' ){
  187. global $wpdb, $ninja_forms_fields;
  188. if(isset($data['show_field'])){
  189. $show_field = $data['show_field'];
  190. }else{
  191. $show_field = true;
  192. }
  193. $field_class = ninja_forms_get_field_class( $field_id, $form_id );
  194. $field_row = ninja_forms_get_field_by_id($field_id);
  195. $type = $field_row['type'];
  196. $type_name = $ninja_forms_fields[$type]['name'];
  197. if ( isset( $data['list_type'] ) ) {
  198. $list_type = $data['list_type'];
  199. } else {
  200. $list_type = '';
  201. }
  202. if(isset($data['list_show_value'])){
  203. $list_show_value = $data['list_show_value'];
  204. }else{
  205. $list_show_value = 0;
  206. }
  207. if( isset( $data['list']['options'] ) AND $data['list']['options'] != '' ){
  208. $options = $data['list']['options'];
  209. }else{
  210. $options = array();
  211. }
  212. if(isset($data['label_pos'])){
  213. $label_pos = $data['label_pos'];
  214. }else{
  215. $label_pos = 'left';
  216. }
  217. if(isset($data['label'])){
  218. $label = $data['label'];
  219. }else{
  220. $label = $type_name;
  221. }
  222. if( isset( $data['multi_size'] ) ){
  223. $multi_size = $data['multi_size'];
  224. }else{
  225. $multi_size = 5;
  226. }
  227. if( isset( $data['default_value'] ) AND !empty( $data['default_value'] ) ){
  228. $selected_value = $data['default_value'];
  229. }else{
  230. $selected_value = '';
  231. }
  232. $list_options_span_class = apply_filters( 'ninja_forms_display_list_options_span_class', '', $field_id );
  233. switch($list_type){
  234. case 'dropdown':
  235. echo apply_filters( 'ninja_forms_dropdown_open_tag', '<select name="ninja_forms_field_'.$field_id.'" id="ninja_forms_field_'.$field_id.'" class="'.$field_class.'" rel="'.$field_id.'">', $field_id, $field_class, $data, $ninja_forms_fields, $options );
  236. ?>
  237. <?php
  238. $select_placeholder = '';
  239. if($label_pos == 'inside'){
  240. $select_placeholder = '<option value="">'.$label.'</option>';
  241. }
  242. echo apply_filters( 'ninja_forms_dropdown_placeholder', $select_placeholder, $label, $label_pos, $field_id, $field_class, $data, $ninja_forms_fields, $options );
  243. foreach($options as $option){
  244. if(isset($option['value'])){
  245. $value = $option['value'];
  246. }else{
  247. $value = $option['label'];
  248. }
  249. $value = htmlspecialchars( $value, ENT_QUOTES );
  250. if(isset($option['label'])){
  251. $label = $option['label'];
  252. }else{
  253. $label = '';
  254. }
  255. if(isset($option['display_style'])){
  256. $display_style = $option['display_style'];
  257. }else{
  258. $display_style = '';
  259. }
  260. if ( isset( $option['disabled'] ) AND $option['disabled'] ){
  261. $disabled = 'disabled';
  262. }else{
  263. $disabled = '';
  264. }
  265. $label = htmlspecialchars( $label, ENT_QUOTES );
  266. $label = stripslashes( $label );
  267. $label = str_replace( '&amp;', '&', $label );
  268. $field_label = $data['label'];
  269. if($list_show_value == 0){
  270. $value = $label;
  271. }
  272. if ( $selected_value == $value OR ( is_array( $selected_value ) AND in_array( $value, $selected_value ) ) ) {
  273. $selected = 'selected';
  274. }else if( ( $selected_value == '' OR $selected_value == $field_label ) AND isset( $option['selected'] ) AND $option['selected'] == 1 ){
  275. $selected = 'selected';
  276. }else{
  277. $selected = '';
  278. }
  279. ?>
  280. <option value="<?php echo $value;?>" <?php echo $selected;?> style="<?php echo $display_style;?>" <?php echo $disabled;?>><?php echo $label;?></option>
  281. <?php
  282. }
  283. ?>
  284. </select>
  285. <?php
  286. break;
  287. case 'radio':
  288. $x = 0;
  289. if( $label_pos == 'left' OR $label_pos == 'above' ){
  290. ?><?php
  291. }
  292. ?><input type="hidden" name="ninja_forms_field_<?php echo $field_id;?>" value=""><span id="ninja_forms_field_<?php echo $field_id;?>_options_span" class="<?php echo $list_options_span_class;?>" rel="<?php echo $field_id;?>"><ul><?php
  293. foreach($options as $option){
  294. if(isset($option['value'])){
  295. $value = $option['value'];
  296. }else{
  297. $value = $option['label'];
  298. }
  299. $value = htmlspecialchars( $value, ENT_QUOTES );
  300. if(isset($option['label'])){
  301. $label = $option['label'];
  302. }else{
  303. $label = '';
  304. }
  305. if(isset($option['display_style'])){
  306. $display_style = $option['display_style'];
  307. }else{
  308. $display_style = '';
  309. }
  310. //$label = htmlspecialchars( $label, ENT_QUOTES );
  311. $label = stripslashes($label);
  312. if($list_show_value == 0){
  313. $value = $label;
  314. }
  315. if ( $selected_value == $value OR ( is_array( $selected_value ) AND in_array( $value, $selected_value ) ) ) {
  316. $selected = 'checked';
  317. }else if( $selected_value == '' AND isset( $option['selected'] ) AND $option['selected'] == 1 ){
  318. $selected = 'checked';
  319. }else{
  320. $selected = '';
  321. }
  322. ?><li><label id="ninja_forms_field_<?php echo $field_id;?>_<?php echo $x;?>_label" class="ninja-forms-field-<?php echo $field_id;?>-options" style="<?php echo $display_style;?>" for="ninja_forms_field_<?php echo $field_id;?>_<?php echo $x;?>"><input id="ninja_forms_field_<?php echo $field_id;?>_<?php echo $x;?>" name="ninja_forms_field_<?php echo $field_id;?>" type="radio" class="<?php echo $field_class;?>" value="<?php echo $value;?>" <?php echo $selected;?> rel="<?php echo $field_id;?>" /><?php echo $label;?></label></li><?php
  323. $x++;
  324. }
  325. ?></ul></span><li style="display:none;" id="ninja_forms_field_<?php echo $field_id;?>_template"><label><input id="ninja_forms_field_<?php echo $field_id;?>_" name="" type="radio" class="<?php echo $field_class;?>" value="" rel="<?php echo $field_id;?>" /></label></li>
  326. <?php
  327. break;
  328. case 'checkbox':
  329. $x = 0;
  330. ?><input type="hidden" id="ninja_forms_field_<?php echo $field_id;?>" name="ninja_forms_field_<?php echo $field_id;?>" value=""><span id="ninja_forms_field_<?php echo $field_id;?>_options_span" class="<?php echo $list_options_span_class;?>" rel="<?php echo $field_id;?>"><ul><?php
  331. foreach($options as $option){
  332. if(isset($option['value'])){
  333. $value = $option['value'];
  334. }else{
  335. $value = $option['label'];
  336. }
  337. $value = htmlspecialchars( $value, ENT_QUOTES );
  338. if(isset($option['label'])){
  339. $label = $option['label'];
  340. }else{
  341. $label = '';
  342. }
  343. if(isset($option['display_style'])){
  344. $display_style = $option['display_style'];
  345. }else{
  346. $display_style = '';
  347. }
  348. //$label = htmlspecialchars( $label, ENT_QUOTES );
  349. $label = stripslashes( $label) ;
  350. if($list_show_value == 0){
  351. $value = $label;
  352. }
  353. if( isset( $option['selected'] ) AND $option['selected'] == 1 ){
  354. $checked = 'checked';
  355. }
  356. if( is_array( $selected_value ) AND in_array($value, $selected_value) ){
  357. $checked = 'checked';
  358. }else if($selected_value == $value){
  359. $checked = 'checked';
  360. }else if( $selected_value == '' AND isset( $option['selected'] ) AND $option['selected'] == 1 ){
  361. $checked = 'checked';
  362. }else{
  363. $checked = '';
  364. }
  365. ?><li><label id="ninja_forms_field_<?php echo $field_id;?>_<?php echo $x;?>_label" class="ninja-forms-field-<?php echo $field_id;?>-options" style="<?php echo $display_style;?>"><input id="ninja_forms_field_<?php echo $field_id;?>_<?php echo $x;?>" name="ninja_forms_field_<?php echo $field_id;?>[]" type="checkbox" class="<?php echo $field_class;?> ninja_forms_field_<?php echo $field_id;?>" value="<?php echo $value;?>" <?php echo $checked;?> rel="<?php echo $field_id;?>"/><?php echo $label;?></label></li><?php
  366. $x++;
  367. }
  368. ?></ul></span><li style="display:none;" id="ninja_forms_field_<?php echo $field_id;?>_template"><label><input id="ninja_forms_field_<?php echo $field_id;?>_" name="" type="checkbox" class="<?php echo $field_class;?>" value="" rel="<?php echo $field_id;?>" /></label></li>
  369. <?php
  370. break;
  371. case 'multi':
  372. ?>
  373. <select name="ninja_forms_field_<?php echo $field_id;?>[]" id="ninja_forms_field_<?php echo $field_id;?>" class="<?php echo $field_class;?>" multiple size="<?php echo $multi_size;?>" rel="<?php echo $field_id;?>" >
  374. <?php
  375. if($label_pos == 'inside'){
  376. ?>
  377. <option value=""><?php echo $label;?></option>
  378. <?php
  379. }
  380. foreach($options as $option){
  381. if(isset($option['value'])){
  382. $value = $option['value'];
  383. }else{
  384. $value = $option['label'];
  385. }
  386. $value = htmlspecialchars( $value, ENT_QUOTES );
  387. if(isset($option['label'])){
  388. $label = $option['label'];
  389. }else{
  390. $label = '';
  391. }
  392. if(isset($option['display_style'])){
  393. $display_style = $option['display_style'];
  394. }else{
  395. $display_style = '';
  396. }
  397. $label = htmlspecialchars( $label, ENT_QUOTES );
  398. $label = stripslashes($label);
  399. if($list_show_value == 0){
  400. $value = $label;
  401. }
  402. if(is_array($selected_value) AND in_array($value, $selected_value)){
  403. $selected = 'selected';
  404. }else if( $selected_value == '' AND isset( $option['selected'] ) AND $option['selected'] == 1 ){
  405. $selected = 'selected';
  406. }else{
  407. $selected = '';
  408. }
  409. if( $display_style == '' ){
  410. ?>
  411. <option value="<?php echo $value;?>" <?php echo $selected;?>><?php echo $label;?></option>
  412. <?php
  413. }
  414. }
  415. ?>
  416. </select>
  417. <select id="ninja_forms_field_<?php echo $field_id;?>_clone" style="display:none;" rel="<?php echo $field_id;?>" >
  418. <?php
  419. $x = 0;
  420. foreach($options as $option){
  421. if(isset($option['value'])){
  422. $value = $option['value'];
  423. }else{
  424. $value = $option['label'];
  425. }
  426. $value = htmlspecialchars( $value, ENT_QUOTES );
  427. if(isset($option['label'])){
  428. $label = $option['label'];
  429. }else{
  430. $label = '';
  431. }
  432. if(isset($option['display_style'])){
  433. $display_style = $option['display_style'];
  434. }else{
  435. $display_style = '';
  436. }
  437. $label = htmlspecialchars( $label, ENT_QUOTES );
  438. $label = stripslashes( $label );
  439. if($list_show_value == 0){
  440. $value = $label;
  441. }
  442. if(is_array($selected_value) AND in_array($value, $selected_value)){
  443. $selected = 'selected';
  444. }else{
  445. $selected = '';
  446. }
  447. if( $display_style != '' ){
  448. ?>
  449. <option value="<?php echo $value;?>" title="<?php echo $x;?>" <?php echo $selected;?>><?php echo $label;?></option>
  450. <?php
  451. }
  452. $x++;
  453. }
  454. ?>
  455. </select>
  456. <?php
  457. break;
  458. }
  459. }
  460. function ninja_forms_field_list_option_output($field_id, $x, $option = '', $hidden = ''){
  461. if($hidden == 1){
  462. $hidden = '';
  463. }else{
  464. $hidden = 'display:none';
  465. }
  466. if(is_array($option)){
  467. $label = htmlspecialchars( $option['label'] );
  468. $label = str_replace( '&amp;', '&', $label );
  469. $value = htmlspecialchars( $option['value'] );
  470. $value = str_replace( '&amp;', '&', $value );
  471. if ( isset ( $option['calc'] ) ) {
  472. $calc = $option['calc'];
  473. } else {
  474. $calc = '';
  475. }
  476. if( isset( $option['selected'] ) ){
  477. $selected = $option['selected'];
  478. }else{
  479. $selected = '';
  480. }
  481. $hide = '';
  482. }else{
  483. $label = '';
  484. $value = '';
  485. $selected = '';
  486. $calc = '';
  487. $hide = 'style="display:none;"';
  488. }
  489. if($selected == 1){
  490. $selected = "checked='checked'";
  491. }
  492. ?>
  493. <div id="ninja_forms_field_<?php echo $field_id;?>_list_option_<?php echo $x;?>" class="ninja-forms-field-<?php echo $field_id;?>-list-option ninja-forms-field-list-option" <?php echo $hide;?> data-field="<?php echo $field_id; ?>">
  494. <table class="list-options">
  495. <tr>
  496. <td class="ninja-forms-delete-list-option-td">
  497. <a href="#" id="ninja_forms_field_<?php echo $field_id;?>_list_remove_option" class="nf-remove-list-option"><span class="dashicons dashicons-dismiss"></span></a>
  498. </td>
  499. <td class="ninja-forms-list-option-label-td">
  500. <?php _e( 'Label', 'ninja-forms' );?>: <input type="text" name="ninja_forms_field_<?php echo $field_id;?>[list][options][<?php echo $x;?>][label]" id="ninja_forms_field_<?php echo $field_id;?>_list_option_label" class="ninja-forms-field-list-option-label" value="<?php echo $label;?>">
  501. </td>
  502. <td class="ninja-forms-list-option-value-td">
  503. <span id="ninja_forms_field_<?php echo $field_id;?>_list_option_<?php echo $x;?>_value_span" name="" class="ninja-forms-field-<?php echo $field_id;?>-list-option-value" style="<?php echo $hidden;?>"><?php _e( 'Value', 'ninja-forms' );?>: <input type="text" name="ninja_forms_field_<?php echo $field_id;?>[list][options][<?php echo $x;?>][value]" id="ninja_forms_field_<?php echo $field_id;?>_list_option_value" class="ninja-forms-field-list-option-value" value="<?php echo $value;?>"></span>
  504. </td>
  505. <td class="ninja-forms-list-option-calc-td">
  506. <?php _ex( 'Calc', 'Short for calculation', 'ninja-forms' );?>: <input type="text" name="ninja_forms_field_<?php echo $field_id;?>[list][options][<?php echo $x;?>][calc]" id="ninja_forms_field_<?php echo $field_id;?>_list_option_calc" class="ninja-forms-field-list-option-calc" value="<?php echo $calc;?>">
  507. </td>
  508. <td class="ninja-forms-list-option-selected-td">
  509. <label for="ninja_forms_field_<?php echo $field_id;?>_options_<?php echo $x;?>_selected"><?php _e( 'Selected', 'ninja-forms' );?> <input type="hidden" value="0" name="ninja_forms_field_<?php echo $field_id;?>[list][options][<?php echo $x;?>][selected]"><input type="checkbox" value="1" name="ninja_forms_field_<?php echo $field_id;?>[list][options][<?php echo $x;?>][selected]" id="ninja_forms_field_<?php echo $field_id;?>_options_<?php echo $x;?>_selected" class="ninja-forms-field-list-option-selected" <?php echo $selected;?>></label>
  510. </td>
  511. <td class="ninja-forms-list-option-drag-td">
  512. <span class="ninja-forms-drag"><span class="dashicons dashicons-menu"></span></span>
  513. </td>
  514. </tr>
  515. </table>
  516. </div>
  517. <?php
  518. }
  519. function ninja_forms_field_filter_list_wrap_class( $field_wrap_class, $field_id ){
  520. $field_row = ninja_forms_get_field_by_id( $field_id );
  521. $field_type = $field_row['type'];
  522. if( $field_type == '_list' ){
  523. $field_data = $field_row['data'];
  524. if( isset( $field_data['list_type'] ) ){
  525. $list_type = $field_data['list_type'];
  526. }else{
  527. $list_type = '';
  528. }
  529. $field_wrap_class = str_replace( 'list-wrap', 'list-'.$list_type.'-wrap', $field_wrap_class );
  530. }
  531. return $field_wrap_class;
  532. }
  533. // Add a filter to change the current field_value to the "selected" list elements on form load, if any exist.
  534. function ninja_forms_field_filter_list_data( $form_id ){
  535. global $ninja_forms_loading, $ninja_forms_processing;
  536. if ( isset ( $ninja_forms_loading ) ) {
  537. $all_fields = $ninja_forms_loading->get_all_fields();
  538. } else {
  539. return false;
  540. }
  541. // Make sure we have some fields before displaying them!
  542. if ( ! $all_fields ) {
  543. return;
  544. }
  545. // Loop through all of our fields and see if we have any list fields.
  546. foreach( $all_fields as $field_id => $user_value ) {
  547. $tmp_array = array();
  548. if ( isset ( $ninja_forms_loading ) ) {
  549. $field = $ninja_forms_loading->get_field_settings( $field_id );
  550. } else {
  551. // $field = $ninja_forms_processing->get_field_settings( $field_id );
  552. }
  553. $field_type = $field['type'];
  554. $data = $field['data'];
  555. // Check to see if we are working with a list. If so, filter the default_value
  556. if( $field_type == '_list' && empty( $user_value ) ){
  557. if( isset( $data['list']['options'] ) AND is_array( $data['list']['options'] ) AND !empty( $data['list']['options'] ) ){
  558. foreach( $data['list']['options'] as $option ){
  559. if( isset( $option['selected'] ) AND $option['selected'] == 1 ){
  560. if( isset( $data['list_show_value'] ) AND $data['list_show_value'] == 1 ){
  561. $tmp_array[] = $option['value'];
  562. }else{
  563. $tmp_array[] = $option['label'];
  564. }
  565. }
  566. }
  567. if ( empty( $tmp_array ) AND $data['list_type'] == 'dropdown' AND $data['label_pos'] != 'inside' ) {
  568. if ( isset ( $data['list_show_value'] ) AND $data['list_show_value'] == 1 AND $data['label_pos'] != 'inside' ) {
  569. $tmp_array[] = $data['list']['options'][0]['value'];
  570. } else {
  571. $tmp_array[] = $data['list']['options'][0]['label'];
  572. }
  573. }
  574. }
  575. if ( isset ( $ninja_forms_loading ) ) {
  576. if ( $ninja_forms_loading->get_field_settings( $field_id ) ) {
  577. $ninja_forms_loading->update_field_value( $field_id, $tmp_array );
  578. }
  579. } else {
  580. // if ( !$ninja_forms_processing->get_field_value( $field_id ) ) {
  581. // $ninja_forms_processing->update_field_value( $field_id, $tmp_array );
  582. // }
  583. }
  584. }
  585. }
  586. }
  587. add_action( 'ninja_forms_display_pre_init', 'ninja_forms_field_filter_list_data', 8 );
  588. /**
  589. * Edit submission value output function
  590. *
  591. * @since 2.7
  592. * @return void
  593. */
  594. function nf_field_list_edit_sub_value( $field_id, $user_value, $field ) {
  595. $label = $field['data']['label'];
  596. $label_pos = $field['data']['label_pos'];
  597. $selected_value = $user_value;
  598. $options = $field['data']['list']['options'];
  599. $field_class = isset( $field['data']['class'] ) ? $field['data']['class'] : '';
  600. if( isset( $field['data']['list_show_value'] ) ){
  601. $list_show_value = $field['data']['list_show_value'];
  602. }else{
  603. $list_show_value = 0;
  604. }
  605. switch( $field['data']['list_type'] ) {
  606. case 'dropdown':
  607. ?>
  608. <select name="fields[<?php echo $field_id; ?>]" id="ninja_forms_field_<?php echo $field_id;?>" class="<?php echo $field_class;?>" rel="<?php echo $field_id;?>">
  609. <?php
  610. if($label_pos == 'inside'){
  611. ?>
  612. <option value=""><?php echo $label;?></option>
  613. <?php
  614. }
  615. foreach( $options as $option ){
  616. if(isset($option['value'])){
  617. $value = $option['value'];
  618. }else{
  619. $value = $option['label'];
  620. }
  621. $value = htmlspecialchars( $value, ENT_QUOTES );
  622. if(isset($option['label'])){
  623. $label = $option['label'];
  624. }else{
  625. $label = '';
  626. }
  627. if(isset($option['display_style'])){
  628. $display_style = $option['display_style'];
  629. }else{
  630. $display_style = '';
  631. }
  632. if ( isset( $option['disabled'] ) AND $option['disabled'] ){
  633. $disabled = 'disabled';
  634. }else{
  635. $disabled = '';
  636. }
  637. $label = htmlspecialchars( $label, ENT_QUOTES );
  638. $label = stripslashes( $label );
  639. $label = str_replace( '&amp;', '&', $label );
  640. $field_label = $field['data']['label'];
  641. if($list_show_value == 0){
  642. $value = $label;
  643. }
  644. if ( $selected_value == $value OR ( is_array( $selected_value ) AND in_array( $value, $selected_value ) ) ) {
  645. $selected = 'selected';
  646. }else if( ( $selected_value == '' OR $selected_value == $field_label ) AND isset( $option['selected'] ) AND $option['selected'] == 1 ){
  647. $selected = 'selected';
  648. }else{
  649. $selected = '';
  650. }
  651. ?>
  652. <option value="<?php echo $value;?>" <?php echo $selected;?> style="<?php echo $display_style;?>" <?php echo $disabled;?>><?php echo $label;?></option>
  653. <?php
  654. }
  655. ?>
  656. </select>
  657. <?php
  658. break;
  659. case 'radio':
  660. ?>
  661. <input type="hidden" name="fields[<?php echo $field_id; ?>]" value=""><ul><?php
  662. foreach($options as $option){
  663. if(isset($option['value'])){
  664. $value = $option['value'];
  665. }else{
  666. $value = $option['label'];
  667. }
  668. $value = htmlspecialchars( $value, ENT_QUOTES );
  669. if(isset($option['label'])){
  670. $label = $option['label'];
  671. }else{
  672. $label = '';
  673. }
  674. if(isset($option['display_style'])){
  675. $display_style = $option['display_style'];
  676. }else{
  677. $display_style = '';
  678. }
  679. $label = htmlspecialchars( $label, ENT_QUOTES );
  680. $label = stripslashes($label);
  681. if($list_show_value == 0){
  682. $value = $label;
  683. }
  684. if ( $selected_value == $value OR ( is_array( $selected_value ) AND in_array( $value, $selected_value ) ) ) {
  685. $selected = 'checked';
  686. }else if( $selected_value == '' AND isset( $option['selected'] ) AND $option['selected'] == 1 ){
  687. $selected = 'checked';
  688. }else{
  689. $selected = '';
  690. }
  691. ?>
  692. <li>
  693. <label><input id="" name="fields[<?php echo $field_id; ?>]" type="radio" class="<?php echo $field_class;?>" value="<?php echo $value;?>" <?php echo $selected;?> /><?php echo $label;?></label>
  694. </li>
  695. <?php
  696. }
  697. ?></ul>
  698. <?php
  699. break;
  700. case 'checkbox':
  701. ?><input type="hidden" name="fields[<?php echo $field_id; ?>][]" value=""><ul><?php
  702. foreach($options as $option){
  703. if(isset($option['value'])){
  704. $value = $option['value'];
  705. }else{
  706. $value = $option['label'];
  707. }
  708. $value = htmlspecialchars( $value, ENT_QUOTES );
  709. if(isset($option['label'])){
  710. $label = $option['label'];
  711. }else{
  712. $label = '';
  713. }
  714. if(isset($option['display_style'])){
  715. $display_style = $option['display_style'];
  716. }else{
  717. $display_style = '';
  718. }
  719. $label = htmlspecialchars( $label, ENT_QUOTES );
  720. $label = stripslashes( $label) ;
  721. if($list_show_value == 0){
  722. $value = $label;
  723. }
  724. if( isset( $option['selected'] ) AND $option['selected'] == 1 ){
  725. $checked = 'checked';
  726. }
  727. if( is_array( $selected_value ) AND in_array($value, $selected_value) ){
  728. $checked = 'checked';
  729. }else if($selected_value == $value){
  730. $checked = 'checked';
  731. }else if( $selected_value == '' AND isset( $option['selected'] ) AND $option['selected'] == 1 ){
  732. $checked = 'checked';
  733. }else{
  734. $checked = '';
  735. }
  736. ?>
  737. <li>
  738. <label>
  739. <input id="" name="fields[<?php echo $field_id; ?>][]" type="checkbox" value="<?php echo $value;?>" <?php echo $checked;?> /> <?php echo $label;?>
  740. </label>
  741. </li>
  742. <?php
  743. }
  744. ?></ul>
  745. <?php
  746. break;
  747. case 'multi';
  748. ?>
  749. <select name="fields[<?php echo $field_id; ?>][]" multiple size="5">
  750. <?php
  751. if($label_pos == 'inside'){
  752. ?>
  753. <option value=""><?php echo $label;?></option>
  754. <?php
  755. }
  756. foreach($options as $option){
  757. if(isset($option['value'])){
  758. $value = $option['value'];
  759. }else{
  760. $value = $option['label'];
  761. }
  762. $value = htmlspecialchars( $value, ENT_QUOTES );
  763. if(isset($option['label'])){
  764. $label = $option['label'];
  765. }else{
  766. $label = '';
  767. }
  768. if(isset($option['display_style'])){
  769. $display_style = $option['display_style'];
  770. }else{
  771. $display_style = '';
  772. }
  773. $label = htmlspecialchars( $label, ENT_QUOTES );
  774. $label = stripslashes($label);
  775. if($list_show_value == 0){
  776. $value = $label;
  777. }
  778. if(is_array($selected_value) AND in_array($value, $selected_value)){
  779. $selected = 'selected';
  780. }else if( $selected_value == '' AND isset( $option['selected'] ) AND $option['selected'] == 1 ){
  781. $selected = 'selected';
  782. }else{
  783. $selected = '';
  784. }
  785. if( $display_style == '' ){
  786. ?>
  787. <option value="<?php echo $value;?>" <?php echo $selected;?>><?php echo $label;?></option>
  788. <?php
  789. }
  790. }
  791. ?>
  792. </select>
  793. <?php
  794. break;
  795. }
  796. }
  797. /**
  798. * Output our user value on the sub table
  799. *
  800. * @since 2.7
  801. * @return void
  802. */
  803. function nf_field_list_sub_table_value( $field_id, $user_value ) {
  804. if ( is_array ( $user_value ) ) {
  805. echo '<ul>';
  806. $max_items = apply_filters( 'nf_sub_table_user_value_max_items', 3, $field_id );
  807. $x = 0;
  808. while ( $x < $max_items && $x <= count( $user_value ) - 1 ) {
  809. echo '<li>' . $user_value[$x] . '</li>';
  810. $x++;
  811. }
  812. echo '</ul>';
  813. } else {
  814. $max_len = apply_filters( 'nf_sub_table_user_value_max_len', 140, $field_id );
  815. if ( strlen( $user_value ) > 140 )
  816. $user_value = substr( $user_value, 0, 140 );
  817. echo nl2br( $user_value );
  818. }
  819. }