<?php
App::uses('AppController', 'Controller');
/**
 * Images Controller
 *
 * @property Image $Image
 * @property PaginatorComponent $Paginator
 */




class ImagesController extends AppController {

	//$usesの指定は最初大文字！！！
	public $uses = array('Image');
	//public $helpers = array('UploadPack.Upload');	
	public $layout = 'image_bootstrap3';
	
//	public function isAuthorized($user) {
//		// 登録済ユーザーは投稿できる
//		if ($this->action === 'add') {
//			return true;
//		}
//
//		// 投稿のオーナーは編集や削除ができる
//		if (in_array($this->action, array('edit', 'delete'))) {
//			$postId = (int) $this->request->params['pass'][0];
//			if ($this->Post->isOwnedBy($postId, $user['id'])) {
//				return true;
//			}
//		}
//
//		return parent::isAuthorized($user);
//	}	
	
/**
 * Components
 *
 * @var array
 */
	//public $components = array('Flash');
	public $components = array('Search.Prg');
	public $presetVars = true;

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Image->recursive = 0;
		$this->Prg->commonProcess();
		$this->paginate = array(
					'conditions' => $this->Image->parseCriteria($this->passedArgs),
						);
		//並び替え 以下のURLになる/images/index/sort:sort_order/direction:asc
		$this->Paginator->settings['order'] = 'Image.sort_order asc';
		//	$this->set('posts', $this->paginate());
		$this->set('images', $this->Paginator->paginate());
	
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Image->exists($id)) {
			throw new NotFoundException(__('Invalid image'));
		}
		$options = array('conditions' => array('Image.' . $this->Image->primaryKey => $id));
		$this->set('image', $this->Image->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Image->create();
			//sort_orderにデータ数プラス１の番号を振る
			$count = $this->Image->find('count');
			$count = $count + 1;
			$this->request->data['Image']['sort_order'] = $count;
						
			if ($this->Image->save($this->request->data)) {
				$this->Flash->success(__('The image has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Flash->error(__('The image could not be saved. Please, try again.'));
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Image->exists($id)) {
			throw new NotFoundException(__('Invalid image'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Image->save($this->request->data)) {
				$this->Flash->success(__('The image has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Flash->error(__('The image could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Image.' . $this->Image->primaryKey => $id));
			$this->request->data = $this->Image->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Image->id = $id;
		if (!$this->Image->exists()) {
			throw new NotFoundException(__('Invalid image'));
		}
		$this->request->allowMethod('post', 'delete');
		//		if ($this->Image->delete()) {
		//		データを削除せず、ステータスを２にする
		if ($this->Image->saveField('status',2)) {
			$this->Flash->success(__('The image has been deleted.'));
		} else {
			$this->Flash->error(__('The image could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}
/**
  * compdelete method
  *
  * @throws NotFoundException
  * @param string $id
  * @return void
  */
 	// public function compdelete($id = null) {
 	// 	$this->Image->id = $id;
 	// 	if (!$this->Image->exists()) {
 	// 		throw new NotFoundException(__('Invalid image'));
 	// 	}
 	// 	$this->request->allowMethod('post', 'delete');
 	// 			if ($this->Image->delete()) {
 	// 		$this->Flash->success(__('The image has been deleted.'));
 	// 	} else {
 	// 		$this->Flash->error(__('The image could not be deleted. Please, try again.'));
 	// 	}
 	// 	return $this->redirect(array('action' => 'index'));
 	// }

	public function ajax_sort_change()
	{
		$this->autoRender=false;
		$ids = $this->request->data['id'];
		for($i=0;$i<count($ids);$i++)
		{
			$this->Image->id = $ids[$i];
			$this->Image->saveField('sort_order', ($i+1));
		}
	}
}
