#!/usr/bin/php
<?php
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
  Codificación: UTF-8
  +----------------------------------------------------------------------+
  | Issabel version 4.0                                                  |
  | http://www.issabel.org                                               |
  +----------------------------------------------------------------------+
  | Copyright (c) 2006 Palosanto Solutions S. A.                         |
  +----------------------------------------------------------------------+
  | 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 Initial Developer of the Original Code is PaloSanto Solutions    |
  +----------------------------------------------------------------------+
 */

/*
This script takes one argument, the name parent module or rpm name section.
This script delete menus and resources
*/

function error_exit($sMsg, $errorcode = 1)
{
    fwrite(STDERR, $sMsg);
    exit($errorcode);
}

if ($argc < 2) error_exit("Please specify the name of parent id menu.\n");

if (!($documentRoot = getenv('ISSABEL_ROOT'))) $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");

$pDB = new paloDB($arrConf['issabel_dsn']['menu']);
$pDBACL = new paloDB($arrConf['issabel_dsn']['acl']);

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

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

$idmenu = $argv[1];

$oMenu->_DB->beginTransaction();
$oACL->_DB->beginTransaction();

if ($oMenu->deleteMenu($idmenu, $oACL)) {
    $oMenu->_DB->commit();
    $oACL->_DB->commit();
} else {
    $errmsg = "ERR: Failed to remove menu item '{$idmenu}': {$oMenu->errMsg}\n";
    $oMenu->_DB->rollBack();
    $oACL->_DB->rollBack();
    error_exit($errmsg);
}
