Split hub roles.
[readifood.git] / lib / hub.php
index 48b45ae..a3a1121 100644 (file)
     exit;
   }
 
+  function show_hub_summary(&$hub, $editing = false) {
+    if ($editing) echo "<p>Hub: <span class=\"strong\">" . htmlspecialchars($hub->getName()) . "</span>";
+    else echo "<br>\nHub " . $hub->getStrongLink();
+    $role = $hub->getRole();
+    $role_string = get_hub_role_string($hub);
+    if ($role_string) echo " $role_string";
+    if ($role & $GLOBALS['ROLE_COLLECTION']) printf(" <a class=\"small\" href=\"/donation/to/hub/%s/%d\">Donations</a>", urlencode($hub->getName()), $hub->getId());
+    if ($role & $GLOBALS['ROLE_DISTRIBUTION']) printf(" <a class=\"small\" href=\"/order/to/hub/%s/%d\">Orders</a>", urlencode($hub->getName()), $hub->getId());
+    if (check_admin(1)) {
+      echo " " . $hub->getDeleteLink();
+    }
+    $area = get_hub_area($hub);
+    if ($area) echo " in " . $area->getLink();
+    $city = get_hub_city($hub);
+    if ($city) echo ", " . $city->getLink(get_city_displayname($city));
+  }
+
   function show_hubs($offset, $per_page, $address_ids) {
     $q = new HubQuery;
     $hubs = $q->filterByAddressId($address_ids)->find();
     if (count($hubs)) {
-      foreach ($hubs as $hub) {
-        echo "<br>\nhub " . $hub->getLink();
-        if (check_admin(1)) {
-          echo " " . $hub->getDeleteLink();
-        }
-        $area = get_hub_area($hub);
-        echo " in " . $area->getLink();
-      }
+      foreach ($hubs as $hub) show_hub_summary($hub);
     }
     else echo " none";
   }
@@ -70,7 +80,7 @@
       $address_ids = array();
       foreach ($addresses as $address) $address_ids[] = $address->getId();
 
-      echo "<p>hubs in city " . $city->getLink(get_city_displayname($city)) . ":";
+      echo "<p>Hubs in city " . $city->getLink(get_city_displayname($city)) . ":";
       return show_hubs($offset, $per_page, $address_ids);
     }
     else echo "<p>No such city!</p>\n";
     show_hub_cities_form($city_id);
   }
 
+  function show_hub_role_form($role) {
+    return show_role_form($role, $GLOBALS['hub_roles']);
+  }
+
   function show_hub_form($hub = null, $area_id = null) {
     if (! $hub) $hub = new Hub;
 
+    /* Role. */
+    echo "<tr>\n";
+    echo "  <td>Role</td>\n";
+    echo "  <td>"; show_hub_role_form($hub->getRole()); echo "</td>\n";
+    echo "</tr>\n";
+
     /* Display name. */
     echo "<tr>\n";
     echo "  <td>Hub name</td>\n";
   }
 
   function update_hub(&$hub, $area_id, $new = false) {
+    global $hub_roles;
+
+    $role = 0;
+    for ($i = 0; $i < count($hub_roles); $i++) {
+      if ($_POST['role_' . $i] == "on") $role |= (1 << $i);
+    }
+
     $displayname = $_POST['displayname'];
 
     if (! $displayname) {
     $telephone2 = $_POST['telephone2'];
     $email = $_POST['email'];
 
+    $hub->setRole($role);
     $hub->setDisplayname($displayname);
     $hub->setTelephone1($telephone1);
     $hub->setTelephone2($telephone2);
     if (! $hub) return;
 
     echo "<form method=\"POST\" action=\"" . $_SERVER['REQUEST_URI'] . "\">\n";
-    echo "<p>Hub: <span class=\"strong\">" . $hub->getDisplayname() . "</span>";
-    if (check_admin(1)) {
-      echo " " . $hub->getDeleteLink();
-    }
-    $city = get_hub_city($hub);
-    if ($city) echo " in " . $city->getLink(get_city_displayname($city));
+    show_hub_summary($hub, true);
     echo ": ";
     echo "\n</p>";