Пример #1
0
 /**
  * parse a single line string for task params
  * @return a task object
  */
 public static function parse($str, &$def, &$dte)
 {
     if (!preg_match('/^(\\* |\\*{2,3})?([+|-][0-9]{0,2}|[0-9]{2}\\/[0-9]{2})?( ?[0-9]+\\))?(.+)?$/', $str, $arr)) {
         return false;
     }
     ArrayHelper::arrayTrim($arr);
     $obj = new TaskModel();
     $tst = empty($arr[1]) ? substr($arr[2], 0, 1) : '*';
     switch ($tst) {
         case '*':
             // multiple
             if ($arr[1] == '**') {
                 // reset default date (**)
                 $dte = '';
             } else {
                 if ($arr[1] == '***') {
                     // reset default title (***)
                     $def = '';
                 } else {
                     if ($arr[2]) {
                         // remember date
                         if ($tst == '+') {
                             if ($n = substr($arr[2], 1)) {
                                 // number of days later
                                 $obj->set('deadline', $arr[2] . ' days');
                             } else {
                                 // no number, means today
                                 $obj->set('deadline', APP_SQL_TODAY);
                             }
                         } else {
                             $dte = $arr[2];
                         }
                     }
                     if ($arr[4]) {
                         // remember title
                         $def = $arr[4];
                     }
                 }
             }
             return false;
             break;
         case '-':
             // no deadline
             $obj->set('deadline', '9999-00-00');
             break;
         case '+':
             // specify deadline
             if ($n = substr($arr[2], 1)) {
                 // number of days later
                 $obj->set('deadline', $arr[2] . ' days');
             } else {
                 // no number, means today
                 $obj->set('deadline', date_format(new DateTime('now', $GLOBALS['config']['datetime']['timezone_user']), 'Y-m-d'));
             }
             break;
         default:
             if ($arr[2]) {
                 // specific date set
                 $obj->set('deadline', $arr[2]);
                 $dte = $obj->get('deadline');
             } else {
                 if (!empty($dte)) {
                     // use default date (from batch)
                     $obj->set('deadline', $dte);
                 } else {
                     if ($GLOBALS['config']['task']['date']) {
                         // use default date (config)
                         $obj->set('deadline', date_format(new DateTime($GLOBALS['config']['task']['date'], $GLOBALS['config']['datetime']['timezone_user']), 'Y-m-d'));
                         // $dte = $obj->get('deadline');
                     } else {
                         // no date by default (config)
                         $dte = '';
                     }
                 }
             }
     }
     $prio = $GLOBALS['config']['task']['priority']['default'];
     // default priority
     if ($arr[3]) {
         // priority ?
         $prio = intval(substr($arr[3], 0, -1));
     }
     $obj->set('priority', $prio);
     $title = $arr[4];
     if ($def) {
         $title = $def . ' : ' . $title;
     }
     $obj->set('title', $title);
     return $obj;
 }