Completed
Push — master ( 0c2cb4...672bed )
by Richard
07:48
created

PluginsPluginHandler::getLC()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
/**
13
 * plugins module
14
 *
15
 * @copyright       XOOPS Project (http://xoops.org)
16
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
17
 * @package         plugins
18
 * @since           2.6.0
19
 * @author          trabis <[email protected]>
20
 * @version         $Id$
21
 */
22
23
use Xoops\Core\Database\Connection;
24
use Xoops\Core\Kernel\XoopsObject;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsObject.

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...
25
use Xoops\Core\Kernel\XoopsPersistableObjectHandler;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsPersistableObjectHandler.

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...
26
use Xoops\Core\Kernel\Dtype;
27
use Xoops\Core\Kernel\Criteria;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Criteria.

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...
28
use Xoops\Core\Kernel\CriteriaCompo;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, CriteriaCompo.

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...
29
30
class PluginsPlugin extends XoopsObject
31
{
32
    /**
33
     * Constructor
34
     */
35
    public function __construct()
36
    {
37
        $this->initVar('plugin_id', Dtype::TYPE_INTEGER, null, false);
38
        $this->initVar('plugin_caller', Dtype::TYPE_TEXT_BOX, null, false);
39
        $this->initVar('plugin_listener', Dtype::TYPE_TEXT_BOX, null, false);
40
        $this->initVar('plugin_status', Dtype::TYPE_INTEGER, null, 1);
41
        $this->initVar('plugin_order', Dtype::TYPE_INTEGER, null, false, 0);
42
    }
43
}
44
45
class PluginsPluginHandler extends XoopsPersistableObjectHandler
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
46
{
47
    /**
48
     * @param null|Connection $db database
49
     */
50
    public function __construct(Connection $db = null)
51
    {
52
        parent::__construct($db, 'plugins_plugin', 'PluginsPlugin', 'plugin_id', 'plugin_caller');
53
    }
54
55
    /**
56
     * @param string $listener
57
     * @param string $caller
58
     * @return bool|PluginsPlugin
59
     */
60
    public function getLC($listener, $caller)
61
    {
62
        $criteria = new CriteriaCompo();
63
        $criteria->add(new Criteria('plugin_listener', $listener));
64
        $criteria->add(new Criteria('plugin_caller', $caller));
65
66
        //Expecting only one result;
67
        if ($objects = $this->getObjects($criteria)) {
68
            return $objects[0];
69
        } else {
70
            return false;
71
        }
72
    }
73
74
    /**
75
     * @param string $listener
76
     * @return array Array of PluginsPlugin
77
     */
78 View Code Duplication
    public function getByListener($listener)
79
    {
80
        $criteria = new CriteriaCompo();
81
        $criteria->add(new Criteria('plugin_listener', (string)$listener));
82
        $criteria->setSort('plugin_status DESC, plugin_order');
83
        $criteria->setOrder('ASC');
84
        return $this->getObjects($criteria);
85
    }
86
87
    /**
88
     * @param string $caller
89
     * @return array Array of PluginsPlugin
90
     */
91 View Code Duplication
    public function getByCaller($caller)
92
    {
93
        $criteria = new CriteriaCompo();
94
        $criteria->add(new Criteria('plugin_caller', (string)$caller));
95
        $criteria->setSort('plugin_status DESC, plugin_order');
96
        $criteria->setOrder('ASC');
97
        return $this->getObjects($criteria);
98
    }
99
100
101
    /**
102
     * @return array Array of PluginsPlugin
103
     */
104
    public function getThemAll()
105
    {
106
        $criteria = new CriteriaCompo();
107
        $criteria->setSort('plugin_status DESC, plugin_order');
108
        $criteria->setOrder('ASC');
109
        return $this->getObjects($criteria);
110
    }
111
112
    /**
113
     * @return array Array of Listeners
114
     */
115 View Code Duplication
    public function getListeners()
116
    {
117
        $ret = array();
118
        $qb = $this->db2->createXoopsQueryBuilder();
119
        $qb->select('plugin_listener')
120
            ->fromPrefix('plugins_plugin', '')
121
            ->groupBy('plugin_listener');
122
        $result = $qb->execute();
123
        while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
124
            $ret[$row['plugin_listener']] = $this->getModuleName($row['plugin_listener']);
125
        }
126
        return $ret;
127
    }
128
129
    /**
130
     * @return array Array of Callers
131
     */
132 View Code Duplication
    public function getCallers()
133
    {
134
        $ret = array();
135
        $qb = $this->db2->createXoopsQueryBuilder();
136
        $qb->select('plugin_caller')
137
            ->fromPrefix('plugins_plugin', '')
138
            ->groupBy('plugin_caller');
139
        $result = $qb->execute();
140
        while ($row = $result->fetch(\PDO::FETCH_ASSOC)) {
141
            $ret[$row['plugin_caller']] = $this->getModuleName($row['plugin_caller']);
142
        }
143
        return $ret;
144
    }
145
146
    /**
147
     * Gets the module name but checks if it is active or not
148
     * There is a preload that calls this method before deleting deactivated module entries
149
     *
150
     * @param string $dirname
151
     * @return mixed
152
     */
153
    public function getModuleName(string $dirname)
154
    {
155
         if ($module = \Xoops::getInstance()->getModuleByDirname((string)$dirname)) {
156
             return $module->getVar('name');
157
         } else {
158
             return $dirname;
159
         }
160
    }
161
162
    /**
163
     * @param string $listener
164
     * @param string $caller
165
     * @param int $status
166
     * @param int $order
167
     * @return bool
168
     */
169
    public function addNew($listener, $caller, $status = 1, $order = 0)
170
    {
171
        $object = new PluginsPlugin();
172
        $object->setNew();
173
        $object->setVar('plugin_listener', $listener);
174
        $object->setVar('plugin_caller', $caller);
175
        $object->setVar('plugin_status', $status);
176
        $object->setVar('plugin_order', $order);
177
        return $this->insert($object, true);
178
    }
179
180
    /**
181
     * Updates the order value after a post request
182
     *
183
     * @param int $id
184
     * @param int $order
185
     */
186
    public function updateOrder(int $id, int $order)
187
    {
188
        $this->updateAll('plugin_order', $order, new Criteria('plugin_id', $id));
189
    }
190
191
    /**
192
     * Updates the status value after a post request
193
     *
194
     * @param int $id
195
     * @param int $status
196
     */
197
    public function updateStatus(int $id, int $status)
198
    {
199
        $this->updateAll('plugin_status', $status, new Criteria('plugin_id', $id));
200
    }
201
202
    /**
203
     * Get Listeners By Caller
204
     * Check if the module is active in case it was deactivated
205
     *
206
     * @param string $caller
207
     * @return array
208
     */
209
    public function getActiveListenersByCaller(string $caller)
210
    {
211
        $xoops = \Xoops::getInstance();
212
        $ret = array();
213
        $criteria = new CriteriaCompo();
214
        $criteria->add(new Criteria('plugin_caller', (string)$caller));
215
        $criteria->add(new Criteria('plugin_status', 1));
216
        $criteria->setSort('plugin_order');
217
        $criteria->setOrder('ASC');
218
        $plugins = $this->getAll($criteria, 'plugin_listener', false, false);
219
        foreach ($plugins as $plugin) {
220
            if ($xoops->isActiveModule($plugin['plugin_listener'])) {
221
                $ret[$plugin['plugin_listener']] = $plugin['plugin_listener'];
222
            }
223
        }
224
        return $ret;
225
226
    }
227
228
    /**
229
     * Deletes all entries by name
230
     * Useful when a module is deleted
231
     *
232
     * @param string $name
233
     * @return bool
234
     */
235
    public function deleteLC(string $name)
236
    {
237
        $criteria = new CriteriaCompo();
238
        $criteria->add(new Criteria('plugin_caller', (string)$name));
239
        $criteria->add(new Criteria('plugin_listener', (string)$name), 'OR');
240
        return $this->deleteAll($criteria, true);
241
    }
242
}
243