From a9f949d54ba588a2e772bf3acc70d730fb19f97d Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@afi-sa.fr>
Date: Tue, 7 Jul 2015 14:56:50 +0200
Subject: [PATCH] rel #27194 : add and delete blocks in front

---
 .../admin/controllers/AccueilController.php   | 43 ++++++++++
 .../views/scripts/accueil/add-block.phtml     | 25 ++++++
 library/Class/Profil.php                      | 30 ++++++-
 library/ZendAfi/View/Helper/Accueil/Base.php  |  2 +-
 .../View/Helper/Admin/ComboModuleAccueil.php  | 45 ++++++++++
 .../ZendAfi/View/Helper/FonctionsAdmin.php    | 86 ++++++++++++++-----
 public/opac/css/global.css                    |  2 +-
 7 files changed, 210 insertions(+), 23 deletions(-)
 create mode 100644 application/modules/admin/views/scripts/accueil/add-block.phtml
 create mode 100644 library/ZendAfi/View/Helper/Admin/ComboModuleAccueil.php

diff --git a/application/modules/admin/controllers/AccueilController.php b/application/modules/admin/controllers/AccueilController.php
index b74e16da5c8..7eb9b0da08b 100644
--- a/application/modules/admin/controllers/AccueilController.php
+++ b/application/modules/admin/controllers/AccueilController.php
@@ -66,6 +66,7 @@ class Admin_AccueilController extends Zend_Controller_Action {
 
 		$this->view->combo_templates = ZendAfi_View_Helper_Accueil_Base::getComboTemplates($boite, $this->profil);
 		$this->view->id_profil = $this->profil->getId();
+		$this->view->profil = $this->profil;
 		$this->view->id_bib = $this->profil->getIdSite();
 
 		$this->_systemeModulesAccueil = new Class_Systeme_ModulesAccueil();
@@ -74,6 +75,48 @@ class Admin_AccueilController extends Zend_Controller_Action {
 	}
 
 
+	public function addBlockAction() {
+		if ($this->_request->isPost()) {
+			$post = $this->_request->getPost();
+			$new_id = $this->profil->createNewModuleAccueilId();
+			$division = $this->_getParam('division');
+			$parent_id = $this->_getParam('id_module');
+
+			$config = ['type_module' => $post['module_type'],
+								 'preferences' => Class_Systeme_ModulesAccueil::getValeursParDefaut($post['module_type']),
+								 'division' => $division];
+
+			$config['preferences']['titre'] = $post['titre'];
+			$config['preferences']['boite'] = $post['boite'];
+
+			$parent_position = $this->profil->getModulePositionInDiv($parent_id, $division);
+
+			$this->profil->updateModuleConfigAccueil($new_id, $config);
+			$my_position = $this->profil->getModulePositionInDiv($new_id, $division);
+			$this->profil
+				->moveModuleOldDivPosNewDivPos($division, $my_position,
+																			 $division, $parent_position + 1)
+				->save();
+
+			$this->view->reload = 'SITE';
+			$viewRenderer = $this->getHelper('ViewRenderer');
+			$viewRenderer->renderScript('accueil/_retour.phtml');
+		}
+	}
+
+
+	public function deleteBlockAction() {
+		$this->profil
+			->removeBoiteFromDiv($this->_getParam('id_module'),
+													 $this->_getParam('division'))
+			->save();
+
+		$this->_redirect($this->view->url(['id_profil' => $this->profil->getId()],
+																			null, true),
+										 ['prependBase' => false]);
+	}
+
+
 	public function calendrierAction()	{
 		$this->_simpleAction('CALENDAR');
 	}
diff --git a/application/modules/admin/views/scripts/accueil/add-block.phtml b/application/modules/admin/views/scripts/accueil/add-block.phtml
new file mode 100644
index 00000000000..f74ad90bf7f
--- /dev/null
+++ b/application/modules/admin/views/scripts/accueil/add-block.phtml
@@ -0,0 +1,25 @@
+<center>
+	<h1><?php echo $this->_('Ajouter une boîte');?></h1><br>
+	<div class="formTable">
+		<form method="post" action="<?php echo $this->url ?>">
+			<fieldset>
+				<legend><?php echo $this->_('Généralités');?></legend>
+				<table cellspacing="2">
+					<tr>
+						<td class="droite"><?php echo $this->_('Type de boîte');?>&nbsp;</td>
+						<td class="gauche"><?php echo $this->comboModuleAccueil($this->profil); ?></td>
+					</tr>
+					<tr>
+						<td class="droite"><?php echo $this->_('Titre');?>&nbsp;</td>
+						<td class="gauche"><input type="text" name="titre" size="50" maxlength="60" value="** Nouvelle boîte **"></td>
+					</tr>
+    			<tr>
+    				<td class="droite"><?php echo $this->_('Style de boite');?>&nbsp;</td>
+    				<td class="gauche"><?php echo $this->combo_templates; ?></td>
+    			</tr>
+  			</table>
+			</fieldset>
+			<?php echo $this->formSubmit('', $this->_('Valider'), ["class" => "bouton"]); ?>
+ 	</form>
+ </div>
+</center>
diff --git a/library/Class/Profil.php b/library/Class/Profil.php
index ea6f1aa9b0a..5c4ff907ab0 100644
--- a/library/Class/Profil.php
+++ b/library/Class/Profil.php
@@ -1414,6 +1414,19 @@ class Class_Profil extends Storm_Model_Abstract {
 	}
 
 
+	public function removeBoiteFromDiv($id_module, $div) {
+		$cfg_accueil = $this->getCfgAccueilAsArray();
+		foreach($cfg_accueil['modules'] as $id => $config) {
+			if ($config['division'] == $div && $id == $id_module) {
+				unset($cfg_accueil['modules'][$id]);
+				break;
+			}
+		}
+
+		return $this->setCfgAccueil($cfg_accueil);
+	}
+
+
 	public function removeBoiteOfTypeBanniere($type_module) {
 		$cfg_accueil = $this->getCfgAccueilAsArray();
 		foreach ($cfg_accueil['modules'] as $index => $module)  {
@@ -1807,6 +1820,21 @@ class Class_Profil extends Storm_Model_Abstract {
 	}
 
 
+	public function getModulePositionInDiv($module_id, $div) {
+		$cfg_accueil = $this->getCfgAccueilAsArray();
+		$i = 0;
+		foreach($cfg_accueil['modules'] as $existing_id => $config) {
+			if ($config['division'] != $div)
+				continue;
+
+			if ($existing_id == $module_id)
+				return $i;
+
+			$i++;
+		}
+	}
+
+
 	public function moveModuleOldDivPosNewDivPos($old_div, $old_pos, $new_div, $new_pos) {
 		$cfg_accueil = $this->getCfgAccueilAsArray();
 		$id = $this->_getIdModuleAtDivPosInCfg($old_div, $old_pos, $cfg_accueil);
@@ -1833,7 +1861,7 @@ class Class_Profil extends Storm_Model_Abstract {
 
 		$cfg_accueil['modules'] = $new_modules;
 
-		$this->setCfgAccueil($cfg_accueil);
+		return $this->setCfgAccueil($cfg_accueil);
 	}
 
 
diff --git a/library/ZendAfi/View/Helper/Accueil/Base.php b/library/ZendAfi/View/Helper/Accueil/Base.php
index 056f226b343..811db4f5ea9 100644
--- a/library/ZendAfi/View/Helper/Accueil/Base.php
+++ b/library/ZendAfi/View/Helper/Accueil/Base.php
@@ -154,7 +154,7 @@ class ZendAfi_View_Helper_Accueil_Base extends ZendAfi_View_Helper_ModuleAbstrac
 //---------------------------------------------------------------------
 	protected function getFonctionAdmin()	{
 		$helper = $this->view->getHelper($this->_fonction_admin_helper);
-		return $helper->fonctionsAdmin($this->id_module, $this->type_module);
+		return $helper->fonctionsAdmin($this->id_module, $this->type_module, $this->division);
 	}
 
 
diff --git a/library/ZendAfi/View/Helper/Admin/ComboModuleAccueil.php b/library/ZendAfi/View/Helper/Admin/ComboModuleAccueil.php
new file mode 100644
index 00000000000..ad4ce9a2648
--- /dev/null
+++ b/library/ZendAfi/View/Helper/Admin/ComboModuleAccueil.php
@@ -0,0 +1,45 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, 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_ComboModuleAccueil
+	extends ZendAfi_View_Helper_BaseHelper{
+
+	public function comboModuleAccueil($profil) {
+		$modules_accueil = Class_Systeme_ModulesAccueil::getInstance();
+		$modules = $modules_accueil->getModules();
+		$groupes = $modules_accueil->getGroupes();
+
+		$hierarchy = [];
+		foreach($modules as $type => $module) {
+			if (!$module->isVisibleForProfil($profil))
+				continue;
+
+			$group_label = $groupes[$module->getGroup()];
+			if (!array_key_exists($group_label, $hierarchy))
+				$hierarchy[$group_label] = [];
+
+			$hierarchy[$group_label][$type] = $module->getLibelle();
+		}
+
+		return $this->view->formSelect('module_type', null, null, $hierarchy);
+	}
+}
\ No newline at end of file
diff --git a/library/ZendAfi/View/Helper/FonctionsAdmin.php b/library/ZendAfi/View/Helper/FonctionsAdmin.php
index 2633c35357c..5b3a47aed16 100644
--- a/library/ZendAfi/View/Helper/FonctionsAdmin.php
+++ b/library/ZendAfi/View/Helper/FonctionsAdmin.php
@@ -16,50 +16,96 @@
  *
  * 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 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
 class ZendAfi_View_Helper_FonctionsAdmin extends ZendAfi_View_Helper_BaseHelper {
 	protected $id_profil,	$type_module,	$id_module;
-	
-	public function fonctionsAdmin($id_module=0, $type_module=false) {
-		if (!Class_Users::isCurrentUserCanConfigFront())
-			return '';
 
+	public function fonctionsAdmin($id_module=0, $type_module=false, $division=null) {
 		$this->id_profil = Class_Profil::getCurrentProfil()->getId();
 		$this->type_module = $type_module;
 		$this->id_module = $id_module;
-		
-		$fonction = $this->getPopupParams();
-		if (!$fonction) 
+		$this->division = $division;
+
+		if (!Class_Users::isCurrentUserCanConfigFront())
 			return '';
 
-		$label = $this->translate()->_('Propriétés du module');
-		$onclick = "showPopWin('" . htmlspecialchars(BASE_URL . $fonction["url"]) . "'," 
-			. $fonction["popup_width"] ."," 
+		return ($actions = trim($this->_config() . ' ' . $this->_add() . ' ' . $this->_delete())) ?
+			$this->_tag('div', $actions,
+									['class' => 'configuration_module',
+									 'style' => 'text-align:right']) :
+			'';
+	}
+
+
+	protected function _config() {
+		if (!$fonction = $this->getPopupParams())
+			return '';
+
+		$label = $this->_('Propriétés du module');
+		$onclick = "showPopWin('" . htmlspecialchars(BASE_URL . $fonction["url"]) . "',"
+			. $fonction["popup_width"] .","
 			. $fonction["popup_height"] . ", null)";
 
-		return '<div class="configuration_module" style="text-align:right">'
-			. $this->view->tagImg(URL_ADMIN_IMG . 'ico/fonctions_admin.png',
-														['onclick' => $onclick,
-														 'alt' => $label,
-														 'title' => $label])
-			. '</div>';
+		return $this->view->tagImg(URL_ADMIN_IMG . 'ico/fonctions_admin.png',
+															 ['onclick' => $onclick,
+																'alt' => $label,
+																'title' => $label]);
 	}
 
+
 	protected function getPopupParams()	{
 		$controller = $this->view->_current_module['controller'];
 		$action = $this->view->_current_module['action'];
 		$action2 = $this->view->_current_module['action2'];
-		
+
 		$cls_module = new Class_Systeme_ModulesAppli();
 		$props = $cls_module->getModule($controller, $action);
-		if(!$props) 
+		if(!$props)
 			return false;
-		
+
 		$ret["url"] = "/admin/modules/" . $controller . "?config=site&type_module=" . $controller . "&id_profil=" . $this->id_profil . "&action1=" . $action . "&action2=" . $action2;
 		$ret["popup_width"] = $props["popup_width"];
 		$ret["popup_height"] = $props["popup_height"];
 		return $ret;
 	}
+
+
+	protected function _add() {
+		$label = $this->_('Ajouter une boîte en-dessous');
+		$url = $this->view->url(['module' => 'admin',
+														 'controller' => 'accueil',
+														 'action' => 'add-block',
+														 'id_module' => $this->id_module,
+														 'division' => $this->division],
+														null, true);
+
+		$onclick = "showPopWin('" . htmlspecialchars($url) . "',800, 600, null)";
+
+		return $this->view->tagImg(URL_ADMIN_IMG . 'ico/copier.gif',
+															 ['onclick' => $onclick,
+																'alt' => $label,
+																'title' => $label]);
+	}
+
+
+	protected function _delete() {
+		$label = $this->_('Supprimer');
+		$url = $this->view->url(['module' => 'admin',
+														 'controller' => 'accueil',
+														 'action' => 'delete-block',
+														 'id_module' => $this->id_module,
+														 'division' => $this->division],
+														null, true);
+
+		$onclick = "return confirm('". htmlspecialchars($this->_('Êtes-vous sur de vouloir supprimer cette boîte ?')) . "');";
+
+		return $this->_tag('a',
+											 $this->view->tagImg(URL_ADMIN_IMG . 'ico/del.gif',
+																					 ['onclick' => $onclick,
+																						'alt' => $label,
+																						'title' => $label]),
+											 ['href' => $url]);
+	}
 }
\ No newline at end of file
diff --git a/public/opac/css/global.css b/public/opac/css/global.css
index 7255c900e70..c2f1b67cfbc 100644
--- a/public/opac/css/global.css
+++ b/public/opac/css/global.css
@@ -45,7 +45,7 @@ a {
 }
 
 .configuration_module + .newsadd {
-    margin-right:25px;
+    margin-right:60px;
 }
 
 .select_kiosque_form {
-- 
GitLab