public function executeGetRepetitionParamSelectTag(sfWebRequest $request) { $id = $request->getParameter('id'); $value = $request->getParameter('value'); if (!$id) { return $this->renderText(''); } $summary = ''; $tag = ''; if ($repetition = PcRepetitionPeer::retrieveByPk($id)) { if ($repetition->needsParam()) { if ($repetition->getSpecial() == 'selected_wkdays') { $weekdaysSelected = DateFormat::fromIntegerToWeekdaysSetForRepetition($value); $selectedSun = $weekdaysSelected['sun'] ? "checked='checked'" : ''; $selectedMon = $weekdaysSelected['mon'] ? "checked='checked'" : ''; $selectedTue = $weekdaysSelected['tue'] ? "checked='checked'" : ''; $selectedWed = $weekdaysSelected['wed'] ? "checked='checked'" : ''; $selectedThu = $weekdaysSelected['thu'] ? "checked='checked'" : ''; $selectedFri = $weekdaysSelected['fri'] ? "checked='checked'" : ''; $selectedSat = $weekdaysSelected['sat'] ? "checked='checked'" : ''; $tag = ""; if (PcUserPeer::getLoggedInUser()->getWeekStart() == 0) { $tag .= "<input type='checkbox' id='wdfr_sun' name='weekdaysForRepetition' value='sun' {$selectedSun} ><label for='wdfr_sun'>" . __('ACCOUNT_DOW_SUN') . "</label>"; } $tag .= "<input type='checkbox' id='wdfr_mon' name='weekdaysForRepetition' value='mon' {$selectedMon} /><label for='wdfr_mon'>" . __('ACCOUNT_DOW_MON') . "</label>"; $tag .= "<input type='checkbox' id='wdfr_tue' name='weekdaysForRepetition' value='tue' {$selectedTue} /><label for='wdfr_tue'>" . __('ACCOUNT_DOW_TUE') . "</label>"; $tag .= "<input type='checkbox' id='wdfr_wed' name='weekdaysForRepetition' value='wed' {$selectedWed} /><label for='wdfr_wed'>" . __('ACCOUNT_DOW_WED') . "</label>"; $tag .= "<input type='checkbox' id='wdfr_thu' name='weekdaysForRepetition' value='thu' {$selectedThu} /><label for='wdfr_thu'>" . __('ACCOUNT_DOW_THU') . "</label>"; $tag .= "<input type='checkbox' id='wdfr_fri' name='weekdaysForRepetition' value='fri' {$selectedFri} /><label for='wdfr_fri'>" . __('ACCOUNT_DOW_FRI') . "</label>"; $tag .= "<input type='checkbox' id='wdfr_sat' name='weekdaysForRepetition' value='sat' {$selectedSat} /><label for='wdfr_sat'>" . __('ACCOUNT_DOW_SAT') . "</label>"; if (PcUserPeer::getLoggedInUser()->getWeekStart() == 1) { $tag .= "<input type='checkbox' id='wdfr_sun' name='weekdaysForRepetition' value='sun' {$selectedSun} ><label for='wdfr_sun'>" . __('ACCOUNT_DOW_SUN') . "</label>"; } $summary = $tag; } else { $min = $repetition->getMinParam(); $max = $repetition->getMaxParam(); $tag = "<select name=\"repetitionParam\">"; for ($i = $min; $i <= $max; $i++) { $selected = $value == $i ? 'selected="selected"' : ''; $label = $repetition->isParamCardinal() ? $i : PcUtils::getOrdinalFromCardinal($i); $tag .= "<option value=\"{$i}\" {$selected}>{$label}</option>"; } $tag .= "</select>"; $summary = str_replace(__('ACCOUNT_TASK_REPETITION_SELECT_LATER'), $tag, $repetition->getLocalizedHumanExpression()); } } } return $this->renderText($summary); }
/** * Re-arranges due date to the next occurrence on the repetition. * IMPORTANT: if the task hasn't got a due date, it uses the today's date * * @param bool $isInitialAdjustment - whether we want just to compute an initial adjustment */ public function setNextOccurrence($isInitialAdjustment = false) { $repetition = PcRepetitionPeer::retrieveByPk($this->getRepetitionId()); if ($repetition->getSpecial() == 'selected_wkdays') { $startingPointForDueDateTimestamp = time() > $this->getDueDate('U') ? time() : $this->getDueDate('U'); $weekdaysSet = DateFormat::fromIntegerToWeekdaysSetForRepetition($this->getRepetitionParam()); $closestWeekdayInSet = '9999999999'; // this big to make sure the first attempt of the following loop will set a value foreach ($weekdaysSet as $k => $v) { if ($v) { $next = $isInitialAdjustment ? '' : 'next'; $closestWeekdayInSetTemp = strtotime("{$next} {$k}", $startingPointForDueDateTimestamp); // i.e.: next mon if ($closestWeekdayInSetTemp < $closestWeekdayInSet) { $closestWeekdayInSet = $closestWeekdayInSetTemp; } } } $nextTimestamp = $closestWeekdayInSet; } else { $rce = $isInitialAdjustment ? $repetition->getInitialComputerExpression() : $repetition->getComputerExpression(); $param = $repetition->isParamCardinal() ? $this->getRepetitionParam() : PcUtils::getOrdinalFromCardinal($this->getRepetitionParam()); $rce = str_replace('_X_', $param, $rce); //$rce = str_replace('_Xlong_', PcUtils::getOrdinalFromCardinal($this->getRepetitionParam(), false), $rce); $todayTimestamp = strtotime('today'); $dateFormat = DateFormat::getInstance(); $oldDueDateTimestamp = 0; if ($this->getDueDate()) { $loggedUser = PcUserPeer::getLoggedInUser(); $oldDueDateTimestamp = $dateFormat->getTimestamp($this->getDueDate($loggedUser->getDateFormat())); } else { $oldDueDateTimestamp = $todayTimestamp; } $nextTimestamp = $oldDueDateTimestamp; if (strpos($rce, '_month') === FALSE) { if ($isInitialAdjustment) { $nextTimestamp = strtotime($rce, $nextTimestamp); } else { // we are in the case we need to apply the computer expression just once or // over and over again do { $nextTimestamp = strtotime($rce, $nextTimestamp); } while ($nextTimestamp < $todayTimestamp || $nextTimestamp <= $oldDueDateTimestamp); } } else { $i = 1; $repetitionParam = $this->getRepetitionParam(); // we are in the case where to deal with months. We have to go through the months if ($isInitialAdjustment) { $monthPlus = date('F Y', strtotime('+ 0 months', $oldDueDateTimestamp)); $rceWithReplacement = str_replace('_month_', $monthPlus, $rce); $nextTimestamp = strtotime($rceWithReplacement); // comparing just the timestamps was giving some unexpected results if (date('Ymd', $nextTimestamp) < date('Ymd', $oldDueDateTimestamp)) { $monthPlus = date('F Y', strtotime('+ 1 months', $oldDueDateTimestamp)); $rceWithReplacement = str_replace('_month_', $monthPlus, $rce); $nextTimestamp = strtotime($rceWithReplacement); } } else { do { $oldDueDateTimestamp = $nextTimestamp; $oldDueDateTimestampFirstDayOfMonth = strtotime('first day of this month', $oldDueDateTimestamp); $monthPlus = date('F Y', strtotime('+' . $repetitionParam . 'months', $oldDueDateTimestampFirstDayOfMonth)); $rceWithReplacement = str_replace('_month_', $monthPlus, $rce); $nextTimestamp = strtotime($rceWithReplacement); // {{{ added this code to troubleshoot a PHP max execution error in this loop if ($i > 50) { $watchdog = new PcWatchdog(); $watchdog->setType("NEXT RECURRENCE")->setDescription("taskId: {$this->getId()}")->save(); $watchdog3 = new PcWatchdog(); $watchdog3->setType("NEXT RECURRENCE")->setDescription("monthPlus: {$monthPlus}")->save(); $watchdog4 = new PcWatchdog(); $watchdog4->setType("NEXT RECURRENCE")->setDescription("rceWithReplacement: {$rceWithReplacement}")->save(); $watchdog6 = new PcWatchdog(); $watchdog6->setType("NEXT RECURRENCE")->setDescription($nextTimestamp)->save(); $watchdog7 = new PcWatchdog(); $watchdog7->setType("NEXT RECURRENCE")->setDescription($todayTimestamp)->save(); break; } // }}} $i++; } while ($nextTimestamp < $todayTimestamp || $nextTimestamp <= $oldDueDateTimestamp); } } } $this->setDueDate(date('Y-m-d', $nextTimestamp), 'Y-m-d'); $this->save(); }