/application/models/Console/Task.php

https://github.com/ohapps/TaskConsole · PHP · 422 lines · 290 code · 127 blank · 5 comment · 50 complexity · f8ad37a6553117a2313aaa6f81a841bb MD5 · raw file

  1. <?php
  2. class Console_Task extends Doctrine_Record {
  3. public function setTableDefinition(){
  4. $this->setTableName('tasks');
  5. $this->hasColumn('ID', 'integer', 11, array(
  6. 'type' => 'integer',
  7. 'length' => 11,
  8. 'unsigned' => 1,
  9. 'primary' => true,
  10. 'autoincrement' => true,
  11. ));
  12. $this->hasColumn('USER_ID', 'integer', 11, array(
  13. 'type' => 'integer',
  14. 'length' => 11,
  15. 'unsigned' => 1,
  16. 'primary' => false,
  17. 'notnull' => true,
  18. 'autoincrement' => false,
  19. ));
  20. $this->hasColumn('PROJECT_ID', 'integer', 11, array(
  21. 'type' => 'integer',
  22. 'length' => 11,
  23. //'unsigned' => 1,
  24. 'primary' => false,
  25. 'notnull' => false,
  26. 'autoincrement' => false,
  27. ));
  28. $this->hasColumn('DESCRIPTION', 'string', null, array(
  29. 'type' => 'string',
  30. 'fixed' => false,
  31. 'primary' => false,
  32. 'notnull' => false,
  33. 'autoincrement' => false,
  34. ));
  35. $this->hasColumn('PRIORITY_ID', 'integer', 4, array(
  36. 'type' => 'integer',
  37. 'length' => 4,
  38. 'unsigned' => 1,
  39. 'primary' => false,
  40. 'notnull' => true,
  41. 'autoincrement' => false,
  42. ));
  43. $this->hasColumn('DUE_DATE', 'timestamp', null, array(
  44. 'type' => 'timestamp',
  45. 'primary' => false,
  46. 'notnull' => false,
  47. 'autoincrement' => false,
  48. ));
  49. $this->hasColumn('COMPLETED', 'timestamp', null, array(
  50. 'type' => 'timestamp',
  51. 'primary' => false,
  52. 'notnull' => false,
  53. 'autoincrement' => false,
  54. ));
  55. $this->hasColumn('RECUR_UNIT_TYPE', 'string', null, array(
  56. 'type' => 'string',
  57. 'length' => 25,
  58. 'fixed' => false,
  59. 'primary' => false,
  60. 'notnull' => false,
  61. 'autoincrement' => false,
  62. ));
  63. $this->hasColumn('RECUR_UNITS', 'integer', 11, array(
  64. 'type' => 'integer',
  65. 'length' => 11,
  66. 'primary' => false,
  67. 'notnull' => false,
  68. 'autoincrement' => false,
  69. ));
  70. $this->hasColumn('DISPLAY_DATE', 'timestamp', null, array(
  71. 'type' => 'timestamp',
  72. 'primary' => false,
  73. 'notnull' => false,
  74. 'autoincrement' => false,
  75. ));
  76. $this->hasColumn('ORIG_ID', 'integer', 11, array(
  77. 'type' => 'integer',
  78. 'length' => 11,
  79. 'unsigned' => 1,
  80. 'primary' => false,
  81. 'notnull' => false,
  82. 'autoincrement' => false,
  83. ));
  84. $this->hasColumn('GCAL_ID', 'string', null, array(
  85. 'type' => 'string',
  86. 'length' => 255,
  87. 'fixed' => false,
  88. 'primary' => false,
  89. 'notnull' => false,
  90. 'autoincrement' => false,
  91. ));
  92. $this->hasColumn('QUEUE_ORDER', 'integer', 11, array(
  93. 'type' => 'integer',
  94. 'length' => 11,
  95. 'unsigned' => 1,
  96. 'primary' => false,
  97. 'notnull' => false,
  98. 'autoincrement' => false,
  99. ));
  100. }
  101. public function setUp(){
  102. $this->hasOne('Console_Project as Project', array(
  103. 'local' => 'PROJECT_ID',
  104. 'foreign' => 'ID'
  105. )
  106. );
  107. $this->hasOne('Console_Priority as Priority', array(
  108. 'local' => 'PRIORITY_ID',
  109. 'foreign' => 'ID'
  110. )
  111. );
  112. $this->hasMany('Console_Category as Categories', array(
  113. 'local' => 'TASK_ID',
  114. 'foreign' => 'CATEGORY_ID',
  115. 'refClass' => 'Console_TaskCategory'
  116. )
  117. );
  118. }
  119. public function preSave($event){
  120. if($this->PROJECT_ID == 0){
  121. $this->PROJECT_ID = null;
  122. }
  123. }
  124. protected function validate(){
  125. $errorStack = $this->getErrorStack();
  126. $priority = Doctrine_Core::getTable('Console_Priority')->find($this->PRIORITY_ID);
  127. if( $priority == false ){
  128. $errorStack->add('PRIORITY_ID', 'invalid priority');
  129. }
  130. if( $this->PROJECT_ID != null ){
  131. $project = Doctrine_Core::getTable('Console_Project')->find($this->PROJECT_ID);
  132. if( $project == false ){
  133. $errorStack->add('PROJECT_ID', 'invalid project');
  134. }
  135. }
  136. if( $this->RECUR_UNIT_TYPE != '' && in_array( $this->RECUR_UNIT_TYPE, array('days','months','years') ) == false ){
  137. $errorStack->add('RECUR_UNIT_TYPE', 'invalid recurring unit type');
  138. }
  139. }
  140. public function markComplete($format){
  141. if( $this->COMPLETED == null || $this->COMPLETED == '' ){
  142. $today = new Zend_Date();
  143. $this->COMPLETED = $today->toString($format);
  144. $this->save();
  145. // CHECK IF A RECURRING TASK IS REQUIRED
  146. if( $this->RECUR_UNIT_TYPE != "" && $this->RECUR_UNITS != "" && $this->RECUR_UNITS != 0 ){
  147. $tasks = Doctrine_Core::getTable('Console_Task')->getRecurringEvents($this->ID);
  148. // CHECK IF A RECURRING TASK ALREADY EXISTS
  149. if( count( $tasks ) == 0 ){
  150. // CALCULATE DISPLAY DATE
  151. $display_date = new Zend_Date();
  152. switch( $this->RECUR_UNIT_TYPE ){
  153. case "days":
  154. $display_date->addDay( $this->RECUR_UNITS );
  155. break;
  156. case "months":
  157. $display_date->addMonth( $this->RECUR_UNITS );
  158. break;
  159. case "years":
  160. $display_date->addYear( $this->RECUR_UNITS );
  161. break;
  162. }
  163. // CREATE RECURRING TASK
  164. $task = new Console_Task();
  165. $task->USER_ID = $this->USER_ID;
  166. $task->PROJECT_ID = $this->PROJECT_ID;
  167. $task->DESCRIPTION = $this->DESCRIPTION;
  168. $task->PRIORITY_ID = $this->PRIORITY_ID;
  169. $task->COMPLETED = null;
  170. $task->RECUR_UNIT_TYPE = $this->RECUR_UNIT_TYPE;
  171. $task->RECUR_UNITS = $this->RECUR_UNITS;
  172. $task->ORIG_ID = $this->ID;
  173. $task->DISPLAY_DATE = $display_date->toString($format);
  174. $task->save();
  175. foreach( $this->Categories as $category ){
  176. $task->applyCategory($category);
  177. }
  178. }
  179. }
  180. if($this->PROJECT_ID != '' && $this->PROJECT_ID != null){
  181. if( $this->Project->AUTO_COMPLETE == 1 ){
  182. if( $this->Project->getTaskPendingCount() == 0 ){
  183. $this->Project->markComplete($format);
  184. }
  185. }
  186. }
  187. }
  188. }
  189. public function markIncomplete(){
  190. $this->COMPLETED = null;
  191. $this->save();
  192. $this->deleteRecurringTasks();
  193. }
  194. public function isComplete(){
  195. if( $this->COMPLETED == null ){
  196. return "no";
  197. }else{
  198. return "yes";
  199. }
  200. }
  201. public function deleteRecurringTasks(){
  202. $tasks = Doctrine_Core::getTable('Console_Task')->getRecurringEvents($this->ID);
  203. foreach( $tasks as $task ){
  204. $task->delete();
  205. }
  206. }
  207. public function postDelete($event){
  208. $this->deleteRecurringTasks();
  209. }
  210. public function isUserTask($userId){
  211. if($userId == $this->USER_ID){
  212. return true;
  213. }
  214. return false;
  215. }
  216. public function isQueued(){
  217. if( $this->QUEUE_ORDER != null ){
  218. return true;
  219. }
  220. return false;
  221. }
  222. public function addToQueue(){
  223. $max = 0;
  224. $tasks = Doctrine_Core::getTable('Console_Task')->getTasksInQueue($this->USER_ID);
  225. foreach( $tasks as $task ){
  226. if( $task->ID == $this->ID ){
  227. return false;
  228. }
  229. if( $task->QUEUE_ORDER > $max ){
  230. $max = $task->QUEUE_ORDER;
  231. }
  232. }
  233. $this->QUEUE_ORDER = ( $max + 1 );
  234. $this->save();
  235. return true;
  236. }
  237. public function removeFromQueue(){
  238. $this->QUEUE_ORDER = null;
  239. $this->save();
  240. return true;
  241. }
  242. public function categoryList(){
  243. $cats = array();
  244. foreach( $this->Categories as $category ){
  245. $cats[] = $category->DESCRIPTION;
  246. }
  247. asort($cats);
  248. return implode(', ',$cats);
  249. }
  250. public function categoryIdsList(){
  251. $cats = array();
  252. foreach( $this->Categories as $category ){
  253. $cats[] = $category->ID;
  254. }
  255. asort($cats);
  256. return implode(',',$cats);
  257. }
  258. public function hasCategory(Console_Category $category){
  259. foreach( $this->Categories as $cat ){
  260. if( $category->ID == $cat->ID ){
  261. return true;
  262. }
  263. }
  264. return false;
  265. }
  266. public function setCategoriesFromArray(array $ids){
  267. foreach( $this->Categories as $category ){
  268. if( in_array($category->ID,$ids) === false ){
  269. $this->removeCategory($category);
  270. }
  271. }
  272. foreach( $ids as $id ){
  273. $category = Doctrine_Core::getTable('Console_Category')->find($id);
  274. if( $category == false ){
  275. throw new Exception('invalid category id');
  276. }
  277. if( $category->isUserCategory($this->USER_ID) == false ){
  278. throw new Exception('category does not belong to current user');
  279. }
  280. $this->applyCategory($category);
  281. }
  282. }
  283. public function applyCategory(Console_Category $category){
  284. if( $this->hasCategory($category) === false ){
  285. $this->link('Categories', array($category->ID));
  286. $this->save();
  287. }
  288. }
  289. public function removeCategory(Console_Category $category){
  290. if( $this->hasCategory($category) === true ){
  291. $this->unlink('Categories', array($category->ID));
  292. $this->save();
  293. }
  294. }
  295. }