/Helios Engine/helios/renderers/xtable.php
PHP | 281 lines | 252 code | 15 blank | 14 comment | 26 complexity | 72a9276b26919edeb86d2a15af6bbe54 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0
- <?php
- class XTable {
- // Variable Table
- public $conf = NULL; // Configuration variable
- public $result = NULL; // Result storage.
- public function __construct($id,$IO,$params) {
- // Pass required variables:
- $this->conf["id"] = $id;
- if ($IO === SQL) {
- // Validate Pool
- $config = $GLOBALS["SQL"]->config;
- foreach ($config["databases"] as $database) {
- if ($params === $database[0])
- {
- $valid = TRUE;
- $params = $database[1];
- }
- }
- if (!$valid) {
- echo "<p>Invalid Pool selected.</p>";
- return -1;
- } else {
- }
- $this->conf["pool"] = $params;
- }
- require_once "functions/xhmlentities.php";
- }
- // Load a query:
- public function query($query) {
- $qid = $GLOBALS["SQL"]->query($this->conf["pool"],FE,$query);
- if ($qid < 0) {
- echo "<p> Xtable: An error occured. Check the logs! </p>";
- return -1;
- } else {
- $this->conf["query"] = $qid;
- }}
- // Set Table CSS information
- public function SetProperties($class,$style,$labels) {
- $this->conf["class"] = $class;
- $this->conf["style"] = $style;
- $this->conf["labels"] = $labels;
- $this->conf["counter"] = 0;
- $this->conf["ncounter"] = 0;
- }
- // Add a Behaviour:
- public function AddBehaviour($behaviour) {
- $this->conf["behaviour"] = $behaviour;
- }
- // Add Extra row:
- public function AddExtra($behaviour) {
- $this->conf["extrafunc"][] = $behaviour;
- }
- // Add form entries (optional)
- public function AddEntry($entry) {
- $this->conf["entries"][] = $entry;
- }
- // Add a table-wide form:
- public function AddTableForm($form) {
- $this->conf["formbehaviour"] = $form;
- }
- // Set counter values:
- public function InitCounter($counter,$ncounter) {
- $this->conf["counter"] = $counter;
- $this->conf["ncounter"] = $ncounter;
- }
- // Set a custom value:
- public function SetCProperty($property,$value) {
- $this->conf["$property"] = $value;
- }
- // Generate the Table:
- public function Render($query) {
- $qid = $GLOBALS["SQL"]->query($this->conf["pool"],FE,$query);
- $this->result = $GLOBALS["SQL"]->execute($qid,ASSOC);
- array_shift($this->result);
- $counter = $this->conf["counter"];
- if (isset($this->conf["formbehaviour"])) {
- ?>
- <form id="<?php echo $this->conf["id"];?>FORM" onsubmit="<?php echo $this->conf["formbehaviour"];?> return 0;" method="post" action="">
- <?
- }
- ?>
- <table id="<?php echo $this->conf["id"];?>" class="<?php echo $this->conf["class"] ?>" style="<?php echo $this->conf["style"];?>">
- <thead>
- <tr class="tableinxbdr">
- <?
- $ncounter = $this->conf["ncounter"];
- foreach($this->conf["labels"] as $labels) {
- ?>
- <td><?php echo $labels;?></td>
- <?
- }
- ?>
- </tr>
- </thead>
- <tbody>
- <?
- foreach ($this->result as $result) {
- $rand = rand(0,1000);
- ?>
- <tr id="<?php echo $this->conf["id"];?>TR<?php echo $counter+$rand;?>" class="<?php if ($counter %2) {echo "tableeven";} else {echo "tableodd";} ?>">
- <?
- $ncounter++;
- $keys = array_keys($result);
- foreach ($keys as $keys) {
- if ($keys != "id") {
- $data = $result["$keys"];
- ?>
- <td id="<?php echo $this->conf["id"];?>TD<?php echo $ncounter;?>">
- <?php if (empty($this->conf["behaviour"])) { echo xhtmlentities($data); } else {
- { require $this->conf["behaviour"]; }
- } ?>
- </td>
- <?
- }}
- $ncounter++;
- if (isset($this->conf["extrafunc"])) {
- foreach ($this->conf["extrafunc"] as $extra) {
- $ncounter++;
- ?>
- <td id="<?php echo $this->conf["id"];?>TD<?php echo $ncounter;?>">
- <?
- require $extra;
- ?>
- </td>
- <?
- }}
- ?>
- </tr>
- <?
- $counter++;
- }
- ?>
- </tbody>
- </table>
- <?
- if (isset($this->conf["formbehaviour"])) {
- ?>
- <input type="hidden" id="TableREF" value="<?php echo $this->conf["id"];?>"/>
- <input type="hidden" id="<?php echo $this->conf["id"];?>TR" value="<?php echo $counter; ?>"/>
- <input type="hidden" id="<?php echo $this->conf["id"];?>TD" value="<?php echo $ncounter; ?>"/>
- </form>
- <?
- }
- $this->conf["counter"] = $counter;
- $this->conf["ncounter"] = $ncounter;
- }
- // Just give me one TR, will you?
- public function RenderTR($query) {
- $qid = $GLOBALS["SQL"]->query($this->conf["pool"],FE,$query);
- $this->result = $GLOBALS["SQL"]->execute($qid,ASSOC);
- array_shift($this->result);
- $ncounter = $this->conf["ncounter"];
- $counter = $this->conf["counter"];
- ?>
- <?
- $ncounter++;
- $keys = array_keys($this->result[0]);
- foreach ($keys as $keys) {
- if ($keys != "id") {
- $data = $this->result[0]["$keys"]
- ?>
- <td id="<?php echo $this->conf["id"];?>TD<?php echo $ncounter;?>">
- <?php if (empty($this->conf["behaviour"])) { echo xhtmlentities($data); } else {
- {require $this->conf["behaviour"]; }
- } ?>
- </td>
- <?
- $ncounter++;
- }}
- if (isset($this->conf["extrafunc"])) {
- foreach ($this->conf["extrafunc"] as $extra) {
- $ncounter++;
- ?>
- <td id="<?php echo $this->conf["id"];?>TD<?php echo $ncounter;?>">
- <?
- require $extra;
- ?>
- </td>
- <?
- }}
- ?>
- <?
- $this->conf["ncounter"] = $ncounter;
- }
- # Overloaded for convenience:
- public function MultiVertTable($query) {
- $this->QuickVertTable($query);
- }
- public function QuickVertTable($query) {
- $qid = $GLOBALS["SQL"]->query($this->conf["pool"],FE,$query);
- $this->result = $GLOBALS["SQL"]->execute($qid,ASSOC);
- array_shift($this->result);
- $counter = 0;
- if (is_array($this->result[0])) {
- $labels = array_keys($this->result[0]);
- ?>
- <table id="<?php echo $this->conf["id"];?>" class="<?php echo $this->conf["class"] ?>" style="<?php echo $this->conf["style"]; ?>">
- <tbody>
- <?
- foreach ($this->result[0] as $data) {
- echo "<tr>\n";
- echo "<td class=\"tableinxbdr\">".htmlentities(ucfirst($labels[$counter]),UTF-8)."</td>\n";
- echo "<td class=\"";
- if ($counter %2) {
- echo "tableeven";
- } else {
- echo "tableodd";
- }
- echo "\">";
- if (empty($this->conf["behaviour"])) {
- echo xhtmlentities($data);
- } else {
- require $this->conf["behaviour"]; }
- echo "</td>\n";
- echo "</tr>\n";
- $counter++;
- }
- ?>
- </tbody>
- </table>
- <?
- }}
- public function MultiHorizTable($query) {
- $qid = $GLOBALS["SQL"]->query($this->conf["pool"],FE,$query);
- $this->result = $GLOBALS["SQL"]->execute($qid,ASSOC);
- array_shift($this->result);
- $counter = 0;
- if (is_array($this->result[0])) {
- $labels = array_keys($this->result[0]);
- ?>
- <table id="<?php echo $this->conf["id"];?>" class="<?php echo $this->conf["class"] ?>" style="<?php echo $this->conf["style"]; ?>">
- <tbody>
- <?
- echo "<tr class=\"tableinxbdr\">";
- foreach ($labels as $label) {
- echo "<td>";
- echo htmlentities(ucfirst($label),UTF-8);
- echo "</td>\n";
- }
- echo "</tr>\n";
- foreach ($this->result as $result){
- echo "<tr class=\"";
- if ($counter %2) {
- echo "tableeven";
- } else {
- echo "tableodd";
- }
- echo "\">";
- foreach ($result as $data) {
- echo "<td>\n";
- if (empty($this->conf["behaviour"])) {
- echo xhtmlentities($data);
- } else {
- require $this->conf["behaviour"]; }
- echo "</td>\n";
- }
- echo "</tr>\n";
- $counter++;
- }
- ?>
- </tbody>
- </table>
- <?
- }}
- }
- ?>