Skip to content
This repository has been archived by the owner on May 1, 2023. It is now read-only.

Commit

Permalink
Added code from end of Day 2 workshop.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Sep 2, 2010
1 parent 71ba4a6 commit 912f24e
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app_controller.php
Expand Up @@ -44,6 +44,10 @@ protected function _setupAuth(){
'action' => 'display',
'come-back'
);
$this->Auth->loginRedirect = array(
'controller' => 'events',
'action' => 'index'
);
}

protected function _setupUser() {
Expand Down
21 changes: 21 additions & 0 deletions controllers/events_controller.php
Expand Up @@ -2,9 +2,16 @@
class EventsController extends AppController {

var $name = 'Events';
public $paginate = array(
'conditions' => array()
);

function index() {
$this->Event->recursive = 0;
if (!empty($this->params['named'])) {
$this->paginate['conditions'] = isset($this->paginate['conditions']) ? $this->paginate['conditions'] : array();
$this->paginate['conditions'] += $this->params['named'];
}
$this->set('events', $this->paginate());
}

Expand All @@ -19,6 +26,7 @@ function view($id = null) {
function add() {
if (!empty($this->data)) {
$this->Event->create();
$this->data['Event']['user_id'] = $this->Auth->user('id');
if ($this->Event->save($this->data)) {
$this->Session->setFlash(__('The event has been saved', true));
$this->redirect(array('action' => 'index'));
Expand Down Expand Up @@ -62,5 +70,18 @@ function delete($id = null) {
$this->Session->setFlash(__('Event was not deleted', true));
$this->redirect(array('action' => 'index'));
}

public function day($day = null, $month = null, $year = null) {
if (!$day) {
$day = date('d');
}
$this->paginate = array('coming') + compact('day', 'month', 'year');
$this->setAction('index');
}

public function month($month = null, $year = null) {
$this->paginate = array('coming') + compact('month', 'year');
$this->setAction('index');
}
}
?>
49 changes: 49 additions & 0 deletions models/event.php
Expand Up @@ -55,5 +55,54 @@ class Event extends AppModel {
'order' => ''
)
);

public $_findMethods = array('coming' => true);

public function _findComing($state, $query, $results = array()) {
if ($state == 'before') {
if (empty($query['month'])){
$query['month'] = date('m');
}
if (empty($query['year'])){
$query['year'] = date('Y');
}
$startDay = '01';
$endingDay = 31;
if (!empty($query['day'])) {
$startDay = $endingDay = $query['day'];
}
$query['conditions']['Event.date >='] = $query['year'] . '-' . $query['month'] . '-' . $startDay;
$query['conditions']['Event.date <='] = $query['year'] . '-' . $query['month'] . '-' . $endingDay;

if (!empty($query['operation'])) {
return $this->_findCount($state, $query, $results);
}
return $query;
}
if (!empty($query['operation'])) {
return $this->_findCount($state, $query, $results);
}
return $results;
}

public function paginateCount($conditions = array(), $recursive = 0, $extra = array()) {
$parameters = compact('conditions');
if (isset($extra['type'])) {
$extra['operation'] = 'count';
return $this->find($extra['type'], array_merge($parameters, $extra));
} else {
return $this->find('count',array_merge($parameters, $extra));
}
}










}
?>
48 changes: 47 additions & 1 deletion tests/cases/models/event.test.php
Expand Up @@ -34,6 +34,52 @@ public function testValidationWithValidaData() {
$this->assertTrue($result);
$this->assertEqual(array_keys($this->Event->validationErrors), array());
}


public function testFindComing() {
$month = date('m');
$data = array(
array(
'user_id' => 'user-1',
'title' => 'Event 1',
'date' => "2010-$month-29"
),
array(
'user_id' => 'user-1',
'title' => 'Event 2',
'date' => "2010-$month-29"
),
array(
'user_id' => 'user-1',
'title' => 'Event 3',
'date' => "2010-$month-29"
),
array(
'user_id' => 'user-1',
'title' => 'Event 4',
'date' => "2010-$month-29"
),
array(
'user_id' => 'user-1',
'title' => 'Event 5',
'date' => "2010-$month-30"
),
array(
'user_id' => 'user-1',
'title' => 'Event 6',
'date' => "2011-$month-29"
),
);
$this->assertTrue($this->Event->saveAll($data));
$result = $this->Event->find('coming', array(
'day' => 29,
'month' => $month,
'year' => 2010,
'order' => 'Event.title'
));
$this->assertEqual(array('Event 1', 'Event 2', 'Event 3', 'Event 4'), Set::extract('/Event/title', $result));

$result = $this->Event->find('coming', array('year' => 2011));
$this->assertEqual(array('Event 6'), Set::extract('/Event/title', $result));
}
}
?>
1 change: 0 additions & 1 deletion views/events/add.ctp
Expand Up @@ -3,7 +3,6 @@
<fieldset>
<legend><?php __('Add Event'); ?></legend>
<?php
echo $this->Form->input('user_id');
echo $this->Form->input('title');
echo $this->Form->input('description');
echo $this->Form->input('date');
Expand Down

0 comments on commit 912f24e

Please sign in to comment.