Browse code

Add possibility to define default layout for new articles

Benjamin Roth authored on10/01/2017 13:37:54
Showing6 changed files
... ...
@@ -14,7 +14,7 @@
14 14
  */
15 15
 
16 16
 $GLOBALS['TL_DCA']['tl_article']['config']['onload_callback'][] = array('tl_article_eSM_artLayout','registerSubPalettes');
17
-$GLOBALS['TL_DCA']['tl_article']['config']['onload_callback'][] = array('tl_article_eSM_artLayout','registerSubPalettes');
17
+$GLOBALS['TL_DCA']['tl_article']['config']['onload_callback'][] = array('tl_article_eSM_artLayout','setDefaultLayout');
18 18
 
19 19
 /**
20 20
  * List
... ...
@@ -41,7 +41,7 @@ $GLOBALS['TL_DCA']['tl_article']['fields']['es_type'] = array
41 41
   'exclude'                 => true,
42 42
   'inputType'               => 'select',
43 43
   'options_callback'        => array('tl_article_eSM_artLayout','getArticleLayouts'),
44
-  'eval'                    => array('chosen'=>true, 'tl_class'=>'w50', 'includeBlankOption'=>true, 'submitOnChange'=>true),
44
+  'eval'                    => array('chosen'=>true, 'tl_class'=>'w50', 'includeBlankOption'=>true, 'submitOnChange'=>true, 'alwaysSave'=>true),
45 45
   'sql'                     => "int(10) unsigned NOT NULL default '0'",
46 46
   'relation'                => array
47 47
   (
... ...
@@ -148,4 +148,32 @@ class tl_article_eSM_artLayout extends \Backend
148 148
 
149 149
     return '<a href="contao/main.php?do=feRedirect&amp;page='.$row['pid'].'&amp;article='.(($row['alias'] != '' && !Config::get('disableAlias')) ? $row['alias'] : $row['id']).'" title="'.specialchars($GLOBALS['TL_LANG']['MSC']['view']).'" target="_blank">'.Image::getHtml('articles'.($published ? '' : '_').'.gif', '', 'data-icon="articles.gif" data-icon-disabled="articles_.gif"').'</a> '.$strLabel;
150 150
   }
151
+
152
+  /**
153
+   * Sets default layout if needed
154
+   *
155
+   * @param DataContainer $dc
156
+   */
157
+  public function setDefaultLayout(\DataContainer $dc)
158
+  {
159
+    if ($_GET['act'] == 'edit')
160
+    {
161
+      $ArticleModel = \ArticleModel::findByPk($dc->id);
162
+
163
+      if ($ArticleModel !== null)
164
+      {
165
+        // No change or empty value
166
+        if (!$ArticleModel->tstamp && !$ArticleModel->es_type)
167
+        {
168
+          $ArticleDefaultLayout = \Database::getInstance()->execute("SELECT al.id FROM tl_article_layouts al, tl_theme t WHERE t.id = al.pid AND isDefault = '1' ORDER BY t.name, al.sorting LIMIT 1");
169
+
170
+          if ($ArticleDefaultLayout->numRows)
171
+          {
172
+            $ArticleModel->es_type = $ArticleDefaultLayout->id;
173
+            $ArticleModel->save();
174
+          }
175
+        }
176
+      }
177
+    }
178
+  }
151 179
 }
... ...
@@ -100,7 +100,7 @@ $GLOBALS['TL_DCA']['tl_article_layouts'] = array
100 100
 	'palettes' => array
101 101
 	(
102 102
 		'__selector__'              => array('showBackgroundImage','restrictContentElements'),
103
-		'default'                   => '{title_legend},title,alias;{content_legend},maxWidth,center,useHelper;{image_legend},showBackgroundImage;{expert_legend:hide},restrictContentElements'
103
+		'default'                   => '{title_legend},title,alias;{content_legend},maxWidth,center,useHelper;{image_legend},showBackgroundImage;{expert_legend:hide},restrictContentElements,isDefault'
104 104
 	),
105 105
 
106 106
 	// Subpalettes
... ...
@@ -221,6 +221,18 @@ $GLOBALS['TL_DCA']['tl_article_layouts'] = array
221 221
 			'eval'                    => array('multiple'=>true,'mandatory'=>true),
222 222
 			'sql'                     => "blob NULL"
223 223
 		),
224
+    'isDefault' => array
225
+    (
226
+      'label'                   => &$GLOBALS['TL_LANG']['tl_article_layouts']['isDefault'],
227
+      'exclude'                 => true,
228
+      'inputType'               => 'checkbox',
229
+      'eval'                    => array('doNotCopy'=>true, 'tl_class'=>'clr'),
230
+      'save_callback' => array
231
+      (
232
+        array('tl_article_layouts', 'checkDefault')
233
+      ),
234
+      'sql'                     => "char(1) NOT NULL default ''"
235
+    ),
224 236
 	)
225 237
 );
226 238
 
... ...
@@ -308,7 +320,7 @@ class tl_article_layouts extends \Backend
308 320
 	 */
309 321
 	public function listLayout($row)
310 322
 	{
311
-		return '<div style="float:left">'. $row['title'] .' <span style="color:#b3b3b3;">['.$row['alias']."]</span></div>\n";
323
+		return '<div style="float:left;'.($row['isDefault'] ? 'font-weight: bold; font-style: italic;' : '').'">'. $row['title'] .' <span style="color:#b3b3b3;">['.$row['alias']."]</span></div>\n";
312 324
 	}
313 325
 
314 326
 	/**
... ...
@@ -331,4 +343,32 @@ class tl_article_layouts extends \Backend
331 343
 		return $groups;
332 344
 	}
333 345
 
346
+  /**
347
+   * Make sure there is only one default per pid
348
+   *
349
+   * @param mixed         $varValue
350
+   * @param DataContainer $dc
351
+   *
352
+   * @return mixed
353
+   *
354
+   * @throws Exception
355
+   */
356
+  public function checkDefault($varValue, DataContainer $dc)
357
+  {
358
+    if ($varValue == '')
359
+    {
360
+      return '';
361
+    }
362
+
363
+    $objPage = $this->Database->prepare("SELECT id FROM tl_article_layouts WHERE `isDefault`=1 AND pid=? AND id!=?")
364
+      ->execute($dc->activeRecord->pid, $dc->activeRecord->id);
365
+
366
+    if ($objPage->numRows)
367
+    {
368
+      throw new Exception($GLOBALS['TL_LANG']['ERR']['multipleArticleLayoutDefaults']);
369
+    }
370
+
371
+    return $varValue;
372
+  }
373
+
334 374
 }
335 375
new file mode 100644
... ...
@@ -0,0 +1,15 @@
1
+<?php
2
+
3
+/**
4
+ * ArtLayout for Contao
5
+ *
6
+ * Copyright (c) 2016 Benjamin Roth
7
+ *
8
+ * @link    http://www.esales-media.de
9
+ * @license commercial
10
+ */
11
+
12
+/**
13
+ * Errors
14
+ */
15
+$GLOBALS['TL_LANG']['ERR']['multipleArticleLayoutDefaults'] = 'Sie können nur ein Layout pro Theme als Standard definieren.';
0 16
\ No newline at end of file
... ...
@@ -32,6 +32,8 @@ $GLOBALS['TL_LANG']['tl_article_layouts']['restrictContentElements'][0] = 'Artik
32 32
 $GLOBALS['TL_LANG']['tl_article_layouts']['restrictContentElements'][1] = 'Erlaubt es, die im Artikel zur Verfügung stehenden Content-Elemente einzuschränken.';
33 33
 $GLOBALS['TL_LANG']['tl_article_layouts']['allowedElements'][0] = 'Erlaubte Content Elemente';
34 34
 $GLOBALS['TL_LANG']['tl_article_layouts']['allowedElements'][1] = 'Nur die ausgewählten Content-Elemente stehen im Artikel zur Verfügung.';
35
+$GLOBALS['TL_LANG']['tl_article_layouts']['isDefault'][0] = 'Standard';
36
+$GLOBALS['TL_LANG']['tl_article_layouts']['isDefault'][1] = 'Das Layout wird als Standard-Layout bei neuen Artikeln gesetzt.';
35 37
 
36 38
 /**
37 39
  * Legends
38 40
new file mode 100644
... ...
@@ -0,0 +1,15 @@
1
+<?php
2
+
3
+/**
4
+ * ArtLayout for Contao
5
+ *
6
+ * Copyright (c) 2016 Benjamin Roth
7
+ *
8
+ * @link    http://www.esales-media.de
9
+ * @license commercial
10
+ */
11
+
12
+/**
13
+ * Errors
14
+ */
15
+$GLOBALS['TL_LANG']['ERR']['multipleArticleLayoutDefaults'] = 'You can define a layout as default only once per theme.';
0 16
\ No newline at end of file
... ...
@@ -32,6 +32,8 @@ $GLOBALS['TL_LANG']['tl_article_layouts']['restrictContentElements'][0] = 'Restr
32 32
 $GLOBALS['TL_LANG']['tl_article_layouts']['restrictContentElements'][1] = 'Allows to restrict the available content elements in the article.';
33 33
 $GLOBALS['TL_LANG']['tl_article_layouts']['allowedElements'][0] = 'Allowed content elements';
34 34
 $GLOBALS['TL_LANG']['tl_article_layouts']['allowedElements'][1] = 'Only selected content elements are available in the article.';
35
+$GLOBALS['TL_LANG']['tl_article_layouts']['isDefault'][0] = 'Default';
36
+$GLOBALS['TL_LANG']['tl_article_layouts']['isDefault'][1] = 'The layout will be set as default upon new article creation.';
35 37
 
36 38
 /**
37 39
  * Legends