PlanningEvent.class.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Define a planning event.
  4. * @author Julien NARBONI
  5. * @category Plugin
  6. * @license copyright
  7. */
  8. class PlanningEvent extends Entity{
  9. public $id,$label,$type,$startDate,$endDate,$description,$street,$city,$zip,$planning,$notificationNumber,$notificationUnity,$notificationState,
  10. $repeatType,
  11. $repeatDailyNumber,
  12. $repeatWeeklyDay,
  13. $repeatMonthlyNumber,
  14. $repeatMonthlyDay,
  15. $repeatYearlyNumber,
  16. $repeatYearlyMonth,
  17. $repeatUntil,
  18. $repeatOccurence;
  19. protected $TABLE_NAME = 'planning_event';
  20. public $links = array(
  21. 'planning' => 'Planning',
  22. 'type' => 'PlanningEventType',
  23. );
  24. public $fields =
  25. array(
  26. 'id' => 'key',
  27. 'label' => 'string',
  28. 'type' => 'int',
  29. 'startDate' => 'date',
  30. 'endDate' => 'date',
  31. 'description' => 'longstring',
  32. 'street' => 'string',
  33. 'city' => 'string',
  34. 'zip' => 'string',
  35. 'planning' => 'int',
  36. 'notificationNumber' => 'int',
  37. 'notificationState' => 'string',
  38. 'notificationUnity' => 'string',
  39. 'repeatType' => 'string',
  40. 'repeatDailyNumber' => 'int',
  41. 'repeatWeeklyDay' => 'string',
  42. 'repeatMonthlyNumber' => 'int',
  43. 'repeatMonthlyDay' => 'int',
  44. 'repeatYearlyNumber' => 'int',
  45. 'repeatYearlyMonth' => 'int',
  46. 'repeatUntil' => 'string',
  47. 'repeatOccurence' => 'int'
  48. );
  49. const NOTIFIED = 'notified';
  50. public function __construct(){
  51. parent::__construct();
  52. $this->notificationNumber = 0;
  53. }
  54. public static function getAll($user,$ranks,$plannings,$start = null,$end = null){
  55. require_once(__DIR__.SLASH.'Planning.class.php');
  56. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  57. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  58. if(!isset($start)) $start = strtotime('-1month');
  59. if(!isset($end)) $end = strtotime('+1month');
  60. $types = array();
  61. foreach(PlanningEventType::getAll() as $type){
  62. $types[$type->id] = $type;
  63. }
  64. $rankIds = array();
  65. foreach ($ranks as $rank) {
  66. if($rank->id==0) continue;
  67. $rankIds[] = $rank->id;
  68. }
  69. $allowedPlannings = array();
  70. $sql = 'SELECT * FROM {{table}} p WHERE p.owner = ? OR p.id IN(SELECT s.planning FROM '.PlanningShare::tableName().' s WHERE (recipient = ? AND recipientEntity="user") ';
  71. if(count($rankIds)>0) $sql .= ' OR (recipient IN('.implode(',',$rankIds).') AND recipientEntity="rank" )';
  72. $sql .= ')';
  73. foreach(Planning::staticQuery($sql,array($user,$user)) as $row){
  74. $allowedPlannings[] = $row['id'];
  75. }
  76. $originalPlannings = $plannings;
  77. foreach ($plannings as $key=>$id) {
  78. if(!in_array($id, $allowedPlannings)) unset($plannings[$key]);
  79. }
  80. $query = 'SELECT e.*,p.id AS '.Planning::tableName().'_join_id,p.color AS '.Planning::tableName().'_join_color FROM {{table}} e LEFT JOIN '.Planning::tableName().' p ON e.planning=p.id WHERE ((startDate BETWEEN ? AND ?) OR (endDate BETWEEN ? AND ?) OR (startDate<? && endDate>?)) AND planning IN('.implode(',',$plannings).')';
  81. $events = array();
  82. foreach(self::staticQuery($query,array($start,$end,$start,$end,$start,$end),true,1) as $event){
  83. $event->type = isset($types[$event->type]) ? $types[$event->type] : new PlanningEventType();
  84. $event->planning = $event->join('planning');
  85. $events[] = $event;
  86. }
  87. Plugin::callHook('planning_event_search',array(&$events,$originalPlannings,$start,$end));
  88. return $events;
  89. }
  90. public static function removeAll($user,$events){
  91. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  92. require_once(__DIR__.SLASH.'Planning.class.php');
  93. $events = self::staticQuery('SELECT e.*,p.id AS '.Planning::tableName().'_join_id,p.owner AS '.Planning::tableName().'_join_owner FROM {{table}} e LEFT JOIN '.Planning::tableName().' p ON p.id=e.planning WHERE e.id IN ('.implode(',',$events).')',array(),true,1);
  94. $errors = array();
  95. foreach ($events as $event) {
  96. $planning = $event->join('planning');
  97. if($planning->owner != $user){
  98. if(PlanningShare::rowCount(array('planning'=>$planning->id,'edit'=>1,'recipient'=>$user))==0) {
  99. $errors[] = $event->id;
  100. continue;
  101. }
  102. }
  103. self::deleteById($event->id);
  104. }
  105. return $errors;
  106. }
  107. public function address(){
  108. if($this->street=='') return '';
  109. return $this->street.', '.$this->zip.' '.$this->city;
  110. }
  111. }
  112. ?>