public function refresh_step_parts()
 {
     Diagnostic_Tree_Model::$step_parts = $this->step_part_model->get();
     if (!empty(Diagnostic_Tree_Model::$step_parts)) {
         $step_parts_keys = array();
         foreach (Diagnostic_Tree_Model::$step_parts as $key => $step_part) {
             if (empty($step_parts_keys[$step_part->part_type_issue_type_step_id])) {
                 $step_parts_keys[$step_part->part_type_issue_type_step_id] = array();
             }
             $step_parts_keys[$step_part->part_type_issue_type_step_id][] = $key;
             Diagnostic_Tree_Model::$step_parts[$key]->name = Diagnostic_Tree_Model::$part_types_dropdown[$step_part->part_type_id];
         }
         Diagnostic_Tree_Model::$step_parts_keys = $step_parts_keys;
     }
 }
 public function parse_rules($unit_type_id)
 {
     $part_types = array();
     $counter = 0;
     $this->load->model('miniant/diagnostic_tree_model');
     $files = array(18 => 'evap_rules', 19 => 'refrig_rules', 20 => 'transport_rules', 37 => 'other_rules', 41 => 'mechanical_rules');
     $handle = fopen(APPPATH . 'modules/miniant/' . $files[$unit_type_id] . '.txt', 'r');
     if ($handle) {
         while (($buffer = fgets($handle, 4096)) != false) {
             if (strstr($buffer, '---')) {
                 $counter++;
                 continue;
             }
             if (empty($part_types[$counter])) {
                 $part_types[$counter] = new stdClass();
                 $part_types[$counter]->name = $buffer;
                 continue;
             }
             if (stristr($buffer, 'NEW ISSUE TYPE -')) {
                 continue;
             }
             if (stristr($buffer, 'EDIT -')) {
                 continue;
             }
             if (stristr($buffer, 'NEW STEP')) {
                 $bits = explode(' NEW STEP - ', $buffer);
                 if (empty($part_types[$counter]->issue_types)) {
                     $part_types[$counter]->issue_types = array();
                 }
                 $issue_type = new stdClass();
                 $issue_type->name = $bits[0];
                 $issue_type->steps = explode(',', $bits[1]);
                 $part_types[$counter]->issue_types[] = $issue_type;
                 continue;
             }
             // only notes left now
             if (empty($part_types[$counter]->notes)) {
                 $part_types[$counter]->notes = $buffer;
             } else {
                 $part_types[$counter]->notes .= $buffer;
             }
         }
     }
     foreach ($part_types as $part_type) {
         if ($existing_part_type = $this->part_type_model->get(array('unit_type_id' => $unit_type_id, 'name' => $part_type->name), true)) {
             $this->part_type_model->delete($existing_part_type->id);
         }
         if ($existing_part_type = $this->part_type_model->get(array('unit_type_id' => $unit_type_id, 'name' => ucfirst(strtolower($part_type->name))), true)) {
             $this->part_type_model->delete($existing_part_type->id);
         }
         $part_type->name = ucfirst(strtolower($part_type->name));
         $part_type->unit_type_id = $unit_type_id;
         $part_type->can_have_issues = true;
         $part_type->in_template = false;
         $part_type->instructions = $part_type->notes;
         unset($part_type->notes);
         $issue_types = $part_type->issue_types;
         unset($part_type->issue_types);
         $part_type_id = $this->part_type_model->add($part_type);
         Diagnostic_Tree_Model::$part_types[] = $part_type;
         Diagnostic_Tree_Model::$part_types_dropdown[$part_type_id] = $part_type->name;
         foreach ($issue_types as $part_type_issue_type) {
             $issue_type_params = array('name' => $part_type_issue_type->name);
             if (!($issue_type = $this->issue_type_model->get($issue_type_params, true))) {
                 $issue_type = (object) $issue_type_params;
                 $issue_type->id = $this->issue_type_model->add($issue_type_params);
             }
             $part_type_issue_type_id = $this->diagnostic_tree_model->add_part_type_issue_type($part_type_id, $issue_type->id);
             foreach ($part_type_issue_type->steps as $step) {
                 if ($this->step_model->get(array('name' => trim($step)), true)) {
                     $step_id = $this->step_model->get(array('name' => trim($step)), true)->id;
                 } else {
                     $step_id = $this->step_model->add(array('name' => trim($step), 'past_tense' => trim($step)));
                     Diagnostic_Tree_Model::$steps = $this->step_model->get_dropdown('name', false);
                 }
                 $part_type_issue_type_step = new stdClass();
                 $part_type_issue_type_step->required = false;
                 $step_part = new stdClass();
                 if ($step == 'Replace') {
                     $step_part->part_type_id = $part_type_id;
                     $part_type_issue_type_step->required = true;
                 } else {
                     $part_type_issue_type_step->required = true;
                     $step_part->part_type_id = $this->part_type_model->get_id('Labour', $unit_type_id);
                 }
                 $part_type_issue_type_step->immediate = true;
                 $part_type_issue_type_step->needs_sq = false;
                 $part_type_issue_type_step->part_type_issue_type_id = $part_type_issue_type_id;
                 $part_type_issue_type_step->step_id = $step_id;
                 $part_type_issue_type_step->id = $this->diagnostic_tree_model->add_part_type_issue_type_step($part_type_issue_type_step);
                 $step_part->part_type_issue_type_step_id = $part_type_issue_type_step->id;
                 $step_part->quantity = 1;
                 $step_part->id = $this->diagnostic_tree_model->add_required_part($step_part);
             }
         }
     }
 }