openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 4ca60099235656be44dbade8eca55bc458933988
parent e769b36a2a257922c17b7259df2b3ead3f85e1ff
Author: dankert <openrat@jandankert.de>
Date:   Mon,  6 Dec 2021 22:33:10 +0100

Some fixes for deleting objects.

Diffstat:
Mmodules/cms/action/folder/FolderRemoveAction.class.php | 10++++++++--
Mmodules/cms/action/language/LanguageRemoveAction.class.php | 20++++++++++++++++++--
Mmodules/cms/model/BaseObject.class.php | 3++-
Mmodules/cms/model/Content.class.php | 6+++---
Mmodules/cms/model/Element.class.php | 50++++++++++++++++++++++++++++++++++++++++----------
Mmodules/cms/model/Language.class.php | 61++++++++++++++++++++++++++++++-------------------------------
Mmodules/cms/model/PageContent.class.php | 14++++++--------
Mmodules/cms/model/Template.class.php | 2+-
8 files changed, 108 insertions(+), 58 deletions(-)

diff --git a/modules/cms/action/folder/FolderRemoveAction.class.php b/modules/cms/action/folder/FolderRemoveAction.class.php @@ -4,6 +4,7 @@ use cms\action\FolderAction; use cms\action\Method; use cms\model\Permission; use language\Messages; +use util\exception\ValidationException; class FolderRemoveAction extends FolderAction implements Method { @@ -20,11 +21,16 @@ class FolderRemoveAction extends FolderAction implements Method { public function post() { - if ( $this->request->has( 'withChildren')) + + if ( $this->folder->isRoot() ) + // Could not delete the root folder on user request. + throw new ValidationException("parent",Messages::FOLDER_ROOT); + + if ( $this->request->has( 'withChildren')) $this->folder->deleteAll(); // Delete with children else if ( $this->folder->hasChildren() ) - throw new \util\exception\ValidationException("withChildren",Messages::CONTAINS_CHILDREN); + throw new ValidationException("withChildren",Messages::CONTAINS_CHILDREN); else $this->folder->delete(); // Only delete current folder. diff --git a/modules/cms/action/language/LanguageRemoveAction.class.php b/modules/cms/action/language/LanguageRemoveAction.class.php @@ -2,14 +2,30 @@ namespace cms\action\language; use cms\action\LanguageAction; use cms\action\Method; +use cms\model\Project; +use language\Messages; class LanguageRemoveAction extends LanguageAction implements Method { + public function view() { + $this->setTemplateVar('name' ,$this->language->name ); } + + public function post() { - if ( $this->request->getText('confirm') == '1' ) - $this->language->delete(); + if ( $this->request->getRequiredNumber('confirm') ) { + + $project = Project::create( $this->language->projectid ); + + // There must be at least 1 language + if ( count( $project->getLanguageIds() ) > 1 ) { + + $this->language->delete(); + + $this->addNoticeFor( $this->language, Messages::DELETED ); + } + } } } diff --git a/modules/cms/model/BaseObject.class.php b/modules/cms/model/BaseObject.class.php @@ -982,9 +982,10 @@ SQL $user = \util\Session::getUser(); - if ( ! $user ) { // User logged in? + if ( $user ) { // User logged in? $permission = new Permission(); + $permission->type = Permission::TYPE_USER; $permission->userid = $user->userid; $permission->objectid = $this->objectid; diff --git a/modules/cms/model/Content.class.php b/modules/cms/model/Content.class.php @@ -320,7 +320,7 @@ SQL { // Delete all values $stmt = DB::sql( <<<SQL - DELETE * FROM {{value}} + DELETE FROM {{value}} WHERE contentid = {contentid} SQL ); @@ -329,8 +329,8 @@ SQL // Delete the content $stmt = DB::sql( <<<SQL - DELETE * FROM {{content}} - WHERE contentid = {contentid} + DELETE FROM {{content}} + WHERE id = {contentid} SQL ); $stmt->setInt( 'contentid' ,$this->id ); diff --git a/modules/cms/model/Element.class.php b/modules/cms/model/Element.class.php @@ -222,7 +222,7 @@ class Element extends ModelBase { if ( intval($this->elementid) != 0 ) { - $db = \cms\base\DB::get(); + $db = Db::get(); $sql = $db->sql( <<<SQL SELECT * FROM {{element}} WHERE id={elementid} @@ -355,7 +355,7 @@ SQL */ public function delete() { - $db = \cms\base\DB::get(); + $db = Db::get(); // Inhalte loeschen. // notwendig, damit die Fremdschl├╝sselbeziehungen auf diesen Element aufgehoben werden. @@ -371,18 +371,48 @@ SQL /** - * L?schen aller Seiteninhalte mit diesem Element - * Das Element wird nicht gel?scht. + * Deletes all values for this element. */ public function deleteValues() { - $db = \cms\base\DB::get(); - - // Alle Inhalte mit diesem Element l?schen - $sql = $db->sql('DELETE FROM {{value}} '. - ' WHERE elementid={elementid}' ); + $sql = DB::sql( <<<SQL + SELECT id as pagecontentid,contentid + FROM {{pagecontent}} + WHERE elementid = {elementid} +SQL + ); $sql->setInt( 'elementid',$this->elementid ); - $sql->execute(); + + foreach( $sql->getAll() as $pagecontentRow ) { + $sql = DB::sql( <<<SQL + DELETE + FROM {{value}} + WHERE contentid = {contentid} +SQL + ); + $sql->setInt( 'contentid',$pagecontentRow['contentid'] ); + $sql->execute(); + + + $sql = DB::sql( <<<SQL + DELETE + FROM {{pagecontent}} + WHERE id = {pagecontentid} +SQL + ); + $sql->setInt( 'pagecontentid',$pagecontentRow['pagecontentid'] ); + $sql->execute(); + + + $sql = DB::sql( <<<SQL + DELETE + FROM {{content}} + WHERE id = {contentid} +SQL + ); + $sql->setInt( 'contentid',$pagecontentRow['contentid'] ); + $sql->execute(); + } } diff --git a/modules/cms/model/Language.class.php b/modules/cms/model/Language.class.php @@ -171,7 +171,7 @@ class Language extends ModelBase // Diese Sprache als 'default' markieren. - function setDefault() + public function setDefault() { $db = \cms\base\DB::get(); @@ -191,43 +191,42 @@ class Language extends ModelBase } - // Sprache entfernen - function delete() + /** + * Delete language + */ + public function delete() { $db = \cms\base\DB::get(); - // Sprache l?schen -// $sql = $db->sql( 'SELECT COUNT(*) FROM {{language}} WHERE projectid={projectid}' ); -// $sql->setInt( 'projectid',$this->projectid ); -// $count = $sql->getOne( $sql ); -// -// // Nur l?schen, wenn es mindestens 2 Sprachen gibt -// if ( $count >= 2 ) -// { - // Inhalte mit dieser Sprache l?schen - $sql = $db->sql( 'DELETE FROM {{value}} WHERE languageid={languageid}' ); - $sql->setInt( 'languageid',$this->languageid ); - $sql->execute(); - - // Inhalte mit dieser Sprache l?schen - $sql = $db->sql( 'DELETE FROM {{name}} WHERE languageid={languageid}' ); - $sql->setInt( 'languageid',$this->languageid ); - $sql->execute(); - - // Sprache l?schen - $sql = $db->sql( 'DELETE FROM {{language}} WHERE id={languageid}' ); - $sql->setInt( 'languageid',$this->languageid ); - $sql->execute(); - - // Andere Sprache auf "Default" setzen + // Inhalte mit dieser Sprache l?schen + $sql = $db->sql( 'DELETE FROM {{pagecontent}} WHERE languageid={languageid}' ); + $sql->setInt( 'languageid',$this->languageid ); + $sql->execute(); + + // Inhalte mit dieser Sprache l?schen + $sql = $db->sql( 'DELETE FROM {{name}} WHERE languageid={languageid}' ); + $sql->setInt( 'languageid',$this->languageid ); + $sql->execute(); + + // Andere Sprache auf "Default" setzen + if ( $this->isDefault ) { + $sql = $db->sql( 'SELECT id FROM {{language}} WHERE projectid={projectid}' ); $sql->setInt( 'projectid',$this->projectid ); $new_default_languageid = $sql->getOne(); - $sql = $db->sql( 'UPDATE {{language}} SET is_default=1 WHERE id={languageid}' ); - $sql->setInt( 'languageid',$new_default_languageid ); - $sql->execute(); -// } + if ( $new_default_languageid ) + { + $sql = $db->sql( 'UPDATE {{language}} SET is_default=1 WHERE id={languageid}' ); + $sql->setInt( 'languageid',$new_default_languageid ); + $sql->execute(); + } + } + + // Sprache l?schen + $sql = $db->sql( 'DELETE FROM {{language}} WHERE id={languageid}' ); + $sql->setInt( 'languageid',$this->languageid ); + $sql->execute(); } public function setCurrentLocale() diff --git a/modules/cms/model/PageContent.class.php b/modules/cms/model/PageContent.class.php @@ -132,22 +132,20 @@ SQL /** - * Diesen Inhalt loeschen + * Delete this page content. */ - function delete() + public function delete() { - - $content = new Content( $this->contentId ); - $content->delete(); - $stmt = DB::sql( <<<SQL - DELETE * FROM {{pagecontent}} + DELETE FROM {{pagecontent}} WHERE id ={id} SQL ); $stmt->setInt( 'id' ,$this->id ); - $stmt->execute(); + + $content = new Content( $this->contentId ); + $content->delete(); } diff --git a/modules/cms/model/Template.class.php b/modules/cms/model/Template.class.php @@ -302,7 +302,7 @@ SQL * * Entfernen alle Templateinhalte und des Templates selber */ - function delete() + public function delete() { $db = \cms\base\DB::get();