WJ_FieldHandler

Class WJ_FieldHandler It handles the custom fields in a controller.

Defined (1)

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

/classes/WJ_FieldHandler.php  
  1. class WJ_FieldHandler { 
  2.  
  3. public function __construct() { 
  4.  
  5. /** 
  6. Handler to save all the fields, by giving an array 
  7. that specifies the custom fields columns names, and 
  8. the ID of the user. 
  9. WJ_FieldHandler::handle_all(array( 
  10. 'cf_1' => 'Value',  
  11. 'cf_2' => 'Value 2' 
  12. ), 1 
  13. ); 
  14. */ 
  15. public static function handle_all(array $fields_values, $user_id) { 
  16. $user_fields = WJ_FieldUser::get_all($user_id); 
  17.  
  18. if(isset($user_fields) && !empty($user_fields)) { 
  19. foreach ($user_fields as $user_field) { 
  20. $key = $user_field->column_name(); 
  21.  
  22. // check if there is a value for this custom field 
  23. if(array_key_exists($key, $fields_values)) { 
  24.  
  25. // set value 
  26. $new_value = $fields_values[$key]; 
  27.  
  28. // extra process for specific types 
  29. if($user_field->field->type === 'checkbox') { 
  30. // limit the value between [0, 1] 
  31. $new_value = min(max(0, (int)$new_value), 1); 
  32.  
  33. } else if($user_field->field->type === 'date') { 
  34. // get date parameters 
  35. $year = (isset($fields_values[$key]['year'])) ? (int)$fields_values[$key]['year'] : (int)strftime('%Y'); 
  36. $month = (isset($fields_values[$key]['month'])) ? (int)$fields_values[$key]['month'] : 1; 
  37. $day = (isset($fields_values[$key]['day'])) ? (int)$fields_values[$key]['day'] : 1; 
  38.  
  39. $new_value = null; 
  40. if($year !== 0 && $month !== 0 && $day !== 0) { 
  41. // make timestamp from date parameters 
  42. $new_value = mktime(0, 0, 0, $month, $day, $year); 
  43.  
  44. // update value 
  45. $user_field->update($new_value);