public function main() { $name = Utility::cleanString($_POST["name"]); $notes = Utility::cleanString($_POST["notes"]); $is_chapter = Utility::cleanBoolean($_POST["is_chapter"]); $all_day_event = !$is_chapter && Utility::cleanBoolean($_POST["is_all_day"]); $start = Utility::getDateTimeFromDateYmdHis(Utility::cleanString($_POST["time_start"])); $end = Utility::getDateTimeFromDateYmdHis(Utility::cleanString($_POST["time_end"])); $is_repeating = !$is_chapter && Utility::cleanBoolean($_POST["is_repeating"]); $n_times = Utility::cleanInt($_POST["n_times"], 2); $repeat_type = Utility::cleanInt($_POST["repeat_type"], Group::TYPE_DAYS, Group::TYPE_YEARS); if ($name == "") { $this->setError(self::$E_INVALID_NAME); } else { if (!$all_day_event && !$start) { $this->setError(self::$E_INVALID_DATE_START); } else { if (!$all_day_event && !$is_chapter && !$end) { $this->setError(self::$E_INVALID_DATE_END); } else { if (!$all_day_event && $end <= $start) { $this->setError(self::$E_INVALID_DATE_END_BEFORE_START); } else { if ($is_repeating) { if ($n_times === false) { $this->setError(self::$E_INVALID_REPEAT_TIME); } else { if ($repeat_type === false) { $this->setError(self::$E_INVALID_REPEAT_TYPE); } } } } } } } if ($this->hasError()) { return []; } $_events = []; if ($all_day_event) { $start = $start ?: null; $end = $end ?: null; } if (!$is_repeating) { $_event = new Event($this->_pdo); $_event->create($name, $notes, $all_day_event, $is_chapter, $this->_auth->getUser(), $end, $start); $_events[] = $_event; } else { $_group = new Group($this->_pdo); $_group->create($name, $notes, $all_day_event, $is_chapter, $n_times, $this->_auth->getUser(), $repeat_type, $start, $end); $_events = $_group->generateEvents(); } return $_events; }
/** * @return Event[] */ public function generateEvents() { $start = new DateTime($this->start); $end = new DateTime($this->end); $end = $end->add($start->diff($end)); switch ($this->repeat_type) { case self::TYPE_WEEKS: $interval = DateInterval::createFromDateString("1 week"); break; case self::TYPE_MONTHS: $interval = DateInterval::createFromDateString("1 month"); break; case self::TYPE_YEARS: $interval = DateInterval::createFromDateString("1 year"); break; default: $interval = DateInterval::createFromDateString("1 day"); break; } $_existing_events = Event::findAllForGroup($this->_pdo, $this); $_existing_events_keyed = []; foreach ($_existing_events as $_existing_event) { $key = Utility::getDateTimeForMySQLDate($_existing_event->getStart()); $_existing_events_keyed[$key] = $_existing_event; } $_events = []; while (count($_events) < $this->n_times) { if (array_key_exists(Utility::getDateTimeForMySQLDate($start), $_existing_events_keyed)) { continue; } $_event = new Event($this->_pdo); $_event->create($this->name, $this->notes, $this->all_day_event, $this->is_chapter, $this->getCreator(), $end, $start, $this->id); $_events[] = $_event; $start->add($interval); $end->add($interval); } return array_merge($_existing_events, $_events); }