Completed
Push — develop ( a071ba...23abb0 )
by James
9s
created

MOASElementsPlugin::_updateElementSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
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;
27
        $this->_elementSetMetadata = $elementSetMetadata;
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->_updateElementSet();
72
                $this->_updateElements();
73
        }
74
    }
75
76
    /**
77
     * Adds any elements that have been defined but are not present on the site.
78
     */
79
    private function _updateElements($remove = false)
80
    {
81
        // get database records.
82
        /** @var Table_ElementSet $elementSetTable */
83
        $elementSetTable = $this->_db->getTable('ElementSet');
84
        /** @var ElementSet $elementSet */
85
        $elementSet = $elementSetTable->findByName($this->_elementSetMetadata['name']);
86
        $elements = $elementSet->getElements();
87
88
        // walk the MOAS element array and add where needed.
89
        array_walk($this->_elements, function($element, $index) use (&$elementSet, $elements) {
90
            foreach ($elements as $dbElement) {
91
                if ($element['name'] === $dbElement->name) {
92
                    return;
93
                }
94
            }
95
96
            // if we get here it means we didn't find this element in the database.
97
            $elementSet->addElements(array($index => $element));
98
        });
99
100
        $elementSet->save();
101
    }
102
103
    /**
104
     * Updates the element information - record type and description only
105
     */
106
    private function _updateElementSet()
107
    {
108
        // get database records.
109
        /** @var Table_ElementSet $elementSetTable */
110
        $elementSetTable = $this->_db->getTable('ElementSet');
111
        /** @var ElementSet $elementSet */
112
        $elementSet = $elementSetTable->findByName($this->_elementSetMetadata['name']);
113
114
        $elementSet->record_type = $this->_elementSetMetadata['record_type'];
115
        $elementSet->description = $this->_elementSetMetadata['description'];
116
117
        $elementSet->save();
118
    }
119
}
120