Skip to content

Commit

Permalink
update to 1.3
Browse files Browse the repository at this point in the history
added link templates
  • Loading branch information
kanduvisla committed Apr 24, 2012
1 parent 0776213 commit 18dec17
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 122 deletions.
8 changes: 4 additions & 4 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>, Giel Berkers <[email protected]>
* Repository: <http://github.com/kanduvisla/symphony-ckeditor/>
* Requirements: Symphony CMS 2.2 or higher <http://github.com/symphony/symphony-2/tree/master>
Expand Down Expand Up @@ -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:
Expand All @@ -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.
from where to show these links. You can set these in the preferences page of your Symphony installation.
137 changes: 85 additions & 52 deletions content/content.pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,86 @@ function build()

// The pages:
$tree = array();
$tree[] = array('name'=>__('Pages'), 'items'=>$this->buildTree());
$tree[] = array('name'=>'', 'items'=>$this->buildTree());

/* echo '<pre>';
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 &nbsp; 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'];
Expand All @@ -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 &nbsp; 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;
}
}
?>
144 changes: 83 additions & 61 deletions extension.driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '<a href="http://thecocoabots.com">Tony Arnold</a>, <a href="http://gielberkers.com">Giel Berkers</a>'
),
Expand Down Expand Up @@ -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();
Expand All @@ -73,64 +73,86 @@ 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);

// Some JavaScript:
$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')));
Expand All @@ -148,7 +170,7 @@ private function __buildDuplicatorItem($page, $template=NULL) {

$divgroup = new XMLElement('div');

$label = Widget::Label(__('Link template') . '<i>' . __('Use {$param} for field-placeholders') . '</i>');
$label = Widget::Label(__('Link template') . '<i>' . __('Use {$fieldname} for field-placeholders. If the field has a handle, this is automatically used.') . '</i>');
$label->appendChild(Widget::Input(
"ckeditor_link_templates[" . $index . "][link]",
General::sanitize($template['link']
Expand Down Expand Up @@ -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');
Expand All @@ -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");
}
}
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions extension.meta.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</author>
</authors>
<releases>
<release version="1.3" date="2012-04-24" min="2.2.x"/>
<release version="1.2.4" date="2011-12-12" min="2.3"/>
<release version="1.2.3" date="2011-08-18" min="2.2.x"/>
<release version="1.2" date="2011-02-25" max="2.2"/>
Expand Down
Loading

0 comments on commit 18dec17

Please sign in to comment.