From 18dec173ad2bb4f5175195ddf60b325730d0988c Mon Sep 17 00:00:00 2001 From: Giel Berkers Date: Tue, 24 Apr 2012 17:06:12 +0200 Subject: [PATCH] update to 1.3 added link templates --- README.markdown | 8 +- content/content.pages.php | 137 ++++++++++------- extension.driver.php | 144 ++++++++++-------- extension.meta.xml | 1 + lib/ckeditor/plugins/link/dialogs/link.js | 8 +- text-formatters/formatter.ckeditor.php | 4 +- .../formatter.ckeditor_compact.php | 4 +- 7 files changed, 184 insertions(+), 122 deletions(-) diff --git a/README.markdown b/README.markdown index 6d8eee7..9dcb0b7 100755 --- a/README.markdown +++ b/README.markdown @@ -1,8 +1,8 @@ # CKEditor for Symphony CMS - * Version: 1.2.4 + * Version: 1.3 * CKEditor version: 3.6.3 - * Date: 19-04-2011 + * Date: 24-04-2011 * Authors: Tony Arnold , Giel Berkers * Repository: * Requirements: Symphony CMS 2.2 or higher @@ -53,7 +53,7 @@ This Symphony extension comes with a built-in file browser that is compatible wi ## Link templates -As of Symphony 2.3, it's possible to create link templates for CKEditor. This works as following: +As of version 1.3, it's possible to create link templates for CKEditor. This works as following: Say, you got a page called 'News', and a section called 'News articles' in which you store all your news articles. This means your news articles probably got URL's like: @@ -65,4 +65,4 @@ your news articles probably got URL's like: You might want to link to these pages from other pages, but you don't want to copy/paste those links. You just want them to be there in your dropdown of Symphony Pages when you select a link. This is where link templates come in: You can create a link template like: `/news/detail/{$id}/{$title}/`, choose a section the generate the list from, and select the page -where to show these links. You can set these in the preferences page of your Symphony installation. \ No newline at end of file +from where to show these links. You can set these in the preferences page of your Symphony installation. \ No newline at end of file diff --git a/content/content.pages.php b/content/content.pages.php index bcfce18..156f36e 100755 --- a/content/content.pages.php +++ b/content/content.pages.php @@ -14,16 +14,86 @@ function build() // The pages: $tree = array(); - $tree[] = array('name'=>__('Pages'), 'items'=>$this->buildTree()); + $tree[] = array('name'=>'', 'items'=>$this->buildTree()); + +/* echo '
';
+			print_r($tree);*/
+
+			echo json_encode($tree);
+			die();
+		}
+		
+		private function buildTree($parent = null, $indent = 0)
+		{
+			if($parent == null)
+			{
+				$results = Symphony::Database()->fetch('SELECT `id`, `title`, `handle`, `path` FROM `tbl_pages` WHERE `parent` IS NULL ORDER BY `sortorder` ASC;');
+			} else {
+				$results = Symphony::Database()->fetch('SELECT `id`, `title`, `handle`, `path` FROM `tbl_pages` WHERE `parent` = '.$parent.' ORDER BY `sortorder` ASC;');
+			}
+			$tree = array();
+			foreach($results as $result)
+			{
+				// Check if the page should be shown:
+				if(Symphony::Database()->fetchVar('count', 0,
+					sprintf('SELECT COUNT(*) AS `count` FROM `tbl_pages_types` WHERE `page_id` = %d AND `type` = \'ck_hide\';', $result['id'])) == 0)
+				{
+					$prefix = '';
+					$info = array('handle'=>$result['handle'], 'path'=>$result['path']);
+					if($result['path'] == null)
+					{
+						$info['url'] = '/'.$result['handle'].'/';
+						$info['title'] = $result['title'];
+					} else {
+						$info['url'] = '/'.$result['path'].'/'.$result['handle'].'/';
+						for($i = 0; $i < $indent; $i++)
+						{
+							$prefix .= ' '; // Please note: this might look like an empty space (nbsp) but it's an em space (emsp).
+							// This was necessary because   kept showing as plain text in the dropdown.
+						}
+
+						$info['title'] = $prefix.' › '.General::sanitize($result['title']);
+					}
+					$tree[] = $info;
+
+					// Check if there are templates for this page:
+					$tree = array_merge($tree, $this->checkTemplates($result['id'], $prefix.' ')); // also an emsp
+
+					// Get the children:
+					$children = $this->buildTree($result['id'], $indent + 1);
+
+					// Join arrays:
+					$tree = array_merge($tree, $children);
+				}
+			}
+			
+			return $tree;
+		}
+
+		private function checkTemplates($pageId, $prefix = '')
+		{
+			$new = version_compare(Administration::Configuration()->get('version', 'symphony'), '2.2.5', '>');
 
             // Link templates:
-            $templates = Symphony::Database()->fetch('SELECT * FROM `tbl_ckeditor_link_templates`;');
+            $templates = Symphony::Database()->fetch(
+				sprintf('SELECT * FROM `tbl_ckeditor_link_templates` WHERE `page_id` = %d;', $pageId)
+			);
+
+			$entryTree = array();
+
             foreach($templates as $template)
             {
-                $section = SectionManager::fetch($template['section_id']);
-                $entries = EntryManager::fetch(null, $template['section_id']);
+				if($new)
+				{
+					$section = SectionManager::fetch($template['section_id']);
+					$entries = EntryManager::fetch(null, $template['section_id']);
+				} else {
+					$sm = new SectionManager($this);
+					$em = new EntryManager(Administration::instance());
+					$section = $sm->fetch($template['section_id']);
+					$entries = $em->fetch(null, $template['section_id']);
+				}
                 $fields  = $section->fetchFields();
-                $entryTree = array();
                 foreach($entries as $entry)
                 {
                     $link    = $template['link'];
@@ -39,57 +109,20 @@ function build()
                         if(isset($testData['handle']))
                         {
                             $link = str_replace('{$'.$field->get('element_name').'}', $data[$field->get('id')]['handle'], $link);
-                            $entryTree[] = array(
-                                'handle' => $data[$field->get('id')]['handle'],
-                                'path' => '',
-                                'url' => $link,
-                                'title' => General::sanitize($data[$template['field_id']]['value'])
-                            );
                         }
                     }
+
+					$entryTree[] = array(
+						 'handle' => $data[$field->get('id')]['handle'],
+						 'path' => '',
+						 'url' => $link,
+						 'title' => $prefix.' › '.General::sanitize($data[$template['field_id']]['value'])
+					 );
+
                 }
-                $tree[] = array('name'=>__($section->get('name')), 'items'=>$entryTree);
+                // $tree[] = array('name'=>__($section->get('name')), 'items'=>$entryTree);
             }
 
-			echo json_encode($tree);
-			die();
-		}
-		
-		private function buildTree($parent = null, $indent = 0)
-		{
-			if($parent == null)
-			{
-				$results = Symphony::Database()->fetch('SELECT `id`, `title`, `handle`, `path` FROM `tbl_pages` WHERE `parent` IS NULL ORDER BY `sortorder` ASC;');
-			} else {
-				$results = Symphony::Database()->fetch('SELECT `id`, `title`, `handle`, `path` FROM `tbl_pages` WHERE `parent` = '.$parent.' ORDER BY `sortorder` ASC;');
-			}
-			$tree = array();
-			foreach($results as $result)
-			{
-				$info = array('handle'=>$result['handle'], 'path'=>$result['path']);
-				if($result['path'] == null)
-				{
-					$info['url'] = '/'.$result['handle'].'/';
-					$info['title'] = $result['title'];
-				} else {
-					$info['url'] = '/'.$result['path'].'/'.$result['handle'].'/';
-                    $prefix = '';
-                    for($i = 0; $i < $indent; $i++)
-                    {
-                        $prefix .= ' '; // Please note: this might look like an empty space (nbsp) but it's an em space (emsp).
-                        // This was necessary because   kept showing as plain text in the dropdown.
-                    }
-                    
-					$info['title'] = $prefix.' › '.General::sanitize($result['title']);
-				}
-				$tree[] = $info;
-				// Get the children:
-				$children = $this->buildTree($result['id'], $indent + 1);
-				// Join arrays:
-				$tree = array_merge($tree, $children);
-			}
-			
-			return $tree;
+			return $entryTree;
 		}
 	}
-?>
\ No newline at end of file
diff --git a/extension.driver.php b/extension.driver.php
index 7e30470..999127d 100755
--- a/extension.driver.php
+++ b/extension.driver.php
@@ -12,8 +12,8 @@
 		public function about() {
 			return array(
 				'name' => 'Text Formatter: CKEditor',
-				'version' => '1.2.4',
-				'release-date' => '2011-12-12',
+				'version' => '1.3',
+				'release-date' => '2012-04-24',
 				'author' => array(
 					'name'     => 'Tony Arnold, Giel Berkers'
 				),
@@ -62,7 +62,7 @@ public function appendPresets($context)
 			$data = Symphony::Configuration()->get('sections', 'ckeditor');
 			$checkedSections = $data != false ? explode(',', $data) : array();
 			
-			// Bugfix for if there are no sections found:
+			// If there are no sections found:
 			if($sections)
 			{
 				$options = array();
@@ -73,48 +73,63 @@ public function appendPresets($context)
 				$fieldset->appendChild(Widget::Select('ckeditor_sections[]', $options, array('multiple'=>'multiple')));
 			}
 
-            // Link templates for CKEditor, only for 2.3+:
+            // Link templates for CKEditor:
             if(version_compare(Administration::Configuration()->get('version', 'symphony'), '2.2.5', '>'))
             {
-                $sections = SectionManager::fetch();
-                $this->sections = array();
-                foreach($sections as $s)
-                {
-                    $a = array('id'=>$s->get('id'), 'name'=>$s->get('name'), 'fields'=>array());
-                    $fields = FieldManager::fetch(null, $s->get('id'));
-                    foreach($fields as $field)
-                    {
-                        // For now, only allow fields of the type 'input' to be used as a handle:
-                        if($field->get('type') == 'input')
-                        {
-                            $a['fields'][] = array('id'=>$field->get('id'), 'label'=>$field->get('label'), 'element_name'=>$field->get('element_name'));
-                        }
-                    }
-                    $this->sections[] = $a;
-                }
+				$sections = SectionManager::fetch();
+				$pages 	= PageManager::fetch();
+				$new   	= true;
+			} else {
+				$sm 	= new SectionManager($this);
+				$fm 	= new FieldManager(Administration::instance());
+				$sections = $sm->fetch();
+				$pages    = Symphony::Database()->fetch('SELECT * FROM `tbl_pages` ORDER BY `sortorder`;');
+				$new 	= false;
+			}
 
-                $fieldset->appendChild(new XMLElement('p', __('Link templates:')));
-                $ol = new XMLElement('ol');
-                $ol->setAttribute('id', 'fields-duplicator');
+			$this->sections = array();
+			foreach($sections as $s)
+			{
+				$a = array('id'=>$s->get('id'), 'name'=>$s->get('name'), 'fields'=>array());
+				if($new)
+				{
+					$fields = FieldManager::fetch(null, $s->get('id'));
+				} else {
+					$fields = $fm->fetch(null, $s->get('id'));
+				}
+				foreach($fields as $field)
+				{
+					// For now, only allow fields of the type 'input' to be used as a handle:
+					if($field->get('type') == 'input')
+					{
+						$a['fields'][] = array('id'=>$field->get('id'), 'label'=>$field->get('label'), 'element_name'=>$field->get('element_name'));
+					}
+				}
+				$this->sections[] = $a;
+			}
 
-                $pages = PageManager::fetch();
-                $templates = Symphony::Database()->fetch('SELECT * FROM `tbl_ckeditor_link_templates`;');
-                if(!is_array($pages)) $pages = array($pages);
+			$fieldset->appendChild(new XMLElement('p', __('Link templates:')));
+			$ol = new XMLElement('ol');
+			$ol->setAttribute('id', 'fields-duplicator');
+			$ol->setAttribute('class', 'ckeditor-templates');
 
-                foreach($pages as $page)
-                {
-                    foreach($templates as $template) {
-                        if($template['page_id'] != $page['id']) continue;
-                        $duplicator = $this->__buildDuplicatorItem($page, $template);
-                        $ol->appendChild($duplicator);
-                    }
+			$templates = Symphony::Database()->fetch('SELECT * FROM `tbl_ckeditor_link_templates`;');
+			if(!is_array($pages)) $pages = array($pages);
 
-                    $duplicator = $this->__buildDuplicatorItem($page, NULL);
-                    $ol->appendChild($duplicator);
-                }
+			foreach($pages as $page)
+			{
+				foreach($templates as $template) {
+					if($template['page_id'] != $page['id']) continue;
+					$duplicator = $this->__buildDuplicatorItem($page, $template);
+					$ol->appendChild($duplicator);
+				}
+
+				$duplicator = $this->__buildDuplicatorItem($page, NULL);
+				$ol->appendChild($duplicator);
+			}
+
+			$fieldset->appendChild($ol);
 
-                $fieldset->appendChild($ol);
-            }
 
 			$wrapper->appendChild($fieldset);
 
@@ -122,15 +137,22 @@ public function appendPresets($context)
             $wrapper->appendChild(new XMLElement('script', '
                 jQuery(function($){
                     var first = true;
-                    $("select[name^=ckeditor_link_templates][name$=\'[section_id]\']").change(function(){
-                        var label = $(":selected", this).text();
-                        $("optgroup, option", $(this).parent().next()).hide();
-                        $("optgroup[label=" + label + "], optgroup[label=" + label + "] option", $(this).parent().next()).show();
-                        if(!first)
-                        {
-                            $("option:first", $(this).parent().next()).show().attr("selected", "selected");
-                        }
-                    }).change();
+                    function bindFunctionality()
+                    {
+						$("select[name^=ckeditor_link_templates][name$=\'[section_id]\']").change(function(){
+							var label = $(":selected", this).text();
+							$("optgroup, option", $(this).parent().next()).hide();
+							$("optgroup[label=" + label + "], optgroup[label=" + label + "] option", $(this).parent().next()).show();
+							if(!first)
+							{
+								$("option:first", $(this).parent().next()).show().attr("selected", "selected");
+							}
+						}).change();
+                    }
+                    $("ol.ckeditor-templates a.constructor").click(function(){
+                		bindFunctionality();
+                    });
+                    bindFunctionality();
                     first = false;
                 });
             ', array('type'=>'text/javascript')));
@@ -148,7 +170,7 @@ private function __buildDuplicatorItem($page, $template=NULL) {
 
             $divgroup = new XMLElement('div');
 
-            $label = Widget::Label(__('Link template') . '' . __('Use {$param} for field-placeholders') . '');
+            $label = Widget::Label(__('Link template') . '' . __('Use {$fieldname} for field-placeholders. If the field has a handle, this is automatically used.') . '');
             $label->appendChild(Widget::Input(
                 "ckeditor_link_templates[" . $index . "][link]",
                 General::sanitize($template['link']
@@ -201,19 +223,6 @@ private function __buildDuplicatorItem($page, $template=NULL) {
 		public function savePresets($context)
 		{
 			if(isset($_POST['ckeditor_sections'])) {
-                // Save the link templates to the database:
-                Symphony::Database()->query("DELETE FROM `tbl_ckeditor_link_templates`");
-
-                $shortcuts = $_POST['ckeditor_link_templates'];
-                unset($_POST['ckeditor_link_templates']);
-
-                if(!empty($shortcuts))
-                {
-                    foreach($shortcuts as $i => $shortcut) {
-                        Symphony::Database()->insert($shortcut, "tbl_ckeditor_link_templates");
-                    }
-                }
-
                 // Save the sections to the config-file
 				$sectionStr = implode(',', $_POST['ckeditor_sections']);
                 Symphony::Configuration()->set('sections', $sectionStr, 'ckeditor');
@@ -231,7 +240,20 @@ public function savePresets($context)
                 Symphony::Configuration()->remove('sections', 'ckeditor');
                 Administration::instance()->saveConfig();
 			}
+			if(isset($_POST['ckeditor_link_templates'])) {
+                // Save the link templates to the database:
+                Symphony::Database()->query("DELETE FROM `tbl_ckeditor_link_templates`");
+
+				$shortcuts = $_POST['ckeditor_link_templates'];
+                unset($_POST['ckeditor_link_templates']);
 
+                if(!empty($shortcuts))
+                {
+                    foreach($shortcuts as $i => $shortcut) {
+                        Symphony::Database()->insert($shortcut, "tbl_ckeditor_link_templates");
+                    }
+                }
+			}
 		}
 
         /**
diff --git a/extension.meta.xml b/extension.meta.xml
index 0aaba4c..4417aca 100644
--- a/extension.meta.xml
+++ b/extension.meta.xml
@@ -14,6 +14,7 @@
 		
 	
 	
+		
         
 		
         
diff --git a/lib/ckeditor/plugins/link/dialogs/link.js b/lib/ckeditor/plugins/link/dialogs/link.js
index dc68a00..fcd2b6f 100755
--- a/lib/ckeditor/plugins/link/dialogs/link.js
+++ b/lib/ckeditor/plugins/link/dialogs/link.js
@@ -407,7 +407,13 @@ CKEDITOR.dialog.add( 'link', function( editor )
             {
                 for(var j in data[i].items)
                 {
-                    symPages.push([data[i].name + ' : ' + data[i].items[j].title, data[i].items[j].url]);
+					if(data[i].name != '')
+					{
+						symPages.push([data[i].name + ' : ' + data[i].items[j].title, data[i].items[j].url]);
+					} else {
+						symPages.push([data[i].items[j].title, data[i].items[j].url]);
+					}
+
                 }
             }
 		}
diff --git a/text-formatters/formatter.ckeditor.php b/text-formatters/formatter.ckeditor.php
index 000a999..1258b8f 100755
--- a/text-formatters/formatter.ckeditor.php
+++ b/text-formatters/formatter.ckeditor.php
@@ -5,8 +5,8 @@
 		function about(){
 			return array(
 				'name' => 'CKEditor',
-				'version' => '1.2.3',
-				'release-date' => '2011-08-18',
+				'version' => '1.3',
+				'release-date' => '2012-04-24',
 				'author' => array(
 					'name'     => 'Tony Arnold, Giel Berkers'
 				),
diff --git a/text-formatters/formatter.ckeditor_compact.php b/text-formatters/formatter.ckeditor_compact.php
index c97f082..ba4fa12 100755
--- a/text-formatters/formatter.ckeditor_compact.php
+++ b/text-formatters/formatter.ckeditor_compact.php
@@ -5,8 +5,8 @@
 		function about(){
 			return array(
 				'name' => 'CKEditor : Compact',
-				'version' => '1.2.3',
-				'release-date' => '2011-08-18',
+				'version' => '1.3',
+				'release-date' => '2012-04-24',
 				'author' => array(
 					'name'     => 'Tony Arnold, Giel Berkers'
 				),