Example #1
0
 public function create()
 {
     if (!$this->has_post()) {
         return redirect_message(array('admin', 'work_tags', $this->tag->id, 'works', 'add'), array('_flash_message' => '非 POST 方法,錯誤的頁面請求。'));
     }
     $posts = OAInput::post();
     $cover = OAInput::file('cover');
     $pictures = OAInput::file('pictures[]');
     if (!$cover) {
         return redirect_message(array('admin', 'work_tags', $this->tag->id, 'works', 'add'), array('_flash_message' => '請選擇照片(gif、jpg、png)檔案,或提供照片網址!', 'posts' => $posts));
     }
     if ($msg = $this->_validation_posts($posts)) {
         return redirect_message(array('admin', 'work_tags', $this->tag->id, 'works', 'add'), array('_flash_message' => $msg, 'posts' => $posts));
     }
     $work = null;
     $create = Work::transaction(function () use($posts, $cover, &$work) {
         return verifyCreateOrm($work = Work::create(array_intersect_key($posts, Work::table()->columns))) && $work->cover->put($cover);
     });
     if (!($create && $work)) {
         return redirect_message(array('admin', 'work_tags', $this->tag->id, 'works', 'add'), array('_flash_message' => '新增失敗!', 'posts' => $posts));
     }
     $tag = $this->tag;
     WorkTagMapping::transaction(function () use($tag, $work) {
         return verifyCreateOrm(WorkTagMapping::create(array_intersect_key(array('work_tag_id' => $tag->id, 'work_id' => $work->id), WorkTagMapping::table()->columns)));
     });
     if ($blocks = $posts['blocks']) {
         foreach ($blocks as $block) {
             if (!($b = null) && WorkBlock::transaction(function () use($block, $work, &$b) {
                 return verifyCreateOrm($b = WorkBlock::create(array_intersect_key(array_merge($block, array('work_id' => $work->id)), WorkBlock::table()->columns)));
             })) {
                 if (($items = $block['items']) && $b) {
                     foreach ($items as $item) {
                         WorkBlockItem::transaction(function () use($item, $b) {
                             return verifyCreateOrm(WorkBlockItem::create(array_intersect_key(array_merge($item, array('work_block_id' => $b->id)), WorkBlockItem::table()->columns)));
                         });
                     }
                 }
             }
         }
     }
     if ($pictures) {
         foreach ($pictures as $picture) {
             WorkPicture::transaction(function () use($picture, $work) {
                 return verifyCreateOrm($pic = WorkPicture::create(array_intersect_key(array_merge($picture, array('work_id' => $work->id)), WorkPicture::table()->columns))) && $pic->name->put($picture);
             });
         }
     }
     return redirect_message(array('admin', 'work_tags', $tag->id, 'works'), array('_flash_message' => '新增成功!'));
 }
Example #2
0
 public function update()
 {
     if (!$this->has_post()) {
         return redirect_message(array('admin', $this->get_class(), $this->work->id, 'edit'), array('_flash_message' => '非 POST 方法,錯誤的頁面請求。'));
     }
     $posts = OAInput::post();
     $cover = OAInput::file('cover');
     if (!((string) $this->work->cover || $cover)) {
         return redirect_message(array('admin', $this->get_class(), $this->work->id, 'edit'), array('_flash_message' => '請選擇圖片(gif、jpg、png)檔案!', 'posts' => $posts));
     }
     if ($msg = $this->_validation_posts($posts)) {
         return redirect_message(array('admin', $this->get_class(), $this->work->id, 'edit'), array('_flash_message' => $msg, 'posts' => $posts));
     }
     if ($columns = array_intersect_key($posts, $this->work->table()->columns)) {
         foreach ($columns as $column => $value) {
             $this->work->{$column} = $value;
         }
     }
     $work = $this->work;
     $update = WorkTag::transaction(function () use($work, $cover) {
         if (!$work->save()) {
             return false;
         }
         if ($cover && !$work->cover->put($cover)) {
             return false;
         }
         return true;
     });
     if (!$update) {
         return redirect_message(array('admin', $this->get_class(), $this->work->id, 'edit'), array('_flash_message' => '更新失敗!', 'posts' => $posts));
     }
     if (($del_ids = array_diff(column_array($work->pictures, 'id'), $posts['pic_ids'])) && ($pictures = WorkPicture::find('all', array('select' => 'id, name', 'conditions' => array('id IN (?)', $del_ids))))) {
         foreach ($pictures as $picture) {
             WorkPicture::transaction(function () use($picture) {
                 return $picture->destroy();
             });
         }
     }
     if ($pictures = OAInput::file('pictures[]')) {
         foreach ($pictures as $picture) {
             WorkPicture::transaction(function () use($picture, $work) {
                 return verifyCreateOrm($pic = WorkPicture::create(array_intersect_key(array_merge($picture, array('work_id' => $work->id)), WorkPicture::table()->columns))) && $pic->name->put($picture);
             });
         }
     }
     $ori_ids = column_array($work->mappings, 'work_tag_id');
     if (($del_ids = array_diff($ori_ids, $posts['tag_ids'])) && ($mappings = WorkTagMapping::find('all', array('select' => 'id, work_tag_id', 'conditions' => array('work_id = ? AND work_tag_id IN (?)', $work->id, $del_ids))))) {
         foreach ($mappings as $mapping) {
             WorkTagMapping::transaction(function () use($mapping) {
                 return $mapping->destroy();
             });
         }
     }
     if (($add_ids = array_diff($posts['tag_ids'], $ori_ids)) && ($tags = WorkTag::find('all', array('select' => 'id', 'conditions' => array('id IN (?)', $add_ids))))) {
         foreach ($tags as $tag) {
             WorkTagMapping::transaction(function () use($tag, $work) {
                 return verifyCreateOrm(WorkTagMapping::create(array_intersect_key(array('work_tag_id' => $tag->id, 'work_id' => $work->id), WorkTagMapping::table()->columns)));
             });
         }
     }
     $clean_blocks = WorkBlock::transaction(function () use($work) {
         foreach ($work->blocks as $block) {
             if (!$block->destroy()) {
                 return false;
             }
         }
         return true;
     });
     if ($blocks = $posts['blocks']) {
         foreach ($blocks as $block) {
             if (!($b = null) && WorkBlock::transaction(function () use($block, $work, &$b) {
                 return verifyCreateOrm($b = WorkBlock::create(array_intersect_key(array_merge($block, array('work_id' => $work->id)), WorkBlock::table()->columns)));
             })) {
                 if (($items = $block['items']) && $b) {
                     foreach ($items as $item) {
                         WorkBlockItem::transaction(function () use($item, $b) {
                             return verifyCreateOrm(WorkBlockItem::create(array_intersect_key(array_merge($item, array('work_block_id' => $b->id)), WorkBlockItem::table()->columns)));
                         });
                     }
                 }
             }
         }
     }
     $this->_clean_cell($work);
     return redirect_message(($url = Session::getData('admin_works_index_url')) ? $url : array('admin', $this->get_class()), array('_flash_message' => '更新成功!'));
 }