PlanningEvent.class.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Define a planning event.
  4. * @author Julien NARBONI
  5. * @category Plugin
  6. * @license MIT
  7. */
  8. class PlanningEvent extends Entity{
  9. public $id,$label,$type,$startDate,$endDate,$description,$street,$city,$zip,$planning,$notificationNumber,$notificationUnity,
  10. $notificationState,
  11. $resources = array(),
  12. $repeatType,
  13. $repeatDailyNumber,
  14. $repeatWeeklyDay,
  15. $repeatMonthlyNumber,
  16. $repeatMonthlyDay,
  17. $repeatYearlyNumber,
  18. $repeatYearlyMonth,
  19. $repeatUntil,
  20. $repeatOccurence,$group;
  21. protected $TABLE_NAME = 'planning_event';
  22. public $fields =
  23. array(
  24. 'id' => 'key',
  25. 'label' => 'string',
  26. 'type' => array('type'=>'int','label'=>'Type', 'link'=>'plugin/planning/PlanningEventType.class.php'),
  27. 'startDate' => 'date',
  28. 'endDate' => 'date',
  29. 'description' => 'longstring',
  30. 'street' => 'string',
  31. 'city' => 'string',
  32. 'zip' => 'string',
  33. 'planning' => array('type'=>'int','label'=>'Planning', 'link'=>'plugin/planning/Planning.class.php'),
  34. 'group' => 'int',
  35. 'notificationNumber' => 'int',
  36. 'notificationState' => 'string',
  37. 'notificationUnity' => 'string',
  38. 'repeatType' => 'string',
  39. 'repeatDailyNumber' => 'int',
  40. 'repeatWeeklyDay' => 'string',
  41. 'repeatMonthlyNumber' => 'int',
  42. 'repeatMonthlyDay' => 'int',
  43. 'repeatYearlyNumber' => 'int',
  44. 'repeatYearlyMonth' => 'int',
  45. 'repeatUntil' => 'string',
  46. 'repeatOccurence' => 'int'
  47. );
  48. const NOTIFIED = 'notified';
  49. function __construct(){
  50. parent::__construct();
  51. //groupe unique de l'évent
  52. $this->group = time().'g'.mt_rand(0,1000);
  53. $this->notificationNumber = 0;
  54. }
  55. public static function getAll($user,$ranks,$plannings,$start = null,$end = null,$allowedPlannings = null){
  56. require_once(__DIR__.SLASH.'Planning.class.php');
  57. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  58. require_once(__DIR__.SLASH.'PlanningEventType.class.php');
  59. require_once(__DIR__.SLASH.'PlanningEventResource.class.php');
  60. if(!isset($start)) $start = strtotime('-1month');
  61. if(!isset($end)) $end = strtotime('+1month');
  62. $types = array();
  63. foreach(PlanningEventType::getAll() as $type){
  64. $types[$type->id] = $type;
  65. }
  66. $rankIds = array();
  67. foreach ($ranks as $rank) {
  68. if($rank->id==0) continue;
  69. $rankIds[] = $rank->id;
  70. }
  71. if(!isset($allowedPlannings)){
  72. $allowedPlannings = array();
  73. $sql = 'SELECT * FROM {{table}} p WHERE p.owner = ? OR p.id IN(SELECT s.planning FROM '.PlanningShare::tableName().' s WHERE (recipient = ? AND recipientEntity="user") ';
  74. if(count($rankIds)>0) $sql .= ' OR (recipient IN('.implode(',',$rankIds).') AND recipientEntity="rank" )';
  75. $sql .= ')';
  76. foreach(Planning::staticQuery($sql,array($user,$user)) as $row){
  77. $allowedPlannings[] = $row['id'];
  78. }
  79. }
  80. $originalPlannings = $plannings;
  81. foreach ($plannings as $key=>$id) {
  82. if(!in_array($id, $allowedPlannings)) unset($plannings[$key]);
  83. }
  84. //evite de planter le IN si aucun calendrier disponible
  85. if(empty($plannings)) $plannings[] = 0;
  86. $query = 'SELECT e.*,p.id AS '.Planning::tableName().'_join_id,p.color AS '.Planning::tableName().'_join_color,p.owner AS '.Planning::tableName().'_join_owner,p.default AS '.Planning::tableName().'_join_default FROM {{table}} e
  87. LEFT JOIN '.Planning::tableName().' p ON e.planning=p.id
  88. WHERE (
  89. (startDate BETWEEN ? AND ?)
  90. OR (endDate BETWEEN ? AND ?)
  91. OR (startDate<? && endDate>?)
  92. OR (repeatType != "" AND (repeatUntil = "" OR repeatUntil>startDate) )
  93. )
  94. AND planning IN('.implode(',',$plannings).')';
  95. $events = array();
  96. $eventIds = array();
  97. foreach(self::staticQuery($query,array($start,$end,$start,$end,$start,$end),true,1) as $event){
  98. $event->type = isset($types[$event->type]) ? $types[$event->type] : new PlanningEventType();
  99. $event->planning = $event->join('planning');
  100. $eventIds[] = $event->id;
  101. $event->resources = array();
  102. $events[$event->id] = $event;
  103. }
  104. if(!empty($eventIds)){
  105. $resourceLabels = Dictionary::bySlug('planning_event_resource',true);
  106. foreach(PlanningEventResource::loadAll(array('event:IN'=>$eventIds)) as $resource){
  107. $row = $resource->toArray();
  108. $row['label'] = isset($resourceLabels[$row['resource']]) ? $resourceLabels[$row['resource']]->label : '#'.$row['resource'];
  109. $events[$resource->event]->resources[] = $row;
  110. }
  111. }
  112. Plugin::callHook('planning_event_search',array(&$events,$originalPlannings,$start,$end));
  113. return $events;
  114. }
  115. public static function removeAll($user,$events){
  116. global $myUser;
  117. require_once(__DIR__.SLASH.'PlanningShare.class.php');
  118. require_once(__DIR__.SLASH.'Planning.class.php');
  119. require_once(__DIR__.SLASH.'PlanningEventResource.class.php');
  120. $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);
  121. $errors = array();
  122. foreach ($events as $event) {
  123. $planning = $event->join('planning');
  124. PlanningEventResource::delete(array('event'=>$event->id));
  125. if($planning->owner != $user && !$myUser->can('planning','configure')){
  126. if(PlanningShare::rowCount(array('planning'=>$planning->id,'edit'=>1,'recipient'=>$user))==0) {
  127. $errors[] = $event->id;
  128. continue;
  129. }
  130. }
  131. self::deleteById($event->id);
  132. }
  133. return $errors;
  134. }
  135. public function address(){
  136. if($this->street=='') return '';
  137. return $this->street.', '.$this->zip.' '.$this->city;
  138. }
  139. }
  140. ?>