Exemplo n.º 1
0
 /**
  * カテゴリーの登録.
  *
  * @param array $data
  * @return int|bool
  */
 public function save($data)
 {
     $objQuery = Application::alias('eccube.query');
     $category_id = $data['category_id'];
     // ミリ秒付きの時間文字列を取得. CSVへの対応.
     // トランザクション内のCURRENT_TIMESTAMPは全てcommit()時の時間に統一されてしまう為.
     $query = array('update_date' => Utils::getFormattedDateWithMicroSecond());
     $objQuery->begin();
     if ($category_id == '') {
         // 新規登録
         $parent_category_id = $data['parent_category_id'];
         $rank = null;
         if ($parent_category_id == 0) {
             // ROOT階層で最大のランクを取得する。
             $where = 'parent_category_id = ?';
             $rank = $objQuery->max('rank', 'dtb_category', $where, array($parent_category_id)) + 1;
         } else {
             // 親のランクを自分のランクとする。
             $where = 'category_id = ?';
             $rank = $objQuery->get('rank', 'dtb_category', $where, array($parent_category_id));
             // 追加レコードのランク以上のレコードを一つあげる。
             $where = 'rank >= ?';
             $arrRawSql = array('rank' => '(rank + 1)');
             $objQuery->update('dtb_category', array(), $where, array($rank), $arrRawSql);
         }
         $where = 'category_id = ?';
         // 自分のレベルを取得する(親のレベル + 1)
         $level = $objQuery->get('level', 'dtb_category', $where, array($parent_category_id)) + 1;
         $query['category_id'] = $objQuery->nextVal('dtb_category_category_id');
         $query['category_name'] = $data['category_name'];
         $query['parent_category_id'] = $data['parent_category_id'];
         $query['create_date'] = $query['update_date'];
         $query['creator_id'] = $_SESSION['member_id'];
         $query['rank'] = $rank;
         $query['level'] = $level;
         $result = $objQuery->insert('dtb_category', $query);
     } else {
         // 既存編集
         $query['category_id'] = $category_id;
         $query['parent_category_id'] = $data['parent_category_id'];
         $query['category_name'] = $data['category_name'];
         $where = 'category_id = ?';
         $result = $objQuery->update('dtb_category', $query, $where, array($category_id));
     }
     $objQuery->commit();
     return $result ? $query['category_id'] : FALSE;
 }