MOASElementsPlugin::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
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
    private $_elements;
21
    private $_elementSetMetadata;
22
23
    public function __construct()
24
    {
25
        parent::__construct();
26
27
        // Get the data
28
        include 'elements.php';
29
        $this->_elements = $elements;
30
        $this->_elementSetMetadata = $elementSetMetadata;
31
    }
32
33
    /**
34
     * Install the plugin.
35
     */
36
    public function hookInstall()
37
    {
38
        insert_element_set($this->_elementSetMetadata, $this->_elements);
39
        $this->_reorderElements();
40
    }
41
42
    /**
43
     * Uninstall the plugin.
44
     */
45
    public function hookUninstall()
46
    {
47
        /** @var Table_ElementSet $elementSetTable */
48
        $elementSetTable = $this->_db->getTable('ElementSet');
49
50
        // deleting ElementSet also deletes any attached elements.
51
        $est = $elementSetTable->findByName($this->_elementSetMetadata['name']);
52
        if ($est !== null) {
53
            $est->delete();
54
        }
55
    }
56
57
    /**
58
     * Display the uninstall message.
59
     */
60
    public function hookUninstallMessage()
61
    {
62
        echo __('%sWarning%s: This will remove all the %s elements added '
63
            . 'by this plugin and permanently delete all element texts entered in those '
64
            . 'fields.%s', '<p><strong>', '</strong>', $this->_elementSetMetadata['name'], '</p>');
65
    }
66
67
    public function hookUpgrade($oldVersion, $newVersion)
68
    {
69
        switch($oldVersion)
70
        {
71
            // let the plugin cascade its upgrades
72
            case '1.0.0':
73
                // code to upgrade from 1.0.0 to 1.1.0
74
            default :
75
                $this->_updateElementSet();
76
                $this->_updateElements();
77
                $this->_reorderElements();
78
        }
79
    }
80
81
    /**
82
     * Adds any elements that have been defined but are not present on the site.
83
     */
84
    private function _updateElements($remove = false)
85
    {
86
        $elementSet = $this->_getElementSet();
87
        $elements = $elementSet->getElements();
88
89
        // walk the MOAS element array and add where needed.
90
        array_walk($this->_elements, function($element, $index) use (&$elementSet, $elements) {
91
            foreach ($elements as $dbElement) {
92
                if ($element['name'] === $dbElement->name) {
93
                    return;
94
                }
95
            }
96
97
            // if we get here it means we didn't find this element in the database.
98
            $elementSet->addElements(array($index => $element));
99
        });
100
101
        $elementSet->save();
102
    }
103
104
    /**
105
     * Updates the element set information - record type and description only
106
     */
107
    private function _updateElementSet()
108
    {
109
        $elementSet = $this->_getElementSet();
110
111
        $elementSet->record_type = $this->_elementSetMetadata['record_type'];
112
        $elementSet->description = $this->_elementSetMetadata['description'];
113
114
        $elementSet->save();
115
    }
116
117
118
    /**
119
     * Updates the elements ordering within the element set
120
     */
121
    private function _reorderElements()
122
    {
123
        $elementSet = $this->_getElementSet();
124
125
        $this->_db->beginTransaction();
126
127
        try {
128
            // Remove the existing element ordering
129
            $this->_db->update(
130
                $this->_db->Element,
131
                array('order' => null),
132
                array('element_set_id = ?' => $elementSet->id)
133
            );
134
135
            $elements = $elementSet->getElements();
136
            // walk the MOAS element array and add where needed.
137
            array_walk($this->_elements, function ($element) use ($elements) {
138
                /** @var Element $dbElement */
139
                foreach ($elements as $dbElement) {
140
                    if ($element['name'] === $dbElement->name) {
141
                        $dbElement->setOrder($element['order']);
142
                        $dbElement->save();
143
                    }
144
                }
145
            });
146
            $this->_db->commit();
147
        } catch (Exception $e) {
148
            $this->_db->rollBack();
149
        }
150
    }
151
152
    /**
153
     * Get the MOAS element set
154
     *
155
     * @return ElementSet
156
     */
157
    private function _getElementSet()
158
    {
159
        $elementSetTable = $this->_db->getTable('ElementSet');
160
        return $elementSetTable->findByName($this->_elementSetMetadata['name']);
161
    }
162
}
163