|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Cross Copy plugin for Craft CMS 3.x |
| 4 | + * |
| 5 | + * Copy a matrix from one project to another |
| 6 | + * |
| 7 | + * @link michael@ipopdigital.com |
| 8 | + * @copyright Copyright (c) 2019 Michael Butler |
| 9 | + */ |
| 10 | + |
| 11 | +namespace crosscopy\crosscopy; |
| 12 | + |
| 13 | +use crosscopy\crosscopy\services\CrossCopyService as CrossCopyServiceService; |
| 14 | +use crosscopy\crosscopy\models\Settings; |
| 15 | + |
| 16 | +use Craft; |
| 17 | +use craft\base\Plugin; |
| 18 | +use craft\services\Plugins; |
| 19 | +use craft\events\PluginEvent; |
| 20 | +use craft\web\UrlManager; |
| 21 | +use craft\events\RegisterUrlRulesEvent; |
| 22 | + |
| 23 | +use yii\base\Event; |
| 24 | + |
| 25 | +/** |
| 26 | + * Craft plugins are very much like little applications in and of themselves. We’ve made |
| 27 | + * it as simple as we can, but the training wheels are off. A little prior knowledge is |
| 28 | + * going to be required to write a plugin. |
| 29 | + * |
| 30 | + * For the purposes of the plugin docs, we’re going to assume that you know PHP and SQL, |
| 31 | + * as well as some semi-advanced concepts like object-oriented programming and PHP namespaces. |
| 32 | + * |
| 33 | + * https://craftcms.com/docs/plugins/introduction |
| 34 | + * |
| 35 | + * @author Michael Butler |
| 36 | + * @package CrossCopy |
| 37 | + * @since 1.0.0 |
| 38 | + * |
| 39 | + * @property CrossCopyServiceService $crossCopyService |
| 40 | + * @property Settings $settings |
| 41 | + * @method Settings getSettings() |
| 42 | + */ |
| 43 | +class CrossCopy extends Plugin |
| 44 | +{ |
| 45 | + // Static Properties |
| 46 | + // ========================================================================= |
| 47 | + |
| 48 | + /** |
| 49 | + * Static property that is an instance of this plugin class so that it can be accessed via |
| 50 | + * CrossCopy::$plugin |
| 51 | + * |
| 52 | + * @var CrossCopy |
| 53 | + */ |
| 54 | + public static $plugin; |
| 55 | + |
| 56 | + // Public Properties |
| 57 | + // ========================================================================= |
| 58 | + |
| 59 | + /** |
| 60 | + * To execute your plugin’s migrations, you’ll need to increase its schema version. |
| 61 | + * |
| 62 | + * @var string |
| 63 | + */ |
| 64 | + public $schemaVersion = '1.0.0'; |
| 65 | + |
| 66 | + // Public Methods |
| 67 | + // ========================================================================= |
| 68 | + |
| 69 | + /** |
| 70 | + * Set our $plugin static property to this class so that it can be accessed via |
| 71 | + * CrossCopy::$plugin |
| 72 | + * |
| 73 | + * Called after the plugin class is instantiated; do any one-time initialization |
| 74 | + * here such as hooks and events. |
| 75 | + * |
| 76 | + * If you have a '/vendor/autoload.php' file, it will be loaded for you automatically; |
| 77 | + * you do not need to load it in your init() method. |
| 78 | + * |
| 79 | + */ |
| 80 | + public function init() |
| 81 | + { |
| 82 | + parent::init(); |
| 83 | + self::$plugin = $this; |
| 84 | + |
| 85 | + // Register our site routes |
| 86 | + Event::on( |
| 87 | + UrlManager::class, |
| 88 | + UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
| 89 | + function (RegisterUrlRulesEvent $event) { |
| 90 | + $event->rules['siteActionTrigger1'] = 'cross-copy/default'; |
| 91 | + } |
| 92 | + ); |
| 93 | + |
| 94 | + // Register our CP routes |
| 95 | + Event::on( |
| 96 | + UrlManager::class, |
| 97 | + UrlManager::EVENT_REGISTER_CP_URL_RULES, |
| 98 | + function (RegisterUrlRulesEvent $event) { |
| 99 | + $event->rules['cpActionTrigger1'] = 'cross-copy/default/do-something'; |
| 100 | + } |
| 101 | + ); |
| 102 | + |
| 103 | + // Do something after we're installed |
| 104 | + Event::on( |
| 105 | + Plugins::class, |
| 106 | + Plugins::EVENT_AFTER_INSTALL_PLUGIN, |
| 107 | + function (PluginEvent $event) { |
| 108 | + if ($event->plugin === $this) { |
| 109 | + // We were just installed |
| 110 | + } |
| 111 | + } |
| 112 | + ); |
| 113 | + |
| 114 | +/** |
| 115 | + * Logging in Craft involves using one of the following methods: |
| 116 | + * |
| 117 | + * Craft::trace(): record a message to trace how a piece of code runs. This is mainly for development use. |
| 118 | + * Craft::info(): record a message that conveys some useful information. |
| 119 | + * Craft::warning(): record a warning message that indicates something unexpected has happened. |
| 120 | + * Craft::error(): record a fatal error that should be investigated as soon as possible. |
| 121 | + * |
| 122 | + * Unless `devMode` is on, only Craft::warning() & Craft::error() will log to `craft/storage/logs/web.log` |
| 123 | + * |
| 124 | + * It's recommended that you pass in the magic constant `__METHOD__` as the second parameter, which sets |
| 125 | + * the category to the method (prefixed with the fully qualified class name) where the constant appears. |
| 126 | + * |
| 127 | + * To enable the Yii debug toolbar, go to your user account in the AdminCP and check the |
| 128 | + * [] Show the debug toolbar on the front end & [] Show the debug toolbar on the Control Panel |
| 129 | + * |
| 130 | + * http://www.yiiframework.com/doc-2.0/guide-runtime-logging.html |
| 131 | + */ |
| 132 | + Craft::info( |
| 133 | + Craft::t( |
| 134 | + 'cross-copy', |
| 135 | + '{name} plugin loaded', |
| 136 | + ['name' => $this->name] |
| 137 | + ), |
| 138 | + __METHOD__ |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + // Protected Methods |
| 143 | + // ========================================================================= |
| 144 | + |
| 145 | + /** |
| 146 | + * Creates and returns the model used to store the plugin’s settings. |
| 147 | + * |
| 148 | + * @return \craft\base\Model|null |
| 149 | + */ |
| 150 | + protected function createSettingsModel() |
| 151 | + { |
| 152 | + return new Settings(); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Returns the rendered settings HTML, which will be inserted into the content |
| 157 | + * block on the settings page. |
| 158 | + * |
| 159 | + * @return string The rendered settings HTML |
| 160 | + */ |
| 161 | + protected function settingsHtml(): string |
| 162 | + { |
| 163 | + return Craft::$app->view->renderTemplate( |
| 164 | + 'cross-copy/settings', |
| 165 | + [ |
| 166 | + 'settings' => $this->getSettings() |
| 167 | + ] |
| 168 | + ); |
| 169 | + } |
| 170 | +} |
0 commit comments