Example #1
0
 public function insertProductAttribute($attribute_id, $attribute_value)
 {
     $attr = new Attribute();
     if (is_numeric($this->product_id) && $this->product_id > 0) {
         $attribute_details = $attr->getAttributeDetails($attribute_id);
         if (count($attribute_details) > 0) {
             $rules_arr = $message_arr = array();
             $add_data = true;
             $error_message = '';
             $id = $attribute_id;
             $key = 'attribute_' . $id;
             $input_arr[$key] = $attribute_value;
             if ($attribute_details['validation_rules'] != '') {
                 $rule_str = str_replace('minlength-', 'min:', $attribute_details['validation_rules']);
                 $rule_str = str_replace('maxlength-', 'max:', $rule_str);
                 $rules_arr[$key] = $rule_str;
                 $message_arr[$key . '.required'] = $attribute_details['attribute_label'] . ' required';
                 $message_arr[$key . '.alpha'] = $attribute_details['attribute_label'] . ' should contain alphabets only';
                 $message_arr[$key . '.numeric'] = $attribute_details['attribute_label'] . ' should contain numeric only';
             }
             if (count($rules_arr) > 0) {
                 $validator = \Validator::make($input_arr, $rules_arr, $message_arr);
                 if ($validator->fails()) {
                     $add_data = false;
                     $error_message = $validator->errors()->all();
                 }
             }
             if ($add_data) {
                 $data_arr = array('product_id' => $this->product_id, 'attribute_id' => $attribute_id, 'attribute_value' => $attribute_value);
                 ProductAttributesValues::insertGetId($data_arr);
                 return json_encode(array('status' => 'success'));
             } else {
                 return json_encode(array('status' => 'error', 'error_messages' => $error_message));
             }
         }
     }
 }