From: Ben Klang Date: Fri, 2 Apr 2010 23:34:02 +0000 (-0400) Subject: Shout: begin work on admin screen to manage numbers X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=7aca9011fd48ddc1ffe2f381cdc2222cbd77e841;p=horde.git Shout: begin work on admin screen to manage numbers --- diff --git a/shout/admin.php b/shout/admin.php index 023f36ed3..9e59bde1d 100644 --- a/shout/admin.php +++ b/shout/admin.php @@ -14,89 +14,13 @@ $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['code']); - $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]['name'])); - $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['code']); - $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['code']); - $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'); @@ -105,6 +29,7 @@ require SHOUT_TEMPLATES . '/menu.inc'; $notification->notify(); -require SHOUT_TEMPLATES . '/accounts/' . $action . '.inc'; +Shout::getAdminTabs(); require $registry->get('templates', 'horde') . '/common-footer.inc'; + diff --git a/shout/admin/accounts.php b/shout/admin/accounts.php new file mode 100644 index 000000000..6969872ce --- /dev/null +++ b/shout/admin/accounts.php @@ -0,0 +1,112 @@ + + */ + +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['code']); + $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]['name'])); + $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['code']); + $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['code']); + $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(); + +Shout::getAdminTabs(); + +require SHOUT_TEMPLATES . '/accounts/' . $action . '.inc'; + +require $registry->get('templates', 'horde') . '/common-footer.inc'; diff --git a/shout/admin/numbers.php b/shout/admin/numbers.php new file mode 100644 index 000000000..3187eea92 --- /dev/null +++ b/shout/admin/numbers.php @@ -0,0 +1,105 @@ + + */ + +require_once dirname(__FILE__) . '/../lib/Application.php'; +$shout = Horde_Registry::appInit('shout'); + +require_once SHOUT_BASE . '/lib/Forms/NumberForm.php'; + +$action = Horde_Util::getFormData('action'); +$curaccount = $_SESSION['shout']['curaccount']; + +$RENDERER = new Horde_Form_Renderer(); + +$title = _("Numbers: "); + +switch ($action) { +case 'add': +case 'edit': + $vars = Horde_Variables::getDefaultVariables(); + $Form = new NumberDetailsForm($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'); + } + + // FIXME: Preserve vars on edit + + // 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['code']); + $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['code']); + $Form = new ExtensionDeleteForm($vars); + + break; + +case 'list': +default: + $action = 'list'; + $title .= _("List Numbers"); +} + +try { + $numbers = $shout->storage->getNumbers(); +} catch (Exception $e) { + $notification->push($e); +} + + +Horde::addScriptFile('stripe.js', 'horde'); +Horde::addScriptFile('prototype.js', 'horde'); + +require SHOUT_TEMPLATES . '/common-header.inc'; +require SHOUT_TEMPLATES . '/menu.inc'; + +$notification->notify(); + +Shout::getAdminTabs(); + +require SHOUT_TEMPLATES . '/admin/numbers/' . $action . '.inc.php'; + +require $registry->get('templates', 'horde') . '/common-footer.inc'; diff --git a/shout/lib/Driver/Sql.php b/shout/lib/Driver/Sql.php index ddd7dc2cb..24d3a95ed 100644 --- a/shout/lib/Driver/Sql.php +++ b/shout/lib/Driver/Sql.php @@ -120,7 +120,6 @@ class Shout_Driver_Sql extends Shout_Driver throw new Shout_Exception($row); } - $menus = array(); while ($row && !($row instanceof PEAR_Error)) { $menu = $row['name']; $menus[$menu] = array( @@ -592,6 +591,39 @@ class Shout_Driver_Sql extends Shout_Driver return true; } + public function getNumbers() + { + $sql = 'SELECT numbers.id AS id, numbers.did AS did, ' . + 'accounts.code AS accountcode, menus.name AS menuName ' . + 'FROM numbers ' . + 'INNER JOIN accounts ON numbers.account_id = accounts.id ' . + 'INNER JOIN menus ON numbers.menu_id = menus.id'; + + $msg = 'SQL query in Shout_Driver_Sql#getNumbers(): ' . $sql; + Horde::logMessage($msg, 'DEBUG'); + $result = $this->_db->query($sql, $args); + if ($result instanceof PEAR_Error) { + throw new Shout_Exception($result); + } + + $row = $result->fetchRow(DB_FETCHMODE_ASSOC); + if ($row instanceof PEAR_Error) { + throw new Shout_Exception($row); + } + + $numbers = array(); + while ($row && !($row instanceof PEAR_Error)) { + $id = $numbers['did']; + $numbers[$id] = $row; + + /* Advance to the new row in the result set. */ + $row = $result->fetchRow(DB_FETCHMODE_ASSOC); + } + + $result->free(); + return $numbers; + } + /** * Attempts to open a persistent connection to the SQL server. * diff --git a/shout/lib/Forms/NumberForm.php b/shout/lib/Forms/NumberForm.php new file mode 100644 index 000000000..8086cb614 --- /dev/null +++ b/shout/lib/Forms/NumberForm.php @@ -0,0 +1,105 @@ + + * @package Shout + */ + +class NumberDetailsForm extends Horde_Form { + + /** + * AccountDetailsForm constructor. + * + * @param mixed reference $vars + * @return boolean + */ + function __construct(&$vars) + { + $action = $vars->get('action'); + if ($action == 'edit') { + $title = _("Edit Number"); + } else { + $title = _("Add Number"); + } + + parent::__construct($vars, $title); + + $this->addHidden('', 'action', 'text', true); + //$this->addHidden('', 'oldaccount', 'text', false); + $this->addVariable(_("Telephone Number"), 'number', 'phone', true); + + $shout = $GLOBALS['registry']->getApiInstance('shout', 'application'); + $accounts = $shout->storage->getAccounts(); + foreach ($accounts as $id => $info) { + $list[$id] = $info['name']; + } + $select = $this->addVariable(_("Account Code"), 'accountcode', + 'enum', false, false, null, array($list)); + $action = &Horde_Form_Action::factory('reload'); + $select->setAction($action); + $select->setOption('trackchange', true); + + $accountcode = $vars->get('accountcode'); + $menus = $shout->storage->getMenus($accountcode); + $list = array('' => _("-- none --")); + foreach ($menus as $id => $info) { + $list[$id] = $info['name']; + } + $this->addVariable(_("Menu"), 'menuName', 'enum', false, + false, null, array($list)); + return true; + } + + /** + * Process this form, saving its information to the backend. + */ + function execute() + { + die("FIXME"); + $shout = $GLOBALS['registry']->getApiInstance('shout', 'application'); + + $code = $this->_vars->get('code'); + $name = $this->_vars->get('name'); + $adminpin = $this->_vars->get('adminpin'); + if (empty($adminpin)) { + $adminpin = rand(1000, 9999); + } + + $shout->storage->saveAccount($code, $name, $adminpin); + } + +} + +class NumberDeleteForm extends Horde_Form +{ + function __construct(&$vars) + { + die("FIXME"); + $extension = $vars->get('extension'); + $account = $vars->get('account'); + + $title = _("Delete Extension %s - Account: %s"); + $title = sprintf($title, $extension, $_SESSION['shout']['accounts'][$account]['name']); + 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() + { + die("FIXME"); + $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 9518a0019..27796cb18 100644 --- a/shout/lib/Shout.php +++ b/shout/lib/Shout.php @@ -240,4 +240,21 @@ class Shout return $res; } + static public function getAdminTabs() + { + $tabname = Horde_Util::getFormData('tabname'); + $tabs = new Horde_Ui_Tabs('view', Horde_Variables::getDefaultVariables()); + $tabs->addTab(_("Telephone Numbers"), + Horde::applicationUrl('admin/numbers.php'), + array('tabname' => 'numbers', id => 'tabnumbers')); + $tabs->addTab(_("Accounts"), + Horde::applicationUrl('admin/accounts.php'), + array('tabname' => 'accounts', id => 'tabaccounts')); + if ($tabname === null) { + $tabname = 'numbers'; + } + + echo $tabs->render($tabname); + } + } diff --git a/shout/templates/admin/numbers/edit.inc.php b/shout/templates/admin/numbers/edit.inc.php new file mode 100644 index 000000000..f65b322cc --- /dev/null +++ b/shout/templates/admin/numbers/edit.inc.php @@ -0,0 +1 @@ +renderActive(); \ No newline at end of file diff --git a/shout/templates/admin/numbers/list.inc.php b/shout/templates/admin/numbers/list.inc.php new file mode 100644 index 000000000..f615bf440 --- /dev/null +++ b/shout/templates/admin/numbers/list.inc.php @@ -0,0 +1,40 @@ +
+ + + + + + + + + + + + + +
Telephone NumberAccount NameMenu Name
+ $code))); echo $code; echo ''; ?> + + + + +
+
+ \ No newline at end of file