Location: PHPKode > scripts > Prioritexter > TaskForm.php
<?
class TaskForm extends HtmlFragment {

  function TaskForm($title="Enter new task", $task=NULL) {
    global $categories;

    $params[PAGE_CGI_VAR_NAME] = Http::getCgiVar(PAGE_CGI_VAR_NAME);
    $form = Indenter::indent(Http::openForm($params),"++");

    // IF we know the task id ALREADY for this task, we must prepare
    // to have it be a form for EDITING the task, not entering a new one.
    if (isset($task)) {

      // EDIT TASK:
      $form .= Indenter::indent("\n<input type=hidden name=task_id ")
	 . "value=". $task->task_id .">"
	 . Indenter::indent("\n<input type=hidden name=action ")
	 . "value=updateTask>"
	 . Indenter::indent("\n<input type=hidden name=page ")
	 . "value=". Http::getCgiVar(OLD_PAGE_CGI_VAR_NAME) .">";
    } else { // NEW TASK:
      $form .= Indenter::indent("\n<input type=hidden name=action ")
	 . "value=newTask>";
    }

    $form .= Indenter::indent("\n\n<table border=0 ","++")
       . "cellspacing=0 cellpadding=2>"
       . Indenter::indent("\n<tr>","++")
       . Indenter::indent("\n<td>name:</td>")
       . Indenter::indent("\n<td><input type=text name=name size=14");
    // IF we know the name, put it in here!
    if (isset($task))
      $form .= " value=\"". Text::htmlContentClean($task->name) ."\"";
    $form .= "></td>"
       . Indenter::indent("\n</tr>","--")
       . Indenter::indent("\n<tr>","++")
       . Indenter::indent("\n<td>priority:</td>")
       . Indenter::indent("\n<td><select name=priority>","++");
    for ($i=1; $i<=10; $i++) {
      $form .= Indenter::indent("\n<option value=$i");
      if ($i == $task->priority) $form .= " selected";
      $form .= ">$i</option>";
    }
  
    $form .= Indenter::indent("\n</select>")
       . Indenter::indent("\n</td>","--")
       . Indenter::indent("\n</tr>","--")
       . Indenter::indent("\n<tr>","++");

    // PROBLEM: how to take existing due date, and translate that
    // into setting this thing dynamically?
    // SOLUTION: forget all the following crap. if the date is already
    // set, let's use traditional date methods! this is no time to
    // be concerned with rapid entry.
    // DEPRECATED, then:
    // the following values are each about twice as big as the last.
    // so if the due date is, say, 40 days, how do we have it match up
    // with 1 month, vs. 60 days, which should be 80?
    // simply waiting until the day count is too big, then selecting the
    // previous unit, would work for 40 but not for 60.
    // but we COULD do this is we make the actual # of days bigger or
    // smaller first! since it would be nice if we could just pick the
    // first one that is bigger than the # of days, let's make it
    // smaller -- we can actually divide it by 2. eg: 8 days: 1 week
    // is first one bigger than 4. 9 months: 6 months is first one
    // bigger than 140. BUT NO-- this won't work for 300 days, 
    // or for 6 days. how about -- divide by 1.5?
    // orig #      orig/1.5        category
    // 5           3.3             1 week
    // 10          6.8             1 week
    // 20          13.3            few weeks
    // 40          26.7            1 month
    // 80          53              few months
    // 160         106             6 months
    // 320         213             1 year
    // pretty nice!
    if (isset($task)) {
      $form .= Indenter::indent("\n<td>due:</td>")
	 . Indenter::indent("\n<td><select name=due_month>","++");
      $date_at_jan_15th_of_this_year = time()-bcmod(time(),(365.25*86400))
	 + 15*86400;
      $adjusted_due = $task->due + $task->adjustment;
      for($i=0; $i<12; $i++) {
	$month_date = $date_at_jan_15th_of_this_year + $i*30.4*86400;
	$month_name = date("M", $month_date);
	$form .= Indenter::indent("\n<option value=$month_name");
	if ($month_name == date("M", $adjusted_due))
	  $form .= " selected";
	$form .= ">$month_name</option>";
      }
      $form .= Indenter::indent("\n</select>")
	 . Indenter::indent("\n<select name=due_day>");
      for($i=1; $i<=31; $i++) {
	$form .= Indenter::indent("\n<option value=$i");
	if ($i == date("j", $adjusted_due))
	  $form .= " selected";
	$form .= ">$i</option>";
      }	
      $form .= Indenter::indent("\n</select>")
	 . Indenter::indent("\n<select name=due_year>");
      for($i=0; $i<=10; $i++) {
	$year = date("Y", $date_at_jan_15th_of_this_year+$i*365.25*86400);
	$form .= Indenter::indent("\n<option value=$year");
	if ($year == date("Y", $adjusted_due))
	  $form .= " selected";
	$form .= ">". $year ."</option>";
      }	
      $form .= Indenter::indent("\n</select>")
	 . Indenter::indent("\n</td>","--");
    
    } else { // NEW MODE:
      $form .= Indenter::indent("\n<td>timeframe:</td>")
	. Indenter::indent("\n<td><select name=due>","++")
	. Indenter::indent("\n<option value=0>today</option>")
	. Indenter::indent("\n<option value=1>tomorrow</option> ")
	. Indenter::indent("\n<option value=3>a few days</option>")
	. Indenter::indent("\n<option value=7>1 week</option>")
	. Indenter::indent("\n<option value=19>a few weeks</option>")
	. Indenter::indent("\n<option value=30.44>1 month</option>")
	. Indenter::indent("\n<option value=80>a few months</option>")
	. Indenter::indent("\n<option value=182.7>6 months</option>")
	. Indenter::indent("\n<option value=365.25>1 year</option>")
	. Indenter::indent("\n<option value=900>a few years</option>")
	. Indenter::indent("\n</select>")
	. Indenter::indent("\n</td>","--");
    }

    $form .= Indenter::indent("\n</tr>","--")
       . Indenter::indent("\n<tr>","++")
       . Indenter::indent("\n<td>type:</td>")
       . Indenter::indent("\n<td><select name=type>","++")
       . Indenter::indent("\n<option value=task");
    
    if ($type == "task")
      $form .= " selected";
    $form .= ">Task</option>";
  
    $form .= Indenter::indent("\n<option value=project");
    if ($type == "project")
      $form .= " selected";
    $form .= ">Project</option>";
    
    $form .= Indenter::indent("\n<option value=dream");
    if ($type == "dream")
      $form .= " selected";
    $form .= ">Dream</option>";
    
    $form .= Indenter::indent("\n</select>")
       . Indenter::indent("\n</td>","--")
       . Indenter::indent("\n</tr>","--")
       . Indenter::indent("\n<tr>","++")
       . Indenter::indent("\n<td>category:</td>")
       . Indenter::indent("\n<td><select multiple size=4 ","++")
       . "name=\"categories[]\">";

    $first_flag = true;
    foreach($categories->categories as $category) {
      $form .= Indenter::indent("\n<option value=\"")
	 . $category ."\"";
      if (isset($task)) {
	if (array_search($category,$task->categories) !== FALSE)
	  $form .= " selected";
      } elseif ($first_flag) // if nothing else, at least
	  // select the first category.
	  $form .= " selected";	
      $form .= ">". Text::capitalizeFirstChar($category)
	 . "</option>";
      $first_flag = false;
    }

    $form .= Indenter::indent("\n</select>")
       . Indenter::indent("\n</td>","--")
       . Indenter::indent("\n</tr>","--");

    // at first i set this so it would only show the completed choice
    // if the oldpage was finished, meaning, if you are editing a completed
    // task. but that's too confusing -- user doesn't know where that
    // choice went, if editing normal task! So just keep it everywhere.
    //    $oldpage = Http::getCgiVar("oldpage");
    if (isset($task)) {
      $form .= Indenter::indent("\n<tr>","++")
	 . Indenter::indent("\n<td valign=top>completed:</td>")
	 . Indenter::indent("\n<td><select name=active>","++");

      // select the correct option according to active-ness of task
      $form .= Indenter::indent("\n<option value=1");
      if ($task->active) $form .= " selected";
      $form .= ">No</option>";
      $form .= Indenter::indent("\n<option value=0");
      if (!$task->active) $form .= " selected";
      $form .= ">Yes</option>";

      $form .= Indenter::indent("\n</select>")
	 . Indenter::indent("\n</td>","--")
	 . Indenter::indent("\n</tr>","--");
    }


    $form .= Indenter::indent("\n<tr>","++")
       . Indenter::indent("\n<td valign=top>notes:</td>")
       . Indenter::indent("\n<td><textarea name=notes rows=4 cols=11>");
    if (isset($task))
      $form .= Text::htmlContentClean($task->notes);
    $form .= "</textarea></td>"
       . Indenter::indent("\n</tr>","--")
       . Indenter::indent("\n<tr>","++")
       . Indenter::indent("\n<td></td>")
       . Indenter::indent("\n<td><input type=submit value=Submit>","++")
       . "</input></td>"
       . Indenter::indent("\n</tr>","--")
       . Indenter::indent("\n</table>","--")
       . Indenter::indent("\n</form>","--");

    $task_form = new TextBox($title,$form, 175);
    $task_form->box();
    // NOTE: once textbox is concat-versioned, replace display with
    // line making this->html - box->tostr
    $task_form->display();
  }

  function getCgiValues() {
    $values["name"] = Http::getCgiVar("name");
    $values["priority"] = Http::getCgiVar("priority");
    $values["type"] = Http::getCgiVar("type");
    $values["notes"] = Http::getCgiVar("notes");
    $active = Http::getCgiVar("active");
    if ($active !== FALSE)
      $values["active"] = $active;

    // make sure when task entered, the create time is set to whatever 
    // 4pm was on the current day, and due is added to that, not just 
    // added outright. 
    $values["created"] = Date::logicalTime(time());

    $values["due"] = Date::logicalTime($values["created"]+ 
       Http::getCgiVar("due") * 86400);

    // as for other values...
    // active defaults to 1, finished defaults to null...
    // user wouldn't want to set these to anything on creating new task,
    // so no need to set these.

    return $values;
  }
}
?>
Return current item: Prioritexter