#!/usr/bin/php
<?php
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
  Codificación: UTF-8
  +----------------------------------------------------------------------+
  | Elastix version 2.0                                                  |
  | http://www.elastix.org                                               |
  +----------------------------------------------------------------------+
  | Copyright (c) 2006 Palosanto Solutions S. A.                         |
  +----------------------------------------------------------------------+
  | Cdla. Nueva Kennedy Calle E 222 y 9na. Este                          |
  | Telfs. 2283-268, 2294-440, 2284-356                                  |
  | Guayaquil - Ecuador                                                  |
  | http://www.palosanto.com                                             |
  +----------------------------------------------------------------------+
  | The contents of this file are subject to the General Public License  |
  | (GPL) Version 2 (the "License"); you may not use this file except in |
  | compliance with the License. You may obtain a copy of the License at |
  | http://www.opensource.org/licenses/gpl-license.php                   |
  |                                                                      |
  | Software distributed under the License is distributed on an "AS IS"  |
  | basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See  |
  | the License for the specific language governing rights and           |
  | limitations under the License.                                       |
  +----------------------------------------------------------------------+
  | The Original Code is: Elastix Open Source.                           |
  | The Initial Developer of the Original Code is PaloSanto Solutions    |
  +----------------------------------------------------------------------+
 */

/*
This script takes one argument that indicates the path to a module.xml or menu.xml
file. This file indicates the menus that are to be merged into an Elastix menu
installation. The corresponding folders in /var/www/html/modules/MENU must exist.
*/

if ($argc < 2) die("Please specify path to menu.xml for module to be installed.\n");
if (!is_readable($argv[1])) die("FATAL: unable to read $argv[1] \n");

$documentRoot="/var/www/html";
require_once("$documentRoot/libs/misc.lib.php");
require_once("$documentRoot/configs/default.conf.php");

//global variables framework
global $arrConf;
global $arrLang;
require_once("$arrConf[basePath]/libs/paloSantoDB.class.php");
require_once("$arrConf[basePath]/libs/paloSantoMenu.class.php");
require_once("$arrConf[basePath]/libs/paloSantoACL.class.php");
require_once("$arrConf[basePath]/libs/paloSantoModuloXML.class.php");
require_once("$arrConf[basePath]/libs/paloSantoInstaller.class.php");

$pDB = new paloDB("sqlite3:///{$arrConf['elastix_dbdir']}/menu.db");
$pDBACL = new paloDB("sqlite3:///{$arrConf['elastix_dbdir']}/acl.db");

if(!empty($pDB->errMsg)) echo "ERROR DE DB: {$pDB->errMsg}\n";
if(!empty($pDBACL->errMsg)) echo "ERROR DE DB: {$pDBACL->errMsg}\n";

$oMenu = new paloMenu($pDB);
$oACL = new paloACL($pDBACL);
$oInstaller = new Installer();

if(isset($_SERVER['argv'][1])){
    $file_xml = $_SERVER['argv'][1];
    $oModuloXML= new ModuloXML($file_xml);

    if(count($oModuloXML->_arbolMenu)>0){
        foreach ($oModuloXML->_arbolMenu as $menuitem){
            $idmenu=$menuitem['menuid'];
            echo "Menu: $idmenu found\n";
            if(!empty($idmenu)){
                //buscar si ya existe ese menu
                if(!$oMenu->existeMenu($idmenu)){
                    $newMenu["menuid"]=$idmenu;
                    $newMenu["module"]=$menuitem['module'];
                    $newMenu["parent"]=$menuitem['parent'];
                    $newMenu["tag"]=$menuitem['desc'];
                    $newMenu["link"]=$menuitem['link'];
                    $newMenu["order"]=$menuitem['order'];
                    $newPermission = array();
                    $newPermission = $menuitem['groups'];

                    $oInstaller->addMenu($oMenu,$newMenu);
                    if($newMenu['parent']!="") //agregar los permisos
                        $oInstaller->addResourceMembership($oACL,$newMenu,$newPermission);
                }else{// si ya existe solo se debe actualizar el Parent, tag, link y order
                    echo "Updating: $idmenu found\n";
                    $newMenu["menuid"]=$idmenu;
                    $newMenu["module"]=$menuitem['module'];
                    $newMenu["parent"]=$menuitem['parent'];
                    $newMenu["tag"]=$menuitem['desc'];
                    $newMenu["link"]=$menuitem['link'];
                    $newMenu["order"]=$menuitem['order'];
                    $oInstaller->UpdateMenu($oMenu,$newMenu);
					if($newMenu['parent']!="") //agregar los permisos
                        $oInstaller->updateResourceMembership($oACL,$newMenu);
                }
            }
            else{
                echo "Menu for module is not defined\n";
                return 1;
            }
        }
    }
}
else{
    echo "Error: File xml not defined. Usage: {$_SERVER['argv'][0]} menu.xml\n";
    return 1;
}

deleteSmartyCompileFiles();
function deleteSmartyCompileFiles()
{
    $files="/var/www/html/var/templates_c/*.php";
    foreach(glob($files) as $filename)
        unlink($filename);
}
?>
