root/websites/events.php.gr.jp/branches/cake/app/controllers/events_controller.php @ 18489

Revision 18489, 7.0 kB (checked in by ha1t, 5 years ago)

rss周りの実装

Line 
1<?php
2/**
3 *
4 * vim: fenc=utf-8
5 *
6 */
7
8class EventsController extends AppController
9{
10    var $name = 'Event';
11    var $helpers = array('Rss', 'Datespan');
12    var $uses = array('Event', 'Trackback');
13
14    /**
15     * index
16     *
17     */
18    function index()
19    {
20        $result = $this->Event->find(
21            'all',
22            array(
23                'conditions' => array(
24                    'Event.private' => 0,
25                    'Event.publish_date <' => date('Y-m-d H:i:s')
26                ),
27                'limit' => 5,
28                'order' => array('Event.id' => 'desc')
29            )
30        );
31
32        $this->set('events', $result);
33
34    }
35
36    /**
37     * show
38     *
39     */
40    function show($id)
41    {
42        $id = (int)$id;
43        $this->set('event_id', $id);
44
45        $has_many = array(
46            'EventComment' => array(
47                'className' => 'EventComment',
48                'foreignKey' => 'event_id'
49            ),
50            'EventAttendee' => array(
51                'className' => 'EventAttendee',
52                'foreignKey' => 'event_id'
53            ),
54            'Trackback' => array(
55                'className' => 'Trackback',
56                'foreignKey' => 'event_id'
57            )
58        );
59        $has_one = array(
60            'EventPage' => array(
61                'className' => 'EventPage',
62                'foreignKey' => 'event_id',
63            )
64        );
65
66        $has_one2 = array(
67            'User' => array(
68                'className' => 'User',
69                'foreignKey' => 'user_id',
70            )
71        );
72
73        $this->Event->bindModel(array('hasMany' => $has_many, 'hasOne' => $has_one));
74
75        $this->Event->EventComment->bindModel(array('belongsTo' => $has_one2));
76        $this->Event->EventAttendee->bindModel(array('belongsTo' => $has_one2));
77
78        $re = $this->Event->findById($id, null,null,2);
79
80        // WikiPageのレンダリング
81        require_once APP . 'Text/PukiWiki.php';
82        $pukiwiki = new Text_PukiWiki();
83        $re['EventPage']['content'] = $pukiwiki->toHtml($re['EventPage']['content']);
84
85        $attendee_count = 0;
86        $joined = false;
87        foreach ($re['EventAttendee'] as $row) {
88            // 自分が参加していたらフラグをたてる
89            if ($this->Session->read('id') == $row['User']['id']) {
90                $joined = true;
91                if ($row['canceled'] == 1) {
92                    $this->set('canceled', true);
93                } else {
94                    $this->set('canceled', false);
95                }
96            }
97
98            $this->set('joined', $joined);
99
100            if ($row['canceled'] != 1) {
101                $attendee_count++;
102            }
103        }
104
105        $this->set('attendee_count', $attendee_count);
106        $this->set('attendee_nokori', $re['Event']['max_register'] - $attendee_count);
107        $this->set('is_over', $this->Event->isOver($id));
108        $this->set('data', $re);
109    }
110
111    /**
112     * control
113     *
114     */
115    function control()
116    {
117        // adminじゃなければさようなら
118        if ($this->Session->read('role') != 'admin') {
119            $this->redirect('/');
120        }
121
122        $events = $this->Event->find('all', array('order' => 'Event.id DESC'));
123        $this->set('events', $events);
124    }
125
126    /**
127     * add
128     *
129     */
130    function add()
131    {
132        // adminじゃなければさようなら
133        if ($this->Session->read('role') != 'admin') {
134            $this->redirect('/');
135        }
136
137        if ($this->data) {
138            $this->Event->save($this->data);
139        }
140
141        $this->redirect('/events/control');
142    }
143
144    /**
145     * edit
146     *
147     */
148    function edit($event_id)
149    {
150        // adminじゃなければさようなら
151        if ($this->Session->read('role') != 'admin') {
152            $this->redirect('/');
153        }
154
155        if ($this->data) {
156        } else {
157            $event = $this->Event->findById($event_id);
158            $this->data = $event;
159            $this->set('event', $event);
160        }
161    }
162
163    /**
164     * rss
165     *
166     */
167    function rss($id)
168    {
169        $this->layout = 'rss';
170        $this->set('channel', array(
171            'title' => "events.php.gr.jp",
172            'description' => 'Feed',
173        ));
174
175        if ($id == 'trackback') {
176            $result = $this->Trackback->find('all', array(
177                'order' => 'Trackback.id DESC',
178                'limit' => '20',
179            ));
180
181            $this->set('events', $result);
182
183        } else if (is_numeric($id)) {
184
185            $has_many = array(
186                'EventComment' => array(
187                    'className' => 'EventComment',
188                    'foreignKey' => 'event_id'
189                ),
190                'EventAttendee' => array(
191                    'className' => 'EventAttendee',
192                    'foreignKey' => 'event_id'
193                ),
194            );
195
196            $has_one = array(
197                'User' => array(
198                    'className' => 'User',
199                    'foreignKey' => 'user_id',
200                )
201            );
202
203            $this->Event->bindModel(array('hasMany' => $has_many));
204            $this->Event->EventComment->bindModel(array('belongsTo' => $has_one));
205            $this->Event->EventAttendee->bindModel(array('belongsTo' => $has_one));
206
207            $event = $this->Event->findById($id, null, null, 2);
208            if (!$event) {
209                $this->index();
210                return null;
211            }
212
213            $result = array();
214
215            foreach ($event['EventComment'] as $event_comment) {
216                $item = array();
217                $item['Event']['title'] = 'comment';
218                $item['Event']['description'] = $event_comment['User']['nickname'] .':'.$event_comment['comment'];
219                $item['Event']['id'] = $event_comment['event_id'];
220                $item['Event']['publish_date'] = $event_comment['created'];
221                if (!$event_comment['created']) {
222                    $item['Event']['publish_date'] = date('Y-m-d H:i:s');
223                }
224                $key = strtotime($item['Event']['publish_date']) . '0';
225                $result[$key] = $item;
226            }
227
228            foreach ($event['EventAttendee'] as $event_attendee) {
229                $item = array();
230                $item['Event']['title'] = 'joined';
231                $item['Event']['description'] = $event_attendee['User']['nickname'] .':'.$event_attendee['comment'];
232                $item['Event']['id'] = $event_attendee['event_id'];
233                $item['Event']['publish_date'] = $event_attendee['created'];
234                if (!$event_attendee['created']) {
235                    $item['Event']['publish_date'] = date('Y-m-d H:i:s');
236                }
237                $key = strtotime($item['Event']['publish_date']) . '1';
238                $result[$key] = $item;
239            }
240
241            krsort($result);
242
243            $events = array();
244            foreach ($result as $value) {
245                $events[] = $value;
246            }
247
248            $this->set('events', $events);
249
250        } else {
251            $this->index();
252        }
253
254    }
255
256}
257
258?>
Note: See TracBrowser for help on using the browser.