Advertisement
Guest User

Untitled

a guest
Jan 7th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.80 KB | None | 0 0
  1. <?php
  2.  
  3. class Master_testimonials_model extends CI_Model
  4. {
  5.  
  6. public $language = 'english';
  7. public $incomplete_inputs = '';
  8. public $succesful_validation_msg = '';
  9.  
  10. function __construct()
  11. {
  12.  
  13.     parent::__construct();
  14.    
  15. }
  16.  
  17. /*============================*/
  18. // get_num_of_new_testimonials
  19. /*============================*/
  20. public function get_num_of_new_testimonials() {
  21.  
  22.     $num_of_new_testimonials_q = $this->db->get_where('nv_cms_testimonials',array('testimonial_approved'=>0));
  23.     return $num_of_new_testimonials_q->num_rows();
  24.    
  25. }
  26. /*============================*/
  27. // get_new_testimonials
  28. /*============================*/
  29. public function get_new_testimonials()
  30. {
  31.     $new_testimonials_q = $this->db->get_where('nv_cms_testimonials',array('testimonial_approved'=>0));
  32.     return $new_testimonials_q;
  33. }
  34. /*============================*/
  35. // get_all_testimonials_with_pagination
  36. /*============================*/
  37. public function get_all_testimonials_with_pagination($limit, $offset) {
  38.     $testimonials = $this->db->get_where('nv_cms_testimonials',array('testimonial_approved'=>1),$limit,$offset);
  39.     return $testimonials;
  40. }
  41. /*============================*/
  42. // get_all_testimonials
  43. /*============================*/
  44. public function get_all_testimonials() {
  45.  
  46.     $testimonials = $this->db->get_where('nv_cms_testimonials',array('testimonial_approved'=>1));
  47.     return $testimonials->num_rows();
  48.    
  49. }
  50.  
  51. /*========================*/
  52. //get_config_value_plain()
  53. /*========================*/
  54. public function get_config_value_plain($config_name) {
  55.     $config_value = $this->db->get_where('nv_cms_testimonials_config',array('config_name'=>$config_name))->row()->config_value;
  56.     return $config_value;
  57. }
  58. /*=======================*/
  59. //update_required_fields()
  60. /*=======================*/
  61. public function update_required_fields()
  62. {
  63.     $update = array("config_value"=>$this->input->post('tc_require_email'));
  64.     $this->db->where('config_name','tc_require_email');
  65.     $this->db->update('nv_cms_testimonials_config',$update);
  66.                    
  67.     $update = array("config_value"=>$this->input->post('tc_require_website'));
  68.     $this->db->where('config_name','tc_require_website');
  69.     $this->db->update('nv_cms_testimonials_config',$update);
  70.    
  71.     $update = array("config_value"=>$this->input->post('tc_require_phone'));
  72.     $this->db->where('config_name','tc_require_phone');
  73.     $this->db->update('nv_cms_testimonials_config',$update);
  74.  
  75. }
  76. /*=========================*/
  77. //set_config_value_plain()
  78. /*=========================*/
  79. public function set_config_value_plain($config_name,$config_value) {
  80.  
  81. $update = array("config_value"=>$config_value);
  82. $this->db->where("config_name",$config_name);
  83. $this->db->update('nv_cms_testimonials_config',$update);
  84.    
  85. }
  86.  
  87. /*=========================*/
  88. // get_testimonial_by_id()
  89. /*=========================*/
  90. public function get_testimonial_by_id($id)
  91. {
  92.     $testimonial_q = $this->db->get_where('nv_cms_testimonials',array('id'=>$id));
  93.    
  94.     return $testimonial_q->row();
  95.  
  96. }
  97.  
  98. /*===========================*/
  99. // set_form_validation_rules
  100. /*===========================*/
  101. public function set_form_validation_rules($field) {
  102.  
  103.     if(strcasecmp($field, 'testimonial_author_email') == 0 )
  104.         {
  105.        
  106.             $tc_require_email = $this->get_config_value_plain('tc_require_email');
  107.             $field_rules = $tc_require_email === 'true' ? 'required|valid_email' : '';
  108.             return $field_rules;
  109.        
  110.         }
  111.     elseif(strcasecmp($field, 'testimonial_author_phone') == 0 )
  112.         {
  113.             $tc_require_phone = $this->get_config_value_plain('tc_require_phone');
  114.             $field_rules = $tc_require_phone === 'true' ? 'required': '';
  115.             return $field_rules;
  116.         }
  117.     elseif(strcasecmp($field, 'testimonial_author_website') == 0 )
  118.         {
  119.             $tc_require_website = $this->get_config_value_plain('tc_require_website');
  120.             $field_rules = $tc_require_website === 'true' ? 'required': '';
  121.             return $field_rules;
  122.        
  123.         }
  124. }
  125.  
  126. /*======================*/
  127. // update_testimonial
  128. /*=====================*/
  129.     public function update_testimonial($id)
  130.     {
  131.    
  132.  
  133.         $input_fields = array(
  134.        
  135.                array(
  136.                      'field'   => 'testimonial',
  137.                      'label'   => 'name',
  138.                      'rules'   => 'required'
  139.                   ),
  140.                array(
  141.                      'field'   => 'testimonial_author',
  142.                      'label'   => 'phone',
  143.                      'rules'   => 'required'
  144.                   ),
  145.                 array(
  146.                      'field'   => 'testimonial_author_email',
  147.                      'label'   => 'Email',
  148.                      'rules'   => $this->set_form_validation_rules('testimonial_author_email')
  149.                   ),
  150.                 array(
  151.                      'field'   => 'testimonial_author_phone',
  152.                      'label'   => 'phone',
  153.                      'rules'   => $this->set_form_validation_rules('testimonial_author_phone')
  154.                   ),
  155.                array(
  156.                      'field'   => 'testimonial_author_website',
  157.                      'label'   => 'email',
  158.                      'rules'   => $this->set_form_validation_rules('testimonial_author_website')
  159.                   )
  160.             );
  161.  
  162.     if($this->validate_form($input_fields,$this->language))
  163.         {
  164.        
  165.             /*manually map the inputs to the database table columns*/
  166.            
  167.             $update = array(
  168.                'testimonial' => $this->input->post('testimonial'),
  169.                'author_name' => $this->input->post('testimonial_author'),
  170.                'author_email' => $this->input->post('testimonial_author_email'),
  171.                'author_phone' => preg_replace("/[^0-9]/", "", $this->input->post('testimonial_author_phone')),
  172.                'author_website' => $this->input->post('testimonial_author_website')
  173.             );
  174.             $this->db->where('id',$id);
  175.             $this->db->update('nv_cms_testimonials', $update);
  176.            
  177.             echo json_encode(array('validation_status'=>true,'validation_message'=>$this->succesful_validation_msg));
  178.         }
  179.        
  180.    
  181.     }
  182.    
  183.  
  184.    
  185.  
  186. /*=========================*/
  187. //  validate_form()
  188. /*=========================*/
  189.    
  190.     public function validate_form($input_fields,$language)
  191.     {
  192.    
  193.     if(strcasecmp($language, 'spanish') == 0)
  194.     {
  195.         /*status messages*/
  196.         $this->incomplete_inputs = 'Por favor de llenar todos los datos!';
  197.         $this->succesful_validation_msg = 'Su formulario ha sido enviado! Mucha Gracias!';
  198.        
  199.     } elseif(strcasecmp($language, 'english') == 0){
  200.    
  201.         /*status messages*/
  202.         $this->incomplete_inputs = 'Please fill in all the fields!';
  203.         $this->succesful_validation_msg = 'Your Settings have been saved! ';
  204.         }
  205.        
  206.     //error if some or all of the fields are not completed
  207.     $incomplete_form_errors = array('validation_status'=>false,'validation_message'=>$this->incomplete_inputs);
  208.    
  209.     /*load form validation library*/
  210.     $this->load->library('form_validation');
  211.    
  212.     $this->form_validation->set_rules($input_fields);
  213.    
  214.     $this->form_validation->set_error_delimiters('', '');
  215.    
  216.     //counter to keep track of empty/invalid inputs
  217.     $inputs_incomplete_tracker=0; // start with 0, we assume all to have data in them...
  218.    
  219.     /*check that all $post inputs have data in them */
  220.     foreach($input_fields as $input)
  221.     {
  222.         if(($this->input->post($input['field']) == FALSE) &&
  223.         (strpos($input['rules'],'required') !== FALSE) )
  224.         {
  225.             $inputs_incomplete_tracker++;
  226.             $incomplete_form_errors[$input['field'].'_error'] = true;
  227.             $incomplete_form_errors[$input['field'].'_error_msg'] = $input['field'];
  228.         }
  229.  
  230.     }
  231.    
  232.     if($inputs_incomplete_tracker == 0)//zero means there are 0 uncompleted inputs... all have been completed (filled in by user)
  233.     {
  234.     //all inputs have data in them.. so now we check the validity of the inputs/data....
  235.    
  236.         if($this->form_validation->run() == TRUE)
  237.             {
  238.                    
  239.                     //do what ever we need to do with the data here
  240.                     return true;
  241.             }//if
  242.            
  243.         else   
  244.             {
  245.            
  246.             $json_error_msgs= array("validation_status"=>false,"validation_message"=>false);
  247.            
  248.                 foreach($input_fields as $input)
  249.                     {
  250.                             $input_error_key = $input['field'].'_error';
  251.                             $input_error_msg = $input['field'].'_error_msg';
  252.                            
  253.                         if($this->form_validation->error($input['field']) !== '')// if there error message for a specific input is not blank then we have an error for said input
  254.                         {
  255.                             $json_error_msgs[$input_error_key] = true;
  256.                             $json_error_msgs[$input_error_msg] = $this->form_validation->error($input['field']);
  257.                            
  258.                         }//if
  259.                         else{
  260.                        
  261.                                 $json_error_msgs[$input_error_key] = false;
  262.                                 $json_error_msgs[$input_error_msg] = null;
  263.                              }//else
  264.                        
  265.                     }//foreach
  266.                    
  267.                    
  268.                        
  269.                     //return all error messages back to the client-side js
  270.                     echo json_encode($json_error_msgs);
  271.                     return false;
  272.             }//else
  273.    
  274.     }//if
  275.     else{
  276.    
  277.        
  278.         echo json_encode($incomplete_form_errors);
  279.        
  280.         return false;
  281.  
  282.     }//else
  283.    
  284. }//validate_form
  285.  
  286. }//Master_testimonials_model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement