From: Ben Klang Date: Wed, 24 Mar 2010 21:10:42 +0000 (-0400) Subject: Shout: Create admin section for managing accounts X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=e67605c67d8a96910c7aefbb74a943b18a5232bf;p=horde.git Shout: Create admin section for managing accounts --- diff --git a/shout/admin.php b/shout/admin.php new file mode 100644 index 000000000..dde59bcab --- /dev/null +++ b/shout/admin.php @@ -0,0 +1,110 @@ + + */ + +require_once dirname(__FILE__) . '/lib/Application.php'; +$shout = Horde_Registry::appInit('shout'); + +require_once SHOUT_BASE . '/lib/Forms/AccountForm.php'; + +$action = Horde_Util::getFormData('action'); +$curaccount = $_SESSION['shout']['curaccount']; + +$RENDERER = new Horde_Form_Renderer(); + +$title = _("Accounts: "); + +switch ($action) { +case 'add': +case 'edit': + $vars = Horde_Variables::getDefaultVariables(); + $vars->set('account', $curaccount); + $Form = new AccountDetailsForm($vars); + + if ($Form->isSubmitted() && $Form->validate($vars, true)) { + // Form is Valid and Submitted + try { + $Form->execute(); + $notification->push(_("Account information saved."), + 'horde.success'); + $action = 'list'; + } catch (Exception $e) { + $notification->push($e); + } + break; + } elseif ($Form->isSubmitted()) { + $notification->push(_("Problem processing the form. Please check below and try again."), 'horde.warning'); + } + + // Create a new add/edit form + $account = Horde_Util::getFormData('extension'); + $accounts = $shout->storage->getAccounts(); + $vars = new Horde_Variables(array('code' => $account, 'name' => $accounts[$account])); + $vars->set('action', $action); + + // Make sure we get the right template below. + $action = 'edit'; + break; + +case 'delete': + $title .= sprintf(_("Delete Extension %s"), $extension); + $extension = Horde_Util::getFormData('extension'); + + $vars = Horde_Variables::getDefaultVariables(); + $vars->set('account', $curaccount); + $Form = new ExtensionDeleteForm($vars); + + $FormValid = $Form->validate($vars, true); + + if ($Form->isSubmitted() && $FormValid) { + try { + $Form->execute(); + $notification->push(_("Extension Deleted.")); + $action = 'list'; + } catch (Exception $e) { + $notification->push($e); + } + } elseif ($Form->isSubmitted()) { + // Submitted but not valid + $notification->push(_("Problem processing the form. Please check below and try again."), 'horde.warning'); + } + + $vars = Horde_Variables::getDefaultVariables(array()); + $vars->set('account', $curaccount); + $Form = new ExtensionDeleteForm($vars); + + break; + +case 'list': +default: + $action = 'list'; + $title .= _("List Accounts"); +} + + +// Fetch the (possibly updated) list of extensions +try { + $accounts = $shout->storage->getAccounts(); +} catch (Exception $e) { + $notification->push($e); + $extensions = array(); +} + +Horde::addScriptFile('stripe.js', 'horde'); +Horde::addScriptFile('prototype.js', 'horde'); + +require SHOUT_TEMPLATES . '/common-header.inc'; +require SHOUT_TEMPLATES . '/menu.inc'; + +$notification->notify(); + +require SHOUT_TEMPLATES . '/accounts/' . $action . '.inc'; + +require $registry->get('templates', 'horde') . '/common-footer.inc'; diff --git a/shout/lib/Driver/Sql.php b/shout/lib/Driver/Sql.php index c683e9098..11748a237 100644 --- a/shout/lib/Driver/Sql.php +++ b/shout/lib/Driver/Sql.php @@ -73,6 +73,32 @@ class Shout_Driver_Sql extends Shout_Driver return $accounts; } + public function saveAccount($code, $name) + { + $this->_connect(); + + if (isset($details['oldname'])) { + if (!isset($menus[$details['oldname']])) { + throw new Shout_Exception(_("Old account not found. Edit aborted.")); + } else { + throw new Shout_Exception(_("Unsupported operation.")); + $sql = 'UPDATE accounts SET code = ?, name =? WHERE code = ?'; + } + } else { + $sql = 'INSERT INTO accounts (code, name) VALUES (?,?)'; + } + + $vars = array($code, $name); + + $msg = 'SQL query in Shout_Driver_Sql#getAccounts(): ' . $sql; + Horde::logMessage($msg, 'DEBUG'); + $result = $this->_db->query($sql, $vars); + if ($result instanceof PEAR_Error) { + throw new Shout_Exception($result); + } + + } + public function getMenus($account) { $this->_connect(); diff --git a/shout/lib/Forms/AccountForm.php b/shout/lib/Forms/AccountForm.php new file mode 100644 index 000000000..f2779c819 --- /dev/null +++ b/shout/lib/Forms/AccountForm.php @@ -0,0 +1,84 @@ + + * @package Shout + */ + +class AccountDetailsForm extends Horde_Form { + + /** + * AccountDetailsForm constructor. + * + * @param mixed reference $vars + * @return boolean + */ + function __construct(&$vars) + { + $account = $_SESSION['shout']['curaccount']; + $action = $vars->get('action'); + if ($action == 'edit') { + $formtitle = "Edit Account"; + $vars->set('oldaccount', $account); + } else { + $formtitle = "Add Account"; + } + + $accountname = $_SESSION['shout']['accounts'][$curaccount]; + $title = sprintf(_("$formtitle %s"), $accountname); + parent::__construct($vars, $title); + + $this->addHidden('', 'action', 'text', true); + //$this->addHidden('', 'oldaccount', 'text', false); + $this->addVariable(_("Account Name"), 'name', 'text', true); + $this->addVariable(_("Account Code"), 'code', 'text', true); + + return true; + } + + /** + * Process this form, saving its information to the backend. + */ + function execute() + { + $shout = $GLOBALS['registry']->getApiInstance('shout', 'application'); + + $code = $this->_vars->get('code'); + $name = $this->_vars->get('name'); + + $shout->storage->saveAccount($code, $name);; + } + +} + +class AccountDeleteForm extends Horde_Form +{ + function __construct(&$vars) + { + $extension = $vars->get('extension'); + $account = $vars->get('account'); + + $title = _("Delete Extension %s - Account: %s"); + $title = sprintf($title, $extension, $_SESSION['shout']['accounts'][$account]); + parent::__construct($vars, $title); + + $this->addHidden('', 'account', 'text', true); + $this->addHidden('', 'extension', 'int', true); + $this->addHidden('', 'action', 'text', true); + $this->setButtons(array(_("Delete"), _("Cancel"))); + } + + function execute() + { + $shout = $GLOBALS['registry']->getApiInstance('shout', 'application'); + $account = $this->_vars->get('account'); + $shout->storage->deleteAccount($account); + } +} diff --git a/shout/lib/Shout.php b/shout/lib/Shout.php index a83d4716e..176d5bf36 100644 --- a/shout/lib/Shout.php +++ b/shout/lib/Shout.php @@ -24,10 +24,6 @@ class Shout */ static public function getMenu($returnType = 'object') { - global $conf, $curaccount, $section, $action; - - require_once 'Horde/Menu.php'; - $menu = new Horde_Menu(Horde_Menu::MASK_ALL); $menu->add(Horde::applicationUrl('dialplan.php'), _("Incoming Calls"), "dialplan.png"); @@ -35,6 +31,11 @@ class Shout $menu->add(Horde::applicationUrl('devices.php'), _("Devices"), "shout.png"); $menu->add(Horde::applicationUrl('recordings.php'), _("Recordings"), "recordings.png"); + /* Administration. */ + if (Horde_Auth::isAdmin('shout:admin')) { + $menu->add(Horde::applicationUrl('admin.php'), _("_Admin"), 'admin.png'); + } + if ($returnType == 'object') { return $menu; } else { diff --git a/shout/templates/accounts/edit.inc b/shout/templates/accounts/edit.inc new file mode 100644 index 000000000..e42e0b1a3 --- /dev/null +++ b/shout/templates/accounts/edit.inc @@ -0,0 +1,10 @@ +renderActive($RENDERER, $vars, Horde::applicationUrl('admin.php'), 'post'); + +if ($vars->get('action') == 'edit') { + $deleteUrl = Horde::applicationUrl('admin.php'); + $params = array ('action' => 'delete', + 'account' => $account); + $deleteUrl = Horde_Util::addParameter($deleteUrl, $params); + echo 'Delete Account'; +} diff --git a/shout/templates/accounts/list.inc b/shout/templates/accounts/list.inc new file mode 100644 index 000000000..2278c70b6 --- /dev/null +++ b/shout/templates/accounts/list.inc @@ -0,0 +1,45 @@ +
+ + Account: +
+ +
+ + + + + + $name) { + + $url = Horde::applicationUrl("admin.php"); + $url = Horde_Util::addParameter($url, + array( + 'account' => $code, + ) + ); + $editurl = Horde_Util::addParameter($url, 'action', 'edit'); + $deleteurl = Horde_Util::addParameter($url, 'action', 'delete'); + ?> + + + + + +
Account CodeAccount Name
+ + + +
+
diff --git a/shout/themes/graphics/admin.png b/shout/themes/graphics/admin.png new file mode 100755 index 000000000..4ec1a9281 Binary files /dev/null and b/shout/themes/graphics/admin.png differ