summaryrefslogtreecommitdiffstats
path: root/models/Entry.php
blob: e84d438263107d1e5647eff367eacd71d906e8c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

namespace app\models;

use app\models\Users;
use \MongoDate;
use lithium\util\Validator;

Class entry extends \lithium\data\Model {
	public static function __init() {
		parent::__init();

		//Validators go here
	}

	public $validates = array(
		    'my_watched_episodes' => array('numeric', 'message' => 'please enter a number'),
			'my_start_date' => array('date', 'message' => 'Please enter a valid date'),
			'my_finish_date' => array('date', 'message' => 'Please enter a valid date'),
			'my_score' => array(array('inRange' => array('min' => 0, 'max' => '10'), 'message' => 'Enter a valid score'),
								array('numeric', 'message' => 'Please enter a number')),
			//'my_status' => array('isValidStatus', 'message' => 'please enter valid status'),
			'my_times_watched' => array('numeric', 'message' => 'This must be a number')
		);

	//Add timestamping to entries. :TODO:

	



	/*
	public function add($entity, $username)
	{
		var_dump($entity->_data);
		exit();
		$updateData = array('$push' => array('animelist' => $entity));
		$conditions = array('username' => $username);
		$result = Entry::update($updateData, $conditions, array('atomic' => false));
		return $result; 
	}
	*/

	//Got lazy, the proper way to do it is above, but needs a bit of fiddling. 
	//The below code works, but forces mongo to resave the entire record, which tags
	//longer than just updating what has chaged. 
	public function add($entity, $username)
	{
		$user = User::find('first', array('conditions' => compact('username')));
		$entity->created_on = new MongoDate();
		$entity->updated_on = new MongoDate();
		$user->animelist[] = $entity;

		if (Validator::check($entity->data(), $this->validates, array('skipEmpty' => 'true'))) {
			return $user->save(null, array('validate' => false));
		}
		else 
		{
			return false;
		}
	}

	public function edit($entity, $username)
	{
		$user = User::find('first', array('conditions' => compact('username')));
		$entity->created_on = new MongoDate();
		$entity->updated_on = new MongoDate();
		$user->animelist[] = $entity;
		return $user->save(null, array('validate' => false));
	}
}