Commit 4af6fa32 authored by Ghislain Loas's avatar Ghislain Loas
Browse files

hotline #156533 replace shell albums category deletion by ajax

parent 8f232eef
Pipeline #17519 passed with stage
in 43 minutes and 28 seconds
- correctif #156533 : Administration : Correction et amélioration de la suppression des collections.
\ No newline at end of file
......@@ -9,7 +9,7 @@
$delete_action = array('icon' => 'delete', 'help' => 'Supprimer la catégorie');
if (!$this->categorie->hasSousCategories() and !$this->categorie->hasAlbums())
$delete_action['action'] = 'delete_categorie';
$delete_action['action'] = 'delete-category-dashboard';
else
$delete_action['onclick'] = "alert('Suppression non autorisée: cette catégorie n\'est pas vide')";
......
<?php
echo $this->deleteAlbumCategoryDashboard($this->category);
......@@ -300,4 +300,19 @@ class Class_AlbumCategorie extends Storm_Model_Abstract {
return $ids;
}
public function countAllAlbums() : int {
return (int) $this->numberOfAlbums()
+ $this->_countAlbumsInChildren();
}
protected function _countAlbumsInChildren() : int {
$count = 0;
foreach($this->getChildren() as $child)
$count += $child->countAllAlbums();
return $count;
}
}
\ No newline at end of file
<?php
/**
* Copyright (c) 2012-2022, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_AlbumCategorie_Cleaner {
protected Class_AlbumCategorie $_category;
protected Class_AlbumCategorie_DataCounters $_counters;
public function __construct(Class_AlbumCategorie $category,
Class_AlbumCategorie_DataCounters $counters) {
$this->_category = $category;
$this->_counters = $counters;
}
public function delete() : self {
return $this->_deleteInSubCategories($this->_category);
}
protected function _deleteInSubCategories(Class_AlbumCategorie $category) : self {
if ( ! $category->hasSousCategories())
return $this->_deleteAlbumsAndCategory($category);
foreach ($category->getSousCategories() as $category)
return $this->_deleteInSubCategories($category);
return $this;
}
protected function _deleteAlbumsAndCategory(Class_AlbumCategorie $category) : self {
$albums =
Class_Album::findAllBy(['cat_id' => $category->getId(),
'limit' => 100]);
foreach($albums as $album)
$this->_deleteAlbum($album);
return $this->_deleteCategory($category);
}
protected function _deleteAlbum(Class_Album $album) : void {
$this->_counters->incrementTotalDeletedAlbums();
if ( $album->getNoticeId())
$this->_counters->incrementTotalDeletedRecords();
$album->delete();
}
protected function _deleteCategory(Class_AlbumCategorie $category) : self {
Class_Album::clearCache();
if (0 !== $category->numberOfAlbums())
return $this;
if ( $category->getId() !== $this->_category->getId())
$this->_counters->incrementTotalDeletedSubCategories();
$category->delete();
return $this;
}
public function countDeletedAlbums() : int {
return $this->_counters->totalDeletedAlbums();
}
public function countTotalAlbums() : int {
return $this->_counters->countTotalAlbums();
}
public function countDeletedSubCategories() : int {
return $this->_counters->totalDeletedSubCategories();
}
public function countTotalSubCategories() : int {
return $this->_counters->countTotalSubCategories();
}
public function countDeletedRecords() : int {
return $this->_counters->totalDeletedRecords();
}
public function countTotalRecords() : int {
return $this->_counters->countTotalRecords();
}
public function countDeletedAlbumsPercent() : int {
return ( 0 !== ($total_albums = $this->countTotalAlbums()))
? intval(($this->_counters->totalDeletedAlbums() / $total_albums) * 100)
: 0;
}
public function countRemainingAlbums() : int {
return $this->countTotalAlbums() - $this->_counters->totalDeletedAlbums();
}
public function countRemainingSubCategories() : int {
return $this->countTotalSubCategories() - $this->_counters->totalDeletedSubCategories();
}
public function countRemainingRecords() : int {
return $this->countTotalRecords() - $this->_counters->totalDeletedRecords();
}
}
<?php
/**
* Copyright (c) 2012-2022, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_AlbumCategorie_DataCounters {
protected Class_AlbumCategorie $_category;
protected int $_total_albums = 0;
protected int $_total_sub_categories = 0;
protected int $_total_records = 0;
protected int $_total_deleted_albums = 0;
protected int $_total_deleted_sub_categories = 0;
protected int $_total_deleted_records = 0;
public function __construct(Class_AlbumCategorie $category,
int $total_albums = 0,
int $total_sub_categories = 0,
int $total_records = 0,
int $total_deleted_albums = 0,
int $total_deleted_sub_categories = 0,
int $total_deleted_records = 0) {
$this->_category = $category;
$this->_total_deleted_albums = $total_deleted_albums;
$this->_total_deleted_records = $total_deleted_records;
$this->_total_deleted_sub_categories = $total_deleted_sub_categories;
$this->_initCounters($total_albums,
$total_sub_categories,
$total_records);
}
protected function _initCounters(int $total_albums,
int $total_sub_categories,
int $total_records) : void {
$this->_total_albums = $total_albums
? $total_albums
: $this->_category->countAllAlbums();
$this->_total_sub_categories = $total_sub_categories
? $total_sub_categories
: $this->_countAllSubCategories($this->_category);
$this->_total_records = $total_records
? $total_records
: $this->_countAllRecords($this->_category);
}
public function countTotalAlbums() : int {
return $this->_total_albums;
}
public function countTotalSubCategories() : int {
return $this->_total_sub_categories;
}
public function countTotalRecords() : int {
return $this->_total_records;
}
protected function _countAllSubCategories(Class_AlbumCategorie $category) : int {
return $category->numberOfSousCategories()
+ $this->_countSubCategoriesInSubCategories($category);
}
protected function _countSubCategoriesInSubCategories(Class_AlbumCategorie $category) : int{
$count = 0;
foreach($category->getSousCategories() as $child)
$count += $this->_countAllSubCategories($child);
return $count;
}
protected function _countAllRecords(Class_AlbumCategorie $category) : int {
return Class_Album::countBy(['cat_id' => $category->getId(),
'notice_id not' => null])
+ $this->_countAllRecordsInSubCategories($category);
}
protected function _countAllRecordsInSubCategories(Class_AlbumCategorie $category) : int{
$count = 0;
foreach($category->getSousCategories() as $child)
$count += $this->_countAllRecords($child);
return $count;
}
public function incrementTotalDeletedAlbums() : void {
$this->_total_deleted_albums++;
}
public function incrementTotalDeletedRecords() : void {
$this->_total_deleted_records++;
}
public function incrementTotalDeletedSubCategories() : void {
$this->_total_deleted_sub_categories++;
}
public function totalDeletedAlbums() : int {
return $this->_total_deleted_albums;
}
public function totalDeletedSubCategories() : int {
return $this->_total_deleted_sub_categories;
}
public function totalDeletedRecords() : int {
return $this->_total_deleted_records;
}
}
......@@ -118,20 +118,7 @@ class ZendAfi_Controller_Action_Helper_ListViewMode_Album extends ZendAfi_Contro
if (!$model)
return 0;
return $model->numberOfAlbums()
+ $this->countItemsInChildrenOf($model);
}
protected function countItemsInChildrenOf($model) {
if (!$model)
return 0;
$count = 0;
foreach($model->getChildren() as $child)
$count += $this->countItemsInTreeFrom($child);
return $count;
return $model->countAllAlbums();
}
......
......@@ -58,15 +58,48 @@ class ZendAfi_Controller_Plugin_Manager_Album extends ZendAfi_Controller_Plugin_
}
public function deletecategorieAction() {
if (!$category = Class_AlbumCategorie::find((int)$this->_getParam('id')))
$this->_redirect('admin/album');
public function deleteCategoryDashboardAction() {
if ( ! $this->_view->category = Class_AlbumCategorie::find((int)$this->_getParam('id'))) {
$this->_helper->notify($this->_('Impossible de supprimer la catégorie demandée, celle-ci est introuvable'));
return $this->_redirect('admin/album');
}
$this->_helper->notify($this->_('La suppression de la catégorie %s a bien été prise en compte.',
$category->getLibelle()));
$this->_view->titre = $this->_('Supprimer la catégorie %s',
$this->_view->category->getLibelle());
}
(new Class_AlbumCategorie_Remover(['Id' => $category->getId()]))->run();
$this->_redirect('admin/album');
public function runDeleteCategoryAction() : void {
session_write_close();
if ( ! $category = Class_AlbumCategorie::find((int)$this->_getParam('id'))) {
$this->_helper->notify($this->_('Impossible de supprimer la catégorie demandée, celle-ci est introuvable'));
$this->_redirectClose($this->_getReferer());
return;
}
$this->_view->titre = $this->_('Suppression de la catégorie %s en cours',
$category->getLibelle());
$counts =
new Class_AlbumCategorie_DataCounters($category,
$this->_getParam('count_albums', 0),
$this->_getParam('count_sub_categories', 0),
$this->_getParam('count_records', 0),
$this->_getParam('count_deleted_albums', 0),
$this->_getParam('count_deleted_sub_categories', 0),
$this->_getParam('count_deleted_records', 0));
$cleaner = (new Class_AlbumCategorie_Cleaner($category, $counts))->delete();
$this->_view->content =
$this->_view->deleteAlbumCategory($cleaner);
if ( 'ajax' !== $this->_getParam('render', ''))
return;
$this->_helper->getHelper('HTMLAjaxResponse')
->htmlAjaxResponseWithScript($this->_view->content);
}
......@@ -734,25 +767,10 @@ class ZendAfi_Controller_Plugin_Manager_Album extends ZendAfi_Controller_Plugin_
['url' => ['module' => 'admin',
'controller' => 'album',
'action' => 'delete_categorie',
'action' => 'delete-category-dashboard',
'id' => '%s'],
'icon' => 'delete',
'label' => $this->_('Supprimer la catégorie'),
'anchorOptions' => ['rel' => 'delete-album-category'],
'caption' => function($model)
{
Class_ScriptLoader::getInstance()->addJQueryReady("
function deleteAlbumCategory(event) {
var target = $(event.target).closest('a');
var answer = confirm(\"".$this->_("Etes-vous sûr de vouloir supprimer cette catégorie ?")."\");
if (answer == false) {
event.preventDefault();
return;
}
}
$(\"a[rel='delete-album-category']\").click(deleteAlbumCategory);
");}]
'label' => $this->_('Supprimer la catégorie %s', $model->getLibelle())]
];
}
}
\ No newline at end of file
<?php
/**
* Copyright (c) 2012-2022, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ZendAfi_View_Helper_Admin_DeleteAlbumCategory extends ZendAfi_View_Helper_BaseHelper {
protected Class_AlbumCategorie_Cleaner $_cleaner;
public function deleteAlbumCategory(Class_AlbumCategorie_Cleaner $cleaner) : string {
$this->_cleaner = $cleaner;
$html =
$this->_tag('h3',
$this->_('Suppression en cours'))
. $this->_tagWarning('Pour continuer la suppression, veuillez garder cette page ouverte')
. $this->_tagNotice('Vous serez redirigé sur la liste des collections une fois la suppression terminée')
. $this->_countDeletedAlbums()
. $this->_countDeletedSubCategories()
. $this->_countDeletedRecords()
. $this->_progressBar()
. BR
. $this->_cancelButton();
return $this->_div(['class' => 'delete_album_category',
'data-delete-url' => $this->_getNewDeleteUrl(),
'data-count-remaining-albums' => $this->_cleaner->countRemainingAlbums(),
'data-count-remaining-sub-categories' => $this->_cleaner->countRemainingSubCategories(),
'data-count-remaining-records' => $this->_cleaner->countRemainingRecords()],
$html);
}
protected function _countDeletedAlbums() : string {
return $this->_addTextToCounters($this->_cleaner->countDeletedAlbums(),
$this->_cleaner->countTotalAlbums(),
$this->_('album'));
}
protected function _countDeletedSubCategories() : string {
return $this->_addTextToCounters($this->_cleaner->countDeletedSubCategories(),
$this->_cleaner->countTotalSubCategories(),
$this->_('sous catégorie'));
}
protected function _countDeletedRecords() : string {
return $this->_addTextToCounters($this->_cleaner->countDeletedRecords(),
$this->_cleaner->countTotalRecords(),
$this->_('notice'));
}
protected function _addTextToCounters(int $count_deleted,
int $count_total,
string $label) : string {
if ( 0 === $count_total)
return '';
$plural =
$this->_plural($count_deleted,
$label . ' supprimé(e) : 0 / ' . $count_total,
$label . ' supprimé(e) : 1 / ' . $count_total,
$label . 's supprimé(e)s : %d / ' . $count_total,
$count_deleted);
return $this->_tagWarning($plural);
}
protected function _progressBar() : string {
$id = 'progress_bar_delete_album_category';
$script = sprintf('$("#%s").progressbar({value: %d});',
$id,
$this->_cleaner->countDeletedAlbumsPercent());
Class_ScriptLoader::getInstance()->addJQueryReady($script);
return $this->_div(['id' => $id], '');
}
protected function _getNewDeleteUrl() : string {
$new_counters_params =
['count_deleted_albums' => $this->_cleaner->countDeletedAlbums(),
'count_deleted_records' => $this->_cleaner->countDeletedRecords(),
'count_deleted_sub_categories' => $this->_cleaner->countDeletedSubCategories()];
return $this->view->url($new_counters_params);
}
protected function _cancelButton() : string {
return
$this->view->button((new Class_Button)
->setImage($this->view->renderIcon('class fa fa-fire-extinguisher'))
->setText($this->_('Stopper la suppression'))
->setAttribs(['onclick' => '',
'id' => 'stop_delete']));
}
}
\ No newline at end of file
<?php
/**
* Copyright (c) 2012-2022, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ZendAfi_View_Helper_Admin_DeleteAlbumCategoryDashboard extends ZendAfi_View_Helper_BaseHelper {
public function deleteAlbumCategoryDashboard(Class_AlbumCategorie $category) : string {
$id = 'delete_album_category_dashboard_' . $category->getId();
Class_ScriptLoader::getInstance()
->addScript(Class_Url::absolute('public/admin/js/delete-album-category/delete-album-category.js'))
->addJqueryReady(sprintf('$("#%s").delete_album_category();', $id));
$confirm_message = $this->_('Êtes vous sûr ?');
$counter = new Class_AlbumCategorie_DataCounters($category);
$delete_url = $this->view->url(['module' => 'admin',
'controller' => 'album',
'action' => 'run-delete-category',