/**
  * Render icalendar from milestones
  *
  * @param string $calendar_name
  * @param array $milestones
  * @return null
  */
 private function renderCalendar(User $user, $calendar_name, $milestones)
 {
     $calendar = new iCalendar_Calendar();
     $calendar->setPropertyValue('VERSION', '2.0');
     $calendar->setPropertyValue('PRODID', '-//Apple Computer\\, Inc//iCal 1.5//EN');
     $calendar->setPropertyValue('X-WR-CALNAME', $calendar_name);
     $calendar->setPropertyValue('X-WR-TIMEZONE', 'GMT');
     if (is_array($milestones)) {
         foreach ($milestones as $milestone) {
             if (!$user->isMemberOfOwnerCompany() && $milestone->isPrivate()) {
                 continue;
             }
             // hide private milestone
             if (!$milestone->isCompleted()) {
                 $event = new iCalendar_Event();
                 $date = $milestone->getDueDate();
                 $event->setPropertyValue('DTSTART', $date->format('Ymd'), array('VALUE' => 'DATE'));
                 $date->advance(24 * 60 * 60);
                 $event->setPropertyValue('DTEND', $date->format('Ymd'), array('VALUE' => 'DATE'));
                 $event->setPropertyValue('UID', $milestone->getId());
                 $event->setPropertyValue('SUMMARY', $milestone->getName() . ' (' . $milestone->getProject()->getName() . ')');
                 $event->setPropertyValue('DESCRIPTION', $desc = $milestone->getDescription());
                 /* pre_var_dump($desc); */
                 $calendar->addComponent($event);
             }
             // if
         }
         // foreach
     }
     // if
     header('Content-Disposition: inline; filename=calendar.ics');
     $this->renderText(iCalendar::render($calendar), true);
     die;
 }
 /**
  * Render icalendar from milestones
  *
  * @param string $calendar_name
  * @param array $milestones
  * @return null
  */
 private function renderCalendar(User $user, $calendar_name, $milestones, $user_active_projects)
 {
     $calendar = new iCalendar_Calendar();
     $calendar->setPropertyValue('VERSION', '2.0');
     $calendar->setPropertyValue('PRODID', '-//Apple Computer\\, Inc//iCal 1.5//EN');
     $calendar->setPropertyValue('X-WR-CALNAME', $calendar_name);
     $calendar->setPropertyValue('X-WR-TIMEZONE', 'GMT');
     if (is_array($user_active_projects)) {
         foreach ($user_active_projects as $project) {
             $assigned_tasks = $project->getUsersTasks(logged_user());
             if (is_array($assigned_tasks)) {
                 foreach ($assigned_tasks as $task) {
                     $todo = new iCalendar_Todo();
                     $todo->setPropertyValue('SUMMARY', $project->getName() . ": " . $task->getText());
                     $todo->setPropertyValue('UID', 'a9idfv00fd99q344o' . rand() . '*****@*****.**');
                     $date = $task->getDueDate();
                     if (!is_null($date)) {
                         $todo->setPropertyValue('DTSTART', $date->format('Ymd'), array('VALUE' => 'DATE'));
                     }
                     $priority = $task->getTaskList()->getPriority();
                     $priority = $priority ? $priority : 1;
                     $todo->setPropertyValue('PRIORITY', $priority);
                     $todo->setPropertyValue('STATUS', "NEEDS-ACTION");
                     $todo->setPropertyValue('URL', externalUrl($task->getCompleteUrl()));
                     $todo->setPropertyValue('DESCRIPTION', 'Bla Bla Bla');
                     // seting an alarm
                     $alarm = new iCalendar_Alarm();
                     $alarm->setPropertyValue('ACTION', 'DISPLAY');
                     $alarm->setPropertyValue('TRIGGER', '-P7D');
                     $alarm->setPropertyValue('DESCRIPTION', $project->getName() . ": " . $task->getText());
                     $todo->addComponent($alarm);
                     // end alarm
                     $calendar->addComponent($todo);
                 }
             }
         }
     }
     if (is_array($milestones)) {
         foreach ($milestones as $milestone) {
             if (!$user->isMemberOfOwnerCompany() && $milestone->isPrivate()) {
                 continue;
                 // hide private milestone
             }
             if (!$milestone->isCompleted()) {
                 $event = new iCalendar_Event();
                 $date = $milestone->getDueDate();
                 $event->setPropertyValue('DTSTART', $date->format('Ymd'), array('VALUE' => 'DATE'));
                 $date->advance(24 * 60 * 60);
                 $event->setPropertyValue('DTEND', $date->format('Ymd'), array('VALUE' => 'DATE'));
                 $event->setPropertyValue('UID', 'a9idfv00fd99q344o' . rand() . '*****@*****.**');
                 $event->setPropertyValue('SUMMARY', $milestone->getName() . ' (' . $milestone->getProject()->getName() . ')');
                 $event->setPropertyValue('DESCRIPTION', $desc = $milestone->getDescription());
                 $event->setPropertyValue('URL', externalUrl($milestone->getViewUrl()));
                 // setting an alarm
                 $alarm = new iCalendar_Alarm();
                 $alarm->setPropertyValue('ACTION', 'DISPLAY');
                 $alarm->setPropertyValue('TRIGGER', '-P7D');
                 $alarm->setPropertyValue('DESCRIPTION', $milestone->getName() . ' (' . $milestone->getProject()->getName() . ')');
                 $event->addComponent($alarm);
                 // end alarm
                 /* pre_var_dump($desc); */
                 $calendar->addComponent($event);
             }
             // if
         }
         // foreach
     }
     // if
     header('Content-Disposition: inline; filename=calendar.ics');
     $this->renderText(iCalendar::render($calendar), true);
     session_write_close();
     die;
 }