function findByEventAndRelated($event_id, $original_event_id)
 {
     return ProjectEvents::findAll(array('conditions' => array('(`original_event_id` = ? OR `object_id` = ?) AND `object_id` <> ?', $original_event_id, $original_event_id, $event_id)));
 }
 function ical_export()
 {
     $this->setLayout('ical');
     require_once ROOT . '/environment/classes/event/CalFormatUtilities.php';
     if (!isset($_GET['t']) || !isset($_GET['cal'])) {
         header('HTTP/1.0 404 Not Found');
         die;
     }
     $token = $_GET['t'];
     $cal = $_GET['cal'];
     if (Contacts::tokenExists($token)) {
         $user = Contacts::findOne(array('conditions' => "token='{$token}'"));
         $conditions = " AND EXISTS (SELECT i.contact_id FROM " . TABLE_PREFIX . "event_invitations i WHERE i.event_id=e.object_id AND  i.contact_id=" . $user->getId() . ")";
         if (array_var($_GET, 'cal') != "") {
             $mem_cond = "e.object_id IN (SELECT object_id FROM " . TABLE_PREFIX . "object_members WHERE member_id IN (" . array_var($_GET, 'cal') . "))";
         } else {
             $mem_cond = "true";
         }
         $user_pgs = $user->getPermissionGroupIds();
         $perm_cond = " AND EXISTS (SELECT st.object_id FROM " . TABLE_PREFIX . "sharing_table st WHERE st.object_id=e.object_id AND st.group_id IN (" . implode(',', $user_pgs) . "))";
         $events = ProjectEvents::findAll(array('conditions' => "{$mem_cond} {$perm_cond} {$conditions}"));
         $calendar_name = isset($_GET['n']) ? $_GET['n'] : $user->getObjectName();
         $calendar_name = str_replace(' ', '_', $calendar_name);
         $content = CalFormatUtilities::generateICalInfo($events, $calendar_name, $user);
         tpl_assign('content', $content);
     } else {
         header('HTTP/1.0 404 Not Found');
         die;
     }
 }