Completed
Branch master (b67f97)
by Pierre-Henry
35:51
created

DbConfig::clearCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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 19 and the first side effect is on line 15.

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            DbConfig Class
4
 * @desc             Database Config Class.
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
 * @version          1.1
11
 */
12
13
namespace PH7\Framework\Mvc\Model;
14
15
defined('PH7') or exit('Restricted access');
16
17
use PH7\Framework\Cache\Cache;
18
19
final class DbConfig
20
{
21
    const
22
    CACHE_GROUP = 'db/config',
23
    CACHE_TIME = 999000,
24
    ENABLE_SITE = 'enable',
25
    MAINTENANCE_SITE = 'maintenance';
26
27
    /**
28
     * Private constructor to prevent instantiation of class, because it's a static class.
29
     */
30
    private function __construct() {}
31
32
    /**
33
     * @param string $sSetting You can specify a specific parameter. Default NULL
34
     * @return mixed (string | integer | object) Returns a string or an integer if you specify a specific parameter, otherwise returns an object.
35
     */
36
    public static function getSetting($sSetting = null)
37
    {
38
        $oCache = (new Cache)->start(self::CACHE_GROUP, 'setting' . $sSetting, self::CACHE_TIME);
39
40
        // @return value of config the database
41
        if (!empty($sSetting)) {
42
            if (!$sData = $oCache->get()) {
43
                $rStmt = Engine\Db::getInstance()->prepare('SELECT value FROM' . Engine\Db::prefix('Settings') . 'WHERE name = :setting');
44
                $rStmt->bindParam(':setting', $sSetting, \PDO::PARAM_STR);
45
                $rStmt->execute();
46
                $oRow = $rStmt->fetch(\PDO::FETCH_OBJ);
47
                Engine\Db::free($rStmt);
48
                $sData = $oRow->value;
49
                unset($oRow);
50
                $oCache->put($sData);
51
            }
52
            $mData = $sData;
53
        } else {
54
            if (!$oData = $oCache->get()) {
55
                $rStmt = Engine\Db::getInstance()->prepare('SELECT * FROM' . Engine\Db::prefix('Settings'));
56
                $rStmt->execute();
57
                $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
58
                Engine\Db::free($rStmt);
59
                $oCache->put($oData);
60
            }
61
            $mData = $oData;
62
        }
63
64
        unset($oCache);
65
        return empty($mData) ? 0 : $mData;
66
    }
67
68
    /**
69
     * @param string $sValue Value to set.
70
     * @param string $sName Name of the DB pH7_Settings column.
71
     * @return integer 1 on success.
72
     */
73
    public static function setSetting($sValue, $sName)
74
    {
75
        return Engine\Record::getInstance()->update('Settings', 'value', $sValue, 'name', $sName);
76
    }
77
78
    public static function getMetaMain($sLangId)
79
    {
80
        $oCache = (new Cache)->start(self::CACHE_GROUP, 'metaMain' . $sLangId, self::CACHE_TIME);
81
82
        // @return value of meta tags the database
83
        if (!$oData = $oCache->get()) {
84
            $sSql = 'SELECT * FROM' . Engine\Db::prefix('MetaMain') . 'WHERE langId = :langId';
85
86
            // Get meta data with the current language if it exists in the "MetaMain" table ...
87
            $rStmt = Engine\Db::getInstance()->prepare($sSql);
88
            $rStmt->bindParam(':langId', $sLangId, \PDO::PARAM_STR);
89
            $rStmt->execute();
90
            $oData = $rStmt->fetch(\PDO::FETCH_OBJ);
91
92
            // If the current language doesn't exist in the "MetaMain" table, we create a new table for the new language with default value
93
            if (empty($oData)) {
94
                $aData = [
95
                    'langId' => $sLangId, // The new language key (e.g., de_DE)
96
                    'pageTitle' => 'Home',
97
                    'metaDescription' => 'The Dating Software for creating online dating service or online social community.',
98
                    'metaKeywords' => 'script,CMS,PHP,dating script,dating software,social networking software,social networking script,social network script,free,open source,match clone,friend finder clone,adult friend finder clone',
99
                    'slogan' => 'Free Online Dating Community Site with Chat Rooms',
100
                    'promoText' => 'You\'re on the best place for meeting new people nearby! Chat, Flirt, Socialize and have Fun!<br />Create any Dating Sites like that with the <a href="http://software.hizup.com">PHP Dating Script</a>. It is Professional, Free, Open Source, ...',
101
                    'metaRobots' => 'index, follow, all',
102
                    'metaAuthor' => 'Pierre-Henry Soria',
103
                    'metaCopyright' => 'Copyright Pierre-Henry Soria. All Rights Reserved.',
104
                    'metaRating' => 'general',
105
                    'metaDistribution' => 'global',
106
                    'metaCategory' => 'dating'
107
                ];
108
109
                Engine\Record::getInstance()->insert('MetaMain', $aData); // Create the new meta data language
110
                $oData = (object) $aData;
111
                unset($aData);
112
            }
113
            Engine\Db::free($rStmt);
114
            $oCache->put($oData);
115
        }
116
        unset($oCache);
117
118
        return $oData;
119
    }
120
121
    /**
122
     * Sets the Meta Main Data.
123
     *
124
     * @param string $sSection
125
     * @param string $sValue
126
     * @param string $sLangId
127
     * @return void
128
     */
129
    public static function setMetaMain($sSection, $sValue, $sLangId)
130
    {
131
        Engine\Record::getInstance()->update('MetaMain', $sSection, $sValue, 'langId', $sLangId);
132
    }
133
134
    /**
135
     * @param string $sStatus '0' = Disable | '1' = Enable. (need to be string because in DB it is an "enum").
136
     * @return void
137
     */
138
    public static function setSocialWidgets($sStatus)
139
    {
140
        $sStatus = (string) $sStatus; // Cast into string to be sure it will work as in DB it's an "enum" type
141
142
        self::setSetting($sStatus, 'socialMediaWidgets');
143
144
        // addthis JS file's staticID is '1'
145
        $rStmt = Engine\Db::getInstance()->prepare('UPDATE' . Engine\Db::prefix('StaticFiles') . 'SET active = :status WHERE staticId = 1 AND fileType = \'js\' LIMIT 1');
146
        $rStmt->execute(['status' => $sStatus]);
147
148
        // Clear "db/design/static" cache. '1' matches with TRUE in Design::files(); (note, don't need to clear DbConfig as it'll always be called in SettingFormProcess class which clears the cache anyway)
149
        (new Cache)->start(Design::CACHE_STATIC_GROUP, 'filesjs1', null)->clear();
150
    }
151
152
    /**
153
     * @param string $sStatus The constant 'DbConfig::ENABLE_SITE' or 'DbConfig::MAINTENANCE_SITE'
154
     * @return void
155
     */
156
    public static function setSiteMode($sStatus)
157
    {
158
        if ($sStatus != self::MAINTENANCE_SITE && $sStatus != self::ENABLE_SITE) {
159
            exit('Wrong maintenance mode type!');
0 ignored issues
show
Coding Style Compatibility introduced by
The method setSiteMode() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
160
        }
161
162
        self::setSetting($sStatus, 'siteStatus');
163
164
        /* Clear DbConfig Cache (this method is not always called in SettingFormProcess class, so clear the cache to be sure) */
165
        self::clearCache();
166
    }
167
168
    /**
169
     * Clean the entire DbConfig group Cache.
170
     *
171
     * @return void
172
     */
173
    public static function clearCache()
174
    {
175
        (new Cache)->start(self::CACHE_GROUP, null, null)->clear();
176
    }
177
}
178