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