module_path = str_replace(basename(__FILE__),"",__FILE__);
parent::__construct($id);
}
public function get_manage_form(){
global $base_path;
//variables persos...
global $menu;
$form="
";
if($this->managed_datas['module']['menus']){
foreach($this->managed_datas['module']['menus'] as $key => $menu_infos){
$form.="
".$this->format_text($menu_infos['name'])."
";
}
}
$form.="
Ajouter un menu
";
$form.="
";
if($menu){
$form.=$this->get_managed_form_start(array('menu'=>$menu));
$form.=$this->get_managed_menu_form($menu);
$form.=$this->get_managed_form_end();
}
$form.="
";
return $form;
}
public function save_manage_form(){
global $menu;
global $menu_delete;
global $cms_module_menu_menu_name;
$params = $this->managed_datas['module'];
if($menu_delete){
unset($params['menus'][$menu_delete]);
}else{
//ajout d'un menu
if($menu == "new"){
$menu_infos = array(
'name' => $cms_module_menu_menu_name
);
$params['menus']['menu'.(self::get_max_menu_id($this->managed_datas['module']['menus'])+1)] = $menu_infos;
}else{
//sinon on réécrit juste l'élément
$params['menus'][$menu]['name'] = $cms_module_menu_menu_name;
}
}
return $params;
}
protected function get_max_menu_id($datas){
$max = 0;
if(count($datas)){
foreach ($datas as $key => $val){
$key = str_replace("menu","",$key)*1;
if($key>$max) $max = $key;
}
}
return $max;
}
protected function get_managed_menu_form($menu){
global $opac_url_base;
global $base_path;
$infos = array();
if($menu != "new"){
$infos = $this->managed_datas['module']['menus'][$menu];
}
$form="
";
//nom du menu
$form.="
";
if($menu!="new"){
//sélecteur d'entrée
$form.="
".$this->format_text($this->msg['cms_module_menu_menu_add_entry'])."
";
//composition du menu...
$form.="
".$this->format_text($this->msg['cms_module_menu_manage_form_advertisements'])."
";
}
return $form;
}
function execute_ajax(){
global $charset;
global $do;
global $menu;
$response = array();
switch($do){
case "get_tree" :
if(!isset($this->managed_datas['module']['menus'][$menu]) || !isset($this->managed_datas['module']['menus'][$menu]['items'])){
$items = array(
'identifier' => 'id',
'label' => 'title',
'items' => array()
);
}else {
$items = array(
'identifier' => 'id',
'label' => 'title',
'items' => $this->managed_datas['module']['menus'][$menu]['items']
);
}
$response['content'] = json_encode($items);
$response['content-type'] = "application/json";
break;
case "save_tree" :
global $tree_infos;
global $elements;
// $this->debug("------------------------start-------------------------");
// $this->debug("------------------------posted-------------------------");
// $this->debug(stripslashes($elements));
// $this->debug($tree_infos);
$tree= array();
if($charset != 'utf-8'){
$elements = utf8_encode($elements);
}
$elements = json_decode(stripslashes($elements),true);
//$elements = $this->charset_normalize($elements,"utf-8");
$tree_infos = json_decode(stripslashes($tree_infos),true);
$tree_infos = array_reverse($tree_infos,true);
// $this->debug("------------------------entrée-------------------------");
// $this->debug($elements);
// $this->debug($tree_infos);
// $this->debug("------------------------debut boucle-------------------------");
foreach($tree_infos as $elem => $children){
// $this->debug("------------------------$elem-------------------------");
// $this->debug($children);
if($elements[$elem]){
$tree[$elem] = array(
'id' => $elem ,
'title' => $elements[$elem]['title'],
'link' => $elements[$elem]['link'],
'type' => $elements[$elem]['type']
);
unset($elements[$elem]);
}
if($elem == 0){
$name = 'items';
}else $name = 'children';
foreach($children as $child){
if($elements[$child]){
$tree[$elem][$name][] = array(
'id' => $child ,
'title' => $elements[$child]['title'],
'link' => $elements[$child]['link'],
'type' => $elements[$child]['type']
);
unset($elements[$child]);
}else if($tree[$child]){
$tree[$elem][$name][] = $tree[$child];
unset($tree[$child]);
}
}
// $this->debug("------------------------entrée-------------------------");
// $this->debug($elements);
// $this->debug("------------------------arbre-------------------------");
// $this->debug($tree);
}
$this->managed_datas['module']['menus'][$menu]['items'] = $tree[0]['items'];
$query = "replace into cms_managed_modules set managed_module_name = '".$this->class_name."', managed_module_box = '".$this->addslashes(serialize($this->managed_datas))."'";
mysql_query($query);
$response['content'] = "OK";
$response['content-type'] = "application/json";
break;
default :
$response = parent::execute_ajax();
break;
}
return $response;
}
function get_next_item_id($menu){
$max = $this->_get_max_item_id($this->managed_datas['module']['menus'][$menu]['items'],0)+1;
return $max;
}
function _get_max_item_id($items,$max){
if(is_array($items)){
foreach($items as $item){
if(count($item['children'])){
$max = $this->_get_max_item_id($item['children'],$max);
}
if($item['id'] > $max){
$max = $item['id'];
}
}
}
return $max;
}
}