Install::insertDefaultData()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
eloc 0
nc 1
nop 0
1
<?php
2
/**
3
 * Craft Contact Form Extensions plugin for Craft CMS 3.x.
4
 *
5
 * Adds extensions to the Craft CMS contact form plugin.
6
 *
7
 * @link      https://rias.be
8
 *
9
 * @copyright Copyright (c) 2018 Rias
10
 */
11
12
namespace rias\contactformextensions\migrations;
13
14
use Craft;
15
use craft\db\Migration;
16
17
/**
18
 * Craft Contact Form Extensions Install Migration.
19
 *
20
 * If your plugin needs to create any custom database tables when it gets installed,
21
 * create a migrations/ folder within your plugin folder, and save an Install.php file
22
 * within it using the following template:
23
 *
24
 * If you need to perform any additional actions on install/uninstall, override the
25
 * safeUp() and safeDown() methods.
26
 *
27
 * @author    Rias
28
 *
29
 * @since     1.0.0
30
 */
31
class Install extends Migration
32
{
33
    // Public Properties
34
    // =========================================================================
35
36
    /**
37
     * @var string The database driver to use
38
     */
39
    public $driver;
40
41
    // Public Methods
42
    // =========================================================================
43
44
    /**
45
     * This method contains the logic to be executed when applying this migration.
46
     * This method differs from [[up()]] in that the DB logic implemented here will
47
     * be enclosed within a DB transaction.
48
     * Child classes may implement this method instead of [[up()]] if the DB logic
49
     * needs to be within a transaction.
50
     *
51
     * @return bool return a false value to indicate the migration fails
52
     *              and should not proceed further. All other return values mean the migration succeeds.
53
     */
54
    public function safeUp()
55
    {
56
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
57
        if ($this->createTables()) {
58
            $this->addForeignKeys();
59
            // Refresh the db schema caches
60
            Craft::$app->db->schema->refresh();
61
            $this->insertDefaultData();
62
        }
63
64
        return true;
65
    }
66
67
    /**
68
     * This method contains the logic to be executed when removing this migration.
69
     * This method differs from [[down()]] in that the DB logic implemented here will
70
     * be enclosed within a DB transaction.
71
     * Child classes may implement this method instead of [[down()]] if the DB logic
72
     * needs to be within a transaction.
73
     *
74
     * @return bool return a false value to indicate the migration fails
75
     *              and should not proceed further. All other return values mean the migration succeeds.
76
     */
77
    public function safeDown()
78
    {
79
        $this->driver = Craft::$app->getConfig()->getDb()->driver;
80
        $this->removeTables();
81
82
        return true;
83
    }
84
85
    // Protected Methods
86
    // =========================================================================
87
88
    /**
89
     * Creates the tables needed for the Records used by the plugin.
90
     *
91
     * @return bool
92
     */
93
    protected function createTables()
94
    {
95
        $tablesCreated = false;
96
97
        // contactform_submissions table
98
        $tableSchema = Craft::$app->db->schema->getTableSchema('{{%contactform_submissions}}');
99
        if ($tableSchema === null) {
100
            $tablesCreated = true;
101
            $this->createTable(
102
                '{{%contactform_submissions}}',
103
                [
104
                    'id'          => $this->integer()->notNull(),
105
                    'form'        => $this->string()->null(),
106
                    'subject'     => $this->string()->null(),
107
                    'fromName'    => $this->string()->null(),
108
                    'fromEmail'   => $this->string()->null(),
109
                    'message'     => $this->text()->notNull(),
110
                    'dateCreated' => $this->dateTime()->notNull(),
111
                    'dateUpdated' => $this->dateTime()->notNull(),
112
                    'uid'         => $this->uid(),
113
                    'PRIMARY KEY(id)',
114
                ]
115
            );
116
        }
117
118
        return $tablesCreated;
119
    }
120
121
    /**
122
     * Creates the foreign keys needed for the Records used by the plugin.
123
     *
124
     * @return void
125
     */
126
    protected function addForeignKeys()
127
    {
128
        // contactform_submissions table
129
        $this->addForeignKey(
130
            $this->db->getForeignKeyName('{{%contactform_submissions}}', 'id'),
131
            '{{%contactform_submissions}}',
132
            'id',
133
            '{{%elements}}',
134
            'id',
135
            'CASCADE',
136
            null
137
        );
138
    }
139
140
    /**
141
     * Populates the DB with the default data.
142
     *
143
     * @return void
144
     */
145
    protected function insertDefaultData()
146
    {
147
    }
148
149
    /**
150
     * Removes the tables needed for the Records used by the plugin.
151
     *
152
     * @return void
153
     */
154
    protected function removeTables()
155
    {
156
        // contactform_submissions table
157
        $this->dropTableIfExists('{{%contactform_submissions}}');
158
    }
159
}
160