Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
115 lars 1
<?php
2
    //$Id: resource.mysqli.php 105 2022-06-02 14:08:35Z tiefland $
3
 
4
    /**
5
     * MySQL Resource
6
     *
7
     * Resource Implementation based on the Custom API to use
8
     * MySQLi as the storage resource for Smarty's templates and configs.
9
     *
10
     * @package Resource-examples
11
     * @author  Rodney Rehm
12
     * @author  Lars Tiefland
13
     */
14
    class Smarty_Resource_Mysqli extends Smarty_Resource_Custom
15
    {
16
        private $conn;
17
        // prepared fetch() statement
18
        protected $fetch;
19
        // prepared fetchTimestamp() statement
20
        protected $mtime;
21
 
22
        public function __construct()
23
        {
24
            if ( isset( $GLOBALS["remoteDB"] ) && $GLOBALS["remoteDB"] )
25
            {
26
                $this->conn = $GLOBALS["remoteDB"];
27
            }
28
            elseif ( isset( $GLOBALS["orderDB"] ) && $GLOBALS["orderDB"] )
29
            {
30
                $this->conn = $GLOBALS["orderDB"];
31
            }
32
            else
33
            {
34
                $this->conn = $GLOBALS["shopDB"];
35
            }
36
            if ( isset( $_SESSION["INI"] ) && $_SESSION["INI"] )
37
            {
38
                $dbase = $_SESSION["INI"]["dbConnect"]["order_db"];
39
            }
40
            else
41
            {
42
                $dbase = $GLOBALS["INI"]["dbConnect"]["order_db"];
43
            }
44
            if ( $dbase )
45
            {
46
                $dbase .= ".";
47
            }
48
            $settings_table = $dbase . "web_settings";
49
            $bestellarten = $this->getBestellarten();
50
            $sql = 'SELECT
51
                    letzte_aenderung_am,
52
                    inhalt
53
                FROM
54
                    ' . $settings_table . '
55
                WHERE
56
                    id = ?
57
                AND
58
                    shops_ID=?
59
            ';
60
            if ( $bestellarten )
61
            {
62
                $sql .= "
63
                    AND
64
                        bestellart_id=1
65
                ";
66
            }
67
            $this->fetch = $this->conn->prepare( $sql );
68
 
69
            $sql = 'SELECT
70
                    letzte_aenderung_am
71
                FROM
72
                    ' . $settings_table . '
73
                WHERE
74
                    id = ?
75
                AND
76
                    shops_ID=?
77
            ';
78
            if ( $bestellarten )
79
            {
80
                $sql .= "
81
                    AND
82
                        bestellart_id=1
83
                ";
84
            }
85
            $this->mtime = $this->conn->prepare( $sql );
86
        }
87
 
88
        /**
89
         * Fetch a template and its modification time from database
90
         *
91
         * @param string  $name   template name
92
         * @param string  $source template source
93
         * @param integer $mtime  template modification timestamp (epoch)
94
         *
95
         * @return void
96
         */
97
        protected function fetch( $name, &$source, &$mtime )
98
        {
99
            list( $shops_ID, $id ) = explode( "_", $name );
100
            $this->fetch->bind_param( "ii", $id, $shops_ID );
101
            $this->fetch->execute();
102
            $res = $this->fetch->get_result();
103
            $row = $res->fetch_assoc();
104
            if ( $row )
105
            {
106
                $source = $row['inhalt'];
107
                $mtime = strtotime( $row['letzte_aenderung_am'] );
108
            }
109
            else
110
            {
111
                $source = null;
112
                $mtime = null;
113
            }
114
 
115
        }
116
 
117
        private function getBestellarten()
118
        {
119
            $sql = "SELECT * FROM bestellart";
120
            $result = $this->conn->query( $sql );
121
 
122
            if ( $result )
123
            {
124
                $sql = "SHOW FIELDS FROM web_settings LIKE 'bestellart_id'";
125
                $result = $this->conn->query( $sql );
126
                $test = ( $result->num_rows );
127
                return ( bool )$test;
128
            }
129
            else
130
            {
131
                echo $this->conn->error;
132
            }
133
            return false;
134
        }
135
 
136
        /**
137
         * Fetch a template's modification time from database
138
         *
139
         * @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the compiled template source.
140
         *
141
         * @param string $name template name
142
         *
143
         * @return integer timestamp (epoch) the template was modified
144
         */
145
        protected function fetchTimestamp( $name )
146
        {
147
            list( $shops_ID, $id ) = explode( "_", $name );
148
            $this->mtime->bind_param( "ii", $id, $shops_ID );
149
            $this->mtime->execute();
150
            $res = $this->mtime->get_result();
151
            $row = $res->fetch_assoc();
152
            $mtime = $row["letzte_aenderung_am"];
153
            return strtotime( $mtime );
154
        }
155
    }
156
 
157
    // register the resource name "mysqli"
158
    $GLOBALS["ui"]->registerResource( 'mysqli', new Smarty_Resource_Mysqli() );