/**
  * Adds an entry in the Watchdog log
  *
  * @param  string $type
  * @param  string $description(='')
  */
 public static function alert($type, $description = '')
 {
     $entry = new PcWatchdog();
     $entry->setType($type);
     $entry->setDescription($description);
     $entry->save();
 }
 /**
  * If the email is too big (probably because of attachments), it records
  * only the beginning of it
  *
  * @param string $errorMessage
  * @param string $emailPath
  */
 private function handleFault($errorMessage, $emailPath)
 {
     $this->log($errorMessage);
     sfErrorNotifier::alert($errorMessage);
     $emailFileSize = filesize($emailPath);
     if ($emailFileSize < 32000) {
         $description = file_get_contents($emailPath);
     } else {
         $description = "The email was too big (probably because of attachment). Here is the beginning of it: \n\n";
         $description .= shell_exec("cat {$emailPath} | head -100");
     }
     $watchdog = new PcWatchdog();
     $watchdog->setType('email-to-inbox')->setDescription($description)->save();
     if (is_file($emailPath)) {
         unlink($emailPath);
     }
 }
Exemple #3
0
 /**
  * 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();
 }