Advertisement
Zgragselus

Currency - class

Dec 4th, 2022
1,125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.11 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  
  5. This file is subject to the terms and conditions defined in
  6. file 'LICENSE', which is part of this source code package.
  7.  
  8. © 2022 OtteIT s.r.o.
  9. All Rights Reserved.
  10.  
  11. Author: Vilem Otte <dev@otte.cz>
  12.  
  13. */
  14.  
  15. require_once(__DIR__."/../../db.php");  
  16.  
  17. /**
  18.  * Rum currency class
  19.  *
  20.  * Functionality for currency creating, reading, updating and deleting
  21.  */
  22. class Rum_Currency
  23. {
  24.     /**
  25.      * Database class instance
  26.      */
  27.     private $conn;
  28.  
  29.     /**
  30.      * Currency ID
  31.      */
  32.     public $id;
  33.    
  34.     /**
  35.      * Currency name
  36.      */
  37.     public $name;
  38.    
  39.     /**
  40.      * Currency symbol
  41.      */
  42.     public $symbol;
  43.  
  44.     /**
  45.      * Constructor
  46.      *
  47.      * @param dbconn Database connection instance
  48.      */
  49.     public function __construct($dbconn)
  50.     {
  51.         $this->conn = $dbconn;
  52.  
  53.         $this->id = null;
  54.         $this->name = null;
  55.         $this->symbol = null;
  56.     }
  57.  
  58.     /**
  59.      * Create currency record
  60.      *
  61.      * Creates single currency record with name and symbol
  62.      *
  63.      * @return _ Currency ID on success, null on failure
  64.      */
  65.     public function Create()
  66.     {
  67.         $query = "INSERT INTO currency (name, symbol) VALUES('" . $this->conn->EscapeString($this->name) . "', '" . $this->conn->EscapeString($this->symbol) . "')";
  68.         $result = $this->conn->Query($query);
  69.  
  70.         if ($result == false)
  71.         {
  72.             return null;
  73.         }
  74.         else
  75.         {
  76.             $this->id = $this->conn->GetLastInsertID();
  77.  
  78.             return $this->id;
  79.         }
  80.     }
  81.  
  82.     /**
  83.      * Reads currency records
  84.      *
  85.      * Reads currency records filtered by specific id, name and symbol
  86.      *
  87.      * @return _ Null on failure, array of objects on success ordered by name
  88.      */
  89.     public function Read()
  90.     {
  91.         $query = "SELECT * FROM currency WHERE 1 = 1";
  92.  
  93.         if ($this->id != null)
  94.         {
  95.             $query .= " AND id = '" . $this->conn->EscapeString($this->id) . "'";
  96.         }
  97.  
  98.         if ($this->name != null)
  99.         {
  100.             $query .= " AND name = '" . $this->conn->EscapeString($this->name) . "'";
  101.         }
  102.  
  103.         if ($this->symbol != null)
  104.         {
  105.             $query .= " AND symbol = '" . $this->conn->EscapeString($this->symbol) . "'";
  106.         }
  107.  
  108.         $query .= " ORDER BY name ";
  109.  
  110.         $result = $this->conn->Query($query);
  111.         if ($result == false)
  112.         {
  113.             return null;
  114.         }
  115.         else
  116.         {
  117.             $numRows = $this->conn->GetRowsCount($result);
  118.  
  119.             $output = [];
  120.  
  121.             for ($i = 0; $i < $numRows; $i++)
  122.             {
  123.                 $row = $this->conn->GetRow($result, $i);
  124.  
  125.                 $output[$i] = (object)array();
  126.                 $output[$i]->id = $row['id'];
  127.                 $output[$i]->name = $row['name'];
  128.                 $output[$i]->symbol = $row['symbol'];
  129.             }
  130.  
  131.             return $output;
  132.         }
  133.     }
  134.  
  135.     /**
  136.      * Update currency record
  137.      *
  138.      * Updates single currency record, requires name and symbol
  139.      *
  140.      * @return _ Null on failure, true on success
  141.      */
  142.     public function Update()
  143.     {
  144.         $query = "UPDATE currency SET
  145.            name = '" . $this->conn->EscapeString($this->name) . "',
  146.            symbol = '" . $this->conn->EscapeString($this->symbol) . "' ";
  147.  
  148.         $query .= "WHERE id = '" . $this->conn->EscapeString($this->id) . "'";
  149.  
  150.         $result = $this->conn->Query($query);
  151.         if ($result == false)
  152.         {
  153.             return null;
  154.         }
  155.         else
  156.         {
  157.             return true;
  158.         }
  159.     }
  160.  
  161.     /**
  162.      * Delete currency record
  163.      *
  164.      * Removes currency record from database
  165.      *
  166.      * @return _ Null on failure, true on success
  167.      */
  168.     public function Delete()
  169.     {
  170.         $query = "DELETE FROM currency WHERE id = '" . $this->conn->EscapeString($this->id) . "'";
  171.  
  172.         $result = $this->conn->Query($query);
  173.         if ($result == false)
  174.         {
  175.             return null;
  176.         }
  177.         else
  178.         {
  179.             return true;
  180.         }
  181.     }
  182. }
  183.  
  184. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement