Advertisement
Strahan

Application specific classes

Jul 24th, 2022
1,434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 26.09 KB | None | 0 0
  1. //*******************************************************************************************************************************************
  2. //
  3. // Application specific permissions
  4. //
  5. //*******************************************************************************************************************************************
  6. class SitePerms extends Perm {
  7.   const ManageOwnAccount = 64;
  8.   const Roles = 128;
  9.   const RoleAdd = 256;
  10.   const RoleEdit = 512;
  11.   const RoleDelete = 1024;
  12.   const Users = 2048;
  13.   const UserAdd = 4096;
  14.   const UserEdit = 8192;
  15.   const UserDelete = 16384;
  16.   const Groups = 32768;
  17.   const GroupAdd = 65536;
  18.   const GroupEdit = 131072;
  19.   const GroupEditAny = 262144;
  20.   const GroupDelete = 524288;
  21.   const GroupDeleteAny = 1048576;
  22.   const Fields = 2097152;
  23.   const FieldAdd = 4194304;
  24.   const FieldEdit = 8388608;
  25.   const FieldEditAny = 16777216;
  26.   const FieldDelete = 33554432;
  27.   const FieldDeleteAny = 67108864;
  28.   const Entries = 134217728;
  29.   const EntryAdd = 268435456;
  30.   const EntryEdit = 536870912;
  31.   const EntryEditAny = 1073741824;
  32.   const EntryDelete = 2147483648;
  33.   const EntryDeleteAny = 4294967296;
  34. }
  35.  
  36.  
  37. //*******************************************************************************************************************************************
  38. //
  39. // Flags for various use around the site
  40. //
  41. //*******************************************************************************************************************************************
  42. class SiteFlags {
  43.   const TextFormat_Single = 1;
  44.   const TextFormat_Textarea = 2;
  45.   const TextFormat_RichText = 4;
  46.   const TextFormat_ProtectedSingle = 8;
  47. }
  48.  
  49.  
  50. //*******************************************************************************************************************************************
  51. //
  52. // Holds site configuration
  53. //
  54. //*******************************************************************************************************************************************
  55. class SiteConfig {
  56.   public $cfgid = 0;
  57.   public $rights_anon = 0;
  58.   public $rights_auth = 0;
  59.   public $pacl_group = "";
  60.   public $pacl_entry = "";
  61.  
  62.   function __construct() {
  63.     global $pdo;
  64.    
  65.     $data = getData($pdo, "config");
  66.     $this->cfgid = array_key_first($data);
  67.     foreach ($data[$this->cfgid] AS $f=>$v) $this->$f = $v;
  68.   }
  69.  
  70.   function flush() {
  71.     global $pdo;
  72.    
  73.     $sqldata = [];
  74.     foreach (["cfgid","rights_anon","rights_auth","pacl_group","pacl_entry"] AS $f) $sqldata[$f] = $this->$f;
  75.     setData($pdo, "config", $sqldata);
  76.   }
  77. }
  78.  
  79.  
  80. //*******************************************************************************************************************************************
  81. //
  82. // Fields used in the entries
  83. //
  84. //*******************************************************************************************************************************************
  85. class Field {
  86.   public $id = 0;
  87.   public $name = "";
  88.   public $dispname = "";
  89.   public $placeholder = "";
  90.   public $flags = 0;
  91.   public $rows = 0;
  92.   public $width = 0;
  93.   public $columns = 0;
  94.   public $datecreated = 0;
  95.   public $style = "";
  96.   public $class = "";
  97.   public $createdby = 0;
  98.   public $content = "";
  99.   public $disporder = 0;
  100.   public $nameoverride = "";
  101.  
  102.   function __construct($fid = "", $id = "") {
  103.     if (empty($fid)) return;
  104.     if (is_array($fid)) {
  105.       $this->populate(["data"=>$fid]);
  106.       return;
  107.     }
  108.    
  109.     $this->id = $fid;
  110.     $this->populate(["fid"=>$fid, "id"=>$id]);
  111.   }
  112.  
  113.   function getDateCreated() {
  114.     return date("m/d/Y @ g:i A", $this->datecreated);
  115.   }
  116.      
  117.   function populate($opts = []) {
  118.     global $pdo;
  119.  
  120.     if (array_key_exists("data", $opts) && count($opts["data"]) > 0) {
  121.       foreach (["fid","fname","dispname","flags","datecreated","createdby","rows","columns","style","class","width","placeholder"] AS $f) {
  122.         if (array_key_exists($f, $opts["data"])) continue;
  123.         errorMsg("Attempted to load field object but data fields are missing!  ($f)", false);
  124.         return;
  125.       }
  126.      
  127.       $this->id = $opts["data"]["fid"];
  128.       $this->name = $opts["data"]["fname"];
  129.       foreach (["dispname","style","class","placeholder"] AS $f) empty($opts["data"][$f]) ? "" : $opts["data"][$f];
  130.       foreach (["flags","rows","columns","width","createdby"] AS $f) $this->$f = getInt($opts["data"][$f]);
  131.       $this->datecreated = is_numeric($opts["data"]["datecreated"]) ? $opts["data"]["datecreated"] : strtotime($opts["data"]["datecreated"]);
  132.       if (array_key_exists("id", $opts["data"])) $this->populateData($opts["data"]["id"]);
  133.       return;
  134.     }
  135.    
  136.     $data = getData($pdo, "fieldspec", ["criteria"=>["fid = ?"=>$this->id], "onlyone"=>true]);
  137.     if (count($data) == 0) return;
  138.    
  139.     $this->name = $data["fname"];
  140.     foreach (["dispname","style","class","placeholder"] AS $f) $this->$f = empty($data[$f]) ? "" : $data[$f];
  141.     foreach (["flags","rows","columns","width","createdby"] AS $f) $this->$f = getInt($data[$f]);
  142.     $this->datecreated = strtotime($data["datecreated"]);
  143.     if (array_key_exists("id", $opts) && !empty($opts["id"])) $this->populateData($opts["id"]);
  144.   }
  145.  
  146.   function populateData($id) {
  147.     global $pdo;
  148.     if (empty($id)) return;
  149.    
  150.     $data = getData($pdo, "entries_fields", ["criteria"=>["id = ?"=>$id, "fid = ?"=>$this->id], "onlyone"=>true]);
  151.     if (count($data) == 0) return;
  152.    
  153.     $this->content = $data["datavalue"];
  154.   }
  155.  
  156.   function flush() {
  157.     global $pdo;
  158.    
  159.     $sqldata = [
  160.       "fname"=>$this->name,
  161.       "dispname"=>$this->dispname,
  162.       "placeholder"=>$this->placeholder,
  163.       "flags"=>$this->flags,
  164.       "datecreated"=>sqlDate(),
  165.       "createdby"=>$_SESSION["user"]->uid,
  166.       "rows"=>sqlIntSafe($this->rows),
  167.       "columns"=>sqlIntSafe($this->columns),
  168.       "width"=>sqlIntSafe($this->width),
  169.       "style"=>$this->style,
  170.       "class"=>$this->class
  171.     ];
  172.    
  173.     if (empty($this->id)) {
  174.       $tmp = getData($pdo, "fieldspec", ["criteria"=>["fname = ?"=>$this->name]]);
  175.       if (count($tmp) > 0) {
  176.         errorMsg("Could not save!  That field name already exists!", false);
  177.         return;
  178.       }
  179.      
  180.       $this->id = setData($pdo, "fieldspec", $sqldata);
  181.       return;
  182.     }
  183.    
  184.     $sqldata["fid"] = $this->id;
  185.     setData($pdo, "fieldspec", $sqldata);
  186.   }
  187.  
  188.   function getInputType() {
  189.     if ($this->flags & SiteFlags::TextFormat_Single) return "single";
  190.     if ($this->flags & SiteFlags::TextFormat_ProtectedSingle) return "psingle";
  191.     if ($this->flags & SiteFlags::TextFormat_Textarea) return "textarea";
  192.     if ($this->flags & SiteFlags::TextFormat_RichText) return "richtext";
  193.     return "";
  194.   }
  195.  
  196.   function getInputField() {
  197.     $ret = "";
  198.     $fieldid = empty($this->name) ? mt_rand(1000, 100000) : str_replace(" ", "", $this->name);
  199.     $name = "name=\"$fieldid\" id=\"$fieldid\" ";
  200.     $style = empty($this->style) ? "" : "style=\"{$this->style}\" ";
  201.     $width = empty($this->width) ? "" : "size=\"{$this->width}\" ";
  202.     $class = empty($this->class) ? "" : "class=\"{$this->class}\" ";
  203.    
  204.     if ($this->flags & SiteFlags::TextFormat_Single) return ["id"=>$fieldid, "html"=>"<input type=\"text\" autocomplete=\"off\" $name $width $style $class value=\"{$this->value}\">"];
  205.     if ($this->flags & SiteFlags::TextFormat_ProtectedSingle) return ["id"=>$fieldid, "html"=>"<input type=\"password\" autocomplete=\"off\" $name $width $style $class value=\"{$this->value}\">"];
  206.     if ($this->flags & SiteFlags::TextFormat_Textarea) return ["id"=>$fieldid, "html"=>"<textarea rows=\"{$this->rows}\" cols=\"{$this->columns}\" $name $style $class>{$this->value}</textarea>"];
  207.     if ($this->flags & SiteFlags::TextFormat_RichText) {
  208.     }
  209.   }
  210.  
  211.   function getName() {
  212.     return empty($this->nameoverride) ? $this->name : $this->nameoverride;
  213.   }
  214. }
  215.  
  216.  
  217. //*******************************************************************************************************************************************
  218. //
  219. // Fields used in entry groups
  220. //
  221. //*******************************************************************************************************************************************
  222. class EntryGroupField extends Field {
  223.   public $gfid = 0;
  224.   public $nameoverride = "";
  225.   public $disporder = 0;
  226.  
  227.   function __construct($val = null, $nameoverride = "", $disporder = 0) {
  228.     if ($val == null) return;
  229.    
  230.     if ($val instanceof Field) {
  231.       $this->id = $val->id;
  232.       $this->name = $val->name;
  233.       $this->flags = $val->flags;
  234.       $this->datecreated = $val->datecreated;
  235.       $this->createdby = $val->createdby;
  236.       $this->nameoverride = $nameoverride;
  237.       $this->disporder = $disporder;
  238.     }
  239.     if (is_array($val)) {
  240.       if (!array_key_exists("fid", $val)) return;
  241.      
  242.       $this->id = $val["fid"];
  243.       if (array_key_exists("fname", $val)) $this->name = $val["fname"];
  244.       foreach (["dispname","flags","datecreated","createdby","nameoverride","disporder"] AS $f) {
  245.         if (!array_key_exists($f, $val)) continue;
  246.        
  247.         $this->$f = $val[$f];
  248.       }        
  249.     }
  250.   }
  251. }
  252.  
  253.  
  254. //*******************************************************************************************************************************************
  255. //
  256. // Entry groups
  257. //
  258. //*******************************************************************************************************************************************
  259. class Group {
  260.   public $id = 0;
  261.   public $name = "";
  262.   public $flags = 0;
  263.   public $datecreated = 0;
  264.   public $createdby = 0;
  265.   public $disporder = 0;
  266.   public $acl = null;
  267.   public $entries = [];
  268.   public $fields = [];
  269.  
  270.   function __construct($id = "") {
  271.     $acl = new ACL();
  272.     if (empty($id)) return;
  273.    
  274.     if (is_array($id)) {
  275.       $this->populate($id);
  276.     } else if (is_numeric($id)) {
  277.       $this->id = $id;
  278.     } else {
  279.       $this->name = $id;
  280.     }
  281.     $this->populate();
  282.   }
  283.  
  284.   function loadEntries($user = null) {
  285.     global $pdo;
  286.    
  287.     $sql = $pdo->prepare("SELECT * FROM ");
  288.   }
  289.  
  290.   function getDateCreated() {
  291.     return date("m/d/Y @ g:i A", $this->datecreated);
  292.   }
  293.  
  294.   function getCreatedBy() {
  295.     $system = new User();
  296.     $system->uid = 0;
  297.     $system->username = "System Account";
  298.  
  299.     if ($this->createdby == 0) return $system;
  300.    
  301.     $tmp = getUserObject(["uid"=>$this->createdby]);
  302.     return $tmp == null ? $system : $tmp;      
  303.   }
  304.  
  305.   function populate($row = []) {
  306.     global $pdo;
  307.    
  308.     if (count($row) == 0) {
  309.       if (!empty($this->id)) {
  310.         $sql = $pdo->prepare("SELECT * FROM entrygroups WHERE gid = ?");
  311.         $sql->execute([$this->id]);
  312.         $row = $sql->fetch(PDO::FETCH_ASSOC);
  313.       } else if (!empty($this->name)) {
  314.         $sql = $pdo->prepare("SELECT * FROM entrygroups WHERE gname = ?");
  315.         $sql->execute([$this->name]);
  316.         $row = $sql->fetch(PDO::FETCH_ASSOC);
  317.       } else {
  318.         return;
  319.       }
  320.     } else {
  321.       foreach (["gid","gname","flags","createdby","disporder","datecreated","acl"] AS $f) {
  322.         if (!array_key_exists($f, $row)) errorMsg("Cannot populate group; array is missing field(s)!  ($f)");
  323.       }
  324.     }
  325.     if (!$row) return;
  326.  
  327.     $this->id = $row["gid"];      
  328.     $this->name = $row["gname"];
  329.     $this->flags = $row["flags"];
  330.     $this->createdby = $row["createdby"];
  331.     $this->disporder = $row["disporder"];
  332.     $this->datecreated = strtotime($row["datecreated"]);
  333.     $this->acl = empty($row["acl"]) ? new ACL() : unserialize($row["acl"]);
  334.    
  335.     $sql = $pdo->prepare("SELECT * FROM entrygroups_fields ef LEFT OUTER JOIN fieldspec f ON ef.fid = f.fid WHERE gid = ? ORDER BY disporder");
  336.     $sql->execute([$this->id]);
  337.     $rows = $sql->fetchAll();
  338.     foreach ($rows AS $row) {
  339.       $field = new EntryGroupField($row);
  340.       $this->fields[$row["gfid"]] = $field;
  341.     }
  342.   }
  343.  
  344.   function flush() {
  345.     global $pdo;
  346.    
  347.     if (empty($this->id)) {
  348.       if (empty($this->name)) return;
  349.      
  350.       $tmp = new Group($this->name);
  351.       if (!empty($tmp->id)) {
  352.         showError("Could not save group; one already exists with the name {$this->name}!", false);
  353.         return;
  354.       }
  355.      
  356.       $sql = $pdo->prepare("INSERT INTO entrygroups (gname, flags, datecreated, createdby, acl, disporder) VALUES (?, ?, NOW(), ?, ?, ?)");
  357.       $sql->execute([$this->name, $this->flags, $_SESSION["user"]->uid, sqlObject($this->acl), $this->disporder]);
  358.       $this->id = lastID($pdo, "entrygroups");
  359.       $this->dumpFields();
  360.       return;
  361.     }
  362.    
  363.     $sql = $pdo->prepare("UPDATE entrygroups SET gname = ?, flags = ?, acl = ?, disporder = ? WHERE gid = ?");
  364.     $sql->execute([$this->name, $this->flags, sqlObject($this->acl), $this->disporder, $this->id]);
  365.     $this->dumpFields();
  366.   }
  367.  
  368.   function dumpFields() {
  369.     global $pdo;
  370.  
  371.     $sql = $pdo->prepare("DELETE FROM entrygroups_fields WHERE gid = ?");
  372.     $sql->execute([$this->id]);
  373.  
  374.     foreach ($this->fields AS $field) {
  375.       $sql = $pdo->prepare("INSERT INTO entrygroups_fields (gid, fid, disporder, nameoverride) VALUES (?, ?, ?, ?)");
  376.       $sql->execute([$this->id, $field->id, $field->disporder, $field->nameoverride]);
  377.     }
  378.   }
  379.  
  380.   function delete() {
  381.     global $pdo;
  382.    
  383.     $sql = $pdo->prepare("SELECT COUNT(*) AS tot FROM entries WHERE gid = ?");
  384.     $sql->execute([$this->id]); $row = $sql->fetch(PDO::FETCH_ASSOC);
  385.     if (!empty($row["tot"])) return false;
  386.    
  387.     $sql = $pdo->prepare("DELETE FROM entrygroups WHERE gid = ?");
  388.     $sql->execute([$this->id]);
  389.     $sql = $pdo->prepare("DELETE FROM entrygroups_fields WHERE gid = ?");
  390.     $sql->execute([$this->id]);
  391.     return true;
  392.   }
  393.  
  394.   function hasPermission($user = null, $perm = SitePerms::Entries) {
  395.     if ($user == null) $user = getLoggedInUser();
  396.  
  397.     if (empty($this->acl)) return isLoggedIn() ? $this->createdby == getLoggedInUser()->id : false;
  398.     return $this->acl->hasPermission($user, SitePerms::Entries);
  399.   }
  400.  
  401. }
  402.  
  403.  
  404. //*******************************************************************************************************************************************
  405. //
  406. // Entries
  407. //
  408. //*******************************************************************************************************************************************
  409. class Entry {
  410.   public $id = 0;
  411.   public $gid = 0;
  412.   public $name = "";
  413.   public $flags = 0;
  414.   public $content = "";
  415.   public $datecreated = 0;
  416.   public $createdby = 0;
  417.   public $acl = null;
  418.   public $group = null;
  419.   public $fields = [];
  420.  
  421.   function __construct($id = null, $gid = 0) {
  422.     $this->id = $id;
  423.     $this->gid = $gid;
  424.     $this->acl = new ACL();
  425.     $this->group = new Group();
  426.     $this->populate();
  427.   }
  428.  
  429.   function populate() {
  430.     global $pdo;
  431.     if (empty($this->id)) {
  432.       if (empty($this->gid)) return;
  433.      
  434.       $this->group = new Group($this->gid);
  435.       $sql = $pdo->prepare("SELECT * FROM entrygroups_fields WHERE gid = ?");
  436.       $sql->execute([$this->gid]);
  437.       $rows = $sql->fetchAll();
  438.       foreach ($rows AS $row) {
  439.         $field = new Field($row["fid"]);
  440.         $field->disporder = $row["disporder"];
  441.         $field->nameoverride = $row["nameoverride"];
  442.         $this->fields[] = $field;
  443.       }
  444.       $this->datecreated = time();
  445.       $this->createdby = $_SESSION["user"]->uid;
  446.       return;
  447.     }
  448.  
  449.     $sqlstr  = "SELECT e.id,gid,ename,e.flags AS flags,datecreated,createdby,acl,fid,datavalue, ";
  450.     $sqlstr .= "(SELECT nameoverride FROM entrygroups_fields egf WHERE egf.fid = ef.fid AND egf.gid = e.gid) AS nameoverride, ";
  451.     $sqlstr .= "(SELECT disporder FROM entrygroups_fields egf WHERE egf.fid = ef.fid AND egf.gid = e.gid) AS disporder ";
  452.     $sqlstr .= "FROM entries e ";
  453.     $sqlstr .= "INNER JOIN entries_fields ef ON ef.id = e.id ";
  454.     $sqlstr .= "WHERE e.id = ?";
  455.     $sql = $pdo->prepare($sqlstr);
  456.     $sql->execute([$this->id]);
  457.     $rows = $sql->fetchAll();
  458.     foreach ($rows AS $row) {
  459.       if (empty($this->name)) {
  460.         $this->name = $row["ename"];
  461.         $this->datecreated = strtotime($row["datecreated"]);
  462.         $this->createdby = $row["createdby"];
  463.         $this->acl = empty($row["acl"]) ? new ACL() : unserialize($row["acl"]);
  464.         $this->group = new Group($row["gid"]);
  465.       }
  466.      
  467.       $field = new Field($row["fid"], $this->id);
  468.       $field->disporder = $row["disporder"];
  469.       $field->nameoverride = $row["nameoverride"];
  470.       $field->value = $row["datavalue"];
  471.       $this->fields[] = $field;
  472.     }
  473.   }
  474.  
  475.   function flush() {
  476.     global $pdo;
  477.     if (empty($this->name)) return;
  478.    
  479.     if (empty($this->id)) {
  480.       $this->id = setData($pdo, "entries", ["gid"=>$this->gid, "ename"=>$this->name, "flags"=>$this->flags, "datecreated"=>sqlDate($this->datecreated), "createdby"=>$this->createdby, "acl"=>$this->acl == null ? null : serialize($this->acl)]);
  481.       foreach ($this->fields AS $f) {
  482.         setData($pdo, "entries_fields", ["id"=>$this->id, "fid"=>$f->id, "datavalue"=>$f->content, "flags"=>$f->flags]);
  483.       }
  484.       return;
  485.     }
  486.    
  487.     setData($pdo, "entries", ["id"=>$this->id, "gid"=>$this->gid, "ename"=>$this->name, "flags"=>$this->flags, "datecreated"=>sqlDate($this->datecreated), "createdby"=>$this->createdby, "acl"=>$this->acl == null ? null : serialize($this->acl)]);
  488.     foreach ($this->fields AS $f) {
  489.       $sql = $pdo->prepare("UPDATE entries_fields SET datavalue = ?, flags = ? WHERE id = ? AND fid = ?");
  490.       $sql->execute([$f->content, $f->flags, $this->id, $this->gid]);
  491.     }
  492.   }
  493.  
  494.   function getPanel($opts = []) {
  495.     $ret = "<tr id='row_{$this->id}'" . (array_key_exists("rowclick", $opts)?" class='rowclick'":"") . ">";
  496.     if (array_key_exists("del", $opts)) $ret .= "  <td>" . grabInput("del_{$this->id}", 1, "checkbox") . "</td>";
  497.     if (array_key_exists("showgroup", $opts)) {
  498.       $name = array_key_exists("group", $opts) ? $opts["group"] : ($this->group == null ? "" : $this->group->name);
  499.       $ret .= "  <td>$name</td>";
  500.     }
  501.     $ret .= "  <td>";
  502.     $ret .= "<" . (array_key_exists("clicktag", $opts) ? $opts["clicktag"] : "a") . " id='r_{$this->id}'>";
  503.     $ret .= array_key_exists("maxlen", $opts) ? limitedDisplay($this->name, $opts["maxlen"], array_key_exists("maxlen.str", $opts) ? $opts["maxlen.str"] : "") : $this->name;
  504.     $ret .= "</" . (array_key_exists("clicktag", $opts) ? $opts["clicktag"] : "a") . ">";
  505.     $ret .= "</tr>";
  506.     return $ret;
  507.   }
  508.  
  509.   function hasPermission($user = null, $perm = SitePerms::Entries) {
  510. debug($this);
  511. debug($this->acl);
  512.     if ($user == null) $user = getLoggedInUser();
  513.  
  514.     if ($this->group != null) {
  515.       if (!$this->group->hasPermission($user)) return false;
  516.      
  517.       if (empty($this->acl)) return isLoggedIn() ? $this->createdby == getLoggedInUser()->id : false;
  518.       return $this->acl->hasPermission($user, SitePerms::Entries);
  519.     }
  520.  
  521.     if (empty($this->acl)) return isLoggedIn() ? $this->createdby == getLoggedInUser()->id : false;
  522.     return $this->acl->hasPermission($user, SitePerms::Entries);
  523.   }
  524. }
  525.  
  526.  
  527. //*******************************************************************************************************************************************
  528. //
  529. // Entry search interface
  530. //
  531. //*******************************************************************************************************************************************
  532. class Search {
  533.   public $keyword = "";
  534.   public $gid = 0;
  535.   public $page = 0;
  536.   public $maxpages = 0;
  537.   public $pagesize = 5;
  538.   public $results = [];
  539.   public $hasSearched = false;
  540.  
  541.   function __construct($opts = []) {
  542.     global $cfg;
  543.    
  544.     $this->pagesize = array_key_exists("pagesize", $opts) ? $opts["pagesize"] : (array_key_exists("pagesize", $cfg) ? $cfg["pagesize"] : 5);
  545.     foreach (["keyword","gid","page"] AS $f) {
  546.       if (!array_key_exists($f, $_REQUEST)) continue;
  547.      
  548.       $this->$f = $_REQUEST[$f];
  549.     }
  550.    
  551.     $this->hasSearched = !empty($_REQUEST["gid"]) || !empty($_REQUEST["keyword"]);
  552.     $this->performSearch();
  553.   }
  554.  
  555.  
  556.   function getInterface($opts = []) {
  557.     $ret = ""; $tmp = [];
  558.     $jq = array_key_exists("jquery", $opts) ? $opts["jquery"] : "$";
  559.    
  560.     if (!array_key_exists("buttons", $opts) || array_key_exists("*", $opts["buttons"])) {
  561.       $tmp["btn_submit"] = ["disp"=>"Submit", "code"=>"document.data.submit();"];
  562.       $tmp["btn_reset"] = ["disp"=>"Reset", "code"=>"document.data.gid.value = '';document.data.keyword.value = '';document.data.submit();"];
  563.     }
  564.     if (array_key_exists("buttons", $opts) && array_key_exists("*", $opts["buttons"])) {
  565.       unset($opts["buttons"]["*"]);
  566.       $tmp = array_merge($tmp, $opts["buttons"]);
  567.     }
  568.     $opts["buttons"] = $tmp;
  569.     $opts["jquery"] = array_key_exists("jquery", $opts) ? $opts["jquery"] : "$";
  570.     $groups = getGroups();
  571.  
  572.     if (count($groups) > 0) {
  573.       $ret .= "<p><select name=gid id=gid><option></option>";
  574.       foreach ($groups AS $group) {
  575.         $selected = "";
  576.         if (array_key_exists("default", $opts) && $group->id == $opts["default"]) $selected = "SELECTED";
  577.         if (array_key_exists("gid", $_REQUEST) && $group->id == $_REQUEST["gid"]) $selected = "SELECTED";
  578.         $ret .= "<option $selected value='{$group->id}'>{$group->name}</option>";
  579.       }
  580.       $ret .= "</select></p>" . PHP_EOL;
  581.     }
  582.     $ret .= "<p>" . grabInput("keyword", 15, "text", $this->keyword) . "</p>" . PHP_EOL;
  583.    
  584.     foreach ($opts["buttons"] AS $id=>$data) {
  585.       $ret .= "<input class=\"stdbtn\" type=\"button\" id=\"$id\" value=\"{$data["disp"]}\">" . PHP_EOL;
  586.       $ret .= "<script>" . PHP_EOL;
  587.       $ret .= "{$opts["jquery"]}('#$id').click(function() {" . PHP_EOL;
  588.       $ret .= "  {$data["code"]}" . PHP_EOL;
  589.       $ret .= "});" . PHP_EOL;
  590.       $ret .= "</script>" . PHP_EOL;
  591.     }
  592.    
  593.     if (array_key_exists("hr", $opts)) $ret .= "<hr>" . PHP_EOL;
  594.     if (!array_key_exists("nofocus", $opts)) {
  595.       $focus = array_key_exists("focus", $opts) ? $opts["focus"] : "keyword";
  596.       $ret .= "<script>var focustarget = document.getElementById('$focus');if (focustarget != null) focustarget.focus();</script>" . PHP_EOL;
  597.     }
  598.     if (array_key_exists("groupchange", $opts)) {
  599.       $ret .= "<script>$jq('#gid').change(function() { {$opts["groupchange"]} });</script>";
  600.     }
  601.     return $ret;
  602.   }
  603.  
  604.  
  605.   function performSearch() {
  606.     global $pdo;
  607.  
  608.     $sqldata = [];
  609.     $start = $this->page * $this->pagesize;      
  610.    
  611.     $sqlstr = "";
  612.     if (!empty($this->keyword)) {
  613.       $sqlstr .= (empty($sqlstr) ? "" : " AND ");
  614.       $sqlstr .= "(ename LIKE ?)";
  615.       $sqldata[] = "%{$this->keyword}%";
  616.     }
  617.     if (!empty($this->gid)) {
  618.       $sqlstr .= (empty($sqlstr) ? "" : " AND ");
  619.       $sqlstr .= "(gid = ?)";
  620.       $sqldata[] = $this->gid;
  621.     }
  622.    
  623.     $this->results = [];
  624.     $sql = $pdo->prepare("SELECT id,(SELECT COUNT(id) FROM entries" . (empty($sqlstr)?"":" WHERE ($sqlstr)") . ") AS tot FROM entries" . (empty($sqlstr)?"":" WHERE ($sqlstr)") . " ORDER BY ename LIMIT $start,{$this->pagesize}");
  625.     $sql->execute(array_merge($sqldata, $sqldata));
  626.     $rows = $sql->fetchAll();
  627.     if (count($rows) == 0) return;
  628.    
  629.     $this->maxpages = ceil($rows[0]["tot"] / $this->pagesize);
  630.     foreach ($rows AS $row) {
  631.       $entry = new Entry($row["id"]);
  632.       if (empty($entry->id)) continue;
  633.       if (!$entry->hasPermission(getLoggedInUser())) continue;
  634.      
  635.       $this->results[] = $entry;
  636.     }
  637.   }
  638.  
  639.  
  640.   function getPagination($opts = []) {
  641.     if (count($this->results) == 0) return;
  642.    
  643.     foreach (["allow.first", "allow.prev", "allow.next", "allow.last", "allow.jump", "show.buttons"] AS $f) {
  644.       $opts[$f] = array_key_exists($f, $opts) ? $opts[$f] : true;
  645.     }
  646.     $opts["jquery"] = array_key_exists("jquery", $opts) ? $opts["jquery"] : "$";
  647.    
  648.     $ret  = "<div style='width: 100%; text-align: center;'>" . PHP_EOL;
  649.     if ($opts["allow.first"]) $ret .= "  <button class='btnstd" . ($this->page == 0?" btndisabled":"") . "' id=btn_frst type='button'>| &lt;</button>" . PHP_EOL;
  650.     if ($opts["allow.prev"]) $ret .= "  <button class='btnstd" . ($this->page == 0?" btndisabled":"") . "' id=btn_prev type='button'" . ($this->page == 0?" class='btndisabled'":"") . ">&lt;</button>" . PHP_EOL;
  651.     if ($opts["allow.jump"]) {
  652.       $ret .= "  <select name=page id=page style='width: 50px;'>" . PHP_EOL;
  653.       for ($x=0; $x<$this->maxpages; $x++) $ret .= "<option value='$x' " . ($this->page == $x?"SELECTED":"") . ">" . ($x+1) . "</option>" . PHP_EOL;
  654.       $ret .= "  </select>" . PHP_EOL;
  655.       $ret .= "  <script>" . PHP_EOL;
  656.       $ret .= "  {$opts["jquery"]}('#page').change(function() { document.data.submit(); });" . PHP_EOL;
  657.       $ret .= "  </script>" . PHP_EOL;
  658.     } else {
  659.       Hidden("page", $this->page);
  660.     }
  661.     if ($opts["allow.next"]) $ret .= "  <button class='btnstd" . ($this->page == $this->maxpages-1?" btndisabled":"") . "' id=btn_next type='button'>&gt;</button>" . PHP_EOL;
  662.     if ($opts["allow.last"]) $ret .= "  <button class='btnstd" . ($this->page == $this->maxpages-1?" btndisabled":"") . "' id=btn_last type='button'>&gt; |</button>" . PHP_EOL;
  663.     $ret .= "</div>" . PHP_EOL;
  664.     if (!$opts["show.buttons"]) return $ret;
  665.    
  666.     $ret .= "<script>" . PHP_EOL;
  667.     if ($opts["allow.first"]) $ret .= "{$opts["jquery"]}('#btn_frst').click(function() { document.data.page.value = '0'; document.data.submit(); });" . PHP_EOL;
  668.     if ($opts["allow.prev"] && $this->page > 0) $ret .= "{$opts["jquery"]}('#btn_prev').click(function() { document.data.page.value = '" . ($this->page - 1) . "'; document.data.submit(); });" . PHP_EOL;
  669.     if ($opts["allow.next"] && $this->page < $this->maxpages - 1) $ret .= "{$opts["jquery"]}('#btn_next').click(function() { document.data.page.value = '" . ($this->page + 1) . "'; document.data.submit(); });" . PHP_EOL;
  670.     if ($opts["allow.last"]) $ret .= "{$opts["jquery"]}('#btn_last').click(function() { document.data.page.value = '" . ($this->maxpages - 1) . "'; document.data.submit(); });" . PHP_EOL;
  671.     $ret .= "</script>" . PHP_EOL;
  672.     return $ret;
  673.   }
  674.  
  675.  
  676.   function getResults($opts) {
  677.     if (count($this->results) == 0) return;
  678.    
  679.     $ret = "";
  680.     foreach ($this->results AS $result) {
  681.       $ret .= $result->getPanel($opts) . PHP_EOL;;
  682.     }
  683.     return $ret;
  684.   }
  685. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement