/**
  * action ของตารางหมวดหมู่
  */
 public function action()
 {
     // referer, session, member
     if (self::$request->initSession() && self::$request->isReferer() && ($login = Login::isAdmin())) {
         if ($login['email'] == 'demo') {
             echo Language::get('Unable to complete the transaction');
         } else {
             // รับค่าจากการ POST
             $id = self::$request->post('id')->toString();
             $value = self::$request->post('value')->toInt();
             if (preg_match('/^category_([0-9]+)$/', $id, $match)) {
                 $action = 'category';
                 $module_id = (int) $match[1];
             } else {
                 $action = self::$request->post('action')->toString();
                 $module_id = self::$request->post('mid')->toInt();
             }
             // ตรวจสอบโมดูล
             $index = false;
             if (!empty($module_id)) {
                 $index = \Index\Module\Model::getModule($module_id);
             }
             if ($action === 'category') {
                 // อ่านข้อมูลหมวดหมู่ตอนเขียนบทความ
                 $category = $this->db()->first($this->getFullTableName('category'), array(array('category_id', (int) $value), array('module_id', (int) $index->id)));
                 if ($category) {
                     $config = @unserialize($category->config);
                     if (!is_array($config)) {
                         $config = array('can_reply' => 0);
                     }
                     $ret = array('can_reply' => empty($config['can_reply']) ? 0 : 1, 'published' => empty($category->published) ? 0 : 1);
                     // คืนค่าเป็น JSON
                     echo json_encode($ret);
                 }
             } elseif ($index && Gcms::canConfig($login, $index, 'can_config')) {
                 if ($action === 'delete' && preg_match('/^[0-9,]+$/', $id)) {
                     // ลบหมวด
                     $query = $this->db()->createQuery()->select('id', 'icon')->from('category')->where(array(array('id', explode(',', $id)), array('module_id', $index->id)))->toArray();
                     $ids = array();
                     foreach ($query->execute() as $item) {
                         $ids[] = $item['id'];
                         foreach (ArrayTool::unserialize($item['icon']) as $icon) {
                             @unlink(ROOT_PATH . DATA_FOLDER . $item['owner'] . '/' . $icon);
                         }
                     }
                     if (!empty($ids)) {
                         // ลบหมวดหมู่
                         $this->db()->createQuery()->delete('category', array(array('id', $ids), array('module_id', $index->id)))->execute();
                     }
                 } else {
                     $category = $this->db()->first($this->getFullTableName('category'), array(array('id', (int) $id), array('module_id', (int) $index->id)));
                     if ($category) {
                         if ($action === 'categoryid') {
                             // แก้ไข category_id หน้ารายการหมวดหมู่
                             // ค้นหาหมวดหมู่ซ้ำ
                             $search = $this->db()->createQuery()->from('category')->where(array(array('module_id', (int) $index->id), array('category_id', $value)))->first('id');
                             if ($search) {
                                 // มี category_id อยู่ก่อนแล้วคืนค่ารายการเดิม
                                 $ret['categoryid_' . $index->id . '_' . $category->id] = $category->category_id;
                             } else {
                                 // save
                                 $this->db()->createQuery()->update('category')->set(array('category_id' => $value))->where((int) $category->id)->execute();
                                 // คืนค่ารายการใหม่
                                 $ret['categoryid_' . $index->id . '_' . $category->id] = $value;
                             }
                         } elseif ($action === 'published' || $action === 'can_reply') {
                             // เผยแพร่, การแสดงความคิดเห็น
                             if ($action === 'can_reply') {
                                 $config = @unserialize($category->config);
                                 if (!is_array($config)) {
                                     $config = array();
                                 }
                                 $config['can_reply'] = empty($config['can_reply']) ? 1 : 0;
                                 $save = array('config' => serialize($config));
                                 // คืนค่า
                                 $ret['elem'] = 'can_reply_' . $category->id;
                                 $lng = Language::get('REPLIES');
                                 $ret['title'] = $lng[$config['can_reply']];
                                 $ret['class'] = 'icon-reply reply' . $config['can_reply'];
                             } else {
                                 $save = array('published' => $category->published == 1 ? 0 : 1);
                                 // คืนค่า
                                 $ret['elem'] = 'published_' . $category->id;
                                 $lng = Language::get('PUBLISHEDS');
                                 $ret['title'] = $lng[$save['published']];
                                 $ret['class'] = 'icon-published' . $save['published'];
                             }
                             $this->db()->update($this->getFullTableName('category'), $category->id, $save);
                         }
                         // คืนค่าเป็น JSON
                         echo json_encode($ret);
                     }
                 }
             }
         }
     } else {
         echo Language::get('Unable to complete the transaction');
     }
 }