Fixed pagination of orders.
[readifood.git] / lib / order.php
index 69026bf..622ad18 100644 (file)
@@ -1,10 +1,12 @@
 <?php
 
   if (isset($_POST['show_add_order'])) {
+    set_last_selected("area_id", $_POST['area_id']);
     $area_id = $_POST['area_id'];
     show_new_order_form($area_id);
   }
   else if (isset($_POST['add_order'])) {
+    set_last_selected("area_id", $_POST['area_id']);
     $id = add_order();
     if ($id !== false) {
       echo "<p>Order placed.</p>\n";
     }
   }
   else if ($_POST['show_in_area']) {
+    set_last_selected("area_id", $_POST['area_id']);
     $q = new AreaQuery;
     $area = $q->findOneById($_POST['area_id']);
-    header(sprintf("Location: http%s://%s/%s/in/area/%s/%d%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id'], get_order_state_uri(get_order_state_mask())));
+    header(sprintf("Location: http%s://%s/%s/in/area/%s/%d%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($area->getName()), $_POST['area_id'], get_order_state_query_uri(get_order_state_mask())));
     exit;
   }
   else if ($_POST['show_in_city']) {
+    set_last_selected("city_id", $_POST['city_id']);
     $q = new CityQuery;
     $city = $q->findOneById($_POST['city_id']);
-    header(sprintf("Location: http%s://%s/%s/in/city/%s/%d%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id'], get_order_state_uri(get_order_state_mask())));
+    header(sprintf("Location: http%s://%s/%s/in/city/%s/%d%s", ($_SERVER['HTTPS']) ? "s" : "", $_SERVER['HTTP_HOST'], $module, urlencode($city->getName()), $_POST['city_id'], get_order_state_query_uri(get_order_state_mask())));
     exit;
   }
 
   function show_orders($offset, $per_page, $requester_ids = null, $beneficiary_ids = null, $state_mask = null) {
+    list($offset, $per_page) = pagination();
     /* XXX: Use Propel methods. */
     if (isset($state_mask)) $order_ids = get_order_ids_by_state($state_mask);
     $q = new OrderQuery;
     # XXX: Doesn't work.
     #if (isset($state_mask)) $q->useOrderStateQuery()->addSelectQuery($latest_state, 'latestState')->where("order_id=latestState.order_id")->where("state & $state_mask")->endUse();
     if (isset($state_mask)) $q->filterById($order_ids);
-    $orders = $q->orderByDate()->find();
+    $orders = $q->orderByDate('desc')->orderById('desc')->paginate($offset, $per_page);
     if (count($orders)) {
       foreach ($orders as $order) {
-        echo "<br>\nOrder " . $order->getStrongLink($order->getId()) . ": " . get_order_displayname($order);
-        if (check_admin(1)) {
-          echo " " . $order->getDeleteLink();
-        }
-
-        /* XXX: Should pull from query. */
-        $q = new ContactQuery;
-        $contact = $q->findOneById($order->getBeneficiaryId());
-        if ($contact) {
-          echo " for " . $contact->getLink();
-          $area = get_contact_area($contact);
-          if ($area) echo " in " . $area->getLink();
-        }
-
-        if ($order->getHubId()) {
-          $q = new HubQuery;
-          $hub = $q->findOneById($order->getHubId());
-          if ($hub) echo " to hub " . $hub->getLink();
-          $area = get_hub_area($hub);
-          if ($area) echo " in " . $area->getLink();
-        }
+        echo "<br>\n" . get_order_summary($order) . "<br>\n";
       }
+      show_pagination($orders);
     }
     else echo " none";
   }
 
     echo "<p>Restrict to order states:\n";
     for ($i = 0; $i < count($states); $i++) {
-      echo " <input type=\"checkbox\" name=\"state_$i\"";
+      echo " <input type=\"checkbox\" id=\"state_$i\" name=\"state_$i\"";
       if ($state_mask & (1 << $i)) echo " checked";
-      echo ">$states[$i]\n";
+      echo "><label for=\"state_$i\">$states[$i]</label>\n";
     }
     echo "</p>\n";
   }
     return $mask;
   }
 
-  function get_order_state_string($mask) {
+  function get_order_state_query_string($mask) {
     global $states;
 
     $selected = array();
     return implode("+", $selected);
   }
 
-  function get_order_state_uri($mask) {
+  function get_order_state_query_uri($mask) {
     global $all_states;
 
     if (is_null($mask)) return "";
     if ($mask == $all_states) return "";
 
-    return "/state/" . get_order_state_string($mask);
+    return "/state/" . get_order_state_query_string($mask);
   }
 
   function show_order_areas_form($city_id = null) {
       return;
     }
 
+    $candidates = array();
+    foreach ($areas as $area) {
+      if (! count(get_area_contacts($area->getId()))) continue;
+      $candidates[] = $area;
+    }
+    if (! count($candidates)) return;
+
     echo "<p>Show orders in area\n";
     echo "<select name=\"area_id\">\n";
-    foreach ($areas as $area) {
+    foreach ($candidates as $area) {
       option("area_id", $area->getId(), get_area_displayname($area));
     }
     echo "</select>\n";
       return;
     }
 
+    $candidates = array();
+    foreach ($cities as $city) {
+      if (! count(get_city_contacts($city->getId()))) continue;
+      $candidates[] = $city;
+    }
+    if (! count($candidates)) return;
+
     echo "<p>Show orders in city\n";
     echo "<select name=\"city_id\">\n";
-    foreach ($cities as $city) {
+    foreach ($candidates as $city) {
       option("city_id", $city->getId(), get_city_displayname($city), $city_id);
     }
     echo "</select>\n";
   }
 
   function show_order_forms($city_id, $state_mask) {
-    echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
+    form("noprint standout");
     show_order_state_form($state_mask);
     show_order_areas_form($city_id);
     show_order_cities_form($city_id);
-    echo "</form>\n";
+    end_form();
   }
 
   function show_order_form($order = null, $area_id = null) {
     global $states, $parcel_sizes, $parcel_contents;
 
     if ($order) {
-      $q = new OrderStateQuery;
-      $order_state = $q->filterByOrderId($order->getId())->orderByUpdated('desc')->limit(1)->findOne();
+      $order_state = get_order_state($order);
       if ($order_state) {
         $state = $order_state->getState();
         $driver_id = $order_state->getDriverId();
     }
     else $order = new Order;
 
-
     /* Date. */
     echo "<tr>\n";
     echo "  <td>Delivery</td>\n";
     foreach ($contacts as $contact) {
       option("requester_id", $contact->getId(), $contact->getDisplayname(), $order->getRequesterId());
     }
-    echo "</select></td>\n";
+    echo "</select>";
+    $contact = get_contact_by_id($order->getRequesterId(), false);
+    if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL());
+    echo "</td>\n";
     echo "</tr>\n";
 
     /* Beneficiary. */
         option("beneficiary_id", $contact->getId(), $contact->getDisplayname(), $order->getBeneficiaryId());
       }
     }
-    echo "</select></td>\n";
+    echo "</select>";
+    $contact = get_contact_by_id($order->getBeneficiaryId(), false);
+    if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL());
+    echo "</td>\n";
     echo "</tr>\n";
 
     /* Hub. */
     foreach ($hubs as $hub) {
       option("hub_id", $hub->getId(), $hub->getDisplayname(), $order->getHubId());
     }
-    echo "</select></td>\n";
+    echo "</select>";
+    $hub = get_hub_by_id($order->getHubId(), false);
+    if ($hub) echo " " . get_small_link($hub->getDisplayname(), $hub->getURL());
+    echo "</td>\n";
     echo "</tr>\n";
 
     /* Parcel type. */
     echo "  <td>Parcel contents</td>\n";
     echo "  <td>";
     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
-      echo "  <input type=\"checkbox\" name=\"parcel_$i\"";
+      echo "  <input type=\"checkbox\" id=\"parcel_$i\" name=\"parcel_$i\"";
       if ($order->getParcel() & (1 << $i)) echo " checked";
-      echo ">$parcel_contents[$i]\n";
+      echo "><label for=\"parcel_$i\">$parcel_contents[$i]</label>\n";
     }
     echo "</td>\n";
     echo "</tr>\n";
 
+    /* Notes. */
+    echo "<tr>\n";
+    echo "  <td>Notes</td>\n";
+    echo "  <td>"; textarea("notes", $order->getNotes()); echo "</td>\n";
+    echo "</tr>\n";
+
     /* Driver. */
     echo "<tr>\n";
     echo "  <td>Driver</td>\n";
       foreach ($contacts as $contact) {
         option("driver_id", $contact->getId(), $contact->getDisplayname(), $driver_id);
       }
-      echo "</select></td>\n";
+      echo "</select>";
+      $contact = get_contact_by_id($driver_id, false);
+      if ($contact) echo " " . get_small_link($contact->getDisplayname(), $contact->getURL());
+      echo "</td>\n";
     }
     else echo "  <td>No drivers!</td>\n";
     echo "</tr>\n";
       echo "  <td>State</td>\n";
       echo "  <td><select name=\"state\">\n";
       for ($i = 0; $i < count($states); $i++) {
-        option("state", $i << 1, ucfirst($states[$i]), $state);
+        option("state", 1 << $i, ucfirst($states[$i]), $state);
       }
       echo "</select></td>\n";
       echo "</tr>\n";
       return;
     }
 
-    echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
+    form("noprint");
     echo "<p>Place an order:</p>\n";
 
     echo "<table>\n";
     echo "  <td colspan=2>"; submit("add_order", "Order"); echo "</td></tr>\n";
     echo "</tr>\n";
     echo "</table>\n";
-    echo "</form>\n";
+    end_form();
   }
 
   function show_contact_order_form($contact) {
     $order = new Order;
     $order->setBeneficiaryId($contact->getId());
 
-    echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
+    form("standout");
     echo "<p>Placing order for " . $contact->getStrongLink() . ".";
     $parcel = $contact->getParcel();
     if ($parcel) {
     echo "  <td colspan=2>"; submit("add_order", "Order"); echo "</td></tr>\n";
     echo "</tr>\n";
     echo "</table>\n";
-    echo "</form>\n";
+    end_form();
   }
 
   function show_add_new_order_form() {
       return;
     }
 
-    echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
+    form("noprint standout");
     echo "<p>Place an order in <select name=\"area_id\">\n";
     foreach ($areas as $area) {
       option("area_id", $area->getId(), get_area_displayname($area));
     echo "</select>";
     submit("show_add_order", "Proceed");
     echo "</p>\n";
-    echo "</form>\n";
+    end_form();
   }
 
   function update_order(&$order, $new = false) {
     for ($i = count($parcel_sizes); $i < count($parcel_contents); $i++) {
       if ($_POST['parcel_' . $i] == "on") $parcel |= (1 << $i);
     }
+    $notes = $_POST['notes'];
 
     if ($date) {
       list($y, $m, $d) = explode('-', $date);
       $order->setBeneficiaryId($beneficiary_id);
       $order->setHubId($hub_id);
       $order->setParcel($parcel);
+      $order->setNotes($notes);
 
       /* XXX: begin/commit */
       try {
     return $order->getId();
   }
 
+  function confirm_delete_order($id = null) {
+    if (! check_admin(1, "delete an order")) return;
+
+    if (isset($id)) $order = get_order_by_id($id);
+    if (! $order) return false;
+
+    echo "<h3>Confirm deletion</h3>\n";
+    echo "<p>You must confirm deletion of order $id: " . $order->getDeleteLink(true) . "</p>\n";
+  }
+
   function delete_order($id = null) {
     if (! check_admin(1, "delete an order")) return;
 
     if (! count($order_states)) return;
 
     echo "<h3>Order history</h3>\n";
+    echo "<p class=\"history\">\n";
     foreach ($order_states as $order_state) {
       $date = $order_state->getUpdated();
 
       if ($driver_id) $driver = get_contact_by_id($driver_id);
       else $driver = null;
 
-      /* XXX */
-      $state = $order_state->getState();
-      for ($i = 0; $i < count($states); $i++) {
-        if ((1 << $i) == $state) {
-          $state = $states[$i];
-          break;
-        }
-      }
-      #$state = $states[$order_state->getState()];
-      echo "<p><strong>$username</strong> changed order to state <strong>$state</strong>";
+      echo "<strong>$username</strong> changed order to state <strong>" . get_order_state_string($order_state) . "</strong>";
       if ($driver) echo " for driver " . $driver->getDisplayname();
-      echo " on $date.</p>\n";
+      echo " on $date.<br>\n";
     }
+    echo "</p>\n";
   }
 
   function show_order(&$id = null) {
     if (isset($id)) $order = get_order_by_id($id);
     if (! $order) return;
 
-    echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
+    form();
     echo "<p>Order: <span class=\"strong\">" . $order->getId() . "</span>";
     if (check_admin(1)) {
       echo " " . $order->getDeleteLink();
     }
 
     echo "</table>\n";
-    echo "</form>\n";
+    end_form();
 
     show_order_history($order->getId());
   }
   if (count($args)) {
     switch ($args[0]) {
       case "delete":
+        confirm_delete_order($id);
+      break;
+
+      case "confirmdelete":
         delete_order($id);
       break;
     }
   }
   else if (isset($id)) show_order($id);
   else if ($state_mask) show_orders(0, 10, null, null, $state_mask);
-  else {
-    /* XXX: Shown after adding. */
-    show_order_forms($city_id, $state_mask);
-    show_add_new_order_form($city_id);
-  }
 
-  if (count($parameters)) {
-    show_order_forms($city_id, $state_mask);
-  }
+  show_order_forms($city_id, $state_mask);
+  show_add_new_order_form($city_id);
 
 
 ?>