1<?php
2/*
3 * Copyright Intermesh BV.
4 *
5 * This file is part of Reminder-Office. You should have received a copy of the
6 * Reminder-Office license along with Reminder-Office. See the file /LICENSE.TXT
7 *
8 * If you have questions write an e-mail to info@intermesh.nl
9 *
10 */
11
12/**
13 *
14 * The Reminder model
15 *
16 * The Group-Office core will check for these reminders automatically.
17 *
18 * @version $Id: Reminder.php 7607 2011-08-04 13:41:42Z mschering $
19 * @copyright Copyright Intermesh BV.
20 * @author Merijn Schering <mschering@intermesh.nl>
21 * @package GO.base.model
22 * @property string $text
23 * @property boolean $manual
24 * @property int $snooze_time
25 * @property int $vtime This time will be displayed when showing the reminder
26 * @property int $time This is the time the reminder will be displayed
27 * @property string $name
28 * @property int $user_id
29 * @property int $model_type_id
30 * @property int $model_id
31 * @property int $id
32 */
33
34namespace GO\Base\Model;
35
36
37class Reminder extends \GO\Base\Db\ActiveRecord {
38
39	/**
40	 * Returns a static model of itself
41	 *
42	 * @param String $className
43	 * @return Reminder
44	 */
45	public static function model($className=__CLASS__)
46	{
47		return parent::model($className);
48	}
49
50	protected function init() {
51
52		$this->columns['text']['gotype']='html';
53		$this->columns['time']['gotype']='unixtimestamp';
54		$this->columns['vtime']['gotype']='unixtimestamp';
55
56		parent::init();
57	}
58
59	public function relations() {
60
61		return array('users' => array('type'=>self::MANY_MANY, 'model'=>'GO\Base\Model\User', 'field'=>'reminder_id', 'linkModel' => 'GO\Base\Model\ReminderUser'));
62	}
63
64	/**
65	 * Create a new reminder
66	 *
67	 * @param StringHelper $name The text that will appear in the reminder
68	 * @param int $time Unix timestamp
69	 * @param StringHelper $model_name Optional model name and model id so that the reminder links to the dialog.
70	 * @param int $model_id
71	 * @param int $vtime The time that will be displayed in the reminder
72	 *
73	 * @return Reminder
74	 */
75	public static function newInstance($name, $time, $model_name='', $model_id=0, $vtime=null){
76		$r = new Reminder();
77		$r->name=	\GO\Base\Util\StringHelper::cut_string($name, 100);
78		$r->time=$time;
79		$r->vtime=$vtime;
80		$r->model_type_id=\GO::getModel($model_name)->modelTypeId();
81		$r->model_id=$model_id;
82		$r->save();
83
84		return $r;
85	}
86
87	public function tableName() {
88		return 'go_reminders';
89	}
90
91	/**
92	 * Add a user to the ACL with a permission level.
93	 *
94	 * @param int $userId
95	 * @param int $time Unix timestamp. Defaults to reminder time.
96	 * @return bool True on success
97	 */
98	public function setForUser($userId, $time=0) {
99
100		$usersReminder = $this->hasUser($userId);
101
102		if(!$time)
103			$time=$this->time;
104
105		if($usersReminder){
106			$usersReminder->time=$time;
107		}else
108		{
109			$usersReminder = new ReminderUser();
110			$usersReminder->reminder_id=$this->id;
111			$usersReminder->user_id = $userId;
112			$usersReminder->time = $time;
113		}
114
115		return $usersReminder->save();
116
117	}
118
119	/**
120	 * Returns the links table model if the reminder has the user
121	 *
122	 * @param int $userId
123	 * @return ReminderUser
124	 */
125	public function hasUser($userId){
126		return ReminderUser::model()->findByPk(array(
127				'reminder_id'=>$this->id,
128				'user_id'=>$userId
129						));
130	}
131
132	public function hasUsers(){
133
134		$params = \GO\Base\Db\FindParams::newInstance()
135						->select('count(*) AS count')
136						->single();
137
138		$params->getCriteria()->addModel(ReminderUser::model())->addCondition('reminder_id', $this->id);
139
140		$record = ReminderUser::model()->find($params);
141
142		return $record->count > 0;
143	}
144
145
146	/**
147	 * Remove a user from the reminder
148	 *
149	 * @param int $userId
150	 * @return bool
151	 */
152	public function removeUser($userId) {
153
154		$model = $this->hasUser($userId);
155		if ($model) {
156			if (!$model->delete()) {
157				return false;
158			}
159		}
160
161		$this->fireEvent('dismiss', array($this, $userId));
162
163		//delete the reminder if it doesn't have users anymore.
164		if (!$this->hasUsers()) {
165			$this->delete();
166		}
167
168		return true;
169	}
170
171
172	public function defaultAttributes() {
173		return array(
174			'snooze_time'=>7200,
175			'time' => time()
176		);
177	}
178
179	/**
180	 * Get the model to which this reminder belongs. A reminder can belong to an Event or Task for example.
181	 *
182	 * @return \GO\Base\Db\ActiveRecord|boolean
183	 */
184	public function getRelatedModel(){
185
186		if(!$this->model_type_id || !$this->model_id)
187			return false;
188
189		$modelType = ModelType::model()->findByPk($this->model_type_id);
190
191		$model = \GO::getModel($modelType->model_name)->findByPk($this->model_id);
192
193		return $model;
194	}
195
196
197	public function findByModel($modelName, $id){
198		$model_type_id = \GO::getModel($modelName)->modelTypeId();
199
200		return $this->find(\GO\Base\Db\FindParams::newInstance()
201						->criteria(\GO\Base\Db\FindCriteria::newInstance()
202										->addModel(Reminder::model())
203										->addCondition('model_type_id', $model_type_id)
204										->addCondition('model_id', $id)));
205	}
206
207
208//	public function getUsers($findParams=false){
209//		$stmt = User::model()->find(\GO\Base\Db\FindParams::newInstance()
210//						->mergeWith($findParams)
211//						->order(array('first_name','last_name'),array('ASC','ASC'))
212//						->criteria(\GO\Base\Db\FindCriteria::model()->addModel(ReminderUser::model(),'r')->addCondition('reminder_id',$this->id,'=','r'))
213//						->join(ReminderUser::model()->tableName(),
214//										\GO\Base\Db\FindCriteria::model()->addModel(ReminderUser::model())
215//														->addCondition('id','r.user_id','=','r',true,true)
216//
217//											)
218//
219//						);
220//
221//		return $stmt;
222//	}
223
224}
225