/** * AutoGenerate the on for the specified relationship. * * @return string * @author Justin Palmer **/ private function autoGenerateOn($name) { $options = $this->relationships->get($name); $ret = ''; switch ($options->type) { case 'has-one': $ret = $this->model->alias() . "." . $this->model->primary_key() . " = " . $options->alias . "." . $options->foreign_key; break; case 'has-many': $ret = $options->table . "." . $options->foreign_key . " = ?"; break; } return $ret; }
public function timelineEvents($currUserID, $date, $date2, $view = 0, $mail = false) { if ($view < 2) { $this->loadModel('UserEventShare'); $conditions = array('UserEventShare.acceptance <> ?' => '-1', 'UserEventShare.user_id' => $currUserID); $aEventShare = $this->UserEventShare->find('all', compact('conditions')); $aEventID = Hash::extract($aEventShare, '{n}.UserEventShare.user_event_id'); $aEventShare = Hash::combine($aEventShare, '{n}.UserEventShare.user_event_id', '{n}.UserEventShare'); //$dateRange = $this->dateRange('UserEvent.event_time', $date, $date2); $dateRange = $this->dateRange('ProjectEvent.created', $date2, $date); if (strtotime($date2) > strtotime($date) || $mail) { $dateRange = $this->dateTimeRange('ProjectEvent.created', $date, $date2); } $conditions = array(); $this->loadModel('Group'); $this->loadModel('GroupMember'); $conditions = array('GroupMember.user_id' => $currUserID, 'GroupMember.is_deleted' => 0, 'GroupMember.approved' => 1); $userGroups = $this->GroupMember->find('all', compact('conditions')); $userGroups = Hash::extract($userGroups, '{n}.GroupMember.group_id'); $dateRange = array($this->dateRange('UserEvent.created', $date2, $date), $this->dateRange('UserEvent.event_time', $date2, $date), $this->dateRange('UserEvent.event_end_time', $date2, $date)); if (strtotime($date2) > strtotime($date) || $mail) { $dateRange = array($this->dateRange('UserEvent.created', $date, $date2), $this->dateRange('UserEvent.event_time', $date, $date2), $this->dateRange('UserEvent.event_end_time', $date, $date2)); } $conditions = array('UserEvent.is_delayed' => '0', 'UserEvent.type !=' => 'task', array('OR' => array('AND' => array('UserEvent.user_id' => $currUserID, $dateRange), 'AND' => array('UserEvent.id' => $aEventID, 'UserEvent.shared' => '1'))), array('OR' => $dateRange)); if ($mail) { $conditions[] = $dateRange; } $order = array('UserEvent.event_time', 'UserEvent.created'); $a_Events = $this->find('all', array('conditions' => $conditions, 'order' => $order, 'contain' => array('UserEventShare'))); $conditions = array('UserEvent.is_delayed' => '0', 'UserEvent.type' => 'task', 'OR' => array(array('AND' => array('UserEvent.user_id' => $currUserID)), array('AND' => array('UserEvent.id' => $aEventID, 'UserEvent.shared' => '1')), array('AND' => array('UserEvent.object_id' => $userGroups, 'UserEvent.external' => '1')))); if ($mail) { $conditions[] = $dateRange; } $order = array('UserEvent.event_time', 'UserEvent.created'); $contain = array('UserEventShare'); $t_Events = $this->find('all', array('conditions' => $conditions, 'order' => $order, 'rcursive' => -1)); $aEvents = array_merge($a_Events, $t_Events); // for unique event time $eventTime = hash::get($aEvents, '{0}.UserEvent.event_time'); $count = 0; $unAccepted = 0; $this->loadModel('UserEventShare'); foreach ($aEvents as &$event) { if (!empty($aEventShare[$event['UserEvent']['id']]) && $aEventShare[$event['UserEvent']['id']]['acceptance'] == '0' && !$mail) { $dateX = date('Y-m-d H:') . '00:' . ($unAccepted < 10 ? '0' . $unAccepted : $unAccepted); $event['UserEvent']['real_event_time'] = $event['UserEvent']['event_time']; $event['UserEvent']['event_time'] = $dateX; $unAccepted++; } else { if ($event['UserEvent']['event_time'] == $eventTime) { $count++; $event['UserEvent']['event_time'] = date('Y-m-d H:i', strtotime($eventTime)) . ':0' . $count; } else { $eventTime = $event['UserEvent']['event_time']; $count = 0; } } $conditions = array('UserEventShare.user_event_id' => $event['UserEvent']['id']); //Not sure but do not get all shares for non initiator user if ($event['UserEvent']['user_id'] != $currUserID) { $conditions = array_merge($conditions, array('UserEventShare.user_id' => $currUserID)); } $shareEv = $this->UserEventShare->find('all', array('conditions' => $conditions)); $ownShare = Hash::extract($shareEv, '{n}.user_id'); $aSharesEvents = Hash::combine($shareEv, '{n}.user_id', '{n}'); //Временно, если есть события, в которых в шарах ещё не прописан создатель //$ownShare = $this->UserEventShare->findByUserEventIdAndUserId($event['UserEvent']['id'], $event['UserEvent']['user_id']); if (!in_array($event['UserEvent']['user_id'], $ownShare)) { $this->UserEventShare->save(array('user_event_id' => $event['UserEvent']['id'], 'user_id' => $event['UserEvent']['user_id'], 'acceptance' => '1')); } $aShares = array(); foreach ($aSharesEvents as $key => $value) { $aShares[$key]['UserEventShare'] = $value; } if (empty($aShares[$currUserID])) { $aShares[$currUserID] = array('UserEventShare' => array('id' => null, 'user_id' => $event['UserEvent']['user_id'], 'user_event_id' => $currUserID, 'acceptance' => 0)); } if ($aShares[$currUserID]['UserEventShare']['acceptance'] >= 0) { $event['UserEvent']['accepted'] = $aShares; } else { //remove all element wher acceptance < 1 $event = null; } unset($event['UserEventShare']); } $aEvents = array_filter($aEvents); return array_reverse($aEvents); } return array(); }