Completed
Pull Request — develop (#2)
by James
02:51
created

MOASElementsPlugin.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * @package     omeka
5
 * @subpackage  moas-elements
6
 * @copyright   2015 University of Nottingham
7
 * @license     MIT
8
 * @author      Adam Cooper <[email protected]>
9
 */
10
11
class MOASElementsPlugin extends Omeka_Plugin_AbstractPlugin
12
{
13
    protected $_hooks = array(
14
        'install',
15
        'uninstall',
16
        'uninstall_message',
17
        'upgrade'
18
    );
19
20
    public function __construct()
21
    {
22
        parent::__construct();
23
24
        // Get the data
25
        include 'elements.php';
26
        $this->_elements = $elements;
1 ignored issue
show
The variable $elements does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
27
        $this->_elementSetMetadata = $elementSetMetadata;
1 ignored issue
show
The variable $elementSetMetadata does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
28
    }
29
30
    /**
31
     * Install the plugin.
32
     */
33
    public function hookInstall()
34
    {
35
        insert_element_set($this->_elementSetMetadata, $this->_elements);
36
    }
37
38
    /**
39
     * Uninstall the plugin.
40
     */
41
    public function hookUninstall()
42
    {
43
        /** @var Table_ElementSet $elementSetTable */
44
        $elementSetTable = $this->_db->getTable('ElementSet');
45
46
        // deleting ElementSet also deletes any attached elements.
47
        $est = $elementSetTable->findByName($this->_elementSetMetadata['name']);
48
        if ($est !== null) {
49
            $est->delete();
50
        }
51
    }
52
53
    /**
54
     * Display the uninstall message.
55
     */
56
    public function hookUninstallMessage()
57
    {
58
        echo __('%sWarning%s: This will remove all the %s elements added '
59
            . 'by this plugin and permanently delete all element texts entered in those '
60
            . 'fields.%s', '<p><strong>', '</strong>', $this->_elementSetMetadata['name'], '</p>');
61
    }
62
63
    public function hookUpgrade($oldVersion, $newVersion)
64
    {
65
        switch($oldVersion)
66
        {
67
            // let the plugin cascade its upgrades
68
            case '1.0.0':
69
                // code to upgrade from 1.0.0 to 1.1.0
70
            default :
71
                $this->_updateElements();
72
        }
73
    }
74
75
    /**
76
     * Adds any elements that have been defined but are not present on the site.
77
     */
78
    private function _updateElements($remove = false)
79
    {
80
        // get database records.
81
        /** @var Table_ElementSet $elementSetTable */
82
        $elementSetTable = $this->_db->getTable('ElementSet');
83
        /** @var ElementSet $elementSet */
84
        $elementSet = $elementSetTable->findByName($this->_elementSetMetadata['name']);
85
        $elements = $elementSet->getElements();
86
87
        // walk the MOAS element array and add where needed.
88
        array_walk($this->_elements, function($element, $index) use (&$elementSet, $elements) {
89
            foreach ($elements as $dbElement) {
90
                if ($element['name'] === $dbElement->name) {
91
                    return;
92
                }
93
            }
94
95
            // if we get here it means we didn't find this element in the database.
96
            $elementSet->addElements(array($index => $element));
97
        });
98
99
        $elementSet->save();
100
    }
101
}
102