PageRenderTime 297ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/gravityforms 2/includes/fields/class-gf-field-list.php

https://gitlab.com/plusplusminus/aevitas
PHP | 442 lines | 347 code | 89 blank | 6 comment | 65 complexity | 6408c97c6ee8d80ff5a27cb28d9c91ef MD5 | raw file
  1. <?php
  2. if ( ! class_exists( 'GFForms' ) ) {
  3. die();
  4. }
  5. class GF_Field_List extends GF_Field {
  6. public $type = 'list';
  7. public function get_form_editor_field_title() {
  8. return __( 'List', 'gravityforms' );
  9. }
  10. function get_form_editor_field_settings() {
  11. return array(
  12. 'columns_setting',
  13. 'maxrows_setting',
  14. 'add_icon_url_setting',
  15. 'delete_icon_url_setting',
  16. 'conditional_logic_field_setting',
  17. 'prepopulate_field_setting',
  18. 'error_message_setting',
  19. 'label_setting',
  20. 'label_placement_setting',
  21. 'admin_label_setting',
  22. 'rules_setting',
  23. 'visibility_setting',
  24. 'description_setting',
  25. 'css_class_setting',
  26. );
  27. }
  28. public function get_first_input_id( $form ){
  29. return ! $this->is_form_editor() ? sprintf( 'input_%s_%s_shim', $form['id'], $this->id ) : '';
  30. }
  31. public function get_field_input( $form, $value = '', $entry = null ) {
  32. $form_id = $form['id'];
  33. $is_entry_detail = $this->is_entry_detail();
  34. $is_form_editor = $this->is_form_editor();
  35. $disabled_text = $is_form_editor ? 'disabled="disabled"' : '';
  36. if ( ! empty( $value ) ) {
  37. $value = maybe_unserialize( $value );
  38. }
  39. if ( ! is_array( $value ) ){
  40. $value = array( array() );
  41. }
  42. $has_columns = is_array( $this->choices );
  43. $columns = $has_columns ? $this->choices : array( array() );
  44. $shim_style = is_rtl() ? 'position:absolute;left:999em;' : 'position:absolute;left:-999em;';
  45. $label_target_shim = sprintf( '<input type=\'text\' id=\'input_%1$s_%2$s_shim\' style=\'%3$s\' onfocus=\'jQuery( "#field_%1$s_%2$s table tr td:first-child input" ).focus();\' />', $form_id, $this->id, $shim_style );
  46. $list = "<div class='ginput_container ginput_list'>" .
  47. $label_target_shim .
  48. "<table class='gfield_list'>";
  49. $class_attr = '';
  50. if ( $has_columns ) {
  51. $list .= '<colgroup>';
  52. for ( $colnum = 1; $colnum <= count( $columns ) + 1; $colnum++ ) {
  53. $odd_even = ( $colnum % 2 ) == 0 ? 'even' : 'odd';
  54. $list .= sprintf( "<col id='gfield_list_%d_col_%d' class='gfield_list_col_%s' />", $this->id, $colnum, $odd_even );
  55. }
  56. $list .= '</colgroup>';
  57. $list .= '<thead><tr>';
  58. foreach ( $columns as $column ) {
  59. $list .= '<th>' . esc_html( $column['text'] ) . '</th>';
  60. }
  61. $list .= '<th>&nbsp;</th></tr></thead>';
  62. } else {
  63. $list .=
  64. '<colgroup>' .
  65. "<col id='gfield_list_{$this->id}_col1' class='gfield_list_col_odd' />" .
  66. "<col id='gfield_list_{$this->id}_col2' class='gfield_list_col_even' />" .
  67. '</colgroup>';
  68. }
  69. $delete_display = count( $value ) == 1 ? 'visibility:hidden;' : '';
  70. $maxRow = intval( $this->maxRows );
  71. $disabled_icon_class = ! empty( $maxRow ) && count( $value ) >= $maxRow ? 'gfield_icon_disabled' : '';
  72. $list .= '<tbody>';
  73. $rownum = 1;
  74. foreach ( $value as $item ) {
  75. $odd_even = ( $rownum % 2 ) == 0 ? 'even' : 'odd';
  76. $list .= "<tr class='gfield_list_row_{$odd_even}'>";
  77. $colnum = 1;
  78. foreach ( $columns as $column ) {
  79. //getting value. taking into account columns being added/removed from form meta
  80. if ( is_array( $item ) ) {
  81. if ( $has_columns ) {
  82. $val = rgar( $item, $column['text'] );
  83. } else {
  84. $vals = array_values( $item );
  85. $val = rgar( $vals, 0 );
  86. }
  87. } else {
  88. $val = $colnum == 1 ? $item : '';
  89. }
  90. $list .= "<td class='gfield_list_cell gfield_list_{$this->id}_cell{$colnum}'>" . $this->get_list_input( $has_columns, $column, $val, $form_id ) . '</td>';
  91. $colnum ++;
  92. }
  93. $add_icon = ! empty( $this->addIconUrl ) ? $this->addIconUrl : GFCommon::get_base_url() . '/images/blankspace.png';
  94. $delete_icon = ! empty( $this->deleteIconUrl) ? $this->deleteIconUrl : GFCommon::get_base_url() . '/images/blankspace.png';
  95. $on_click = $is_form_editor ? '' : "onclick='gformAddListItem(this, {$maxRow})'";
  96. if ( $this->maxRows != 1 ) {
  97. // can't replace these icons with the webfont versions since they appear on the front end.
  98. $list .= "<td class='gfield_list_icons'>";
  99. $list .= " <img src='{$add_icon}' class='add_list_item {$disabled_icon_class}' {$disabled_text} title='" . __( 'Add another row', 'gravityforms' ) . "' alt='" . __( 'Add a row', 'gravityforms' ) . "' {$on_click} style='cursor:pointer; margin:0 3px;' />" .
  100. " <img src='{$delete_icon}' {$disabled_text} title='" . __( 'Remove this row', 'gravityforms' ) . "' alt='" . __( 'Remove this row', 'gravityforms' ) . "' class='delete_list_item' style='cursor:pointer; {$delete_display}' onclick='gformDeleteListItem(this, {$maxRow})' />";
  101. $list .= '</td>';
  102. }
  103. $list .= '</tr>';
  104. if ( ! empty( $maxRow ) && $rownum >= $maxRow ){
  105. break;
  106. }
  107. $rownum ++;
  108. }
  109. $list .= '</tbody>';
  110. $list .= $this->maxRows != 1 ? $this->get_svg_image_block() : '';
  111. $list .= '</table></div>';
  112. return $list;
  113. }
  114. public function get_svg_image_block(){
  115. global $_has_image_block;
  116. //return image block once per page load
  117. if ( ! $_has_image_block ){
  118. $_has_image_block = true;
  119. return '
  120. <style type="text/css">
  121. /* add SVG background image support for retina devices -------------------------------*/
  122. img.add_list_item {
  123. background-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4iICJodHRwOi8vd3d3LnczLm9yZy9HcmFwaGljcy9TVkcvMS4xL0RURC9zdmcxMS5kdGQiPjxzdmcgdmVyc2lvbj0iMS4xIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMj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MTQuNjI1LTI1NiAyNTYgMCAxNDEuMzkxIDExNC42MjUgMjU2IDI1NiAyNTYgMTQxLjM5MSAwIDI1Ni0xMTQuNjA5IDI1Ni0yNTYgMC0xNDEuMzc1LTExNC42MDktMjU2LTI1Ni0yNTZ6TTI1NiA0NDhjLTEwNi4wMzEgMC0xOTItODUuOTY5LTE5Mi0xOTJzODUuOTY5LTE5MiAxOTItMTkyYzEwNi4wNDcgMCAxOTIgODUuOTY5IDE5MiAxOTJzLTg1Ljk1MyAxOTItMTkyIDE5MnpNMTI4IDI4OGgyNTZ2LTY0aC0yNTZ2NjR6Ij48L3BhdGg+PC9zdmc+);
  127. }
  128. img.add_list_item,
  129. img.delete_list_item {
  130. width: 1em;
  131. height: 1em;
  132. background-size: 1em 1em;
  133. opacity: 0.5;
  134. }
  135. img.add_list_item:hover,
  136. img.add_list_item:active,
  137. img.delete_list_item:hover,
  138. img.delete_list_item:active {
  139. opacity: 1.0;
  140. }
  141. </style>
  142. ';
  143. }
  144. return '';
  145. }
  146. public function get_list_input( $has_columns, $column, $value, $form_id ) {
  147. $tabindex = GFCommon::get_tabindex();
  148. $disabled = $this->is_form_editor() ? 'disabled' : '';
  149. $column_index = 1;
  150. if ( $has_columns && is_array( $this->choices ) ) {
  151. foreach ( $this->choices as $choice ) {
  152. if ( $choice['text'] == $column['text'] ) {
  153. break;
  154. }
  155. $column_index ++;
  156. }
  157. }
  158. $input_info = array( 'type' => 'text' );
  159. $input_info = apply_filters( "gform_column_input_{$form_id}_{$this->id}_{$column_index}", apply_filters( 'gform_column_input', $input_info, $this, rgar( $column, 'text' ), $value, $form_id ), $this, rgar( $column, 'text' ), $value, $form_id );
  160. switch ( $input_info['type'] ) {
  161. case 'select' :
  162. $input = "<select name='input_{$this->id}[]' {$tabindex} {$disabled} >";
  163. if ( ! is_array( $input_info['choices'] ) ) {
  164. $input_info['choices'] = explode( ',', $input_info['choices'] );
  165. }
  166. foreach ( $input_info['choices'] as $choice ) {
  167. if ( is_array( $choice ) ) {
  168. $choice_value = $choice['value'];
  169. $choice_text = $choice['text'];
  170. $choice_selected = array_key_exists( 'isSelected', $choice ) ? $choice['isSelected'] : false;
  171. } else {
  172. $choice_value = $choice;
  173. $choice_text = $choice;
  174. $choice_selected = false;
  175. }
  176. $is_selected = empty( $value ) ? $choice_selected : $choice_value == $value;
  177. $selected = $is_selected ? "selected='selected'" : '';
  178. $input .= "<option value='" . esc_attr( $choice_value ) . "' {$selected}>" . esc_html( $choice_text ) . '</option>';
  179. }
  180. $input .= '</select>';
  181. break;
  182. default :
  183. $input = "<input type='text' name='input_{$this->id}[]' value='" . esc_attr( $value ) . "' {$tabindex} {$disabled}/>";
  184. break;
  185. }
  186. return apply_filters(
  187. "gform_column_input_content_{$form_id}_{$this->id}_{$column_index}",
  188. apply_filters( 'gform_column_input_content', $input, $input_info, $this, rgar( $column, 'text' ), $value, $form_id ),
  189. $input_info, $this, rgar( $column, 'text' ), $value, $form_id
  190. );
  191. }
  192. public function get_value_submission( $field_values, $get_from_post_global_var = true ) {
  193. $value = $this->get_input_value_submission( 'input_' . $this->id, $this->inputName, $field_values, $get_from_post_global_var );
  194. $value = $this->create_list_array( $value );
  195. return $value;
  196. }
  197. public function is_value_submission_empty( $form_id ){
  198. $value = rgpost( 'input_' . $this->id );
  199. if ( is_array( $value ) ) {
  200. //empty if all inputs are empty (for inputs with the same name)
  201. foreach ( $value as $input ) {
  202. if ( strlen( trim( $input ) ) > 0 ) {
  203. return false;
  204. }
  205. }
  206. }
  207. return true;
  208. }
  209. public function get_value_entry_detail( $value, $currency = '', $use_text = false, $format = 'html', $media = 'screen' ) {
  210. if ( empty( $value ) ) {
  211. return '';
  212. }
  213. $value = unserialize( $value );
  214. $has_columns = is_array( $value[0] );
  215. if ( ! $has_columns ) {
  216. $items = '';
  217. foreach ( $value as $key => $item ) {
  218. if ( ! empty( $item ) ) {
  219. switch ( $format ) {
  220. case 'text' :
  221. $items .= $item . ', ';
  222. break;
  223. case 'url' :
  224. $items .= $item . ',';
  225. break;
  226. default :
  227. if ( $media == 'email' ) {
  228. $items .= "<li>{$item}</li>";
  229. } else {
  230. $items .= "<li>{$item}</li>";
  231. }
  232. break;
  233. }
  234. }
  235. }
  236. if ( empty( $items ) ) {
  237. return '';
  238. } else if ( $format == 'text' ) {
  239. return substr( $items, 0, strlen( $items ) - 2 ); //removing last comma
  240. } else if ( $format == 'url' ) {
  241. return substr( $items, 0, strlen( $items ) - 1 ); //removing last comma
  242. } else if ( $media == 'email' ) {
  243. return "<ul class='bulleted'>{$items}</ul>";
  244. } else {
  245. return "<ul class='bulleted'>{$items}</ul>";
  246. }
  247. } else if ( is_array( $value ) ) {
  248. $columns = array_keys( $value[0] );
  249. $list = '';
  250. switch ( $format ) {
  251. case 'text' :
  252. $is_first_row = true;
  253. foreach ( $value as $item ) {
  254. if ( ! $is_first_row ){
  255. $list .= "\n\n" . $this->label . ': ';
  256. }
  257. $list .= implode( ',', array_values( $item ) );
  258. $is_first_row = false;
  259. }
  260. break;
  261. case 'url' :
  262. foreach ( $value as $item ) {
  263. $list .= implode( "|", array_values( $item ) ) . ',';
  264. }
  265. if ( ! empty( $list ) ){
  266. $list = substr( $list, 0, strlen( $list ) - 1 );
  267. }
  268. break;
  269. default :
  270. if ( $media == 'email' ) {
  271. $list = "<table class='gfield_list' style='border-top: 1px solid #DFDFDF; border-left: 1px solid #DFDFDF; border-spacing: 0; padding: 0; margin: 2px 0 6px; width: 100%'><thead><tr>\n";
  272. //reading columns from entry data
  273. foreach ( $columns as $column ) {
  274. $list .= "<th style='background-image: none; border-right: 1px solid #DFDFDF; border-bottom: 1px solid #DFDFDF; padding: 6px 10px; font-family: sans-serif; font-size: 12px; font-weight: bold; background-color: #F1F1F1; color:#333; text-align:left'>" . esc_html( $column ) . '</th>' . "\n";
  275. }
  276. $list .= '</tr></thead>' . "\n";
  277. $list .= "<tbody style='background-color: #F9F9F9'>";
  278. foreach ( $value as $item ) {
  279. $list .= '<tr>';
  280. foreach ( $columns as $column ) {
  281. $val = rgar( $item, $column );
  282. $list .= "<td style='padding: 6px 10px; border-right: 1px solid #DFDFDF; border-bottom: 1px solid #DFDFDF; border-top: 1px solid #FFF; font-family: sans-serif; font-size:12px;'>{$val}</td>\n";
  283. }
  284. $list .= '</tr>' . "\n";
  285. }
  286. $list .= '<tbody></table>' . "\n";
  287. } else {
  288. $list = "<table class='gfield_list'><thead><tr>";
  289. //reading columns from entry data
  290. foreach ( $columns as $column ) {
  291. $list .= '<th>' . esc_html( $column ) . '</th>' . "\n";
  292. }
  293. $list .= '</tr></thead>' . "\n";
  294. $list .= '<tbody>';
  295. foreach ( $value as $item ) {
  296. $list .= '<tr>';
  297. foreach ( $columns as $column ) {
  298. $val = rgar( $item, $column );
  299. $list .= "<td>{$val}</td>\n";
  300. }
  301. $list .= '</tr>' . "\n";
  302. }
  303. $list .= '<tbody></table>' . "\n";
  304. }
  305. break;
  306. }
  307. return $list;
  308. }
  309. return '';
  310. }
  311. public function get_value_save_entry( $value, $form, $input_name, $lead_id, $lead ) {
  312. if ( $this->adminOnly && $this->allowsPrepopulate ) {
  313. $value = json_decode( $value );
  314. }
  315. if ( GFCommon::is_empty_array( $value ) ) {
  316. $value = '';
  317. } else {
  318. $value = $this->create_list_array( $value );
  319. $value = serialize( $value );
  320. }
  321. return $value;
  322. }
  323. public function get_value_merge_tag( $value, $input_id, $entry, $form, $modifier, $raw_value, $url_encode, $esc_html, $format ) {
  324. $output_format = in_array( $modifier, array( 'text', 'html', 'url' ) ) ? $modifier : $format;
  325. return GFCommon::get_lead_field_display( $this, $raw_value, $entry['currency'], true, $output_format );
  326. }
  327. function create_list_array( $value ) {
  328. if ( ! $this->enableColumns ) {
  329. return $value;
  330. } else {
  331. $col_count = count( $this->choices );
  332. $rows = array();
  333. $row_count = count( $value ) / $col_count;
  334. $col_index = 0;
  335. for ( $i = 0; $i < $row_count; $i ++ ) {
  336. $row = array();
  337. foreach ( $this->choices as $column ) {
  338. $row[ $column['text'] ] = rgar( $value, $col_index );
  339. $col_index ++;
  340. }
  341. $rows[] = $row;
  342. }
  343. return $rows;
  344. }
  345. }
  346. }
  347. GF_Fields::register( new GF_Field_List() );