Commits

Jesse Terry committed dcc6c2f

Inventory: Fixes #122

  • Participants
  • Parent commits 36d1770

Comments (0)

Files changed (2)

File application/language/english/mcb_lang.php

 /* NEW FOR 0.9.5 */
 $lang['email_footer'] = 'Email Footer';
 $lang['footer'] = 'Footer';
+$lang['inventory_type_in_use'] = 'The Inventory Type specified is already in use.';
 
 ?>

File application/modules_core/inventory/models/mdl_inventory_types.php

 
 class Mdl_Inventory_Types extends MY_Model {
 
-	public function __construct() {
+    public function __construct() {
 
-		parent::__construct();
+        parent::__construct();
 
-		$this->table_name = 'mcb_inventory_types';
+        $this->table_name = 'mcb_inventory_types';
 
-		$this->primary_key = 'mcb_inventory_types.inventory_type_id';
+        $this->primary_key = 'mcb_inventory_types.inventory_type_id';
 
-		$this->select_fields = 'SQL_CALC_FOUND_ROWS *';
+        $this->select_fields = 'SQL_CALC_FOUND_ROWS *';
 
-		$this->order_by = 'inventory_type';
+        $this->order_by = 'inventory_type';
 
-		$this->limit = $this->mdl_mcb_data->setting('results_per_page');
+        $this->limit = $this->mdl_mcb_data->setting('results_per_page');
 
-	}
+    }
 
-	public function validate() {
+    public function validate() {
 
-		$this->form_validation->set_rules('inventory_type', $this->lang->line('inventory_type'), 'required');
+        $this->form_validation->set_rules('inventory_type', $this->lang->line('inventory_type'), 'required|callback_is_unique');
 
-		return parent::validate();
+        return parent::validate($this);
 
-	}
+    }
+
+    public function is_unique($inventory_type) {
+
+        /** Checks to make sure the inventory type is not already in use **/
+
+        $inventory_type_id = uri_assoc('inventory_type_id', 4);
+
+        $this->db->where('inventory_type', $inventory_type);
+
+        if ($inventory_type_id) {
+
+            $this->db->where('inventory_type_id <>', $inventory_type_id);
+
+        }
+
+        $num = $this->db->get('mcb_inventory_types')->num_rows();
+
+        if ($num > 0) {
+
+            $this->form_validation->set_message('is_unique', $this->lang->line('inventory_type_in_use'));
+
+            return FALSE;
+
+        }
+
+        return TRUE;
+
+    }
 
     public function delete($inventory_type_id) {
 
+        /** Deletes the inventory type and unlinks any existing inventory items from the type **/
+
         $this->db->where('inventory_type_id', $inventory_type_id);
 
         $this->db->set('inventory_type_id', 0);