リビジョン 29186d1b
| app/Controller/ImagesController.php | ||
|---|---|---|
| 1 |
<?php |
|
| 2 |
App::uses('AppController', 'Controller');
|
|
| 3 |
/** |
|
| 4 |
* Images Controller |
|
| 5 |
* |
|
| 6 |
* @property Image $Image |
|
| 7 |
* @property PaginatorComponent $Paginator |
|
| 8 |
*/ |
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
class ImagesController extends AppController {
|
|
| 14 |
|
|
| 15 |
//$usesの指定は最初大文字!!! |
|
| 16 |
public $uses = array('Image');
|
|
| 17 |
//public $helpers = array('UploadPack.Upload');
|
|
| 18 |
public $layout = 'image_bootstrap3'; |
|
| 19 |
|
|
| 20 |
// public function isAuthorized($user) {
|
|
| 21 |
// // 登録済ユーザーは投稿できる |
|
| 22 |
// if ($this->action === 'add') {
|
|
| 23 |
// return true; |
|
| 24 |
// } |
|
| 25 |
// |
|
| 26 |
// // 投稿のオーナーは編集や削除ができる |
|
| 27 |
// if (in_array($this->action, array('edit', 'delete'))) {
|
|
| 28 |
// $postId = (int) $this->request->params['pass'][0]; |
|
| 29 |
// if ($this->Post->isOwnedBy($postId, $user['id'])) {
|
|
| 30 |
// return true; |
|
| 31 |
// } |
|
| 32 |
// } |
|
| 33 |
// |
|
| 34 |
// return parent::isAuthorized($user); |
|
| 35 |
// } |
|
| 36 |
|
|
| 37 |
/** |
|
| 38 |
* Components |
|
| 39 |
* |
|
| 40 |
* @var array |
|
| 41 |
*/ |
|
| 42 |
//public $components = array('Flash');
|
|
| 43 |
public $components = array('Search.Prg');
|
|
| 44 |
public $presetVars = true; |
|
| 45 |
|
|
| 46 |
/** |
|
| 47 |
* index method |
|
| 48 |
* |
|
| 49 |
* @return void |
|
| 50 |
*/ |
|
| 51 |
public function index() {
|
|
| 52 |
$this->Image->recursive = 0; |
|
| 53 |
$this->Prg->commonProcess(); |
|
| 54 |
$this->paginate = array( |
|
| 55 |
'conditions' => $this->Image->parseCriteria($this->passedArgs), |
|
| 56 |
); |
|
| 57 |
//並び替え 以下のURLになる/images/index/sort:sort_order/direction:asc |
|
| 58 |
$this->Paginator->settings['order'] = 'Image.sort_order asc'; |
|
| 59 |
// $this->set('posts', $this->paginate());
|
|
| 60 |
$this->set('images', $this->Paginator->paginate());
|
|
| 61 |
|
|
| 62 |
} |
|
| 63 |
|
|
| 64 |
/** |
|
| 65 |
* view method |
|
| 66 |
* |
|
| 67 |
* @throws NotFoundException |
|
| 68 |
* @param string $id |
|
| 69 |
* @return void |
|
| 70 |
*/ |
|
| 71 |
public function view($id = null) {
|
|
| 72 |
if (!$this->Image->exists($id)) {
|
|
| 73 |
throw new NotFoundException(__('Invalid image'));
|
|
| 74 |
} |
|
| 75 |
$options = array('conditions' => array('Image.' . $this->Image->primaryKey => $id));
|
|
| 76 |
$this->set('image', $this->Image->find('first', $options));
|
|
| 77 |
} |
|
| 78 |
|
|
| 79 |
/** |
|
| 80 |
* add method |
|
| 81 |
* |
|
| 82 |
* @return void |
|
| 83 |
*/ |
|
| 84 |
public function add() {
|
|
| 85 |
if ($this->request->is('post')) {
|
|
| 86 |
$this->Image->create(); |
|
| 87 |
//sort_orderにデータ数プラス1の番号を振る |
|
| 88 |
$count = $this->Image->find('count');
|
|
| 89 |
$count = $count + 1; |
|
| 90 |
$this->request->data['Image']['sort_order'] = $count; |
|
| 91 |
|
|
| 92 |
if ($this->Image->save($this->request->data)) {
|
|
| 93 |
$this->Flash->success(__('The image has been saved.'));
|
|
| 94 |
return $this->redirect(array('action' => 'index'));
|
|
| 95 |
} else {
|
|
| 96 |
$this->Flash->error(__('The image could not be saved. Please, try again.'));
|
|
| 97 |
} |
|
| 98 |
} |
|
| 99 |
} |
|
| 100 |
|
|
| 101 |
/** |
|
| 102 |
* edit method |
|
| 103 |
* |
|
| 104 |
* @throws NotFoundException |
|
| 105 |
* @param string $id |
|
| 106 |
* @return void |
|
| 107 |
*/ |
|
| 108 |
public function edit($id = null) {
|
|
| 109 |
if (!$this->Image->exists($id)) {
|
|
| 110 |
throw new NotFoundException(__('Invalid image'));
|
|
| 111 |
} |
|
| 112 |
if ($this->request->is(array('post', 'put'))) {
|
|
| 113 |
if ($this->Image->save($this->request->data)) {
|
|
| 114 |
$this->Flash->success(__('The image has been saved.'));
|
|
| 115 |
return $this->redirect(array('action' => 'index'));
|
|
| 116 |
} else {
|
|
| 117 |
$this->Flash->error(__('The image could not be saved. Please, try again.'));
|
|
| 118 |
} |
|
| 119 |
} else {
|
|
| 120 |
$options = array('conditions' => array('Image.' . $this->Image->primaryKey => $id));
|
|
| 121 |
$this->request->data = $this->Image->find('first', $options);
|
|
| 122 |
} |
|
| 123 |
} |
|
| 124 |
|
|
| 125 |
/** |
|
| 126 |
* delete method |
|
| 127 |
* |
|
| 128 |
* @throws NotFoundException |
|
| 129 |
* @param string $id |
|
| 130 |
* @return void |
|
| 131 |
*/ |
|
| 132 |
public function delete($id = null) {
|
|
| 133 |
$this->Image->id = $id; |
|
| 134 |
if (!$this->Image->exists()) {
|
|
| 135 |
throw new NotFoundException(__('Invalid image'));
|
|
| 136 |
} |
|
| 137 |
$this->request->allowMethod('post', 'delete');
|
|
| 138 |
// if ($this->Image->delete()) {
|
|
| 139 |
// データを削除せず、ステータスを2にする |
|
| 140 |
if ($this->Image->saveField('status',2)) {
|
|
| 141 |
$this->Flash->success(__('The image has been deleted.'));
|
|
| 142 |
} else {
|
|
| 143 |
$this->Flash->error(__('The image could not be deleted. Please, try again.'));
|
|
| 144 |
} |
|
| 145 |
return $this->redirect(array('action' => 'index'));
|
|
| 146 |
} |
|
| 147 |
/** |
|
| 148 |
* compdelete method |
|
| 149 |
* |
|
| 150 |
* @throws NotFoundException |
|
| 151 |
* @param string $id |
|
| 152 |
* @return void |
|
| 153 |
*/ |
|
| 154 |
// public function compdelete($id = null) {
|
|
| 155 |
// $this->Image->id = $id; |
|
| 156 |
// if (!$this->Image->exists()) {
|
|
| 157 |
// throw new NotFoundException(__('Invalid image'));
|
|
| 158 |
// } |
|
| 159 |
// $this->request->allowMethod('post', 'delete');
|
|
| 160 |
// if ($this->Image->delete()) {
|
|
| 161 |
// $this->Flash->success(__('The image has been deleted.'));
|
|
| 162 |
// } else {
|
|
| 163 |
// $this->Flash->error(__('The image could not be deleted. Please, try again.'));
|
|
| 164 |
// } |
|
| 165 |
// return $this->redirect(array('action' => 'index'));
|
|
| 166 |
// } |
|
| 167 |
|
|
| 168 |
public function ajax_sort_change() |
|
| 169 |
{
|
|
| 170 |
$this->autoRender=false; |
|
| 171 |
$ids = $this->request->data['id']; |
|
| 172 |
for($i=0;$i<count($ids);$i++) |
|
| 173 |
{
|
|
| 174 |
$this->Image->id = $ids[$i]; |
|
| 175 |
$this->Image->saveField('sort_order', ($i+1));
|
|
| 176 |
} |
|
| 177 |
} |
|
| 178 |
} |
|
| app/Model/Image.php | ||
|---|---|---|
| 1 |
<?php |
|
| 2 |
App::uses('AppModel', 'Model');
|
|
| 3 |
/** |
|
| 4 |
* Image Model |
|
| 5 |
* |
|
| 6 |
*/ |
|
| 7 |
class Image extends AppModel {
|
|
| 8 |
|
|
| 9 |
/** |
|
| 10 |
* Validation rules |
|
| 11 |
* |
|
| 12 |
* @var array |
|
| 13 |
*/ |
|
| 14 |
public $validate = array( |
|
| 15 |
'name' => array( |
|
| 16 |
'notBlank' => array( |
|
| 17 |
'rule' => array('notBlank'),
|
|
| 18 |
//'message' => 'Your custom message here', |
|
| 19 |
//'allowEmpty' => false, |
|
| 20 |
//'required' => false, |
|
| 21 |
//'last' => false, // Stop validation after this rule |
|
| 22 |
//'on' => 'create', // Limit validation to 'create' or 'update' operations |
|
| 23 |
), |
|
| 24 |
), |
|
| 25 |
); |
|
| 26 |
|
|
| 27 |
public $actsAs = array( |
|
| 28 |
'UploadPack.Upload' => array( |
|
| 29 |
'image' => array( //☆ここでは、"_file_name"を除いたカラム名を書く |
|
| 30 |
'quality' => 95, //☆画質指定、デフォルトでは75 |
|
| 31 |
'path' => ':webroot/upload/:model/:id/:hash.:extension', |
|
| 32 |
'styles' => array( |
|
| 33 |
'thumb' => '85x85' //☆リサイズしたいサイズを書く |
|
| 34 |
) |
|
| 35 |
) |
|
| 36 |
), |
|
| 37 |
'Search.Searchable' |
|
| 38 |
); |
|
| 39 |
|
|
| 40 |
public $filterArgs = array( |
|
| 41 |
'name' => array('type' => 'query', 'method' => 'orConditions'),
|
|
| 42 |
'status' => array('type' => 'value'),
|
|
| 43 |
); |
|
| 44 |
|
|
| 45 |
public function orConditions( $data = array() ) {
|
|
| 46 |
$filter = $data['name']; |
|
| 47 |
$cond = array( |
|
| 48 |
'OR' => array( |
|
| 49 |
$this->alias . '.name LIKE' => '%' . $filter . '%', |
|
| 50 |
), |
|
| 51 |
); |
|
| 52 |
return $cond; |
|
| 53 |
} |
|
| 54 |
|
|
| 55 |
|
|
| 56 |
} |
|
| app/View/Images/add.ctp | ||
|---|---|---|
| 1 |
<div class="form"> |
|
| 2 |
<?php echo $this->Form->create('Image',array('type'=>'file')); ?>
|
|
| 3 |
<fieldset> |
|
| 4 |
<legend><?php echo __('Add Image'); ?></legend>
|
|
| 5 |
<?php echo $this->Form->input('name'); ?>
|
|
| 6 |
<?php echo $this->Form->input('image',array('type'=>'file','label'=>'画像')); ?>
|
|
| 7 |
</fieldset> |
|
| 8 |
<?php echo $this->Form->end(__('Submit')); ?>
|
|
| 9 |
</div> |
|
| 10 |
<?php echo $this->element('admin_action');?>
|
|
| 11 |
<div class="actions"> |
|
| 12 |
<h3><?php echo __('Actions'); ?></h3>
|
|
| 13 |
<ul> |
|
| 14 |
|
|
| 15 |
<li><?php echo $this->Html->link(__('List Images'), array('action' => 'index')); ?></li>
|
|
| 16 |
</ul> |
|
| 17 |
</div> |
|
| app/View/Images/edit.ctp | ||
|---|---|---|
| 1 |
<div class="images form"> |
|
| 2 |
<?php //echo $this->Form->create('Image'); ?>
|
|
| 3 |
<?php echo $this->Form->create('Image',array('type'=>'file')); ?>
|
|
| 4 |
<fieldset> |
|
| 5 |
<legend><?php echo __('Edit Image'); ?></legend>
|
|
| 6 |
<?php |
|
| 7 |
echo $this->Form->input('id');
|
|
| 8 |
echo $this->Form->input('name');
|
|
| 9 |
echo $this->Form->input('image_file_name');
|
|
| 10 |
echo $this->Form->input('image',array('type'=>'file','label'=>'画像'));
|
|
| 11 |
echo $this->Form->input('status', array(
|
|
| 12 |
'type' => 'select', |
|
| 13 |
'options' => array('1' => 'Active', '0' => 'Expire')
|
|
| 14 |
)); |
|
| 15 |
?> |
|
| 16 |
</fieldset> |
|
| 17 |
<?php echo $this->Form->end(__('Submit')); ?>
|
|
| 18 |
</div> |
|
| 19 |
<?php echo $this->element('admin_action'); ?>
|
|
| 20 |
<div class="actions"> |
|
| 21 |
<h3><?php echo __('Actions'); ?></h3>
|
|
| 22 |
<ul> |
|
| 23 |
|
|
| 24 |
<li><?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $this->Form->value('Image.id')), array('confirm' => __('Are you sure you want to delete # %s?', $this->Form->value('Image.id')))); ?></li>
|
|
| 25 |
<li><?php echo $this->Html->link(__('List Images'), array('action' => 'index')); ?></li>
|
|
| 26 |
</ul> |
|
| 27 |
</div> |
|
| app/View/Images/index.ctp | ||
|---|---|---|
| 1 |
<script type="text/javascript"> |
|
| 2 |
// When the document is ready set up our sortable with it's inherant function(s) |
|
| 3 |
$(document).ready(function() {
|
|
| 4 |
$("#sortable tbody").sortable({
|
|
| 5 |
update : function () {
|
|
| 6 |
var order = $('#sortable tbody').sortable('toArray');
|
|
| 7 |
$.post("/images/ajax_sort_change/",{
|
|
| 8 |
id:order |
|
| 9 |
},function(data){
|
|
| 10 |
}); |
|
| 11 |
} |
|
| 12 |
}); |
|
| 13 |
}); |
|
| 14 |
</script> |
|
| 15 |
|
|
| 16 |
<div class="images index"> |
|
| 17 |
<h2><?php echo __('Images'); ?></h2>
|
|
| 18 |
|
|
| 19 |
<div class="section form_search"> |
|
| 20 |
<?php echo $this->Form->create('Image', array(
|
|
| 21 |
'novalidate' => true, |
|
| 22 |
'inputDefaults' => array( |
|
| 23 |
'div' => 'form-group', |
|
| 24 |
'wrapInput' => false, |
|
| 25 |
'class' => 'form-control' |
|
| 26 |
), |
|
| 27 |
'div' =>false, |
|
| 28 |
'class' => 'well well-sm', |
|
| 29 |
'url' => array_merge(array( |
|
| 30 |
'action' => 'index'), |
|
| 31 |
$this->params['pass']) )); ?> |
|
| 32 |
<fieldset> |
|
| 33 |
<legend>検索</legend> |
|
| 34 |
<?php echo $this->Form->input('name', array(
|
|
| 35 |
'legend' => false, |
|
| 36 |
'placeholder' => '例)kkk', |
|
| 37 |
'label' => '名前' |
|
| 38 |
) |
|
| 39 |
); ?> |
|
| 40 |
<?php echo $this->Form->input('type', array(
|
|
| 41 |
'label'=>'ステータス', |
|
| 42 |
'legend' => false, |
|
| 43 |
'type'=>'select', |
|
| 44 |
'options'=>array( |
|
| 45 |
'0' => 'expire', '1' => 'active' ,'2'=>'削除済み' |
|
| 46 |
), |
|
| 47 |
'empty'=>'選択してください')); ?> |
|
| 48 |
<?php echo $this->Form->submit('検索', array('div' => false,'class' => 'btn btn-danger')); ?>
|
|
| 49 |
<?php echo $this->Form->end(); ?> |
|
| 50 |
</fieldset> |
|
| 51 |
</div> |
|
| 52 |
|
|
| 53 |
<?php echo $this->Paginator->counter(array('format' => __('全{:count}件中 {:start}-{:end}件を表示'))); ?>
|
|
| 54 |
|
|
| 55 |
|
|
| 56 |
<table id="sortable" cellpadding="0" cellspacing="0" class="table table-striped"> |
|
| 57 |
<thead> |
|
| 58 |
<tr> |
|
| 59 |
<th><?php echo $this->Paginator->sort('id'); ?></th>
|
|
| 60 |
<th><?php echo $this->Paginator->sort('name'); ?></th>
|
|
| 61 |
<th><?php echo $this->Paginator->sort('image_file_name'); ?></th>
|
|
| 62 |
<th><?php echo $this->Paginator->sort('status'); ?></th>
|
|
| 63 |
<th><?php echo $this->Paginator->sort('created'); ?></th>
|
|
| 64 |
<th><?php echo $this->Paginator->sort('updated'); ?></th>
|
|
| 65 |
<th class="actions"><?php echo __('Actions'); ?></th>
|
|
| 66 |
</tr> |
|
| 67 |
</thead> |
|
| 68 |
<tbody> |
|
| 69 |
<?php foreach ($images as $image): ?> |
|
| 70 |
<tr id="<?php echo $image['Image']['id']?>"> |
|
| 71 |
<td><span class="glyphicon glyphicon-list"></span></td> |
|
| 72 |
<td><?php echo h($image['Image']['name']); ?> </td> |
|
| 73 |
<td><?php echo h($image['Image']['image_file_name']); ?> <?php echo $this->upload->uploadImage($image, 'Image.image', array('style' => 'thumb')); ?></td>
|
|
| 74 |
<td><?php echo h($image['Image']['status']); ?> </td> |
|
| 75 |
<td><?php echo h($image['Image']['created']); ?> </td> |
|
| 76 |
<td><?php echo h($image['Image']['updated']); ?> </td> |
|
| 77 |
<td class="actions"> |
|
| 78 |
<?php echo $this->Html->link(__('View'), array('action' => 'view', $image['Image']['id']),array('class'=>'btn btn-primary')); ?>
|
|
| 79 |
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $image['Image']['id']),array('class'=>'btn btn-primary')); ?>
|
|
| 80 |
<?php echo $this->Form->postLink(__('Delete'), array('action' => 'delete', $image['Image']['id']),array('class'=>'btn btn-primary','confirm' => __('削除しますか?', $image['Image']['id']))); ?>
|
|
| 81 |
<?php echo $this->Form->postLink(__('抹消'), array('action' => 'compdelete', $image['Image']['id']),array('class'=>'btn btn-danger-outline','confirm' => __('完全に抹消されます。よろしいすか?', $image['Image']['id']))); ?>
|
|
| 82 |
</td> |
|
| 83 |
</tr> |
|
| 84 |
<?php endforeach; ?> |
|
| 85 |
</tbody> |
|
| 86 |
</table> |
|
| 87 |
<p> |
|
| 88 |
<?php |
|
| 89 |
echo $this->Paginator->counter(array( |
|
| 90 |
'format' => __('Page {:page} of {:pages}, showing {:current} records out of {:count} total, starting on record {:start}, ending on {:end}')
|
|
| 91 |
)); |
|
| 92 |
?> </p> |
|
| 93 |
<div class="paging"> |
|
| 94 |
<nav> |
|
| 95 |
<ul class="pager"> |
|
| 96 |
<?php |
|
| 97 |
echo $this->Paginator->prev('< ' . __('previous'), array(), null, array('class' => 'prev disabled'));
|
|
| 98 |
echo $this->Paginator->numbers(array('separator' => ''));
|
|
| 99 |
echo $this->Paginator->next(__('next') . ' >', array(), null, array('class' => 'next disabled'));
|
|
| 100 |
?> |
|
| 101 |
</ul> |
|
| 102 |
</nav> |
|
| 103 |
</div> |
|
| 104 |
</div> |
|
| 105 |
<!-- <div class="actions"> |
|
| 106 |
<h3><?php echo __('Actions'); ?></h3>
|
|
| 107 |
<ul> |
|
| 108 |
<li><?php echo $this->Html->link(__('New Image'), array('action' => 'add'),array('class'=>'btn btn-warning')); ?></li>
|
|
| 109 |
</ul> |
|
| 110 |
</div>--> |
|
| app/View/Images/view.ctp | ||
|---|---|---|
| 1 |
<div class="images view"> |
|
| 2 |
<h2><?php echo __('Image'); ?></h2>
|
|
| 3 |
<dl> |
|
| 4 |
<dt><?php echo __('Id'); ?></dt>
|
|
| 5 |
<dd> |
|
| 6 |
<?php echo h($image['Image']['id']); ?> |
|
| 7 |
|
|
| 8 |
</dd> |
|
| 9 |
<dt><?php echo __('Name'); ?></dt>
|
|
| 10 |
<dd> |
|
| 11 |
<?php echo h($image['Image']['name']); ?> |
|
| 12 |
|
|
| 13 |
</dd> |
|
| 14 |
<dt><?php echo __('Image File Name'); ?></dt>
|
|
| 15 |
<dd> |
|
| 16 |
<?php echo h($image['Image']['image_file_name']); ?> |
|
| 17 |
|
|
| 18 |
</dd> |
|
| 19 |
<dt><?php echo __('Status'); ?></dt>
|
|
| 20 |
<dd> |
|
| 21 |
<?php echo h($image['Image']['status']); ?> |
|
| 22 |
|
|
| 23 |
</dd> |
|
| 24 |
<dt><?php echo __('Created'); ?></dt>
|
|
| 25 |
<dd> |
|
| 26 |
<?php echo h($image['Image']['created']); ?> |
|
| 27 |
|
|
| 28 |
</dd> |
|
| 29 |
<dt><?php echo __('Updated'); ?></dt>
|
|
| 30 |
<dd> |
|
| 31 |
<?php echo h($image['Image']['updated']); ?> |
|
| 32 |
|
|
| 33 |
</dd> |
|
| 34 |
</dl> |
|
| 35 |
</div> |
|
| 36 |
<div class="actions"> |
|
| 37 |
<h3><?php echo __('Actions'); ?></h3>
|
|
| 38 |
<ul> |
|
| 39 |
<li><?php echo $this->Html->link(__('Edit Image'), array('action' => 'edit', $image['Image']['id'])); ?> </li>
|
|
| 40 |
<li><?php echo $this->Form->postLink(__('Delete Image'), array('action' => 'delete', $image['Image']['id']), array('confirm' => __('Are you sure you want to delete # %s?', $image['Image']['id']))); ?> </li>
|
|
| 41 |
<li><?php echo $this->Html->link(__('List Images'), array('action' => 'index')); ?> </li>
|
|
| 42 |
<li><?php echo $this->Html->link(__('New Image'), array('action' => 'add')); ?> </li>
|
|
| 43 |
</ul> |
|
| 44 |
</div> |
|
他の形式にエクスポート: Unified diff