* @link http://www.netcommons.org NetCommons Project * @license http://www.netcommons.org/license.txt NetCommons License * @copyright Copyright 2014, NetCommons Project */ App::uses('LinksAppController', 'Links.Controller'); /** * Links Controller * * @author Ryuji AMANO * @package NetCommons\Links\Controller * * @property Link $Link */ class LinksController extends LinksAppController { public $helpers = array('Links.LinksStatus'); /** * use model * * @var array */ public $uses = array( 'Links.Link', 'Links.LinkCategory', ); /** * use component * * @var array */ public $components = array( 'NetCommons.NetCommonsBlock', //use Announcement model 'NetCommons.NetCommonsFrame', 'NetCommons.NetCommonsRoomRole', ); /** * beforeFilter * * @return void */ public function beforeFilter() { parent::beforeFilter(); $this->Auth->allow(); $frameId = (isset($this->params['pass'][0]) ? (int)$this->params['pass'][0] : 0); //Frameのデータをviewにセット if (! $this->NetCommonsFrame->setView($this, $frameId)) { // 何もかえしてこなくなった // $this->response->statusCode(400); // return; } //Roleのデータをviewにセット if (! $this->NetCommonsRoomRole->setView($this)) { // $this->response->statusCode(400); // return; } } /** * index method * * @param int $frameId frames.id * @param string $lang ex)"en" or "ja" etc. * @return CakeResponse A response object containing the rendered view. */ public function index($frameId = 0, $type = 'list') { // $type = 'dropdown'; $this->set('type', $type); // カテゴリ一覧を取得 $categories = $this->LinkCategory->getCategories( $this->viewVars['blockId'] // MyTodo まだブロックレコードがないときは0なので、どうする?s ); foreach($categories as &$category){ //Linkデータを取得 $links = $this->Link->getLinksByCategoryId( $category['LinkCategory']['id'], $this->viewVars['blockId'], $this->viewVars['contentEditable'] ); $category['links'] = $links; } $this->set('categories', $categories); return $this->render('Links/index'); } /** * show linkAdd method * * @param int $frameId frames.id * @return CakeResponse A response object containing the rendered view. */ public function linkAdd($frameId = 0) { if ($this->response->statusCode() !== 200) { return $this->render(false); } //編集権限チェック if (! $this->viewVars['contentEditable']) { $this->response->statusCode(403); return $this->render(false); } return $this->render('Links/link_add', false); } /** * show manage method * * @param int $frameId frames.id * @return CakeResponse A response object containing the rendered view. */ public function manage($frameId = 0) { if ($this->response->statusCode() !== 200) { return $this->render(false); } //編集権限チェック if (! $this->viewVars['contentEditable']) { $this->response->statusCode(403); return $this->render(false); } return $this->render('Links/manage', false); } public function add_form($frameId = 0) { return $this->render('Links/add_form', false); } public function add($frameId) { if (! $this->request->isPost()) { throw new MethodNotAllowedException(); } $postData = $this->data; //保存 if ($this->Link->add($this->data)) { $result = array( 'name' => __d('net_commons', 'Successfully finished.'), 'link' => $postData, ); $this->set(compact('result')); $this->set('_serialize', 'result'); return $this->render(false); } else { $error = __d('net_commons', 'Failed to register data.'); $error .= $this->formatValidationErrors($this->Link->validationErrors); throw new ForbiddenException($error); } } // json で呼ぶこと前提 indexとほぼ一緒 // MyTodo indexもjson apiでデータもらう形に変えたらよさげ public function all($frameId = 0) { // カテゴリ一覧を取得 $categories = $this->LinkCategory->getCategories( $this->viewVars['blockId'] // MyTodo まだブロックレコードがないときは0なので、どうする?s ); foreach($categories as &$category){ //Linkデータを取得 $links = $this->Link->getLinksByCategoryId( $category['LinkCategory']['id'], $this->viewVars['blockId'], $this->viewVars['contentEditable'] ); $category['links'] = $links; } $this->set('categories', $categories); $this->set('_serialize', 'categories'); return $this->render(false); } public function update_weight_form($frameId = 0) { // return $this->render('Links/update_weight_form', false); } public function delete_form($frameId = 0, $linkId) { $this->set(compact('frameId', 'linkId')); } public function delete($frameId = 0) { if (! $this->request->isPost()) { throw new MethodNotAllowedException(); } // MyTodo リンクIDから削除する権限があるか確認 $id = $this->data['Link']['id']; //保存 if ($this->Link->delete($id)) { // MyTodo 同一リンクの別バリエーションデータが存在しなければorderも削除。 $result = array( 'name' => __d('net_commons', 'Successfully finished.'), ); $this->set(compact('result')); $this->set('_serialize', 'result'); return $this->render(false); } else { throw new ForbiddenException(__d('net_commons', 'Failed to register data.')); } } public function edit_form($frameId = 0, $linkId) { $this->set(compact('frameId', 'linkId')); } public function edit($frameId, $linkId) { if (! $this->request->isPost()) { throw new MethodNotAllowedException(); } $postData = $this->data; //保存 if ($this->Link->save($this->data)) { $result = array( 'name' => __d('net_commons', 'Successfully finished.'), 'link' => $postData, ); $this->set(compact('result')); $this->set('_serialize', 'result'); return $this->render(false); } else { $error = __d('net_commons', 'Failed to register data.'); $error .= $this->formatValidationErrors($this->Link->validationErrors); throw new ForbiddenException($error); } } public function visit($linkId) { $link = $this->Link->findById($linkId); $link['Link']['click_number']++; $this->Link->save($link); $this->redirect($link['Link']['url']); return false; } // MyTodo モデルに移動するか、ヘルパかコンポーネントかビヘイビアにする… protected function formatValidationErrors($validationErrors) { $errors = array(); foreach($validationErrors as $field => $fieldErrors){ foreach($fieldErrors as $errorMessage){ $errors[] = __d('links', $field) . ':' . __d('links', $errorMessage); } } $returnMessage = implode("\n", $errors); return $returnMessage; } public function test(){ } }