WJ_FieldRender

Class WJ_FieldRender It is able to render Custom Fields in a form.

Defined (1)

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

/classes/WJ_FieldRender.php  
  1. class WJ_FieldRender { 
  2.  
  3. // WJ_FieldUser Object. 
  4. private $field_user; 
  5. // The column, like 'cf_1' 
  6. private $identifier; 
  7. // Name of the field. 
  8. private $name; 
  9. // Value of the field. 
  10. private $value; 
  11.  
  12. public function __construct( $field_user ) { 
  13. $this->field_user = $field_user; 
  14. $this->identifier = $field_user->column_name(); 
  15. $this->name = esc_attr( $field_user->field->name ); 
  16. $this->value = esc_attr( $field_user->value() ); 
  17.  
  18. public function validation() { 
  19. $rules = array(); 
  20. $validation_class = ''; 
  21.  
  22. // If there are any validations, add correct class names. 
  23. if ( ! empty( $this->field_user->field->settings['validate'] ) ) { 
  24. $validation_value = $this->field_user->field->settings['validate']; 
  25. $rules[] = 'custom[' . $validation_value . ']'; 
  26.  
  27. // If there's any validation, join all classes. 
  28. if ( ! empty( $rules ) ) { 
  29. $validation_class = 'class="validate[' . join( ', ', $rules ) . ']"'; 
  30.  
  31. return $validation_class; 
  32.  
  33. /** 
  34. Returns the HTML label for this field, to be used 
  35. in a form. 
  36. $field_render->label(); 
  37. # => '<label ...>Address</label>' 
  38. */ 
  39. public function label() { 
  40. $form_field_name = $this->identifier; 
  41. $label = 
  42. '<label for="' . $form_field_name . '">' . 
  43. $this->name . 
  44. '</label>'; 
  45. return $label; 
  46.  
  47. /** 
  48. Returns the input related to the type of the field,  
  49. to be used in a form. 
  50. $field_render->input(); 
  51. # => '<input type=..../> 
  52. */ 
  53. public function input() { 
  54. $input = ''; 
  55.  
  56. switch ( $this->field_user->field->type ) { 
  57. case 'input': 
  58. $input = 
  59. '<input type="text" size="40" id="'. $this->identifier . '"' . 
  60. ' value="' . $this->value . 
  61. '" name="wysija[field][' . $this->identifier . ']" ' . 
  62. $this->validation() . 
  63. ' />'; 
  64. break; 
  65. case 'textarea': 
  66. $input = 
  67. '<textarea id="'. $this->identifier . '"' . 
  68. 'name="wysija[field][' . $this->identifier . ']" ' . 
  69. $this->validation() . 
  70. ' />' . 
  71. $this->value . 
  72. '</textarea>'; 
  73. break; 
  74. case 'checkbox': 
  75. if ( empty( $this->field_user->field->settings['values'] ) ) { 
  76. $input = esc_attr__( 'This field contains no values', WYSIJA ); 
  77. break; 
  78. $check_value = ''; 
  79. $label = $this->field_user->field->settings['values'][0]['value']; 
  80. if ( $this->value == 1 ) { 
  81. $check_value = ' checked="checked"'; 
  82. $input = 
  83. '<label for="' . $this->identifier . '">' . 
  84. '<input type="hidden" '. 
  85. $check_value . 
  86. ' name="wysija[field][' . $this->identifier . ']"' . 
  87. ' value="0" ' . 
  88. ' />' . 
  89. '<input type="checkbox" id="'. $this->identifier . '"' . 
  90. $check_value . 
  91. ' name="wysija[field][' . $this->identifier . ']"' . 
  92. ' value="1" ' . 
  93. $this->validation() . 
  94. ' />' . $label . '</label>'; 
  95. break; 
  96. case 'radio': 
  97. $field = $this->field_user->field; 
  98. if ( empty( $this->field_user->field->settings['values'] ) ) { 
  99. $input = esc_attr__( 'This field contains no values', WYSIJA ); 
  100. break; 
  101. foreach ( $field->settings['values'] as $index => $content ) { 
  102. $check_value = ''; 
  103. if ( $this->value == $content['value'] ) { 
  104. $check_value = 'checked="checked"'; 
  105. $local_identifier = $this->identifier . '_' . $content['value']; 
  106. $input .= 
  107. '<label for="' . $local_identifier . '">' . 
  108. '<input type="radio" id="'. $local_identifier . '"' . 
  109. $check_value . 
  110. ' name="wysija[field][' . $this->identifier . ']"' . 
  111. ' value="' . $content['value'] . '" />' . 
  112. $content['value'] . 
  113. '</label>'; 
  114. break; 
  115. case 'select': 
  116. if ( empty( $this->field_user->field->settings['values'] ) ) { 
  117. $input = esc_attr__( 'This field contains no values', WYSIJA ); 
  118. break; 
  119.  
  120. $input = '<select id="' . $this->identifier . '"' . 
  121. ' name="wysija[field][' . $this->identifier . ']" ' . 
  122. $this->validation() . 
  123. ' />'; 
  124. $field = $this->field_user->field; 
  125. foreach ( $field->settings['values'] as $index => $content ) { 
  126. $check_value = ''; 
  127. if ( $this->value == $content['value'] ) { 
  128. $check_value = 'selected="selected"'; 
  129. $input .= 
  130. '<option ' . $check_value . 
  131. ' value="' . $content['value'] . '" >' . 
  132. $content['value'] . 
  133. '</option>'; 
  134. $input .= '</select>'; 
  135. break; 
  136.  
  137. case 'date': 
  138. // get date format from field settings 
  139. $field = $this->field_user->field; 
  140.  
  141. // get timestamp value 
  142. $value = (int) $this->value; 
  143.  
  144. // get date type (defaults to year + month + day) 
  145. $date_type = ( isset( $field->settings['date_type'] ) ) ? $field->settings['date_type'] : 'year_month_day'; 
  146. // get an array of all required date components (year, month, day) 
  147. $display_date_fields = explode( '_', $date_type ); 
  148. // form engine to get date data 
  149. $helper_form_engine = WYSIJA::get( 'form_engine', 'helper' ); 
  150. $date_order = explode('/', $field->settings['date_order']); 
  151.  
  152. foreach ($date_order as $date_element) { 
  153. if (strpos($date_element, 'yy') !== false) { 
  154. // year selection 
  155. if ( in_array( 'year', $display_date_fields ) ) { 
  156. $years = $helper_form_engine->get_years(); 
  157.  
  158. $selected_year = null; 
  159. if ( $value !== null ) { 
  160. $selected_year = (int) strftime( '%Y', $value ); 
  161.  
  162. // select 
  163. $input .= '<select name="wysija[field]['.$this->identifier.'][year]">'; 
  164. $input .= '<option value="">' . __( 'Year' ) .'</option>'; 
  165. foreach ( $years as $year ) { 
  166. $is_selected = ((int)$year['year'] === $selected_year) ? ' selected="selected"' : ''; 
  167. $input .= '<option value="'.$year['year'].'"'.$is_selected.'>'.$year['year'].'</option>'; 
  168. $input .= '</select>'; 
  169. } elseif (strpos($date_element, 'mm') !== false) { 
  170. // month selection 
  171. if ( in_array( 'month', $display_date_fields ) ) { 
  172. $months = $helper_form_engine->get_months(); 
  173.  
  174. $selected_month = null; 
  175. if ( $value !== null ) { 
  176. $selected_month = (int) strftime( '%m', $value ); 
  177.  
  178. // select 
  179. $input .= '<select name="wysija[field]['.$this->identifier.'][month]">'; 
  180. $input .= '<option value="">' . __( 'Month' ) . '</option>'; 
  181. foreach ( $months as $month ) { 
  182. $is_selected = ((int)$month['month'] === $selected_month) ? ' selected="selected"' : ''; 
  183. $input .= '<option value="'.$month['month'].'"'.$is_selected.'>'.$month['month_name'].'</option>'; 
  184. $input .= '</select>'; 
  185. } elseif (strpos($date_element, 'dd') !== false) { 
  186. // day selection 
  187. if ( in_array( 'day', $display_date_fields ) ) { 
  188. $days = $helper_form_engine->get_days(); 
  189.  
  190. $selected_day = null; 
  191. if ( $value !== null ) { 
  192. $selected_day = (int) strftime( '%d', $value ); 
  193.  
  194. // select 
  195. $input .= '<select name="wysija[field]['.$this->identifier.'][day]">'; 
  196. $input .= '<option value="">' . __( 'Day' ) . '</option>'; 
  197. foreach ( $days as $day ) { 
  198. $is_selected = ((int)$day['day'] === $selected_day) ? ' selected="selected"' : ''; 
  199. $input .= '<option value="'.$day['day'].'"'.$is_selected.'>'.$day['day'].'</option>'; 
  200. $input .= '</select>'; 
  201.  
  202. break; 
  203. default: 
  204. $input = ''; 
  205. break; 
  206. return $input; 
  207.  
  208. /** 
  209. Render all custom fields in a table, given the user id. 
  210. $field_render::render_all(1); 
  211. # => '<tr><th><label...<input...</td></tr>' 
  212. */ 
  213. public static function render_all( $user_id ) { 
  214. $fields = WJ_FieldUser::get_all( $user_id ); 
  215. if ( isset( $fields ) ) { 
  216. $output = ''; 
  217. foreach ( $fields as $field ) { 
  218. $field_render = new self($field); 
  219. $output .= 
  220. '<tr>' . 
  221. '<th scope="row">' . 
  222. $field_render->label() . 
  223. '</th><td>' . 
  224. $field_render->input() . 
  225. '</td></tr>'; 
  226. return $output; 
  227. } else { 
  228. return null; 
  229.  
  230.