Manager.php 8.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
<?php
// Dobrado Content Management System
// Copyright (C) 2012 Malcolm Blaney
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

class Manager extends Base {

  public function Add($id) {

  }

  public function Callback() {
    $object = array();
    if (!$this->user->canEditPage) {
      $object["error"] = "You don't have permission to access manager.";
      return $object;
    }

    $mysqli = connect_db();
    $action = $mysqli->escape_string($_POST["action"]);

    if ($action == "list") {
      // Autocomplete users and products.
      $query = 'SELECT user FROM users';
      if ($result = $mysqli->query($query)) {
        while ($users = $result->fetch_assoc()) {
          $object["users"][] = $users["user"];
        }
        $result->close();
      }
      else {
        $this->Log('Manager->Callback: '.$mysqli->error);
      }
      $detail = new Module($this->user, $this->owner, "detail", $this->config);
      $object["details"] = $detail->Factory("AllUsers");

      $stock = new Module($this->user, $this->owner, "stock", $this->config);
      $object["products"] = $stock->Factory("AllProducts");
    }
    else if ($action == "search") {
      $username = $mysqli->escape_string($_POST["username"]);
      $timestamp = (int)$mysqli->escape_string($_POST["timestamp"]) / 1000;
      $product = $mysqli->escape_string($_POST["product"]);
      $quantity = $mysqli->escape_string($_POST["quantity"]);
      $price = $mysqli->escape_string($_POST["price"]);
      $start = (int)$mysqli->escape_string($_POST["start"]) / 1000;
      $end = (int)$mysqli->escape_string($_POST["end"]) / 1000;
      $purchase = new Module($this->user, $this->owner, "purchase",
                             $this->config);
      $object = $purchase->Factory(array("Search", $username, $timestamp,
                                         $product, $quantity, $price, $start,
                                         $end));
    }
    else if ($action == "submit") {
      $username = $mysqli->escape_string($_POST["username"]);
      $timestamp = (int)$mysqli->escape_string($_POST["timestamp"]) / 1000;
      $product = $mysqli->escape_string($_POST["product"]);
      $quantity = $mysqli->escape_string($_POST["quantity"]);
      $price = $mysqli->escape_string($_POST["price"]);
      $purchase = new Module($this->user, $this->owner, "purchase",
                             $this->config);
      $purchase->Factory(array("AddPurchase", $username, $timestamp, $product,
                               $quantity, $price, $this->user->name));
      $object["done"] = true;
    }
    else if ($action == "remove") {
      $username = $mysqli->escape_string($_POST["username"]);
      $product = $mysqli->escape_string($_POST["product"]);
      $timestamp = (int)$mysqli->escape_string($_POST["timestamp"]) / 1000;
      $purchase = new Module($this->user, $this->owner, "purchase",
                             $this->config);
      $purchase->Factory(array("RemovePurchase", $username, $timestamp,
                               $product));
      $object["done"] = true;
    }
    else if ($action == "editDetails") {
      $username = $mysqli->escape_string($_POST["username"]);
      $first = $mysqli->escape_string($_POST["first"]);
      $last = $mysqli->escape_string($_POST["last"]);
      $phone = $mysqli->escape_string($_POST["phone"]);
      $detail = new Module($this->user, $this->owner, "detail", $this->config);
      $detail->Factory(array("UpdateUser", $username, $first, $last, $phone));
      $object["done"] = true;
    }
    $mysqli->close();
    return $object;
  }

  public function CanAdd($page) {
    return true;
  }

  public function CanEdit($id) {
    return true;
  }

  public function CanRemove($id) {
    return true;
  }

  public function Content($id) {
    return '<form id="manager-form">'.
        '<div class="form-spacing">'.
          '<label for="manager-username-input">Username:</label>'.
          '<input id="manager-username-input" size="15" maxlength="50">'.
        '</div>'.
        '<div class="details"><b>Contact details:</b> '.
          '<button class="edit">edit</button><br>'.
          '<span class="fullname"></span> '.
          '<span class="email"></span>'.
        '</div>'.
        '<hr>'.
        '<div class="form-spacing">'.
          '<label for="manager-product-input">Product:</label>'.
          '<input id="manager-product-input" size="15" maxlength="100">'.
        '</div>'.
        '<div class="form-spacing">'.
          '<label for="manager-quantity-input">Quantity*:</label>'.
          '<input id="manager-quantity-input" size="6" maxlength="8">'.
        '</div>'.
        '<div class="form-spacing">'.
          '<label for="manager-price-input">Price*:</label>'.
          '<input id="manager-price-input" size="15" maxlength="50" '.
            'readonly="true">'.
        '</div>'.
        '<div class="form-spacing">'.
          '<label for="manager-date-input">Date:</label>'.
          '<input id="manager-date-input" size="15" maxlength="50">'.
        '</div>'.
        '*Price and Quantity are not currently searchable.'.
        '<hr>'.
        '<div>Search between start and end dates (leave above date blank):'.
        '</div>'.
        '<div class="form-spacing">'.
          '<label for="manager-start-date-input">Start:</label>'.
          '<input id="manager-start-date-input" size="15" maxlength="50">'.
        '</div>'.
        '<div class="form-spacing">'.
          '<label for="manager-end-date-input">End:</label>'.
          '<input id="manager-end-date-input" size="15" maxlength="50">'.
        '</div>'.
        '<button class="submit">submit</button>'.
        '<button class="search">search</button>'.
        '<button class="remove">remove</button>'.
      '</form>'.
      '<div class="search-info"></div>'.
      '<form id="manager-details-form" class="hidden">'.
        '<div class="form-spacing">'.
          '<label for="manager-details-first-input">First Name:</label>'.
          '<input id="manager-details-first-input" size="15" maxlength="50">'.
        '</div>'.
        '<div class="form-spacing">'.
          '<label for="manager-details-last-input">Last Name:</label>'.
          '<input id="manager-details-last-input" size="15" maxlength="50">'.
        '</div>'.
        '<div class="form-spacing">'.
          '<label for="manager-details-phone-input">Phone Number:</label>'.
          '<input id="manager-details-phone-input" size="15" maxlength="50">'.
        '</div><br>'.
        '<div id="manager-details-info"></div>'.
        '<button class="submit">submit</button>'.
      '</form>';
  }

  public function Copy($id, $old_owner, $old_id) {

  }

  public function Factory($fn) {
    
  }

  public function Group() {

  }

  public function IncludeScript() {
    return true;
  }

  public function Install($path) {
    // Append dobrado.purchase.js to the existing dobrado.js file.
    // Note that the module is only available when logged in.
    $this->AppendScript($path, "dobrado.manager.js", false);
    $site_style = array('"","#manager-form","background-color","#eeeeee"',
                        '"","#manager-form","border","1px solid #aaaaaa"',
                        '"","#manager-form","border-radius","2px"',
                        '"","#manager-form","padding","5px"',
                        '"","#manager-form label","width","6em"',
                        '"","#manager-details-form label","width","7.5em"',
                        '"","#manager-details-form .submit","margin-left",'.
                          '"8em"',
                        '"","#manager-form .submit","float","right"',
                        '"","#manager-form .search","float","right"',
                        '"","#manager-form .search","margin-right","10px"');
    $this->AddSiteStyle($site_style);
  }

  public function Placement() {
    return "middle";
  }

  public function Remove($id) {

  }

  public function SetContent($id, $us_content) {

  }

  public function Update() {
    // This is called when the version of the module is updated,
    // to provide a way to update or modify tables etc..
  }

  public function UpdateScript($path) {
    $this->AppendScript($path, "dobrado.manager.js", false);
  }

  // Private functions below here ////////////////////////////////////////////

}

?>