Completed
Branch master (d0398b)
by Pierre-Henry
36:04
created

Design::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 24 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @title            Design Model Class
4
 * @desc             Design Model for the HTML contents.
5
 *
6
 * @author           Pierre-Henry Soria <[email protected]>
7
 * @copyright        (c) 2012-2017, Pierre-Henry Soria. All Rights Reserved.
8
 * @license          GNU General Public License; See PH7.LICENSE.txt and PH7.COPYRIGHT.txt in the root directory.
9
 * @package          PH7 / Framework / Mvc / Model
10
 */
11
12
namespace PH7\Framework\Mvc\Model;
13
14
defined('PH7') or exit('Restricted access');
15
16
use PH7\Framework\Layout\Html\Design as HtmlDesign;
17
use PH7\Framework\Mvc\Model\Engine\Db;
18
use PH7\Framework\Registry\Registry;
19
use PH7\Framework\Cache\Cache;
20
use PH7\Framework\Parse\SysVar;
21
use PH7\Framework\Navigation\Page;
22
use PH7\Framework\Ads\Ads;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, PH7\Framework\Mvc\Model\Ads.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
23
24
class Design extends HtmlDesign
25
{
26
    const CACHE_STATIC_GROUP = 'db/design/static', CACHE_TIME = 172800;
27
28
    private $_oCache;
29
30
    public function __construct()
31
    {
32
        parent::__construct();
33
        $this->_oCache = new Cache;
34
    }
35
36
    public function langList()
37
    {
38
        $sCurrentPage = Page::cleanDynamicUrl('l');
39
        $oLangs = (new Lang)->getInfos();
40
41
        foreach ($oLangs as $sLang) {
0 ignored issues
show
Bug introduced by
The expression $oLangs of type object<stdClass> is not traversable.
Loading history...
42
            if ($sLang->langId === PH7_LANG_NAME) {
43
                // Skip the current lang
44
                continue;
45
            }
46
47
            // Retrieve only the first two characters
48
            $sAbbrLang = substr($sLang->langId,0,2);
49
50
            echo '<a href="', $sCurrentPage, $sLang->langId, '" hreflang="', $sAbbrLang, '"><img src="', PH7_URL_STATIC, PH7_IMG, 'flag/s/', $sAbbrLang, '.gif" alt="', t($sAbbrLang),'" title="', t($sAbbrLang),'" /></a>&nbsp;';
51
        }
52
53
        unset($oLangs);
54
    }
55
56
    /**
57
     * Gets Ads with ORDER BY RAND() SQL aggregate function.
58
     * With caching, advertising changes every hour.
59
     *
60
     * @param integer $iWidth
61
     * @param integer $iHeight
62
     * @param boolean $bOnlyActive
63
     *
64
     * @return void
65
     */
66
    public function ad($iWidth, $iHeight, $bOnlyActive = true)
67
    {
68
        if (!PH7_VALID_LICENSE) {
69
            return false;
70
        }
71
72
        $this->_oCache->start(self::CACHE_STATIC_GROUP, 'ads' . $iWidth . $iHeight . $bOnlyActive, static::CACHE_TIME);
73
74
        if (!$oData = $this->_oCache->get()) {
75
            $sSqlActive = ($bOnlyActive) ? ' AND (active=\'1\') ' : ' ';
76
            $rStmt = Db::getInstance()->prepare('SELECT * FROM ' . Db::prefix('Ads') . 'WHERE (width=:width) AND (height=:height)' . $sSqlActive . 'ORDER BY RAND() LIMIT 1');
77
            $rStmt->bindValue(':width', $iWidth, \PDO::PARAM_INT);
78
            $rStmt->bindValue(':height', $iHeight, \PDO::PARAM_INT);
79
            $rStmt->execute();
80
            $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
81
            Db::free($rStmt);
82
            $this->_oCache->put($oData);
83
        }
84
85
        /**
86
         * Don't display ads on the admin panel.
87
         */
88
        if (!(Registry::getInstance()->module === PH7_ADMIN_MOD) && $oData) {
89
            echo '<div class="inline" onclick="$(\'#ad_' . $oData->adsId . '\').attr(\'src\',\'' . PH7_URL_ROOT . '?' . Ads::PARAM_URL . '=' . $oData->adsId . '\');return true;">';
90
            echo Ads::output($oData);
91
            echo '<img src="' . PH7_URL_STATIC . PH7_IMG . 'useful/blank.gif" style="border:0;width:0px;height:0px;" alt="" id="ad_' . $oData->adsId . '" /></div>';
92
        }
93
        unset($oData);
94
    }
95
96
    /**
97
     * Analytics API code.
98
     *
99
     * @param boolean $bPrint Print the analytics HTML code.
100
     * @param boolean $bOnlyActive Only active code.
101
     *
102
     * @return string|void
103
     */
104
    public function analyticsApi($bPrint = true, $bOnlyActive = true)
105
    {
106
        $this->_oCache->start(self::CACHE_STATIC_GROUP, 'analyticsApi' . $bOnlyActive, static::CACHE_TIME);
107
108
        if (!$sData = $this->_oCache->get()) {
109
            $sSqlWhere = ($bOnlyActive) ? 'WHERE active=\'1\'' : '';
110
            $rStmt = Db::getInstance()->prepare('SELECT code FROM ' . Db::prefix('AnalyticsApi') . $sSqlWhere . ' LIMIT 1');
111
            $rStmt->execute();
112
            $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
113
            Db::free($rStmt);
114
            $sData = $oRow->code;
115
            unset($oRow);
116
            $this->_oCache->put($sData);
117
        }
118
119
        if ($bPrint) {
120
            echo $sData;
121
        } else {
122
            return $sData;
123
        }
124
    }
125
126
    /**
127
     * Get the custom code.
128
     *
129
     * @param string $sType  Choose between 'css' and 'js'.
130
     *
131
     * @return string
132
     */
133
    public function customCode($sType)
134
    {
135
        $this->_oCache->start(self::CACHE_STATIC_GROUP, 'customCode' . $sType, static::CACHE_TIME);
136
137
        if (!$sData = $this->_oCache->get()) {
138
            $rStmt = Db::getInstance()->prepare('SELECT code FROM ' . Db::prefix('CustomCode') . 'WHERE codeType = :type LIMIT 1');
139
            $rStmt->bindValue(':type', $sType, \PDO::PARAM_STR);
140
            $rStmt->execute();
141
            $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
142
            Db::free($rStmt);
143
            $sData = (!empty($oRow->code)) ? $oRow->code : null;
144
            unset($oRow);
145
            $this->_oCache->put($sData);
146
        }
147
148
        return $sData;
149
    }
150
151
    /**
152
     * Get CSS/JS files.
153
     *
154
     * @param string $sType  Choose between 'css' and 'js'.
155
     * @param boolean $bOnlyActive If TRUE, it will get only the files activated.
156
     *
157
     * @return void HTML output.
158
     */
159
    public function files($sType, $bOnlyActive = true)
160
    {
161
        $this->_oCache->start(self::CACHE_STATIC_GROUP, 'files' . $sType . $bOnlyActive, static::CACHE_TIME);
162
163
        if (!$oData = $this->_oCache->get()) {
164
            $sSqlWhere = ($bOnlyActive) ? ' AND active=\'1\'' : '';
165
            $rStmt = Db::getInstance()->prepare('SELECT file FROM ' . Db::prefix('StaticFiles') . 'WHERE fileType = :type' . $sSqlWhere);
166
            $rStmt->bindValue(':type', $sType, \PDO::PARAM_STR);
167
            $rStmt->execute();
168
            $oData = $rStmt->fetchAll(\PDO::FETCH_OBJ);
169
            Db::free($rStmt);
170
            $this->_oCache->put($oData);
171
        }
172
173
        if (!empty($oData)) {
174
            foreach ($oData as $oFile) {
175
                $sFullPath = (new SysVar)->parse($oFile->file);
176
                $sMethodName = 'external' . ($sType == 'js' ? 'Js' : 'Css') . 'File';
177
                $this->$sMethodName($sFullPath);
178
            }
179
        }
180
        unset($oData);
181
    }
182
}
183