From 9421d4b432c8cfb9c73c2193e1d09c04a8955d1e Mon Sep 17 00:00:00 2001 From: Romain Date: Fri, 12 Aug 2011 18:00:51 +0200 Subject: [PATCH] Added a method to remove an aside from any resource --- .../asides/js/mgr/widgets/asides.grid.js | 20 +++++++++ .../asides/controllers/mgr/header.php | 4 +- .../asides/lexicon/en/default.inc.php | 16 ++++--- .../asides/lexicon/fr/default.inc.php | 14 +++--- .../asides/model/asides/asides.class.php | 30 ++++++++++++- .../asides/processors/mgr/asides/clear.php | 44 +++++++++++++++++++ .../asides/processors/mgr/asides/update.php | 8 ++-- 7 files changed, 117 insertions(+), 19 deletions(-) create mode 100644 core/components/asides/processors/mgr/asides/clear.php diff --git a/assets/components/asides/js/mgr/widgets/asides.grid.js b/assets/components/asides/js/mgr/widgets/asides.grid.js index bac7348..eec3904 100644 --- a/assets/components/asides/js/mgr/widgets/asides.grid.js +++ b/assets/components/asides/js/mgr/widgets/asides.grid.js @@ -88,6 +88,11 @@ Ext.extend(Asides.grid.Items,MODx.grid.Grid,{ ,handler: this.updateAside }); m.push('-'); + m.push({ + text: _('asides.aside_clear') + ,handler: this.clearAside + }); + m.push('-'); m.push({ text: _('asides.aside_remove') ,handler: this.removeAside @@ -125,7 +130,22 @@ Ext.extend(Asides.grid.Items,MODx.grid.Grid,{ this.windows.updateAside.fp.getForm().setValues(r); this.windows.updateAside.show(e.target); } + ,clearAside: function(btn,e) { + if (!this.menu.record) return false; + MODx.msg.confirm({ + title: _('asides.aside_clear') + ,text: _('asides.aside_clear_confirm') + ,url: this.config.url + ,params: { + action: 'mgr/asides/clear' + ,id: this.menu.record.id + } + ,listeners: { + 'success': {fn:function(r) { this.refresh(); },scope:this} + } + }); + } ,removeAside: function(btn,e) { if (!this.menu.record) return false; diff --git a/core/components/asides/controllers/mgr/header.php b/core/components/asides/controllers/mgr/header.php index c5334aa..91f6b98 100644 --- a/core/components/asides/controllers/mgr/header.php +++ b/core/components/asides/controllers/mgr/header.php @@ -25,7 +25,8 @@ * @package asides * @subpackage controllers */ -$modx->regClientCSS($asides->config['cssUrl'].'mgr.css'); + +//$modx->regClientCSS($asides->config['cssUrl'].'mgr.css'); $modx->regClientStartupScript($asides->config['jsUrl'].'mgr/asides.js'); $modx->regClientStartupScript($modx->config['assets_url'].'components/tinymce/jscripts/tiny_mce/tiny_mce.js'); @@ -40,5 +41,6 @@ Asides.action = "'.(!empty($_REQUEST['a']) ? $_REQUEST['a'] : 0).'"; }); '); +//$modx->loadRichTextEditor(); return ''; \ No newline at end of file diff --git a/core/components/asides/lexicon/en/default.inc.php b/core/components/asides/lexicon/en/default.inc.php index d5ad9a4..38304b9 100644 --- a/core/components/asides/lexicon/en/default.inc.php +++ b/core/components/asides/lexicon/en/default.inc.php @@ -25,23 +25,25 @@ * @package asides * @subpackage lexicon */ -$_lang['asides.menu'] = 'Asides'; -$_lang['asides.menu_desc'] = 'Manage your asides.'; -$_lang['asides.intro_msg'] = 'Manage your asides.'; - -$_lang['asides.items'] = 'Asides'; - +$_lang['asides.aside_clear'] = 'Clear this aside'; +$_lang['asides.aside.content'] = 'Content'; +$_lang['asides.aside_clear_confirm'] = 'Are you sure you want to remove this aside from any resource?'; $_lang['asides.aside_create'] = 'Create an aside'; $_lang['asides.aside_err_ae'] = 'An aside already exists with that name.'; $_lang['asides.aside_err_nf'] = 'Aside not found.'; $_lang['asides.aside_err_ns'] = 'Aside not specified.'; $_lang['asides.aside_err_remove'] = 'An error occurred while trying to remove the aside.'; $_lang['asides.aside_err_remove_in_use'] = 'This aside is still used in the following(s) resource(s):

[[+ids]].

Please remove it from those resource(s) before deleting it.'; +$_lang['asides.aside_err_remove_not_in_use'] = 'This aside isn\'t used in any resource.'; $_lang['asides.aside_err_save'] = 'An error occurred while trying to save the aside.'; $_lang['asides.aside_remove'] = 'Remove this aside'; $_lang['asides.aside_remove_confirm'] = 'Are you sure you want to remove this aside?'; +$_lang['asides.aside_remove_not_in_use'] = 'This aside isn\'t used in any resource'; $_lang['asides.aside_update'] = 'Update this aside'; -$_lang['asides.aside.content'] = 'Content'; +$_lang['asides.menu'] = 'Asides'; +$_lang['asides.menu_desc'] = 'Manage your asides.'; +$_lang['asides.intro_msg'] = 'Manage your asides.'; +$_lang['asides.items'] = 'Asides'; // sortable check box TV $_lang['cbs_name'] = 'Name'; diff --git a/core/components/asides/lexicon/fr/default.inc.php b/core/components/asides/lexicon/fr/default.inc.php index c51043d..e7890a6 100644 --- a/core/components/asides/lexicon/fr/default.inc.php +++ b/core/components/asides/lexicon/fr/default.inc.php @@ -25,23 +25,25 @@ * @package asides * @subpackage lexicon */ -$_lang['asides.menu'] = 'Asides'; -$_lang['asides.menu_desc'] = 'Gérez vos « modules » de colonne'; -$_lang['asides.intro_msg'] = 'Gérez vos « modules » de colonne.'; - -$_lang['asides.items'] = 'Asides'; - +$_lang['asides.aside_clear'] = 'Enlever ce module'; +$_lang['asides.aside_clear_confirm'] = 'Êtes-vous sûr de vouloir supprimer ce module de toute ressource ?'; $_lang['asides.aside_create'] = 'Créer un module'; $_lang['asides.aside_err_ae'] = 'Un module portant ce nom existe déjà.'; $_lang['asides.aside_err_nf'] = 'Module non trouvé.'; $_lang['asides.aside_err_ns'] = 'Module non indiqué.'; $_lang['asides.aside_err_remove'] = 'Une erreur est survenue lors de la suppression du module.'; $_lang['asides.aside_err_remove_in_use'] = 'Ce module est toujours utilisé dans les ressources suivantes :

[[+ids]].

Veuillez retirer ce module des ressources avant de le supprimer.'; +$_lang['asides.aside_err_remove_not_in_use'] = 'Ce module n\'est utilisé dans aucune ressource.'; $_lang['asides.aside_err_save'] = 'Une erreur est survenue lors de la sauvegarde du module.'; $_lang['asides.aside_remove'] = 'Supprimer ce module'; $_lang['asides.aside_remove_confirm'] = 'Êtes-vous sûr de vouloir supprimer ce module ?'; +$_lang['asides.aside_remove_not_in_use'] = 'Ce module n\'est utilisé dans aucune ressource'; $_lang['asides.aside_update'] = 'Mettre à jour ce module'; $_lang['asides.aside.content'] = 'Contenu'; +$_lang['asides.menu'] = 'Asides'; +$_lang['asides.menu_desc'] = 'Gérez vos « modules » de colonne'; +$_lang['asides.intro_msg'] = 'Gérez vos « modules » de colonne.'; +$_lang['asides.items'] = 'Asides'; // sortable check box TV $_lang['cbs_name'] = 'Nom'; diff --git a/core/components/asides/model/asides/asides.class.php b/core/components/asides/model/asides/asides.class.php index 87b3232..5e57fce 100644 --- a/core/components/asides/model/asides/asides.class.php +++ b/core/components/asides/model/asides/asides.class.php @@ -84,12 +84,17 @@ public function initialize($ctx = 'web') { * Returns a string of resources using a given aside * * @access public - * @param string $aside The aside (chunk) object + * @param array $aside The aside (chunk) object */ public function inResource($aside) { // grab the TV $asideTV = $this->modx->getObject('modTemplateVar',array('name' => 'aside')); - $asideTVid = $asideTV->get('id'); + $asideTVid = ''; + if ($asideTV) { + $asideTVid = $asideTV->get('id'); + } else { + //@TODO: what if it's been renamed ? + } // target the modTemplates with this TV $asideTpls = $this->modx->getCollection('modTemplateVarTemplate',array('tmplvarid' => $asideTVid)); @@ -128,4 +133,25 @@ public function inResource($aside) { return trim($o,','); } + + /** + * Removes an aside from any existing resource using it + * + * @acces public + * @param string $aside the aside (chunk) name + * @param string $ids a list of comma separated ids using this aside + */ + public function cleanAside($aside, $ids) { + $resIds = explode(',',$ids); + $tv = $this->modx->getObject('modTemplateVar',array('name' => 'aside')); + //@TODO: what if the tv has been renamed ? + foreach ($resIds as $resourceId) { + $tvValue = $this->modx->getObject('modTemplateVarResource',array('tmplvarid' => $tv->get('id'), 'contentid' => $resourceId)); + $value = str_replace($aside,'',$tvValue->get('value')); + $value = str_replace('||||','||',$value); + $value = trim($value,'||'); + $tv->setValue($resourceId,$value); + $tv->save(); + } + } } \ No newline at end of file diff --git a/core/components/asides/processors/mgr/asides/clear.php b/core/components/asides/processors/mgr/asides/clear.php new file mode 100644 index 0000000..f608779 --- /dev/null +++ b/core/components/asides/processors/mgr/asides/clear.php @@ -0,0 +1,44 @@ + + * + * Asides is free software; you can redistribute it and/or modify it under the + * terms of the GNU General Public License as published by the Free Software + * Foundation; either version 2 of the License, or (at your option) any later + * version. + * + * Asides is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * Asides; if not, write to the Free Software Foundation, Inc., 59 Temple + * Place, Suite 330, Boston, MA 02111-1307 USA + * + * @package asides + */ +/** + * Remove an aside chunk from any resource (clears its name from the aside TV). + * + * @package asides + * @subpackage processors + */ + +$Asides = $modx->getService('asides','Asides',$modx->getOption('asides.core_path',null,$modx->getOption('core_path').'components/asides/').'model/asides/',$scriptProperties); +if (!($Asides instanceof Asides)) return ''; + +if (empty($scriptProperties['id'])) return $modx->error->failure($modx->lexicon('asides.aside_err_ns')); +$aside = $modx->getObject('modChunk',$scriptProperties['id']); +if (!$aside) return $modx->error->failure($modx->lexicon('asides.aside_err_nf')); + +// grabs the resources where this aside is +$resources = $Asides->inResource($aside); +if (!$resources) { + return $modx->error->failure($modx->lexicon('asides.aside_err_remove_not_in_use')); +} +$Asides->cleanAside($aside->get('name'), $resources); + +// output +return $modx->error->success('',$aside); \ No newline at end of file diff --git a/core/components/asides/processors/mgr/asides/update.php b/core/components/asides/processors/mgr/asides/update.php index f186fb3..f20b87d 100644 --- a/core/components/asides/processors/mgr/asides/update.php +++ b/core/components/asides/processors/mgr/asides/update.php @@ -21,11 +21,11 @@ */ /** * Update an aside chunk - * + * * @package asides * @subpackage processors */ -/* get board */ + if (empty($scriptProperties['id'])) return $modx->error->failure($modx->lexicon('asides.aside_err_ns')); $aside = $modx->getObject('modChunk',$scriptProperties['id']); if (!$aside) return $modx->error->failure($modx->lexicon('asides.aside_err_nf')); @@ -36,6 +36,8 @@ return $modx->error->failure($modx->lexicon('asides.aside_err_save')); } -/* output */ +// output $asideArray = $aside->toArray('',true); + +//@TODO: optimum cache clearing return $modx->error->success('',$asideArray); \ No newline at end of file