public function saveAttributeOption(JosAttributeOption $attribute_option) { $data = array('attribute_id' => $attribute_option->getAttributeId(), 'key' => $attribute_option->getKey(), 'label' => $attribute_option->getLabel()); $value_id = (int) $attribute_option->getValueId(); if ($value_id == 0) { $this->tableGateway->insert($data); } else { if ($this->getAttributeOptionByArrayConditionAndArrayColumn(array('value_id' => $value_id), array('key', 'value'))) { $this->tableGateway->update($data, array('value_id' => $value_id)); } else { return false; } } return true; }
public function editOptionAction() { $attribute_id = $this->params('id'); $return_array = array(); if ($attribute_id) { // kiểm tra nếu attribute id vừa đưa qua thuộc loại select $jos_attribute_table = $this->getServiceLocator()->get('Attribute\\Model\\JosAttributeTable'); $attribute_exist = $jos_attribute_table->getAttributeByArrayConditionAndArrayColumn(array('attribute_id' => $attribute_id), array('frontend_input')); // lỗi không phải select if (!$attribute_exist or $attribute_exist[0]['frontend_input'] != 'Select') { $this->flashMessenger()->addErrorMessage('Lỗi, thuộc tính cần sửa không có dữ liệu option. Vui lòng kiểm tra lại!'); return $this->redirect()->toRoute('thuoc_tinh/crud', array('action' => 'index')); } // gửi attribute_id ra view $return_array['attribute_id'] = $attribute_id; // attribute_option $jos_attribute_option_table = $this->getServiceLocator()->get('Attribute\\Model\\JosAttributeOptionTable'); $options = $jos_attribute_option_table->getAttributeOptionByArrayConditionAndArrayColumn(array('attribute_id' => $attribute_id)); $return_array['options'] = $options; // edit form $edit_form = new EditAttributeOptionForm($this->getServiceLocator()); $edit_form->setData(array('attribute_id' => $attribute_id)); $return_array['edit_form'] = $edit_form; // kiểm tra nếu post $request = $this->getRequest(); if ($request->isPost()) { $post = $request->getPost(); // kiểm tra nếu không có post option nào hết if (!isset($post['frontend_input_value']) or !$post['frontend_input_value'][0] or !$post['frontend_input_label'][0]) { $this->flashMessenger()->addErrorMessage('Lỗi, option không tồn tại. Vui lòng kiểm tra lại!'); return $this->redirect()->toRoute('thuoc_tinh/crud', array('action' => 'index')); } $edit_form->setData(array('attribute_id' => $attribute_id)); // form hợp lý if ($edit_form->isValid()) { // tạo điểm truy cập csdl bảng attribute_option $jos_attribute_option_table = $this->getServiceLocator()->get('Attribute\\Model\\JosAttributeOptionTable'); // xóa option cũ $jos_attribute_option_table->deleteAttributeOptionByAttributeId($attribute_id); // xử lý lưu foreach ($post['frontend_input_value'] as $key => $value) { if ($value and $post['frontend_input_label'][$key]) { $jos_attribute_option_exist = $jos_attribute_option_table->getAttributeOptionByArrayConditionAndArrayColumn(array('attribute_id' => $attribute_id, 'key' => $post['frontend_input_label'][$key]), array('value_id')); if ($jos_attribute_option_exist) { continue; } $new_attribute_option = new JosAttributeOption(); $new_attribute_option->setAttributeId($attribute_id); $new_attribute_option->setKey($post['frontend_input_label'][$key]); $new_attribute_option->setLabel($value); $jos_attribute_option_table->saveAttributeOption($new_attribute_option); } } // lưu thành công $this->flashMessenger()->addSuccessMessage('Chúc mừng, sửa thuộc tính thành công!'); return $this->redirect()->toRoute('thuoc_tinh/crud', array('action' => 'index')); } else { $this->flashMessenger()->addErrorMessage('Lỗi, form không hợp lệ. Vui lòng kiểm tra lại!'); return $this->redirect()->toRoute('thuoc_tinh/crud', array('action' => 'index')); } } return $return_array; } $this->flashMessenger()->addErrorMessage('Lỗi, không tìm thấy id. Vui lòng kiểm tra lại!'); return $this->redirect()->toRoute('thuoc_tinh/crud', array('action' => 'index')); }