Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Master_testimonials_model extends CI_Model
- {
- public $language = 'english';
- public $incomplete_inputs = '';
- public $succesful_validation_msg = '';
- function __construct()
- {
- parent::__construct();
- }
- /*============================*/
- // get_num_of_new_testimonials
- /*============================*/
- public function get_num_of_new_testimonials() {
- $num_of_new_testimonials_q = $this->db->get_where('nv_cms_testimonials',array('testimonial_approved'=>0));
- return $num_of_new_testimonials_q->num_rows();
- }
- /*============================*/
- // get_new_testimonials
- /*============================*/
- public function get_new_testimonials()
- {
- $new_testimonials_q = $this->db->get_where('nv_cms_testimonials',array('testimonial_approved'=>0));
- return $new_testimonials_q;
- }
- /*============================*/
- // get_all_testimonials_with_pagination
- /*============================*/
- public function get_all_testimonials_with_pagination($limit, $offset) {
- $testimonials = $this->db->get_where('nv_cms_testimonials',array('testimonial_approved'=>1),$limit,$offset);
- return $testimonials;
- }
- /*============================*/
- // get_all_testimonials
- /*============================*/
- public function get_all_testimonials() {
- $testimonials = $this->db->get_where('nv_cms_testimonials',array('testimonial_approved'=>1));
- return $testimonials->num_rows();
- }
- /*========================*/
- //get_config_value_plain()
- /*========================*/
- public function get_config_value_plain($config_name) {
- $config_value = $this->db->get_where('nv_cms_testimonials_config',array('config_name'=>$config_name))->row()->config_value;
- return $config_value;
- }
- /*=======================*/
- //update_required_fields()
- /*=======================*/
- public function update_required_fields()
- {
- $update = array("config_value"=>$this->input->post('tc_require_email'));
- $this->db->where('config_name','tc_require_email');
- $this->db->update('nv_cms_testimonials_config',$update);
- $update = array("config_value"=>$this->input->post('tc_require_website'));
- $this->db->where('config_name','tc_require_website');
- $this->db->update('nv_cms_testimonials_config',$update);
- $update = array("config_value"=>$this->input->post('tc_require_phone'));
- $this->db->where('config_name','tc_require_phone');
- $this->db->update('nv_cms_testimonials_config',$update);
- }
- /*=========================*/
- //set_config_value_plain()
- /*=========================*/
- public function set_config_value_plain($config_name,$config_value) {
- $update = array("config_value"=>$config_value);
- $this->db->where("config_name",$config_name);
- $this->db->update('nv_cms_testimonials_config',$update);
- }
- /*=========================*/
- // get_testimonial_by_id()
- /*=========================*/
- public function get_testimonial_by_id($id)
- {
- $testimonial_q = $this->db->get_where('nv_cms_testimonials',array('id'=>$id));
- return $testimonial_q->row();
- }
- /*===========================*/
- // set_form_validation_rules
- /*===========================*/
- public function set_form_validation_rules($field) {
- if(strcasecmp($field, 'testimonial_author_email') == 0 )
- {
- $tc_require_email = $this->get_config_value_plain('tc_require_email');
- $field_rules = $tc_require_email === 'true' ? 'required|valid_email' : '';
- return $field_rules;
- }
- elseif(strcasecmp($field, 'testimonial_author_phone') == 0 )
- {
- $tc_require_phone = $this->get_config_value_plain('tc_require_phone');
- $field_rules = $tc_require_phone === 'true' ? 'required': '';
- return $field_rules;
- }
- elseif(strcasecmp($field, 'testimonial_author_website') == 0 )
- {
- $tc_require_website = $this->get_config_value_plain('tc_require_website');
- $field_rules = $tc_require_website === 'true' ? 'required': '';
- return $field_rules;
- }
- }
- /*======================*/
- // update_testimonial
- /*=====================*/
- public function update_testimonial($id)
- {
- $input_fields = array(
- array(
- 'field' => 'testimonial',
- 'label' => 'name',
- 'rules' => 'required'
- ),
- array(
- 'field' => 'testimonial_author',
- 'label' => 'phone',
- 'rules' => 'required'
- ),
- array(
- 'field' => 'testimonial_author_email',
- 'label' => 'Email',
- 'rules' => $this->set_form_validation_rules('testimonial_author_email')
- ),
- array(
- 'field' => 'testimonial_author_phone',
- 'label' => 'phone',
- 'rules' => $this->set_form_validation_rules('testimonial_author_phone')
- ),
- array(
- 'field' => 'testimonial_author_website',
- 'label' => 'email',
- 'rules' => $this->set_form_validation_rules('testimonial_author_website')
- )
- );
- if($this->validate_form($input_fields,$this->language))
- {
- /*manually map the inputs to the database table columns*/
- $update = array(
- 'testimonial' => $this->input->post('testimonial'),
- 'author_name' => $this->input->post('testimonial_author'),
- 'author_email' => $this->input->post('testimonial_author_email'),
- 'author_phone' => preg_replace("/[^0-9]/", "", $this->input->post('testimonial_author_phone')),
- 'author_website' => $this->input->post('testimonial_author_website')
- );
- $this->db->where('id',$id);
- $this->db->update('nv_cms_testimonials', $update);
- echo json_encode(array('validation_status'=>true,'validation_message'=>$this->succesful_validation_msg));
- }
- }
- /*=========================*/
- // validate_form()
- /*=========================*/
- public function validate_form($input_fields,$language)
- {
- if(strcasecmp($language, 'spanish') == 0)
- {
- /*status messages*/
- $this->incomplete_inputs = 'Por favor de llenar todos los datos!';
- $this->succesful_validation_msg = 'Su formulario ha sido enviado! Mucha Gracias!';
- } elseif(strcasecmp($language, 'english') == 0){
- /*status messages*/
- $this->incomplete_inputs = 'Please fill in all the fields!';
- $this->succesful_validation_msg = 'Your Settings have been saved! ';
- }
- //error if some or all of the fields are not completed
- $incomplete_form_errors = array('validation_status'=>false,'validation_message'=>$this->incomplete_inputs);
- /*load form validation library*/
- $this->load->library('form_validation');
- $this->form_validation->set_rules($input_fields);
- $this->form_validation->set_error_delimiters('', '');
- //counter to keep track of empty/invalid inputs
- $inputs_incomplete_tracker=0; // start with 0, we assume all to have data in them...
- /*check that all $post inputs have data in them */
- foreach($input_fields as $input)
- {
- if(($this->input->post($input['field']) == FALSE) &&
- (strpos($input['rules'],'required') !== FALSE) )
- {
- $inputs_incomplete_tracker++;
- $incomplete_form_errors[$input['field'].'_error'] = true;
- $incomplete_form_errors[$input['field'].'_error_msg'] = $input['field'];
- }
- }
- if($inputs_incomplete_tracker == 0)//zero means there are 0 uncompleted inputs... all have been completed (filled in by user)
- {
- //all inputs have data in them.. so now we check the validity of the inputs/data....
- if($this->form_validation->run() == TRUE)
- {
- //do what ever we need to do with the data here
- return true;
- }//if
- else
- {
- $json_error_msgs= array("validation_status"=>false,"validation_message"=>false);
- foreach($input_fields as $input)
- {
- $input_error_key = $input['field'].'_error';
- $input_error_msg = $input['field'].'_error_msg';
- 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
- {
- $json_error_msgs[$input_error_key] = true;
- $json_error_msgs[$input_error_msg] = $this->form_validation->error($input['field']);
- }//if
- else{
- $json_error_msgs[$input_error_key] = false;
- $json_error_msgs[$input_error_msg] = null;
- }//else
- }//foreach
- //return all error messages back to the client-side js
- echo json_encode($json_error_msgs);
- return false;
- }//else
- }//if
- else{
- echo json_encode($incomplete_form_errors);
- return false;
- }//else
- }//validate_form
- }//Master_testimonials_model
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement