edit_menu.php

Go to the documentation of this file.
00001 <?php
00002   /*
00003    This file is part of DocBookWiki.  DocBookWiki is a web application
00004    that displays and edits DocBook documents.
00005 
00006    Copyright (C) 2004, 2005, 2006, 2007
00007    Dashamir Hoxha, dashohoxha@users.sourceforge.net
00008 
00009    DocBookWiki is free software; you can redistribute it and/or modify
00010    it under the  terms of the GNU General  Public License as published
00011    by the Free  Software Foundation; either version 2  of the License,
00012    or (at your option) any later version.
00013 
00014    DocBookWiki is distributed in the  hope that it will be useful, but
00015    WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
00016    MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
00017    General Public License for more details.
00018 
00019    You should have  received a copy of the  GNU General Public License
00020    along  with  DocBookWiki;  if  not,  write  to  the  Free  Software
00021    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00022    USA
00023   */
00024 
00032 class edit_menu extends WebObject
00033 {
00034   function init()
00035   {
00036     //the menu item that is being edited, initially the root
00037     $this->addSVar('item_id', 'menu');
00038     $this->addSVar('item_bookid', '');
00039     $this->addSVar('item_caption', '');
00040 
00041     //keeps the items that are copied and can be pasted somewhere else
00042     $this->addSVar('clipboard', '');
00043   }
00044 
00046   function on_select($event_args)
00047   {
00048     $item_id = $event_args['item_id'];
00049     $this->select_item($item_id);
00050   }
00051 
00056   function select_item($item_id)
00057   {
00058     //get the bookid and the caption of the selected item
00059     $output = $this->transform('get_bookid.xsl', array('id'=>$item_id));
00060     list($bookid, $caption) = split(' ', $output, 2);
00061 
00062     //set state variables
00063     $this->setSVar('item_id', $item_id);
00064     $this->setSVar('item_bookid', $bookid);
00065     $this->setSVar('item_caption', $caption);
00066   }
00067 
00068   function transform_menu($transformer, $arr_params =array())
00069   {
00070     //apply the transformer and get the new menu
00071     $new_menu = $this->transform($transformer, $arr_params);
00072 
00073     //save the transformed menu
00074     $menu_path = ADMIN.'edit_menu/menu/';
00075     write_file($menu_path.'menu.xml', $new_menu);
00076   }
00077 
00082   function transform($transformer, $arr_params =array())
00083   {
00084     //construct the string $params
00085     $params = '';
00086     while (list($p_name, $p_value) = each($arr_params))
00087       {
00088         $params .= "--stringparam $p_name \"$p_value\" ";
00089       }
00090 
00091     //apply the $transformer with $params to menu.xml
00092     $menu_path = ADMIN.'edit_menu/menu/';
00093     $menu_xml = $menu_path.'menu.xml';
00094     $xsl_file = $menu_path."xsl/$transformer";
00095     $result = shell("xsltproc $params $xsl_file $menu_xml");
00096     //print "<xmp>$result</xmp>\n";
00097 
00098     return $result;
00099   }
00100 
00102   function on_update($event_args)
00103   {
00104     $params = $event_args;
00105     $item_id = $this->getSVar('item_id');
00106     $params['id'] = $item_id;
00107     $this->transform_menu('update.xsl', $params);
00108     $this->select_item($item_id);
00109   }
00110 
00112   function on_delete($event_args)
00113   {
00114     $params['id'] = $this->getSVar('item_id');
00115     $parent_id = $this->transform('get_parent_id.xsl', $params);
00116     $this->transform_menu('delete.xsl', $params);
00117     $this->select_item($parent_id);
00118   }
00119 
00121   function on_move_up($event_args)
00122   {
00123     $params['id'] = $event_args['item_id'];
00124     $this->transform_menu('move_up.xsl', $params);
00125   }
00126 
00128   function on_move_down($event_args)
00129   {
00130     $params['id'] = $event_args['item_id'];
00131     $this->transform_menu('move_down.xsl', $params);
00132   }
00133 
00135   function on_add_subitem($event_args)
00136   {
00137     $params = $event_args;
00138     $params['id'] = $this->getSVar('item_id');
00139     $this->transform_menu('add_subitem.xsl', $params);
00140   }
00141 
00143   function on_paste($event_args)
00144   {
00145     $clipboard = $this->getSVar('clipboard');
00146     $clipboard_items = explode("\n", $clipboard);
00147 
00148     //remove any doublicated items in clipboard
00149     $arr_items = array();
00150     for ($i=0; $i < sizeof($clipboard_items); $i++)
00151       {
00152         $item = trim($clipboard_items[$i]);
00153         if ($item=='')  continue;
00154         if (in_array($item, $arr_items))  continue;
00155         $arr_items[] = $item;
00156       }
00157 
00158     //add the items of the clipboard as subitems
00159     $item_id = $this->getSVar('item_id');
00160     for ($i=0; $i < sizeof($arr_items); $i++)
00161       {
00162         $item = $arr_items[$i];
00163         list($copy_id, $bookid, $caption) = split(':', $item, 3);
00164 
00165         //add the subitem
00166         $params['id'] = $item_id;
00167         $params['copy_id'] = $copy_id;
00168         $this->transform_menu('copy_item.xsl', $params);
00169       }
00170   }
00171 
00173   function on_apply($event_args)
00174   {
00175     //update menu.xml
00176     $menu_xml = MENU.'menu.xml';
00177     $edit_menu_xml = ADMIN.'edit_menu/menu/menu.xml';
00178     shell("cp $edit_menu_xml $menu_xml");
00179 
00180     //update menu_items.js
00181     $menu_items = $this->transform('menu_items.xsl');
00182     write_file(MENU.'menu_items.js', $menu_items);
00183 
00184     //update book_list.php
00185     $book_list = $this->transform('book_list.xsl');
00186     write_file(MENU.'book_list.php', $book_list);
00187 
00188     //select the root item
00189     $this->select_item('menu');
00190   }
00191 
00193   function on_cancel($event_args)
00194   {
00195     $menu_xml = MENU.'menu.xml';
00196     $edit_menu_xml = ADMIN.'edit_menu/menu/menu.xml';
00197     shell("cp $menu_xml $edit_menu_xml");
00198 
00199     //select the root item
00200     $this->select_item('menu');
00201   }
00202 
00203   function onRender()
00204   {
00205     $this->add_subitems_rs();
00206     $this->add_books_rs();
00207   }
00208 
00210   function add_subitems_rs()
00211   {
00212     //get the subitems
00213     $item_id = $this->getSVar('item_id');
00214     $items = $this->transform('subitems.xsl', array('id'=>$item_id));
00215     $arr_lines = explode("\n", $items);
00216 
00217     //create a recordset with id-s and captions of the subitems
00218     $rs = new EditableRS('subitems');
00219     for ($i=0; $i < sizeof($arr_lines); $i++)
00220       {
00221         list($id, $bookid, $caption) = split(' ', $arr_lines[$i], 3);
00222         if ($id=='')  continue;
00223         $rs->addRec(compact('id', 'bookid', 'caption'));
00224       }
00225     global $webPage;
00226     $webPage->addRecordset($rs);
00227   }
00228 
00233   function add_books_rs()
00234   {
00235     //books
00236     $rs = new EditableRS('booklist');
00237     $rs->addRec(array('id'=>'', 'label'=>'----- '.T_("Select").' -----'));
00238     $output = shell('ls '.BOOKS);
00239     $arr_lines = explode("\n", $output);
00240     for ($i=0; $i < sizeof($arr_lines); $i++)
00241       {
00242         $book_id = trim($arr_lines[$i]);
00243         if ($book_id=='')  continue;
00244         $rs->addRec(array('id' => $book_id, 'label' => $book_id));
00245       }
00246     global $webPage;
00247     $webPage->addRecordset($rs);      
00248   }
00249 }
00250 ?>

Generated on Wed Jan 9 08:27:32 2008 for DokBookWiki by  doxygen 1.5.2