統計
| ブランチ: | リビジョン:

pictcode / app / Controller / ImagesController.php @ 81caa5c2

履歴 | 表示 | アノテート | ダウンロード (4.537 KB)

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
}