diff --git a/_admin.php b/_admin.php index 10bf59e..c0accfd 100644 --- a/_admin.php +++ b/_admin.php @@ -1,152 +1,148 @@ addItem(__('Templates'), - 'plugin.php?p=templator','index.php?pf=templator/icon.png', - preg_match('/plugin.php\?p=templator(&.*)?$/',$_SERVER['REQUEST_URI']), - $core->auth->check('contentadmin,templator',$core->blog->id)); +dcCore::app()->auth->setPermissionType(initTemplator::PERMISSION_TEMPLATOR, __('manage templates')); -$core->auth->setPermissionType('templator',__('manage templates')); +dcCore::app()->menu[dcAdmin::MENU_PLUGINS]->addItem( + __('Templates'), + dcCore::app()->adminurl->get('admin.plugin.templator'), + urldecode(dcPage::getPF('templator/icon.png')), + preg_match('/' . preg_quote(dcCore::app()->adminurl->get('admin.plugin.templator')) . '(&.*)?$/', $_SERVER['REQUEST_URI']), + dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ + dcAuth::PERMISSION_CONTENT_ADMIN, + initTemplator::PERMISSION_TEMPLATOR, + ]), dcCore::app()->blog->id) +); -if ($core->auth->check('templator,contentadmin',$core->blog->id)) { - $core->addBehavior('adminPostFormSidebar',array('templatorBehaviors','adminPostFormSidebar')); - $core->addBehavior('adminPageFormSidebar',array('templatorBehaviors','adminPostFormSidebar')); +if (dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ + dcAuth::PERMISSION_CONTENT_ADMIN, + initTemplator::PERMISSION_TEMPLATOR, +]), dcCore::app()->blog->id)) { + dcCore::app()->addBehavior('adminPostHeaders', ['templatorBehaviors','adminPostHeaders']); + dcCore::app()->addBehavior('adminPostFormItems', ['templatorBehaviors','adminPostFormItems']); + dcCore::app()->addBehavior('adminPageHeaders', ['templatorBehaviors','adminPostHeaders']); + dcCore::app()->addBehavior('adminPageFormItems', ['templatorBehaviors','adminPostFormItems']); - $core->addBehavior('adminAfterPostCreate',array('templatorBehaviors','adminBeforePostUpdate')); - $core->addBehavior('adminBeforePostUpdate',array('templatorBehaviors','adminBeforePostUpdate')); - $core->addBehavior('adminAfterPageCreate',array('templatorBehaviors','adminBeforePostUpdate')); - $core->addBehavior('adminBeforePageUpdate',array('templatorBehaviors','adminBeforePostUpdate')); + dcCore::app()->addBehavior('adminAfterPostCreate', ['templatorBehaviors','adminBeforePostUpdate']); + dcCore::app()->addBehavior('adminBeforePostUpdate', ['templatorBehaviors','adminBeforePostUpdate']); + dcCore::app()->addBehavior('adminAfterPageCreate', ['templatorBehaviors','adminBeforePostUpdate']); + dcCore::app()->addBehavior('adminBeforePageUpdate', ['templatorBehaviors','adminBeforePostUpdate']); + + dcCore::app()->addBehavior('adminPostsActions', ['templatorBehaviors','adminPostsActions']); + dcCore::app()->addBehavior('adminPagesActions', ['templatorBehaviors','adminPostsActions']); - $core->addBehavior('adminPostsActionsCombo',array('templatorBehaviors','adminPostsActionsCombo')); - $core->addBehavior('adminPostsActions',array('templatorBehaviors','adminPostsActions')); - $core->addBehavior('adminPostsActionsContent',array('templatorBehaviors','adminPostsActionsContent')); - $core->addBehavior('adminPagesActionsCombo',array('templatorBehaviors','adminPostsActionsCombo')); - $core->addBehavior('adminPagesActions',array('templatorBehaviors','adminPostsActions')); - $core->addBehavior('adminPagesActionsContent',array('templatorBehaviors','adminPostsActionsContent')); } class templatorBehaviors { - public static function adminPostFormSidebar($post) - { - global $core; - - $tpl = array(' ' => ''); - $tpl_post = array(); - $selected = ''; - - foreach ($core->templator->tpl as $k => $v) { - if (!preg_match('/^category-(.+)$/',$k)) - { - $tpl_post= array_merge($tpl_post, array($k => $k)); - } - } - - $tpl = array_merge($tpl,$tpl_post); - if ($post) - { - $params['meta_type'] = 'template'; - $params['post_id'] = $post->post_id; - $post_meta = $core->meta->getMetadata($params); - $selected = $post_meta->isEmpty()? '' : $post_meta->meta_id ; - } - - echo - '
'.form::combo('post_tpl',$tpl,$selected).'
'; - - } + public static function adminPostHeaders() + { + return dcPage::jsLoad(dcPage::getPF('templator/js/admin.js')); + } - public static function adminBeforePostUpdate($cur,$post_id) - { - global $core; - - $post_id = (integer) $post_id; - - if (isset($_POST['post_tpl'])) { - $tpl = $_POST['post_tpl']; - - $core->meta->delPostMeta($post_id,'template'); - if (!empty($_POST['post_tpl'])) - { - $core->meta->setPostMeta($post_id,'template',$tpl); - } - } - } + public static function adminPostFormItems(ArrayObject $main_items, ArrayObject $sidebar_items, $post) + { + $selected = ''; - public static function adminPostsActionsCombo($args) - { - $args[0][__('Appearance')] = array(__('Select the template') => 'tpl'); - } - - public static function adminPostsActions($core,$posts,$action,$redir) - { - if ($action == 'tpl' && isset($_POST['post_tpl'])) - { - try - { - $tpl = $_POST['post_tpl']; - - while ($posts->fetch()) - { - $core->meta->delPostMeta($posts->post_id,'template'); - if (!empty($_POST['post_tpl'])) - { - $core->meta->setPostMeta($posts->post_id,'template',$tpl); - } - } - - http::redirect($redir); - } - catch (Exception $e) - { - $core->error->add($e->getMessage()); - } - } - } - - public static function adminPostsActionsContent($core,$action,$hidden_fields) - { - if ($action == 'tpl') - { - $tpl = array(' ' => ''); - $tpl_post = array(); - $selected = ''; - - foreach ($core->templator->tpl as $k => $v) { - if (!preg_match('/^category-(.+)$/',$k) && !preg_match('/^list-(.+)$/',$k)) - { - $tpl_post= array_merge($tpl_post, array($k => $k)); - } - } - - $tpl = array_merge($tpl,$tpl_post); - - echo - '

'.__('Select template for these entries').'

'. - '
'. - '

'. - - $hidden_fields. - $core->formNonce(). - form::hidden(array('action'),'tpl'). - '

'. - '
'; - - } - } + if ($post) { + $post_meta = dcCore::app()->meta->getMetadata(['meta_type' => 'template', 'post_id' => $post->post_id]); + $selected = $post_meta->isEmpty() ? '' : $post_meta->meta_id; + } + + $sidebar_items['options-box']['items']['templator'] = + '
' . + '
' . __('Template') . '
' . + '

' . + form::combo('post_tpl', self::getTemplateCombo(), $selected) . '

' . + '
'; + } + + public static function adminBeforePostUpdate($cur, $post_id) + { + $post_id = (int) $post_id; + + if (isset($_POST['post_tpl'])) { + dcCore::app()->meta->delPostMeta($post_id, 'template'); + if (!empty($_POST['post_tpl'])) { + dcCore::app()->meta->setPostMeta($post_id, 'template', $_POST['post_tpl']); + } + } + } + + public static function adminPostsActions(dcPostsActions $pa) + { + $pa->addAction( + [ + __('Appearance') => [ + __('Select the template') => 'tpl', + ], + ], + ['templatorBehaviors', 'adminPostsActionsCallback'] + ); + } + + public static function adminPostsActionsCallback(dcPostsActions $pa, ArrayObject $post) + { + # No entry + $posts_ids = $pa->getIDs(); + if (empty($posts_ids)) { + throw new Exception(__('No entry selected')); + } + + if (isset($post['post_tpl'])) { + try { + foreach ($posts_ids as $post_id) { + dcCore::app()->meta->delPostMeta($post_id, 'template'); + if (!empty($post['post_tpl'])) { + dcCore::app()->meta->setPostMeta($post_id, 'template', $post['post_tpl']); + } + } + + dcAdminNotices::addSuccessNotice(__('Entries template updated.')); + $pa->redirect(true); + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + } + + $pa->beginPage( + dcPage::breadcrumb([ + html::escapeHTML(dcCore::app()->blog->name) => '', + $pa->getCallerTitle() => $pa->getRedirection(true), + __('Entry template') => '', + ]) + ); + + echo + '

' . __('Select template for the selection') . '

' . + '
' . + $pa->getCheckboxes() . + '

' . + form::combo('post_tpl', self::getTemplateCombo()) . '

' . + + '

' . + $pa->getHiddenFields() . + dcCore::app()->formNonce() . + form::hidden(['action'], 'tpl') . + '

' . + '
'; + + $pa->endPage(); + } + + private static function getTemplateCombo() + { + $tpl = [__('No specific template') => '']; + + foreach (dcCore::app()->templator->tpl as $k => $v) { + if (!preg_match('/^category-(.+)$/', $k) && !preg_match('/^list-(.+)$/', $k)) { + $tpl[$k] = $k; + } + } + + return $tpl; + } } -?> \ No newline at end of file diff --git a/_define.php b/_define.php index 50a89c0..60d1f7c 100644 --- a/_define.php +++ b/_define.php @@ -1,22 +1,23 @@ registerModule( - /* Name */ 'Templator', - /* Description*/ 'Create and select more templates for your posts', - /* Author */ 'Osku and contributors', - /* Version */ '1.3.3', - /* Permissions */ 'contentadmin,templator' + 'Templator', + 'Create and select more templates for your posts', + 'Osku and contributors', + '1.4-dev', + [ + 'requires' => [['core', '2.24']], + 'permissions' => dcCore::app()->auth->makePermissions([ + dcAuth::PERMISSION_CONTENT_ADMIN, + initTemplator::PERMISSION_TEMPLATOR, + ]), + 'type' => 'plugin', + 'support' => 'https://github.com/JcDenis/templator', + 'details' => 'https://plugins.dotaddict.org/dc2/details/templator', + 'repository' => 'https://raw.githubusercontent.com/JcDenis/templator/master/dcstore.xml', + ] ); -?> \ No newline at end of file diff --git a/_init.php b/_init.php new file mode 100644 index 0000000..2e55c21 --- /dev/null +++ b/_init.php @@ -0,0 +1,10 @@ +error->add(__('Version 2.3.1 of Dotclear at least is required for this version of Templator.')); - $core->plugins->deactivateModule('templator'); - return false; +if (!defined('DC_CONTEXT_ADMIN')) { + return null; } -$new_version = $core->plugins->moduleInfo('templator','version'); - -$current_version = $core->getVersion('templator'); - -if (version_compare($current_version,$new_version,'>=')) { - return; +try { + # Grab info + $mod_id = basename(__DIR__); + $dc_min = dcCore::app()->plugins->moduleInfo($mod_id, 'requires')[0][1]; + $new_version = dcCore::app()->plugins->moduleInfo($mod_id, 'version'); + + if (version_compare(dcCore::app()->getVersion($mod_id), $new_version, '>=')) { + return null; + } + + # Check Dotclear version + if (!method_exists('dcUtils', 'versionsCompare') + || dcUtils::versionsCompare(DC_VERSION, $dc_min, '<', false)) { + throw new Exception(sprintf( + '%s requires Dotclear %s', + $mod_id, + $dc_min + )); + } + + # Set version + dcCore::app()->setVersion($mod_id, $new_version); + + return true; +} catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); } -$core->setVersion('templator',$new_version); -return true; -?> \ No newline at end of file +return false; diff --git a/_prepend.php b/_prepend.php index c460627..a4500cb 100644 --- a/_prepend.php +++ b/_prepend.php @@ -1,48 +1,38 @@ autoload([ + 'dcTemplator' => __DIR__ . '/inc/class.templator.php', + 'pagerTemplator' => __DIR__ . '/inc/admin.pager.templator.php', +]); -$core->templator = new dcTemplator($core); +dcCore::app()->templator = new dcTemplator(); -$core->addBehavior('initWidgets',array('templatorWidgets','initWidgets')); +dcCore::app()->addBehavior('initWidgets', ['templatorWidgets', 'initWidgets']); class templatorWidgets { - public static function initWidgets($w) - { - $w->create('templatorWidget',__('Templator › Rendering'),array('widgetTemplator','getDataTpl')); - $tpl = array('      .html' => ''); - foreach ($GLOBALS['core']->templator->tpl as $k => $v) { - if (preg_match('/^widget-(.+)$/',$k)) - { - $tpl = array_merge($tpl, array($k => $k)); - } - } - $w->templatorWidget->setting('template',__('Template:'),'','combo',$tpl); - } + public static function initWidgets($w) + { + $w->create('templatorWidget', __('Templator › Rendering'), ['widgetTemplator', 'getDataTpl']); + $tpl = ['      .html' => '']; + foreach (dcCore::app()->templator->tpl as $k => $v) { + if (preg_match('/^widget-(.+)$/', $k)) { + $tpl = array_merge($tpl, [$k => $k]); + } + } + $w->templatorWidget->setting('template', __('Template:'), '', 'combo', $tpl); + } } class widgetTemplator { - public static function getDataTpl($w) - { - if (($GLOBALS['core']->tpl->getFilePath($w->template))) - { - echo $GLOBALS['core']->tpl->getData($w->template); - } - } + public static function getDataTpl($w) + { + if (dcCore::app()->tpl->getFilePath($w->template)) { + echo dcCore::app()->tpl->getData($w->template); + } + } } -?> \ No newline at end of file diff --git a/_public.php b/_public.php index f2aceb0..95de537 100644 --- a/_public.php +++ b/_public.php @@ -1,46 +1,32 @@ tpl->setPath($core->tpl->getPath(), $core->templator->path); -$core->addBehavior('urlHandlerBeforeGetData',array('publicTemplatorBehaviors','BeforeGetData')); +if (!defined('DC_RC_PATH')) { + return null; +} +dcCore::app()->tpl->setPath(dcCore::app()->tpl->getPath(), dcCore::app()->templator->path); +dcCore::app()->addBehavior('urlHandlerBeforeGetData', ['publicTemplatorBehaviors','BeforeGetData']); class publicTemplatorBehaviors { - public static function BeforeGetData ($_ctx) - { - global $core; - - if (array_key_exists($core->url->type,$core->getPostTypes()) || ($core->url->type == 'pages')) - { - $params = array(); - $params['meta_type'] = 'template'; - $params['post_id'] = $_ctx->posts->post_id; - $post_meta = $core->meta->getMetadata($params); - - if (!$post_meta->isEmpty() && ($core->tpl->getFilePath($post_meta->meta_id))) - { - $_ctx->current_tpl = $post_meta->meta_id; - } - } - - if (($_ctx->current_tpl == "category.html") && preg_match('/^[0-9]{1,}/',$_ctx->categories->cat_id,$cat_id)) - { - $tpl = 'category-'.$cat_id[0].'.html'; - if (($core->tpl->getFilePath($tpl))) { - $_ctx->current_tpl = $tpl; - } - } - } + public static function BeforeGetData($_) + { + if (array_key_exists(dcCore::app()->url->type, dcCore::app()->getPostTypes()) || dcCore::app()->url->type == 'pages') { + $params = []; + $params['meta_type'] = 'template'; + $params['post_id'] = dcCore::app()->ctx->posts->post_id; + $post_meta = dcCore::app()->meta->getMetadata($params); + + if (!$post_meta->isEmpty() && dcCore::app()->tpl->getFilePath($post_meta->meta_id)) { + dcCore::app()->ctx->current_tpl = $post_meta->meta_id; + } + } + + if (dcCore::app()->ctx->current_tpl == 'category.html' && preg_match('/^[0-9]{1,}/', dcCore::app()->ctx->categories->cat_id, $cat_id)) { + $tpl = 'category-' . $cat_id[0] . '.html'; + if (dcCore::app()->tpl->getFilePath($tpl)) { + dcCore::app()->ctx->current_tpl = $tpl; + } + } + } } -?> \ No newline at end of file diff --git a/advanced.php b/advanced.php index 8da2431..a80c49c 100644 --- a/advanced.php +++ b/advanced.php @@ -1,79 +1,71 @@ - - - <?php echo __('Templator'); ?> - - - -'.html::escapeHTML($core->blog->name).' › '.__('Supplementary templates').''.__('Database informations').''; -$tags = $core->meta->getMetadata(array('meta_type' => 'template')); -$tags = $core->meta->computeMetaStats($tags); -$tags->sort('meta_id_lower','asc'); +$tags = dcCore::app()->meta->getMetadata(['meta_type' => 'template']); +$tags = dcCore::app()->meta->computeMetaStats($tags); +$tags->sort('meta_id_lower', 'asc'); $last_letter = null; -$cols = array('',''); -$col = 0; -while ($tags->fetch()) -{ - $letter = mb_strtoupper(mb_substr($tags->meta_id,0,1)); - - if ($last_letter != $letter) { - if ($tags->index() >= round($tags->count()/2)) { - $col = 1; - } - $cols[$col] .= ''.$letter.''; - } +$cols = ['','']; +$col = 0; - $img = '%1$s'; - if (array_key_exists($tags->meta_id,$core->templator->tpl)) { - $img_status = sprintf($img,__('available template'),'check-on.png'); - } else { - $img_status = sprintf($img,__('missing template'),'check-off.png'); - } - - $cols[$col] .= - ''. - ''.$tags->meta_id.' '.$img_status.''. - ''.$tags->count.' '. - (($tags->count==1) ? __('entry') : __('entries')).''. - ''; - - $last_letter = $letter; +while ($tags->fetch()) { + $letter = mb_strtoupper(mb_substr($tags->meta_id, 0, 1)); + + if ($last_letter != $letter) { + if ($tags->index() >= round($tags->count() / 2)) { + $col = 1; + } + $cols[$col] .= '' . $letter . ''; + } + + $img = '%1$s'; + if (array_key_exists($tags->meta_id, dcCore::app()->templator->tpl)) { + $img_status = sprintf($img, __('available template'), 'check-on.png'); + } else { + $img_status = sprintf($img, __('missing template'), 'check-off.png'); + } + + $cols[$col] .= '' . + '' . $tags->meta_id . ' ' . $img_status . '' . + '' . $tags->count . ' ' . + (($tags->count == 1) ? __('entry') : __('entries')) . '' . + ''; + + $last_letter = $letter; } $table = '
%s
'; -if ($cols[0]) -{ - echo '
'; - printf($table,$cols[0]); - if ($cols[1]) { - printf($table,$cols[1]); - } - echo '
'; -} -else -{ - echo '

'.__('No specific templates on this blog.').'

'; -} -?> +echo +'' . __('Templator') . '' . +dcPage::cssLoad(dcPage::getPF('tags/style.css')) . +'' . +dcPage::breadcrumb([ + html::escapeHTML(dcCore::app()->blog->name) => '', + __('Additional templates') => '', +]) . +dcPage::notices() . +'

' . __('New template') . '

'; - - \ No newline at end of file +if ($cols[0]) { + echo '
'; + printf($table, $cols[0]); + if ($cols[1]) { + printf($table, $cols[1]); + } + echo '
'; +} else { + echo '

' . __('No specific templates on this blog.') . '

'; +} + +dcPage::helpBlock('templator'); + +echo ''; \ No newline at end of file diff --git a/edit.php b/edit.php index 87735bd..6a984f4 100644 --- a/edit.php +++ b/edit.php @@ -1,97 +1,79 @@ templator->getSourceContent($name); - $name = $file['f']; - } - - if (preg_match('/^category-(.+).html$/',$name,$cat_id)) { - $category = $core->blog->getCategory($cat_id[1]); - $full_name = ''; - $cat_parents = $core->blog->getCategoryParents($cat_id[1]); - while ($cat_parents->fetch()) {$full_name = $cat_parents->cat_title.' › ';}; - $full_name = $full_name.$core->blog->getCategory($cat_id)->cat_title; - $name .= ' ('.$full_name.$category->cat_title.')'; - } - } - catch (Exception $e) - { - $file = $file_default; - throw $e; - } - # Write file - if (!empty($_POST['write'])) - { - $file['c'] = $_POST['file_content']; - $core->templator->writeTpl($file['f'],$file['c']); - } +if (!defined('DC_CONTEXT_ADMIN')) { + return null; } -catch (Exception $e) -{ - $core->error->add($e->getMessage()); + +try { + try { + if (!empty($_GET['edit'])) { + $name = rawurldecode($_GET['edit']); + $file = dcCore::app()->templator->getSourceContent($name); + $name = $file['f']; + } + + if (preg_match('/^category-(.+).html$/', $name, $cat_id)) { + $category = dcCore::app()->blog->getCategory($cat_id[1]); + $full_name = ''; + $cat_parents = dcCore::app()->blog->getCategoryParents($cat_id[1]); + while ($cat_parents->fetch()) { + $full_name = $cat_parents->cat_title . ' › '; + }; + $full_name = $full_name . dcCore::app()->blog->getCategory($cat_id)->cat_title; + $name .= ' (' . $full_name . $category->cat_title . ')'; + } + } catch (Exception $e) { + $file = $file_default; + + throw $e; + } + # Write file + if (!empty($_POST['write'])) { + $file['c'] = $_POST['file_content']; + dcCore::app()->templator->writeTpl($file['f'], $file['c']); + } +} catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); } ?> <?php echo __('Templator'); ?> - + - + '.html::escapeHTML($core->blog->name).' › '.__('Supplementary templates').''.__('Edit the template').''; +'

' . html::escapeHTML(dcCore::app()->blog->name) . ' › ' . __('Supplementary templates') . '' . __('Edit the template') . '

'; -if (($file['c'] !== null)) -{ - echo - '
'. - '
'. - '
'.__('File editor').''. - '

'.sprintf(__('Editing file %s'),''.$name).'

'. - '

'.form::textarea('file_content',72,30,html::escapeHTML($file['c']),'maximal','',!$file['w']).'

'; +if (($file['c'] !== null)) { + echo + '
' . + '' . + '
' . __('File editor') . '' . + '

' . sprintf(__('Editing file %s'), '' . $name) . '

' . + '

' . form::textarea('file_content', 72, 30, html::escapeHTML($file['c']), 'maximal', '', !$file['w']) . '

'; - if ($file['w']) - { - echo - '

'. - $core->formNonce(). - form::hidden(array('file_id'),html::escapeHTML($file['f'])). - '

'; - + if ($file['w']) { + echo + '

' . + dcCore::app()->formNonce() . + form::hidden(['file_id'], html::escapeHTML($file['f'])) . + '

'; + } else { + echo '

' . __('This file is not writable. Please check your files permissions.') . '

'; + } - } - else - { - echo '

'.__('This file is not writable. Please check your files permissions.').'

'; - } - - echo - '
'; + echo + '
'; } ?> diff --git a/inc/admin.pager.templator.php b/inc/admin.pager.templator.php index 477b0e6..3be0836 100644 --- a/inc/admin.pager.templator.php +++ b/inc/admin.pager.templator.php @@ -1,120 +1,123 @@ basename; - $count = ''; - $params = array(); - $link = 'media_item.php?id='.$f->media_id; - $link_edit = $p_url.'&edit='.$fname; - $icon = 'index.php?pf=templator/img/template.png'; - $class = 'media-item media-col-'.($i%2); - $details = $special = ''; - $widget_icon = ''; - $copy_url = '&copy='; - - if (preg_match('/^category-(.+)$/',$f->basename)) { - // That is ugly. - $cat_id = str_replace('category-', '', $f->basename); - $cat_id = str_replace('.html', '', $cat_id); - $cat_parents = $core->blog->getCategoryParents($cat_id); - $full_name = ''; - while ($cat_parents->fetch()) {$full_name = $cat_parents->cat_title.' › ';}; - $fname = ''.__('Category').' : '.$full_name.$core->blog->getCategory($cat_id)->cat_title; - $params['cat_id'] = $cat_id; - $params['post_type'] = ''; - $icon = 'index.php?pf=templator/img/template-alt.png'; - $copy_url = '&copycat='; - try { - $counter = $core->blog->getPosts($params,true); - } catch (Exception $e) { - $core->error->add($e->getMessage()); - } - - if ($counter->f(0) == 0) { - $count = __('No entry'); - } elseif ($counter->f(0) == 1) { - $count = ''.$counter->f(0).' '.__('entry').''; - } else { - $count = ''.$counter->f(0).' '.__('entries').''; - } - } - elseif (preg_match('/^widget-(.+)$/',$f->basename)) { - $count = ' '; - $icon = 'index.php?pf=templator/img/template-widget.png'; - $special = $widget_icon; - } - else { - $params['meta_id'] = $f->basename; - $params['meta_type'] = 'template'; - $params['post_type'] = ''; - try { - $counter = $core->meta->getPostsByMeta($params,true); - } catch (Exception $e) { - $core->error->add($e->getMessage()); - } - if ($counter->f(0) == 0) { - $count = ' '; - } elseif ($counter->f(0) == 1) { - $count = ''.$counter->f(0).' '.__('entry').''; - } else { - $count = ''.$counter->f(0).' '.__('entries').''; - } - } - - $res = - '
'. - ''. - '
'; - - return $res; - } + try { + $counter = dcCore::app()->blog->getPosts($params, true); + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + + if ($counter->f(0) == 0) { + $count = __('No entry'); + } elseif ($counter->f(0) == 1) { + $count = '' . $counter->f(0) . ' ' . __('entry') . ''; + } else { + $count = '' . $counter->f(0) . ' ' . __('entries') . ''; + } + } elseif (preg_match('/^widget-(.+)$/', $f->basename)) { + $count = ' '; + $icon = dcPage::getPF('templator/img/template-widget.png'); + $special = $widget_icon; + } else { + $params['meta_id'] = $f->basename; + $params['meta_type'] = 'template'; + $params['post_type'] = ''; + + try { + $counter = dcCore::app()->meta->getPostsByMeta($params, true); + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } + if ($counter->f(0) == 0) { + $count = ' '; + } elseif ($counter->f(0) == 1) { + $count = '' . $counter->f(0) . ' ' . __('entry') . ''; + } else { + $count = '' . $counter->f(0) . ' ' . __('entries') . ''; + } + } + + $res = '
' . + '' . + '
'; + + return $res; + } } -?> \ No newline at end of file diff --git a/inc/class.templator.php b/inc/class.templator.php index 8f16b7a..42fd5a8 100644 --- a/inc/class.templator.php +++ b/inc/class.templator.php @@ -1,380 +1,353 @@ core =& $core; + public $template_dir_name = 'other-templates'; + public $path; - $this->path = $this->core->blog->public_path.'/'.$this->template_dir_name; - - // Initial templates - $this->post_tpl = DC_ROOT.'/inc/public/default-templates/'.$this->post_default_name; - $this->category_tpl = DC_ROOT.'/inc/public/default-templates/'.$this->category_default_name; + public $tpl = []; + public $theme_tpl = []; - if ($this->core->plugins->moduleExists('pages')) { - $plugin_page = $this->core->plugins->getModules('pages'); - $this->page_tpl = path::real($plugin_page['root'].'/default-templates/'.$this->page_default_name); - } - - $this->user_theme = $this->core->blog->themes_path.'/'.$this->core->blog->settings->system->theme; - $this->user_post_tpl = path::real($this->user_theme.'/tpl/'.$this->post_default_name); - $this->user_category_tpl = path::real($this->user_theme.'/tpl/'.$this->category_default_name); - $this->user_page_tpl = path::real($this->user_theme.'/tpl/'.$this->page_default_name); - - $this->findTemplates(); - } - - /** - * - */ - public function canUseRessources($create=false) - { - if (!is_dir($this->path)) { - if ($create) { - files::makeDir($this->path); - } - return true; - } - - if (!is_writable($this->path)) { - return false; - } - - if (!is_file($this->path.'/.htaccess')) { - try { - file_put_contents($this->path.'/.htaccess',"Deny from all\n"); - } - catch (Exception $e) {return false;} - } - return true; - } - - /** - * - */ - public function getSourceContent($f) - { - $source = $this->tpl; - - if (!isset($source[$f])) { - throw new Exception(__('File does not exist.')); - } - - $F = $source[$f]; - if (!is_readable($F)) { - throw new Exception(sprintf(__('File %s is not readable'),$f)); - } - - return array( - 'c' => file_get_contents($source[$f]), - 'w' => $this->getDestinationFile($f) !== false, - 'f' => $f - ); - } - - /** - * - */ - public function filesList($item='%1$s') - { - $files = $this->tpl; - - if (empty($files)) { - return '

'.__('No file').'

'; - } - - $list = ''; - foreach ($files as $k => $v) - { - $li = sprintf('
  • %s
  • ',$item); + /** + * + */ + public function __construct() + { + $this->path = dcCore::app()->blog->public_path . '/' . $this->template_dir_name; - $list .= sprintf($li,$k,html::escapeHTML($k)); - } - - return sprintf('',$list); - } - - /** - * - */ - public function initializeTpl($name,$type) - { - if ($type == 'category') - { - if ($this->user_category_tpl) { - $base = $this->user_category_tpl; - } else { - $base = $this->category_tpl; - } - } - elseif ($type == 'page') - { - if ($this->user_page_tpl) { - $base = $this->user_page_tpl; - } else { - $base = $this->page_tpl; - } - } - else { - if ($this->user_post_tpl) { - $base = $this->user_post_tpl; - } else { - $base = $this->post_tpl; - } - } - - $source = array( - 'c' => file_get_contents($base), - 'w' => $this->getDestinationFile($name) !== false, - 'f' => $f); - - if (!$source['w']) - { - throw new Exception(sprintf(__('File %s is not readable'),$source)); - } + // Initial templates + $this->post_tpl = DC_ROOT . '/inc/public/default-templates/' . $this->tpls_default_name . '/' . $this->post_default_name; + $this->category_tpl = DC_ROOT . '/inc/public/default-templates/' . $this->tpls_default_name . '/' . $this->category_default_name; - if ($type == 'empty') - { - $source['c'] = ''; - } + if (dcCore::app()->plugins->moduleExists('pages')) { + $plugin_page = dcCore::app()->plugins->getModules('pages'); + $this->page_tpl = path::real($plugin_page['root'] . '/default-templates/' . $this->tpls_default_name . '/' . $this->page_default_name); + } - try - { - $dest = $this->getDestinationFile($name); + $this->user_theme = dcCore::app()->blog->themes_path . '/' . dcCore::app()->blog->settings->system->theme; + $this->user_post_tpl = path::real($this->user_theme . '/tpl/' . $this->post_default_name); + $this->user_category_tpl = path::real($this->user_theme . '/tpl/' . $this->category_default_name); + $this->user_page_tpl = path::real($this->user_theme . '/tpl/' . $this->page_default_name); - if ($dest == false) { - throw new Exception(); - } - - $content = $source['c']; - - if (!is_dir(dirname($dest))) { - files::makeDir(dirname($dest)); - } - - $fp = @fopen($dest,'wb'); - if (!$fp) { - throw new Exception('tocatch'); - } - - $content = preg_replace('/(\r?\n)/m',"\n",$content); - $content = preg_replace('/\r/m',"\n",$content); - - fwrite($fp,$content); - fclose($fp); - } - catch (Exception $e) - { - throw $e; - } - - } - - /** - * - */ - public function copypasteTpl($name,$source) - { - if ($name == $source) {throw new Exception(__('Why copy file content in the same file?'));} - - $file = $this->getSourceContent($source); - - $source = array( - 'c' => $file['c'], - 'w' => $this->getDestinationFile($name) !== false, - 'f' => $f); - - if (!$source['w']) - { - throw new Exception(sprintf(__('File %s is not readable'),$source)); - } + $this->findTemplates(); + } - if ($type == 'empty') - { - $source['c'] = ''; - } + /** + * + */ + public function canUseRessources($create = false) + { + if (!is_dir($this->path)) { + if ($create) { + files::makeDir($this->path); + } - try - { - $dest = $this->getDestinationFile($name); + return true; + } - if ($dest == false) { - throw new Exception(); - } - - $content = $source['c']; - - if (!is_dir(dirname($dest))) { - files::makeDir(dirname($dest)); - } - - $fp = @fopen($dest,'wb'); - if (!$fp) { - throw new Exception('tocatch'); - } - - $content = preg_replace('/(\r?\n)/m',"\n",$content); - $content = preg_replace('/\r/m',"\n",$content); - - fwrite($fp,$content); - fclose($fp); - } - catch (Exception $e) - { - throw $e; - } - } - - /** - * - */ - public function writeTpl($name,$content) - { - try - { - $dest = $this->getDestinationFile($name); - - if ($dest == false) { - throw new Exception(); - } - - if (!is_dir(dirname($dest))) { - files::makeDir(dirname($dest)); - } - - $fp = @fopen($dest,'wb'); - if (!$fp) { - //throw new Exception('tocatch'); - } - - $content = preg_replace('/(\r?\n)/m',"\n",$content); - $content = preg_replace('/\r/m',"\n",$content); - - fwrite($fp,$content); - fclose($fp); - } - catch (Exception $e) - { - throw $e; - } - } - - /** - * - */ - public function copyTpl($name) - { - try - { - $file = $this->getSourceContent($name); - $dest = $this->getDestinationFile($name,true); - - if ($dest == false) { - throw new Exception(); - } - - if (!is_dir(dirname($dest))) { - files::makeDir(dirname($dest)); - } - - $fp = @fopen($dest,'wb'); - if (!$fp) { - throw new Exception('tocatch'); - } - - $content = preg_replace('/(\r?\n)/m',"\n",$file['c']); - $content = preg_replace('/\r/m',"\n",$file['c']); - - fwrite($fp,$file['c']); - fclose($fp); - } - catch (Exception $e) - { - throw $e; - } - } - - protected function getDestinationFile($f,$totheme=false) - { - $dest = $this->path.'/'.$f; - if ($totheme) { - $dest = $this->user_theme.'/tpl/'.$f; - } - - if (file_exists($dest) && is_writable($dest)) { - return $dest; - } - - if (is_writable(dirname($dest))) { - return $dest; - } - - return false; - } - - protected function findTemplates() - { - $this->tpl = $this->getFilesInDir($this->path); - //$this->theme_tpl = $this->getFilesInDir(path::real($this->user_theme).'/tpl'); - - uksort($this->tpl,array($this,'sortFilesHelper')); - //uksort($this->theme_tpl,array($this,'sortFilesHelper')); - } - - protected function getFilesInDir($dir) - { - $dir = path::real($dir); - if (!$dir || !is_dir($dir) || !is_readable($dir)) { - return array(); - } - - $d = dir($dir); - $res = array(); - while (($f = $d->read()) !== false) - { - if (is_file($dir.'/'.$f) && !preg_match('/^\./',$f)) { - $res[$f] = $dir.'/'.$f; - } - } - - return $res; - } - - protected function sortFilesHelper($a,$b) - { - if ($a == $b) { - return 0; - } - - $ext_a = files::getExtension($a); - $ext_b = files::getExtension($b); - - return strcmp($ext_a.'.'.$a,$ext_b.'.'.$b); - } + if (!is_writable($this->path)) { + return false; + } + + if (!is_file($this->path . '/.htaccess')) { + try { + file_put_contents($this->path . '/.htaccess', "Deny from all\n"); + } catch (Exception $e) { + return false; + } + } + + return true; + } + + /** + * + */ + public function getSourceContent($f) + { + $source = $this->tpl; + + if (!isset($source[$f])) { + throw new Exception(__('File does not exist.')); + } + + $F = $source[$f]; + if (!is_readable($F)) { + throw new Exception(sprintf(__('File %s is not readable'), $f)); + } + + return [ + 'c' => file_get_contents($source[$f]), + 'w' => $this->getDestinationFile($f) !== false, + 'f' => $f, + ]; + } + + /** + * + */ + public function filesList($item = '%1$s') + { + $files = $this->tpl; + + if (empty($files)) { + return '

    ' . __('No file') . '

    '; + } + + $list = ''; + foreach ($files as $k => $v) { + $li = sprintf('
  • %s
  • ', $item); + + $list .= sprintf($li, $k, html::escapeHTML($k)); + } + + return sprintf('', $list); + } + + /** + * + */ + public function initializeTpl($name, $type) + { + if ($type == 'category') { + if ($this->user_category_tpl) { + $base = $this->user_category_tpl; + } else { + $base = $this->category_tpl; + } + } elseif ($type == 'page') { + if ($this->user_page_tpl) { + $base = $this->user_page_tpl; + } else { + $base = $this->page_tpl; + } + } else { + if ($this->user_post_tpl) { + $base = $this->user_post_tpl; + } else { + $base = $this->post_tpl; + } + } + + $source = [ + 'c' => file_get_contents($base), + 'w' => $this->getDestinationFile($name) !== false, + 'f' => $f, ]; + + if (!$source['w']) { + throw new Exception(sprintf(__('File %s is not readable'), $source)); + } + + if ($type == 'empty') { + $source['c'] = ''; + } + + try { + $dest = $this->getDestinationFile($name); + + if ($dest == false) { + throw new Exception(); + } + + $content = $source['c']; + + if (!is_dir(dirname($dest))) { + files::makeDir(dirname($dest)); + } + + $fp = @fopen($dest, 'wb'); + if (!$fp) { + throw new Exception('tocatch'); + } + + $content = preg_replace('/(\r?\n)/m', "\n", $content); + $content = preg_replace('/\r/m', "\n", $content); + + fwrite($fp, $content); + fclose($fp); + } catch (Exception $e) { + throw $e; + } + } + + /** + * + */ + public function copypasteTpl($name, $source) + { + if ($name == $source) { + throw new Exception(__('Why copy file content in the same file?')); + } + + $file = $this->getSourceContent($source); + + $source = [ + 'c' => $file['c'], + 'w' => $this->getDestinationFile($name) !== false, + 'f' => $f, ]; + + if (!$source['w']) { + throw new Exception(sprintf(__('File %s is not readable'), $source)); + } + + if ($type == 'empty') { + $source['c'] = ''; + } + + try { + $dest = $this->getDestinationFile($name); + + if ($dest == false) { + throw new Exception(); + } + + $content = $source['c']; + + if (!is_dir(dirname($dest))) { + files::makeDir(dirname($dest)); + } + + $fp = @fopen($dest, 'wb'); + if (!$fp) { + throw new Exception('tocatch'); + } + + $content = preg_replace('/(\r?\n)/m', "\n", $content); + $content = preg_replace('/\r/m', "\n", $content); + + fwrite($fp, $content); + fclose($fp); + } catch (Exception $e) { + throw $e; + } + } + + /** + * + */ + public function writeTpl($name, $content) + { + try { + $dest = $this->getDestinationFile($name); + + if ($dest == false) { + throw new Exception(); + } + + if (!is_dir(dirname($dest))) { + files::makeDir(dirname($dest)); + } + + $fp = @fopen($dest, 'wb'); + if (!$fp) { + //throw new Exception('tocatch'); + } + + $content = preg_replace('/(\r?\n)/m', "\n", $content); + $content = preg_replace('/\r/m', "\n", $content); + + fwrite($fp, $content); + fclose($fp); + } catch (Exception $e) { + throw $e; + } + } + + /** + * + */ + public function copyTpl($name) + { + try { + $file = $this->getSourceContent($name); + $dest = $this->getDestinationFile($name, true); + + if ($dest == false) { + throw new Exception(); + } + + if (!is_dir(dirname($dest))) { + files::makeDir(dirname($dest)); + } + + $fp = @fopen($dest, 'wb'); + if (!$fp) { + throw new Exception('tocatch'); + } + + $content = preg_replace('/(\r?\n)/m', "\n", $file['c']); + $content = preg_replace('/\r/m', "\n", $file['c']); + + fwrite($fp, $file['c']); + fclose($fp); + } catch (Exception $e) { + throw $e; + } + } + + protected function getDestinationFile($f, $totheme = false) + { + $dest = $this->path . '/' . $f; + if ($totheme) { + $dest = $this->user_theme . '/tpl/' . $f; + } + + if (file_exists($dest) && is_writable($dest)) { + return $dest; + } + + if (is_writable(dirname($dest))) { + return $dest; + } + + return false; + } + + protected function findTemplates() + { + $this->tpl = $this->getFilesInDir($this->path); + //$this->theme_tpl = $this->getFilesInDir(path::real($this->user_theme).'/tpl'); + + uksort($this->tpl, [$this,'sortFilesHelper']); + //uksort($this->theme_tpl,array($this,'sortFilesHelper')); + } + + protected function getFilesInDir($dir) + { + $dir = path::real($dir); + if (!$dir || !is_dir($dir) || !is_readable($dir)) { + return []; + } + + $d = dir($dir); + $res = []; + while (($f = $d->read()) !== false) { + if (is_file($dir . '/' . $f) && !preg_match('/^\./', $f)) { + $res[$f] = $dir . '/' . $f; + } + } + + return $res; + } + + protected function sortFilesHelper($a, $b) + { + if ($a == $b) { + return 0; + } + + $ext_a = files::getExtension($a); + $ext_b = files::getExtension($b); + + return strcmp($ext_a . '.' . $a, $ext_b . '.' . $b); + } } -?> \ No newline at end of file diff --git a/index.php b/index.php index 2e2381c..f8280dd 100644 --- a/index.php +++ b/index.php @@ -1,313 +1,292 @@ auth->makePermissions([ + dcAuth::PERMISSION_CONTENT_ADMIN, + initTemplator::PERMISSION_TEMPLATOR, +])); + +$p_url = dcCore::app()->admin->getPageURL(); if ((!empty($_REQUEST['m'])) && ($_REQUEST['m'] = 'template_posts')) { - require dirname(__FILE__).'/'.$_REQUEST['m'].'.php'; - return; + require dirname(__FILE__) . '/' . $_REQUEST['m'] . '.php'; + + return; } if (!empty($_REQUEST['edit'])) { - require dirname(__FILE__).'/edit.php'; - return; + require dirname(__FILE__) . '/edit.php'; + + return; } if (!empty($_REQUEST['database']) && $_REQUEST['database'] = 'on') { - require dirname(__FILE__).'/advanced.php'; - return; + require dirname(__FILE__) . '/advanced.php'; + + return; } -$file_default = $file = array('c'=>null, 'w'=>false, 'type'=>null, 'f'=>null, 'default_file'=>false); -$page = !empty($_GET['page']) ? $_GET['page'] : 1; -$nb_per_page = 20; -$msg = ''; +$file_default = $file = ['c' => null, 'w' => false, 'type' => null, 'f' => null, 'default_file' => false]; +$page = !empty($_GET['page']) ? $_GET['page'] : 1; +$nb_per_page = 20; +$msg = ''; $remove_confirm = false; // Load infos. -$ressources = $core->templator->canUseRessources(true); -$files= $core->templator->tpl; +$ressources = dcCore::app()->templator->canUseRessources(true); +$files = dcCore::app()->templator->tpl; // Media -$media = new dcMedia($core); -$media->chdir($core->templator->template_dir_name); +$media = new dcMedia(); +$media->chdir(dcCore::app()->templator->template_dir_name); // For users with only templator permission, we use sudo. -$core->auth->sudo(array($media,'getDir')); -$dir =& $media->dir; +dcCore::app()->auth->sudo([$media,'getDir']); +$dir = & $media->dir; +//if files did not appear in this list, check blog->settings->media_exclusion $items = array_values($dir['files']); // Categories try { - $categories = $core->blog->getCategories(array('post_type'=>'post')); - - $categories_combo = array(); - $l = $categories->level; - $full_name = array($categories->cat_title); - - while ($categories->fetch()) { - - if ($categories->level < $l) { - $full_name = array(); - } elseif ($categories->level == $l) { - array_pop($full_name); - } - $full_name[] = html::escapeHTML($categories->cat_title); - - $categories_combo[implode(' › ',$full_name)] = $categories->cat_id; - - $l = $categories->level; + $categories = dcCore::app()->blog->getCategories(['post_type' => 'post']); - } -} catch (Exception $e) { } + $categories_combo = []; + $l = $categories->level; + $full_name = [$categories->cat_title]; + while ($categories->fetch()) { + if ($categories->level < $l) { + $full_name = []; + } elseif ($categories->level == $l) { + array_pop($full_name); + } + $full_name[] = html::escapeHTML($categories->cat_title); + + $categories_combo[implode(' › ', $full_name)] = $categories->cat_id; + + $l = $categories->level; + } +} catch (Exception $e) { +} $hasCategories = ($categories->isEmpty()) ? false : true; -$combo_source = array( - 'post.html' => 'post' -); +$combo_source = [ + 'post.html' => 'post', +]; -if ($core->auth->check('pages',$core->blog->id) && $core->plugins->moduleExists('pages')) { - $combo_source['page.html'] = 'page'; +if (dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([dcPages::PERMISSION_PAGES]), dcCore::app()->blog->id) && dcCore::app()->plugins->moduleExists('pages')) { + $combo_source['page.html'] = 'page'; } if (!$categories->isEmpty()) { - $combo_source['category.html'] = 'category'; + $combo_source['category.html'] = 'category'; } $combo_source[' — '] = 'empty'; $add_template = $copy_confirm = $copycat_confirm = false; -if (!$ressources) -{ - $core->error->add(__('The plugin is unusable with your configuration. You have to change file permissions.')); +if (!$ressources) { + dcCore::app()->error->add(__('The plugin is unusable with your configuration. You have to change file permissions.')); } -if (!empty($_POST['filesource'])) -{ - try - { - $source = $_POST['filesource']; - if (empty($_POST['filename']) && $source != 'category') { - throw new Exception(__('Filename is empty.')); - } - $name = files::tidyFileName($_POST['filename']).'.html'; - if ($source == 'category') - { - $name = 'category-'.$_POST['filecat'].'.html'; - } - $core->templator->initializeTpl($name,$source); - } - catch (Exception $e) - { - $core->error->add($e->getMessage()); - } - if (!$core->error->flag()) { - http::redirect($p_url.'&msg=new'); - } +if (!empty($_POST['filesource'])) { + try { + $source = $_POST['filesource']; + if (empty($_POST['filename']) && $source != 'category') { + throw new Exception(__('Filename is empty.')); + } + $name = files::tidyFileName($_POST['filename']) . '.html'; + if ($source == 'category') { + $name = 'category-' . $_POST['filecat'] . '.html'; + } + dcCore::app()->templator->initializeTpl($name, $source); + + if (!dcCore::app()->error->flag()) { + dcAdminNotices::addSuccessNotice(__('The new template has been successfully created.')); + dcCore::app()->adminurl->redirect('admin.plugin.templator'); + } + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } } -if (!empty($_POST['rmyes']) && !empty($_POST['remove']) ) { - try - { - $file = rawurldecode($_POST['remove']); - $media->removeItem($file); - $core->meta->delMeta($file,'template'); - } - catch (Exception $e) - { - $core->error->add($e->getMessage()); - } - if (!$core->error->flag()) { - http::redirect($p_url.'&msg=del'); - } +if (!empty($_POST['rmyes']) && !empty($_POST['remove'])) { + try { + $file = rawurldecode($_POST['remove']); + $media->removeItem($file); + dcCore::app()->meta->delMeta($file, 'template'); + + if (!dcCore::app()->error->flag()) { + dcAdminNotices::addSuccessNotice(__('The template has been successfully removed.')); + dcCore::app()->adminurl->redirect('admin.plugin.templator'); + } + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } } if (!empty($_POST['cpyes']) && !empty($_POST['copy']) && !empty($_POST['newfile'])) { - try - { - $file = rawurldecode($_POST['copy']); - $newfile = rawurldecode($_POST['newfile']).'.html'; - $core->templator->copypasteTpl($newfile,$file); - } - catch (Exception $e) - { - $core->error->add($e->getMessage()); - } - if (!$core->error->flag()) { - http::redirect($p_url.'&msg=cpy'); - } + try { + $file = rawurldecode($_POST['copy']); + $newfile = rawurldecode($_POST['newfile']) . '.html'; + dcCore::app()->templator->copypasteTpl($newfile, $file); + + if (!dcCore::app()->error->flag()) { + dcAdminNotices::addSuccessNotice(__('The template has been successfully copied.')); + dcCore::app()->adminurl->redirect('admin.plugin.templator'); + } + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } } if (!empty($_POST['cpyes']) && !empty($_POST['copycat']) && !empty($_POST['copcat'])) { - try - { - $file = rawurldecode($_POST['copycat']); - $newfile = 'category-'.rawurldecode($_POST['copcat']).'.html'; - $core->templator->copypasteTpl($newfile,$file); - } - catch (Exception $e) - { - $core->error->add($e->getMessage()); - } - if (!$core->error->flag()) { - http::redirect($p_url.'&msg=cpy'); - } + try { + $file = rawurldecode($_POST['copycat']); + $newfile = 'category-' . rawurldecode($_POST['copcat']) . '.html'; + dcCore::app()->templator->copypasteTpl($newfile, $file); + + if (!dcCore::app()->error->flag()) { + dcAdminNotices::addSuccessNotice(__('The template has been successfully copied.')); + dcCore::app()->adminurl->redirect('admin.plugin.templator'); + } + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } } -if (!empty($_GET['remove'])) -{ - $remove_confirm = true; +if (!empty($_GET['remove'])) { + $remove_confirm = true; } -if (!empty($_GET['copy'])) -{ - $copy_confirm = true; +if (!empty($_GET['copy'])) { + $copy_confirm = true; } -if (!empty($_GET['copycat'])) -{ - $copycat_confirm = true; +if (!empty($_GET['copycat'])) { + $copycat_confirm = true; } -$msg = isset($_REQUEST['msg']) ? $_REQUEST['msg'] : ''; -$msg_list = array( - 'new' => __('The new template has been successfully created.'), - 'del' => __('The template has been successfully removed.'), - 'cpy' => __('The template has been successfully copied.') -); -if (isset($msg_list[$msg])) { - $msg = sprintf('

    %s

    ',$msg_list[$msg]); -} - -?> - - - <?php echo __('Templator'); ?> - - - - - - -'.html::escapeHTML($core->blog->name).' › '.__('Supplementary templates').''; +'' . __('Templator') . '' . +dcPage::cssLoad(dcPage::getPF('templator/style/style.css')) . +(!$add_template ? dcPage::jsLoad(dcPage::getPF('templator/js/form.js')) : '') . +dcPage::jsLoad(dcPage::getPF('templator/js/script.js')) . +'' . +dcPage::breadcrumb([ + html::escapeHTML(dcCore::app()->blog->name) => '', + __('Additional templates') => '', +]) . +dcPage::notices(); if ($remove_confirm) { - echo - '
    '. - '

    '.sprintf(__('Are you sure you want to remove the template "%s"?'), - html::escapeHTML($_GET['remove'])).'

    '. - '

    '. - '   '. - $core->formNonce(). - form::hidden('remove',html::escapeHTML($_GET['remove'])).'

    '. - '
    '; + echo + '
    ' . + '

    ' . sprintf( + __('Are you sure you want to remove the template "%s"?'), + html::escapeHTML($_GET['remove']) + ) . '

    ' . + '

    ' . + '   ' . + dcCore::app()->formNonce() . + form::hidden('remove', html::escapeHTML($_GET['remove'])) . '

    ' . + '
    '; } if ($copy_confirm) { - echo - '
    '. - '

    '.sprintf(__('To copy the template %s, you need to fill a new filename.'), - html::escapeHTML($_GET['copy'])).'

    '. - '

    '.html::escapeHTML('.html').'  '. - '  '. - $core->formNonce(). - form::hidden('copy',html::escapeHTML($_GET['copy'])).'

    '. - '
    '; + echo + '
    ' . + '

    ' . sprintf( + __('To copy the template %s, you need to fill a new filename.'), + html::escapeHTML($_GET['copy']) + ) . '

    ' . + '

    ' . html::escapeHTML('.html') . '  ' . + '  ' . + dcCore::app()->formNonce() . + form::hidden('copy', html::escapeHTML($_GET['copy'])) . '

    ' . + '
    '; } if ($copycat_confirm) { - $category_id = str_replace('category-', '', $_GET['copycat']); - $category_id = str_replace('.html', '', $category_id); - $cat_parents = $core->blog->getCategoryParents($category_id); - $full_name = ''; - while ($cat_parents->fetch()) {$full_name = $cat_parents->cat_title.' › ';}; - $name = $full_name.$core->blog->getCategory($category_id)->cat_title; - echo - '
    '. - '

    '.sprintf(__('To copy the template %s (%s), you need to choose a category.'), - html::escapeHTML($_GET['copycat']),$name).'

    '. - '

    '. - '  '. - $core->formNonce(). - form::hidden('copycat',html::escapeHTML($_GET['copycat'])). - '
    '; + $category_id = str_replace('category-', '', $_GET['copycat']); + $category_id = str_replace('.html', '', $category_id); + $cat_parents = dcCore::app()->blog->getCategoryParents($category_id); + $full_name = ''; + while ($cat_parents->fetch()) { + $full_name = $cat_parents->cat_title . ' › '; + }; + $name = $full_name . dcCore::app()->blog->getCategory($category_id)->cat_title; + echo + '
    ' . + '

    ' . sprintf( + __('To copy the template %s (%s), you need to choose a category.'), + html::escapeHTML($_GET['copycat']), + $name + ) . '

    ' . + '

    ' . + '  ' . + $dcCore::app()->formNonce() . + form::hidden('copycat', html::escapeHTML($_GET['copycat'])) . + '
    '; } if (!$add_template) { - echo '

    '. - __('New template').'

    '; + echo '

    ' . + __('New template') . '

    '; } echo -'
    '. -'

    '.__('New template').'

    '. -'

    '. -'


    -

    '.__('Extension .html is automatically added to filename').'

    +'' . +'

    ' . __('New template') . '

    ' . +'

    ' . +'


    +

    ' . __('Extension .html is automatically added to filename') . '

    '; if ($hasCategories) { - echo '

    '; + echo '

    '; } echo -'

    '.form::hidden(array('p'),'templator'). -$core->formNonce(). -'

    '. +'

    ' . form::hidden(['p'], 'templator') . +dcCore::app()->formNonce() . +'

    ' . '
    '; -if (count($items) == 0) -{ - echo '

    '.__('No template.').'

    '; -} -else -{ - $pager = new pager($page,count($items),$nb_per_page,10); - $pager->html_prev = __('«prev.'); - $pager->html_next = __('next»'); - - echo - '
    '. - '
    '. - - '
    '. - '

    '.__('Page(s)').' : '.$pager->getLinks().'

    '; - - for ($i=$pager->index_start, $j=0; $i<=$pager->index_end; $i++, $j++) - { - echo pagerTemplator::templatorItemLine($items[$i],$j); - } - - echo - '

    '.__('Page(s)').' : '.$pager->getLinks().'

    '. - '
    '; +if (count($items) == 0) { + echo '

    ' . __('No template.') . '

    '; +} else { + $pager = new pager($page, count($items), $nb_per_page, 10); + $pager->html_prev = __('«prev.'); + $pager->html_next = __('next»'); + + echo + '
    ' . + '
    ' . + + '
    ' . + '

    ' . __('Page(s)') . ' : ' . $pager->getLinks() . '

    '; + + for ($i = $pager->index_start, $j = 0; $i <= $pager->index_end; $i++, $j++) { + echo pagerTemplator::templatorItemLine($items[$i], $j); + } + + echo + '

    ' . __('Page(s)') . ' : ' . $pager->getLinks() . '

    ' . + '
    '; } -echo - '

    +echo + '

    '. - __('Display templates used for entries in base').' + href="' . $p_url . '&database=on" + title="' . __('Display templates used for entries in base') . '" + >' . + __('Display templates used for entries in base') . '

    '; ?> diff --git a/js/admin.js b/js/admin.js new file mode 100644 index 0000000..beec66e --- /dev/null +++ b/js/admin.js @@ -0,0 +1,7 @@ +$(function(){ + /* toogle admin form sidebar */ + $('#templator h5').toggleWithLegend( + $('#templator').children().not('h5'), + {cookie:'dcx_templator_admin_form_sidebar',legend_click:true} + ); +}); \ No newline at end of file diff --git a/locales/fr/main.lang.php b/locales/fr/main.lang.php index d921b86..2e797d1 100644 --- a/locales/fr/main.lang.php +++ b/locales/fr/main.lang.php @@ -1,8 +1,9 @@ \ No newline at end of file diff --git a/template_posts.php b/template_posts.php index e32ca67..abeeb97 100644 --- a/template_posts.php +++ b/template_posts.php @@ -2,78 +2,86 @@ # -- BEGIN LICENSE BLOCK ---------------------------------- # # This file is part of templator a plugin for Dotclear 2. -# +# # Copyright (c) 2010 Osku and contributors # Licensed under the GPL version 2.0 license. # A copy of this license is available in LICENSE file or at # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html # # -- END LICENSE BLOCK ------------------------------------ -if (!defined('DC_CONTEXT_ADMIN')) { return; } +if (!defined('DC_CONTEXT_ADMIN')) { + return; +} $template = (!empty($_REQUEST['template']) || $_REQUEST['template'] == '0') ? $_REQUEST['template'] : ''; -$this_url = $p_url.'&m=template_posts&template='.rawurlencode($template); +$this_url = $p_url . '&m=template_posts&template=' . rawurlencode($template); -$page = !empty($_GET['page']) ? $_GET['page'] : 1; -$nb_per_page = 30; +$page = !empty($_GET['page']) ? $_GET['page'] : 1; +$nb_per_page = 30; # Unselect the template -if (!empty($_POST['initialise']) && $core->auth->check('publish,contentadmin',$core->blog->id)) -{ - try { - $core->meta->delMeta($template,'template'); - http::redirect($p_url.'&del='.$template); - } catch (Exception $e) { - $core->error->add($e->getMessage()); - } +if (!empty($_POST['initialise']) && dcCore::app()->auth->check('publish,contentadmin', dcCore::app()->blog->id)) { + try { + dcCore::app()->meta->delMeta($template, 'template'); + http::redirect($p_url . '&del=' . $template); + } catch (Exception $e) { + dcCore::app()->error->add($e->getMessage()); + } } -$params = array(); -$params['limit'] = array((($page-1)*$nb_per_page),$nb_per_page); +$params = []; +$params['limit'] = [(($page - 1) * $nb_per_page),$nb_per_page]; $params['no_content'] = true; -$params['meta_id'] = $template; +$params['meta_id'] = $template; $params['meta_type'] = 'template'; $params['post_type'] = ''; # Get posts try { - $posts = $core->meta->getPostsByMeta($params); - $counter = $core->meta->getPostsByMeta($params,true); - $post_list = new adminPostList($core,$posts,$counter->f(0)); + $posts = dcCore::app()->meta->getPostsByMeta($params); + $counter = dcCore::app()->meta->getPostsByMeta($params, true); + $post_list = new adminPostList($posts, $counter->f(0)); } catch (Exception $e) { - $core->error->add($e->getMessage()); + dcCore::app()->error->add($e->getMessage()); } # Actions combo box -$combo_action = array(); -if ($core->auth->check('publish,contentadmin',$core->blog->id)) -{ - $combo_action[__('Status')] = array( - __('Publish') => 'publish', - __('Unpublish') => 'unpublish', - __('Schedule') => 'schedule', - __('Mark as pending') => 'pending' - ); +$combo_action = []; +if (dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ + dcAuth::PERMISSION_PUBLISH, + dcAuth::PERMISSION_CONTENT_ADMIN, +]), dcCore::app()->blog->id)) { + $combo_action[__('Status')] = [ + __('Publish') => 'publish', + __('Unpublish') => 'unpublish', + __('Schedule') => 'schedule', + __('Mark as pending') => 'pending', + ]; } -$combo_action[__('Mark')] = array( - __('Mark as selected') => 'selected', - __('Mark as unselected') => 'unselected' -); -$combo_action[__('Change')] = array(__('Change category') => 'category'); -if ($core->auth->check('admin',$core->blog->id)) -{ - $combo_action[__('Change')] = array_merge($combo_action[__('Change')], - array(__('Change author') => 'author')); +$combo_action[__('Mark')] = [ + __('Mark as selected') => 'selected', + __('Mark as unselected') => 'unselected', +]; +$combo_action[__('Change')] = [__('Change category') => 'category']; +if (dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ + dcAuth::PERMISSION_ADMIN, +]), dcCore::app()->blog->id)) { + $combo_action[__('Change')] = array_merge( + $combo_action[__('Change')], + [__('Change author') => 'author'] + ); } -if ($core->auth->check('delete,contentadmin',$core->blog->id)) -{ - $combo_action[__('Delete')] = array(__('Delete') => 'delete'); +if (dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ + dcAuth::PERMISSION_DELETE, + dcAuth::PERMISSION_CONTENT_ADMIN, +]), dcCore::app()->blog->id)) { + $combo_action[__('Delete')] = [__('Delete') => 'delete']; } # --BEHAVIOR-- adminPostsActionsCombo -$core->callBehavior('adminPostsActionsCombo',array(&$combo_action)); +dcCore::app()->callBehavior('adminPostsActionsCombo', [&$combo_action]); ?> @@ -93,42 +101,46 @@ $core->callBehavior('adminPostsActionsCombo',array(&$combo_action)); -

    blog->name); ?> › +

    blog->name); ?> ›

    '.__('Back to templates list').'

    '; +echo '

    ' . __('Back to templates list') . '

    '; -if (!$core->error->flag()) -{ - # Show posts - $post_list->display($page,$nb_per_page, - '
    '. - - '%s'. - - '
    '. - '

    '. - - '

    '.__('Selected entries action:').' '. - form::combo('action',$combo_action). - '

    '. - form::hidden('post_type',''). - form::hidden('redir',$p_url.'&m=template_posts&tag='. - str_replace('%','%%',rawurlencode($template)).'&page='.$page). - $core->formNonce(). - '
    '. - '
    '); - - # Remove tag - if (!$posts->isEmpty() && $core->auth->check('contentadmin',$core->blog->id)) { - echo - '
    '. - '

    '. - $core->formNonce().'

    '. - '
    '; - } +if (!dcCore::app()->error->flag()) { + # Show posts + $post_list->display( + $page, + $nb_per_page, + '
    ' . + + '%s' . + + '
    ' . + '

    ' . + + '

    ' . __('Selected entries action:') . ' ' . + form::combo('action', $combo_action) . + '

    ' . + form::hidden('post_type', '') . + form::hidden('redir', $p_url . '&m=template_posts&tag=' . + str_replace('%', '%%', rawurlencode($template)) . '&page=' . $page) . + dcCore::app()->formNonce() . + '
    ' . + '
    ' + ); + + # Remove tag + if (!$posts->isEmpty() && dcCore::app()->auth->check(dcCore::app()->auth->makePermissions([ + dcAuth::PERMISSION_CONTENT_ADMIN, + ]), dcCore::app()->blog->id)) { + echo + '
    ' . + '

    ' . + dcCore::app()->formNonce() . '

    ' . + '
    '; + } } ?>