/gui/tools/webmail/plugins/todo/functions.php

https://github.com/BenBE/ispCP · PHP · 275 lines · 236 code · 36 blank · 3 comment · 42 complexity · c126dfa8c66a55af7ecc2366cbf36c20 MD5 · raw file

  1. <?php
  2. function todo_init(&$todos) {
  3. if (defined('SM_PATH'))
  4. include_once(SM_PATH . 'functions/prefs.php');
  5. else
  6. include_once('../functions/prefs.php');
  7. global $username, $hashed_dir, $data_dir, $todo_filesize;
  8. $hashed_dir = getHashedDir($username, $data_dir);
  9. clearstatcache();
  10. $todo_filesize = @filesize("$hashed_dir/$username.todo");
  11. $file = @fopen("$hashed_dir/$username.todo",'r');
  12. $todos = @fread($file ,$todo_filesize);
  13. @fclose($file);
  14. }
  15. function todo_count(&$todos)
  16. {
  17. if(strlen($todos) > 3) {
  18. $todos = trim($todos,"\n");
  19. $todos = explode("\n", $todos);
  20. $todo_count = count($todos);
  21. }
  22. else {
  23. $todos[0] = '';
  24. $todo_count = 0;
  25. }
  26. return $todo_count;
  27. }
  28. function todo_within_limit()
  29. {
  30. global $todo_count, $todo_filesize, $todo_error,$todo_maxtotsize,$todo_maxcount,$todo_maxsize,$todo_action;
  31. $todo_error = 0;
  32. if ($todo_count > $todo_maxcount - 1)
  33. $todo_error=2;
  34. $todo_filesize -= ($todo_count * 1); //don't count the record separator
  35. if ($todo_maxtotsize > 0 && $todo_maxtotsize < $todo_filesize)
  36. $todo_error = 4;
  37. if($todo_error != 0)
  38. return false;
  39. else
  40. return true;
  41. }
  42. function todo_get_todo($i, $todos)
  43. {
  44. return $todos[$i];
  45. }
  46. function todo_get_todo_size($i, $todos)
  47. {
  48. return strlen($todos[$i]);
  49. }
  50. function todo_save_todos(&$todos, $todo_count, $filename)
  51. {
  52. $file = @fopen($filename,'w');
  53. // echo "count = ($todo_count)";
  54. for ($i = 0;$i < $todo_count; $i++){
  55. if ($todos[$i] != '') {
  56. @fputs($file, $todos[$i] . "\n");
  57. }
  58. }
  59. @fclose($file);
  60. }
  61. function todo_get_todo_dl(&$todos, $i)
  62. {
  63. $todo = explode("\t", $todos[$i]);
  64. return $todo[0];
  65. }
  66. function todo_get_todo_title($todos, $i)
  67. {
  68. $todo = explode("\t", $todos[$i]);
  69. return $todo[1];
  70. }
  71. function todo_get_todo_desc($todos, $i)
  72. {
  73. $todo = explode("\t", $todos[$i]);
  74. return $todo[2];
  75. }
  76. function todo_get_todo_priority(&$todos, $i)
  77. {
  78. global $todo_use_todo_priority;
  79. $todo = explode("\t", $todos[$i]);
  80. if ( (array_key_exists(3,$todo)) && ($todo_use_todo_priority == 1) )
  81. return $todo[3];
  82. else
  83. return 0;
  84. }
  85. function todo_get_todo_dl_s(&$todo)
  86. {
  87. $t = explode("\t", $todo);
  88. return $t[0];
  89. }
  90. function todo_get_todo_title_s($todo)
  91. {
  92. $t = explode("\t", $todo);
  93. return $todo[1];
  94. }
  95. function todo_get_todo_desc_s($todo)
  96. {
  97. $t = explode("\t", $todo);
  98. return $t[2];
  99. }
  100. function todo_get_todo_priority_s(&$todo)
  101. {
  102. global $todo_use_todo_priority;
  103. $t = explode("\t", $todo);
  104. if ( (array_key_exists(3,$t)) && ($todo_use_todo_priority == 1) )
  105. return $t[3];
  106. else
  107. return 0;
  108. }
  109. global $todo_use_todo_priority;
  110. if($todo_use_todo_priority == 1) {
  111. function todo_set_todo(&$todos, $i, $deadline, $title, $desc, $prio)
  112. {
  113. if ($title == '')
  114. $title = '(No Title)';
  115. if ($desc == '')
  116. $desc = '(No Description)';
  117. $title = str_replace("\t",'&nbsp;&nbsp;&nbsp;&nbsp;',strip_tags($title));
  118. $desc = str_replace("\t",'&nbsp;&nbsp;&nbsp;&nbsp;',strip_tags($desc));
  119. $desc = str_replace("\n",'<br>',$desc);
  120. $desc = str_replace("\r",'',$desc);
  121. $todos[$i] = $deadline . "\t" . $title . "\t" . $desc . "\t" . $prio;
  122. }
  123. function todo_add_todo(&$todos, $deadline, $title, $desc, $prio) {
  124. if ($title == '')
  125. $title = '(No Title)';
  126. if ($desc == '')
  127. $desc = '(No Description)';
  128. $title = str_replace("\t",'&nbsp;&nbsp;&nbsp;&nbsp;',strip_tags($title));
  129. $desc = str_replace("\t",'&nbsp;&nbsp;&nbsp;&nbsp;',strip_tags($desc));
  130. $desc = str_replace("\n",'<br>',$desc);
  131. $desc = str_replace("\r",'',$desc);
  132. $todo_count = count($todos);
  133. $todos[$todo_count] = $deadline . "\t" . $title . "\t" . $desc . "\t" . $prio;
  134. }
  135. function todo_cmp_prio_asc($todo1, $todo2)
  136. {
  137. $p1 = todo_get_todo_priority_s($todo1);
  138. $p2 = todo_get_todo_priority_s($todo2);
  139. //smaller priority is more important!!
  140. return ($p1 < $p2)?-1:1;
  141. }
  142. function todo_cmp_prio_desc($todo1, $todo2)
  143. {
  144. $p1 = todo_get_todo_priority_s($todo1);
  145. $p2 = todo_get_todo_priority_s($todo2);
  146. //smaller priority is more important!!
  147. return ($p1 >= $p2)?-1:1;
  148. }
  149. function todo_cmp_priodate_asc($todo1, $todo2)
  150. {
  151. $p1 = todo_get_todo_priority_s($todo1);
  152. $p2 = todo_get_todo_priority_s($todo2);
  153. $d1 = todo_get_todo_dl_s($todo1);
  154. $d2 = todo_get_todo_dl_s($todo2);
  155. if(strcmp($d1,$d2) == 0)
  156. return ($p1 < $p2)?-1:1;
  157. else
  158. return strcmp($d1,$d2);
  159. }
  160. function todo_cmp_priodate_desc($todo1, $todo2)
  161. {
  162. $p1 = todo_get_todo_priority_s($todo1);
  163. $p2 = todo_get_todo_priority_s($todo2);
  164. $d1 = todo_get_todo_dl_s($todo1);
  165. $d2 = todo_get_todo_dl_s($todo2);
  166. if(strcmp($d1,$d2) == 0)
  167. return ($p1 >= $p2)?-1:1;
  168. else
  169. return strcmp($d1,$d2);
  170. }
  171. }
  172. else {
  173. function todo_set_todo(&$todos, $i, $deadline, $title, $desc)
  174. {
  175. if ($title == '')
  176. $title = '(No Title)';
  177. if ($desc == '')
  178. $desc = '(No Description)';
  179. $title = str_replace("\t",'&nbsp;&nbsp;&nbsp;&nbsp;',strip_tags($title));
  180. $desc = str_replace("\t",'&nbsp;&nbsp;&nbsp;&nbsp;',strip_tags($desc));
  181. $desc = str_replace("\n",'<br>',$desc);
  182. $desc = str_replace("\r",'',$desc);
  183. $todos[$i] = $deadline . "\t" . $title . "\t" . $desc;
  184. }
  185. function todo_add_todo(&$todos, $deadline, $title, $desc) {
  186. if ($title == '')
  187. $title = '(No Title)';
  188. if ($desc == '')
  189. $desc = '(No Description)';
  190. $title = str_replace("\t",'&nbsp;&nbsp;&nbsp;&nbsp;',strip_tags($title));
  191. $desc = str_replace("\t",'&nbsp;&nbsp;&nbsp;&nbsp;',strip_tags($desc));
  192. $desc = str_replace("\n",'<br>',$desc);
  193. $desc = str_replace("\r",'',$desc);
  194. $todo_count = count($todos);
  195. $todos[$todo_count] = $deadline . "\t" . $title . "\t" . $desc;
  196. }
  197. }
  198. function todo_overdue_days(&$todos, $i)
  199. {
  200. $deadline = todo_get_todo_dl($todos, $i);
  201. $today = date('Y-m-d');
  202. $td_a = explode('-', $today);
  203. $dl_a = explode('-', $deadline);
  204. $overdue = ($td_a[0] - 2000) * 365 + ($td_a[1] - 1) * 30 + ($td_a[2] - 1);
  205. $overdue -= ($dl_a[0] - 2000) * 365 + ($dl_a[1] - 1) * 30 + ($dl_a[2] - 1);
  206. return $overdue;
  207. }
  208. function todo_within_deadline(&$todos)
  209. {
  210. $all_ok = true;
  211. $n = count($todos);
  212. for($i=0;$i<$n;$i++) {
  213. if(todo_overdue_days($todos, $i) > 0)
  214. $all_ok = false;
  215. }
  216. return $all_ok;
  217. }
  218. function todo_cmp_date_asc($todo1, $todo2)
  219. {
  220. $d1 = todo_get_todo_dl_s($todo1);
  221. $d2 = todo_get_todo_dl_s($todo2);
  222. return(strcmp($d1,$d2));
  223. }
  224. function todo_cmp_date_desc($todo1, $todo2)
  225. {
  226. $d1 = todo_get_todo_dl_s($todo1);
  227. $d2 = todo_get_todo_dl_s($todo2);
  228. return(0-strcmp($d1,$d2));
  229. }
  230. function todo_sort(&$todos,$sort_dir, $sort_type)
  231. {
  232. usort($todos,"todo_cmp_".$sort_type."_".$sort_dir);
  233. }
  234. function print_title($text)
  235. {
  236. global $color;
  237. echo "<table bgcolor='$color[0]' border=0 cellpadding=1 width='100%' align='center'><tr><td align='center'><b>$text</b></td></tr></table>";
  238. }
  239. ?>