| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,131 @@ |
| 1 |
+<?php |
|
| 2 |
+ |
|
| 3 |
+ |
|
| 4 |
+namespace iq2\CEAutoWrapperCloseBundle\Resources\contao\classes; |
|
| 5 |
+ |
|
| 6 |
+ |
|
| 7 |
+use Contao\Database; |
|
| 8 |
+use Contao\Input; |
|
| 9 |
+use DC_Table; |
|
| 10 |
+use MadeYourDay\RockSolidCustomElements\CustomElements; |
|
| 11 |
+use function json_encode; |
|
| 12 |
+ |
|
| 13 |
+ |
|
| 14 |
+class RSCustomElementHelper |
|
| 15 |
+{
|
|
| 16 |
+ |
|
| 17 |
+ /** |
|
| 18 |
+ * Automatically creates a wrapper-stop element |
|
| 19 |
+ * |
|
| 20 |
+ * @param $objDC |
|
| 21 |
+ */ |
|
| 22 |
+ public function createWrapperStop($objDC) |
|
| 23 |
+ {
|
|
| 24 |
+ //get active record |
|
| 25 |
+ if (!$objDC->activeRecord) {
|
|
| 26 |
+ $objDC->activeRecord = $this->getActiveRecord($objDC->table, $objDC->id); |
|
| 27 |
+ } |
|
| 28 |
+ |
|
| 29 |
+ //check if custom element has wrapper config |
|
| 30 |
+ $type = $objDC->activeRecord->type; |
|
| 31 |
+ $config = $this->getWrapperStartConfig($type); |
|
| 32 |
+ if ($config) {
|
|
| 33 |
+ $wrapperClose = $config['wrapperClose']; |
|
| 34 |
+ |
|
| 35 |
+ //create wrapper stop if entry is new |
|
| 36 |
+ if ($objDC->activeRecord->rsce_data == null) {
|
|
| 37 |
+ $this->createRsceDatabaseEntry($objDC, $wrapperClose); |
|
| 38 |
+ } |
|
| 39 |
+ } |
|
| 40 |
+ } |
|
| 41 |
+ |
|
| 42 |
+ /** |
|
| 43 |
+ * Creates a wrapper-stop element at copy-action of a single wrapper-start element |
|
| 44 |
+ * |
|
| 45 |
+ * @param $id |
|
| 46 |
+ * @param $objDC |
|
| 47 |
+ */ |
|
| 48 |
+ public function createWrapperStopOnCopy($id, DC_Table $objDC) |
|
| 49 |
+ {
|
|
| 50 |
+ if (Input::get('act') == "copy") {
|
|
| 51 |
+ //get active record of copied entry |
|
| 52 |
+ $objDC->activeRecord = $this->getActiveRecord($objDC->table, $id); |
|
| 53 |
+ //check if custom element has wrapper config |
|
| 54 |
+ $type = $objDC->activeRecord->type; |
|
| 55 |
+ $config = $this->getWrapperStartConfig($type); |
|
| 56 |
+ if ($config) {
|
|
| 57 |
+ $wrapperClose = $config['wrapperClose']; |
|
| 58 |
+ |
|
| 59 |
+ //create wrapper stop |
|
| 60 |
+ $this->createRsceDatabaseEntry($objDC, $wrapperClose); |
|
| 61 |
+ } |
|
| 62 |
+ } |
|
| 63 |
+ } |
|
| 64 |
+ |
|
| 65 |
+ /** |
|
| 66 |
+ * Returns the database entry of the active record |
|
| 67 |
+ * |
|
| 68 |
+ * @param $table |
|
| 69 |
+ * @param $id |
|
| 70 |
+ * @return Database\Result |
|
| 71 |
+ */ |
|
| 72 |
+ private function getActiveRecord($table, $id) |
|
| 73 |
+ {
|
|
| 74 |
+ return Database::getInstance() |
|
| 75 |
+ ->prepare("SELECT * FROM " . $table . " WHERE id=?")
|
|
| 76 |
+ ->limit(1)->execute($id); |
|
| 77 |
+ } |
|
| 78 |
+ |
|
| 79 |
+ /** |
|
| 80 |
+ * Gets the config of a start wrapper, if it contains a wrapperClose definition |
|
| 81 |
+ * |
|
| 82 |
+ * @param $type |
|
| 83 |
+ * @return array|bool|null |
|
| 84 |
+ */ |
|
| 85 |
+ private function getWrapperStartConfig($type) |
|
| 86 |
+ {
|
|
| 87 |
+ //check if element is rocksolid custom element |
|
| 88 |
+ if (preg_match("/^rsce_/", $type)) {
|
|
| 89 |
+ $config = CustomElements::getConfigByType($type); |
|
| 90 |
+ $wrapperType = $config['wrapper']; |
|
| 91 |
+ if ($wrapperType['type'] == 'start' && isset($config['wrapperClose'])) {
|
|
| 92 |
+ return $config; |
|
| 93 |
+ } |
|
| 94 |
+ } |
|
| 95 |
+ return false; |
|
| 96 |
+ } |
|
| 97 |
+ |
|
| 98 |
+ /** |
|
| 99 |
+ * Creates a new database entry after the newly created element |
|
| 100 |
+ * |
|
| 101 |
+ * @param $objDC |
|
| 102 |
+ * @param $type |
|
| 103 |
+ * @param string $rsceData |
|
| 104 |
+ * @return Database\Result |
|
| 105 |
+ */ |
|
| 106 |
+ private function createRsceDatabaseEntry($objDC, $type, $rsceData = "") |
|
| 107 |
+ {
|
|
| 108 |
+ $objDB = Database::getInstance(); |
|
| 109 |
+ |
|
| 110 |
+ //set sorting to value between current and next entry |
|
| 111 |
+ $startWrapperSorting = $objDC->activeRecord->sorting; |
|
| 112 |
+ $nextSorting = $objDB->prepare('SELECT sorting FROM tl_content WHERE sorting > ? AND pid = ? AND ptable = ? ORDER BY sorting LIMIT 1')
|
|
| 113 |
+ ->execute($startWrapperSorting, $objDC->activeRecord->pid, $objDC->activeRecord->ptable)->fetchRow()[0]; |
|
| 114 |
+ $newSorting = (($nextSorting - $startWrapperSorting) / 2 + $startWrapperSorting); |
|
| 115 |
+ |
|
| 116 |
+ //create database entry |
|
| 117 |
+ return Database::getInstance() |
|
| 118 |
+ ->prepare( |
|
| 119 |
+ "INSERT INTO " . $objDC->table . " (pid, ptable, tstamp, type, sorting, invisible, rsce_data) VALUES (?,?,?,'$type',?,?,?)" |
|
| 120 |
+ ) |
|
| 121 |
+ ->execute( |
|
| 122 |
+ $objDC->activeRecord->pid, |
|
| 123 |
+ $objDC->activeRecord->ptable, |
|
| 124 |
+ time(), |
|
| 125 |
+ $newSorting, |
|
| 126 |
+ $objDC->activeRecord->invisible, |
|
| 127 |
+ json_encode($rsceData) |
|
| 128 |
+ ); |
|
| 129 |
+ |
|
| 130 |
+ } |
|
| 131 |
+} |
|
| 0 | 132 |
\ No newline at end of file |