Exemple #1
0
 /**
  * Method to access an event
  *
  * @param  int   $id specific id requested
  * @return mixed
  */
 public function getEvent($id = null, $formatTime = true)
 {
     $app = \Cobalt\Container::fetch('app');
     //db
     $db = JFactory::getDBO();
     if (!$id) {
         $event = new EventTable();
         return $event;
     }
     $data = $app->input->getRequest('post');
     //determine if we are trying to retrieve a virtual event, if so retrieve its parent instead
     $query = $db->getQuery(true);
     $query->select('id')->from("#__events")->where("id=" . $id);
     $db->setQuery($query);
     $result = $db->loadAssocList();
     //if we dont find any results use the parent id instead
     if (count($result) == 0 || count(explode("-", $id)) > 1) {
         $id = $app->input->get('parent_id');
     }
     //gen query
     $query->clear();
     $query->select("e.*,ecf.association_id,ecf.association_type,ecf.event_id,owner.first_name as owner_first_name, owner.last_name as owner_last_name,\n            d.name as deal_name, d.amount as deal_amount, CONCAT(p.first_name,' ',p.last_name) AS person_name, c.name as company_name, c.id as company_id,\n            c.address_1 as company_address_1, c.address_city as company_address_city, c.address_state as company_address_state, c.address_zip as company_address_zip,\n            c.phone as company_phone, c.website as company_website,assignee.first_name AS assignee_first_name,assignee.last_name AS assignee_last_name\n            ");
     $query->from("#__events as e");
     //left join any assocations
     $query->leftJoin('#__events_cf AS ecf ON ecf.event_id = e.id');
     $query->leftJoin('#__users AS owner ON owner.id = e.owner_id');
     $query->leftJoin('#__deals as d ON ecf.association_id = d.id AND ecf.association_type = "deal" AND d.published>0');
     $query->leftJoin('#__people as p ON ecf.association_id = p.id AND ecf.association_type = "person" AND p.published>0');
     $query->leftJoin('#__companies as c ON ecf.association_id = c.id AND ecf.association_type = "company" AND c.published>0');
     $query->leftJoin('#__users AS assignee ON assignee.id = e.assignee_id');
     $query->where("e.id=" . $id);
     //set query
     $db->setQuery($query);
     //load results
     $results = $db->loadAssocList();
     //clean results
     //assign the parent id if needed
     if (count($result) == 0 && is_array($results) && array_key_exists(0, $results)) {
         $results[0]['parent_id'] = $results[0]['id'];
         unset($results[0]['id']);
     }
     if (is_array($results) && array_key_exists(0, $results)) {
         if ($results[0]['repeats'] != "none" && $app->input->get('date') && count($results) > 0) {
             if (array_key_exists('type', $results[0]) && $results[0]['type'] == "event") {
                 $stime = explode(" ", $results[0]['start_time']);
                 $results[0]['start_time_hour'] = $stime[1];
                 $etime = explode(" ", $results[0]['end_time']);
                 $results[0]['end_time_hour'] = $etime[1];
                 $results[0]['start_time'] = $app->input->get('date');
                 $results[0]['end_time'] = $app->input->get('date');
             } elseif (array_key_exists('due_date', $results[0])) {
                 $dtime = explode(" ", $results[0]['due_date']);
                 $results[0]['due_date_hour'] = $dtime[1];
                 $results[0]['due_date'] = $app->input->get('date');
             }
         }
         if ($formatTime) {
             $originalDate = $results[0]['created'];
             if (array_key_exists('created', $results[0])) {
                 $results[0]['created_formatted'] = array_key_exists('created', $results[0]) ? DateHelper::formatDate($originalDate) : "";
                 $results[0]['created'] = DateHelper::formatDate($originalDate, true, false);
             }
             if (array_key_exists('repeat_end', $results[0])) {
                 $results[0]['repeat_end_formatted'] = DateHelper::formatDate($results[0]['repeat_end']);
                 $results[0]['repeat_end'] = DateHelper::formatDate($results[0]['repeat_end'], true, false);
             }
             if (array_key_exists('modified', $results[0])) {
                 $results[0]['modified_formatted'] = DateHelper::formatDate($results[0]['modified']);
                 $results[0]['modified'] = DateHelper::formatDate($results[0]['modified'], true, false);
             }
             if (array_key_exists('actual_close', $results[0])) {
                 $results[0]['actual_close_formatted'] = DateHelper::formatDate($results[0]['actual_close']);
                 $results[0]['actual_close'] = DateHelper::formatDate($results[0]['actual_close'], true, false);
             }
             if (array_key_exists('due_date', $results[0])) {
                 $results[0]['due_date_formatted'] = $app->input->get('date') ? DateHelper::formatDateString($results[0]['due_date']) : DateHelper::formatDate($results[0]['due_date']);
                 $results[0]['due_date'] = $app->input->get('date') ? DateHelper::formatDateString($results[0]['due_date']) : DateHelper::formatDate($results[0]['due_date'], true, false);
             }
             if (array_key_exists('end_date', $results[0])) {
                 $results[0]['end_date_formatted'] = $app->input->get('date') ? DateHelper::formatDateString($results[0]['end_date']) : DateHelper::formatDate($results[0]['end_date']);
                 $results[0]['end_date'] = $app->input->get('date') ? DateHelper::formatDateString($results[0]['end_date']) : DateHelper::formatDate($results[0]['end_date'], true, false);
             }
             if (array_key_exists('start_time', $results[0])) {
                 $results[0]['start_time_formatted'] = $app->input->get('date') ? DateHelper::formatDateString($results[0]['start_time']) : DateHelper::formatDate($results[0]['start_time']);
                 $results[0]['start_time'] = $app->input->get('date') ? DateHelper::formatDateString($results[0]['start_time']) : DateHelper::formatDate($results[0]['start_time'], true, false);
             }
             if (array_key_exists('end_time', $results[0])) {
                 $results[0]['end_time_formatted'] = $app->input->get('date') ? DateHelper::formatDateString($results[0]['end_time']) : DateHelper::formatDate($results[0]['end_time']);
                 $results[0]['end_time'] = $app->input->get('date') ? DateHelper::formatDateString($results[0]['end_time']) : DateHelper::formatDate($results[0]['end_time'], true, false);
             }
             if (array_key_exists(0, $results) && !$app->input->get('date')) {
                 if (array_key_exists('type', $results[0]) && $results[0]['type'] == "event") {
                     $stime = explode(" ", $results[0]['start_time']);
                     $results[0]['start_time_hour'] = $stime[1];
                     $etime = explode(" ", $results[0]['end_time']);
                     $results[0]['end_time_hour'] = $etime[1];
                     $results[0]['start_time'] = $stime[0];
                     $results[0]['end_time'] = $etime[0];
                 } elseif (array_key_exists('due_date', $results[0])) {
                     $dtime = explode(" ", $results[0]['due_date']);
                     $results[0]['due_date_hour'] = $dtime[1];
                     $results[0]['due_date'] = $dtime[0];
                 }
             }
         }
         //filter results for calendar display
         if ($app->input->get('calendar_filter')) {
             $results[0]['title'] = $results[0]['name'];
             $results[0]['allDay'] = $results[0]['all_day'];
             //determine event type
             if ($results[0]['type'] == 'event') {
                 $results[0]['start'] = $results[0]['start_time'];
                 $results[0]['end'] = $results[0]['end_time'];
             }
             if ($results[0]['type'] == 'task') {
                 $results[0]['start'] = $results[0]['due_date'];
             }
         }
         //join any association data
         if (array_key_exists('association_id', $results[0]) and !is_null($results[0]['association_id']) and $results[0]['association_id'] != 0) {
             $query->clear();
             switch ($results[0]['association_type']) {
                 case 'company':
                     $query->select("name as association_name, id as association_id");
                     $query->from("#__companies");
                     break;
                 case 'deal':
                     $query->select("name as association_name, id as association_id");
                     $query->from("#__deals");
                     break;
                 case 'person':
                     $query->select("first_name as association_first_name, last_name as association_last_name, id as association_id");
                     $query->from("#__people");
                     break;
             }
             $query->where("published>0");
             $query->where("id=" . $results[0]['association_id']);
             $db->setQuery($query);
             $data = $db->loadAssocList();
             if (!empty($data)) {
                 $results[0] = array_merge($results[0], $data[0]);
                 if ($results[0]['association_type'] == 'person') {
                     $results[0]['association_name'] = $data[0]['association_first_name'] . " " . $data[0]['association_last_name'];
                 }
             }
         }
         // $dispatcher = JDispatcher::getInstance();
         // $dispatcher->trigger('onEventLoad', array(&$results[0]));
     }
     //return results
     if (is_array($results) && array_key_exists(0, $results)) {
         return $results[0];
     } else {
         $table = new EventTable();
         return $table;
     }
 }