requireLogin();
$app->sslOn();
require_once 'codebase/lib/PageNumbers.inc.php';
require_once 'codebase/lib/Cache.inc.php';
require_once 'codebase/lib/SortOrder.inc.php';
require_once 'codebase/lib/HTML.inc.php';
require_once 'models/Account.inc.php';
/********************************************************************
* CONFIG
********************************************************************/
// Titles and navigation header.
$nav->add(_("Accounts"), null);
// Instantiate a sorting object with the default sort and order. Add SQL for each column.
$so = new SortOrder('account_tbl.added_datetime', 'DESC');
$so->asc_widget = '';
$so->desc_widget = '';
$so->setColumn('account_tbl.account_id', 'account_tbl.account_id ASC', 'account_tbl.account_id DESC');
$so->setColumn('account_tbl.organization', 'account_tbl.organization ASC', 'account_tbl.organization DESC');
$so->setColumn('account_tbl.address1', 'account_tbl.address1 ASC', 'account_tbl.address1 DESC');
$so->setColumn('account_tbl.address2', 'account_tbl.address2 ASC', 'account_tbl.address2 DESC');
$so->setColumn('account_tbl.city', 'account_tbl.city ASC', 'account_tbl.city DESC');
$so->setColumn('account_tbl.state', 'account_tbl.state ASC', 'account_tbl.state DESC');
$so->setColumn('account_tbl.zip', 'account_tbl.zip ASC', 'account_tbl.zip DESC');
$so->setColumn('account_tbl.country', 'account_tbl.country ASC', 'account_tbl.country DESC');
$so->setColumn('account_tbl.phone', 'account_tbl.phone ASC', 'account_tbl.phone DESC');
$so->setColumn('account_tbl.fax', 'account_tbl.fax ASC', 'account_tbl.fax DESC');
$so->setColumn('account_tbl.email', 'account_tbl.email ASC', 'account_tbl.email DESC');
$so->setColumn('account_tbl.url', 'account_tbl.url ASC', 'account_tbl.url DESC');
$so->setColumn('account_tbl.notes', 'account_tbl.notes ASC', 'account_tbl.notes DESC');
$so->setColumn('account_tbl.num_users', 'num_users ASC', 'num_users DESC');
$so->setColumn('account_tbl.num_surveys', 'num_surveys ASC', 'num_surveys DESC');
$so->setColumn('account_tbl.available_credit', 'account_tbl.available_credit ASC', 'account_tbl.available_credit DESC');
$so->setColumn('account_tbl.used_credit', 'account_tbl.used_credit ASC', 'account_tbl.used_credit DESC');
$so->setColumn('account_tbl.recharge_amount', 'account_tbl.recharge_amount ASC', 'account_tbl.recharge_amount DESC');
$so->setColumn('account_tbl.added_datetime', 'account_tbl.added_datetime ASC', 'account_tbl.added_datetime DESC');
$so->setColumn('account_tbl.added_by_user_id', 'account_tbl.added_by_user_id ASC', 'account_tbl.added_by_user_id DESC');
$so->setColumn('account_tbl.modified_datetime', 'account_tbl.modified_datetime ASC', 'account_tbl.modified_datetime DESC');
$so->setColumn('account_tbl.modified_by_user_id', 'account_tbl.modified_by_user_id ASC', 'account_tbl.modified_by_user_id DESC');
// Instantiate page numbers. Total items are set and calculation is done in the getList method.
$page = new PageNumbers();
$page->setPerPage(getFormData('per_page'), 100);
$page->setPageNumber(getFormData('page_number'));
// Query parameters to retain always.
$app->carryQuery(array(
'filter_country',
));
// Query parameters to retain only locally.
$locally_carried_queries = array(
'q',
);
/********************************************************************
* MAIN
********************************************************************/
// We may want to use the add/edit interface from another script, so this
// allows us to remember which page we came from so we can go back there.
if (getFormData('boomerang', false) && isset($_SERVER['HTTP_REFERER'])) {
$app->setBoomerangURL($_SERVER['HTTP_REFERER'], 'account');
}
if (getFormData('break_list_cache', false)) {
// Remove any stale cached list data.
$cache->delete('account list');
}
// What action to take.
switch (getFormData('op')) {
case 'add' :
// Bounce the user if they don't have permission to create a record.
Account::requireAllow('create');
// Initialize variables for the form template.
$frm = getAddFields();
$nav->add(_("Add new account"));
$main_template = 'account_form.inc.html';
break;
case 'edit' :
// Bounce the user if they don't have permission to update a record.
Account::requireAllow('update');
// Initialize variables for the form template.
$frm = getEditFields(getFormData('account_id'));
$nav->add(_("Edit Account"));
$main_template = 'account_form.inc.html';
break;
case 'del' :
$app->requireValidCSRFToken();
// Bounce the user if they don't have permission to delete the specified record.
Account::requireAllow('delete', Account::get(array('account_id' => getFormData('account_id')), 1));
if ($del_row = Account::get(array('account_id' => getFormData('account_id')), 1)) {
Account::delete(getFormData('account_id'));
$app->raiseMsg(sprintf(_("The account %s has been deleted."), $del_row['organization']), MSG_SUCCESS, __FILE__, __LINE__);
}
if ($app->validBoomerangURL('account')) {
// Display boomerang page.
$app->dieBoomerangURL('account', $locally_carried_queries);
}
// Display default page.
$app->dieURL($_SERVER['PHP_SELF'], $locally_carried_queries);
break;
case 'insert' :
$app->requireValidCSRFToken();
// Bounce the user if they don't have permission to create a record at all.
Account::requireAllow('create');
if (getFormdata('btn_cancel', false)) {
if ($app->validBoomerangURL('account')) {
// Display boomerang page.
$app->dieBoomerangURL('account', $locally_carried_queries);
}
// Display default page.
$app->dieURL($_SERVER['PHP_SELF'], $locally_carried_queries);
}
$fv = validateInput($fv);
if ($fv->anyErrors()) {
$frm = getAddFields();
$frm = array_merge($frm, getFormData());
$nav->add(_("Add new account"));
$main_template = 'account_form.inc.html';
} else {
$account_id = Account::insert(getFormData());
$app->raiseMsg(sprintf(_("The account %s has been added."), getFormData('organization')), MSG_SUCCESS, __FILE__, __LINE__);
if (getFormdata('btn_repeat', false)) {
// Display function again.
$app->dieURL($_SERVER['PHP_SELF'] . '?op=add', $locally_carried_queries);
} else if ($app->validBoomerangURL('account')) {
// Display boomerang page.
$app->dieBoomerangURL('account', $locally_carried_queries);
}
// Display default page.
$app->dieURL($_SERVER['PHP_SELF'], $locally_carried_queries);
}
break;
case 'update' :
$app->requireValidCSRFToken();
// Bounce the user if they don't have permission to update the specified record.
Account::requireAllow('update', Account::get(array('account_id' => getFormData('account_id')), 1));
if (getFormdata('btn_reset', false)) {
$app->raiseMsg(_("Saved values have been reloaded."), MSG_NOTICE, __FILE__, __LINE__);
$app->dieURL($_SERVER['PHP_SELF'] . '?op=edit&account_id=' . getFormData('account_id'), $locally_carried_queries);
}
if (getFormdata('btn_cancel', false)) {
// Remove lock
$lock->select('account_tbl', 'account_id', getFormData('account_id'));
$lock->remove();
if ($app->validBoomerangURL('account')) {
// Display boomerang page.
$app->dieBoomerangURL('account', $locally_carried_queries);
}
// Display default page.
$app->dieURL($_SERVER['PHP_SELF'], $locally_carried_queries);
}
$fv = validateInput($fv);
if ($fv->anyErrors()) {
$frm = getEditFields(getFormData('account_id'));
$frm = array_merge($frm, getFormData());
$nav->add(_("Edit Account"));
$main_template = 'account_form.inc.html';
} else {
Account::update(getFormData());
$app->raiseMsg(sprintf(_("The account %s has been updated."), getFormData('organization')), MSG_SUCCESS, __FILE__, __LINE__);
if (getFormdata('btn_repeat', false)) {
// Display edit function with next available ID.
$qid = $db->query("SELECT account_id FROM account_tbl WHERE account_id > '" . $db->escapeString(getFormData('account_id')) . "' ORDER BY account_id ASC LIMIT 1");
if (list($next_id) = mysql_fetch_row($qid)) {
$app->dieURL($_SERVER['PHP_SELF'] . '?op=edit&account_id=' . $next_id, $locally_carried_queries);
} else {
$app->raiseMsg(_("Cannot edit next, the end of the list was reached"), MSG_NOTICE, __FILE__, __LINE__);
}
} else if ($app->validBoomerangURL('account')) {
// Display boomerang page.
$app->dieBoomerangURL('account', $locally_carried_queries);
}
// Display default page.
$app->dieURL($_SERVER['PHP_SELF'], $locally_carried_queries);
}
break;
default :
// Bounce the user if they don't have permission to list records.
Account::requireAllow('read');
$list = Account::getPaginatedList();
$main_template = 'account_list.inc.html';
break;
}
/********************************************************************
* OUTPUT
********************************************************************/
include 'header.inc.html';
$app->carryQuery($locally_carried_queries);
include $main_template;
include 'footer.inc.html';
/********************************************************************
* FUNCTIONS
********************************************************************/
/*
*
*
* @access public
* @param
* @return
* @author Quinn Comendant
* @version 1.0
* @since 16 Nov 2014 17:45:40
*/
function validateInput($fv)
{
$fv->numericRange('account_id', 0, 16777215, sprintf(_("%s must be a number between %d and %d."), _("Account ID"), 0, 16777215));
$fv->isInteger('account_id', sprintf(_("%s must be an integer."), _("Account ID")));
// $fv->notEmpty('organization', sprintf(_("%s cannot be blank."), _("Organization")));
$fv->stringLength('organization', 0, 100, sprintf(_("%s must be %d-to-%d characters in length."), _("Organization"), 0, 100));
$fv->notEmpty('address1', sprintf(_("%s cannot be blank."), _("Address line 1")));
$fv->stringLength('address1', 0, 100, sprintf(_("%s must be %d-to-%d characters in length."), _("Address line 1"), 0, 100));
// $fv->notEmpty('address2', sprintf(_("%s cannot be blank."), _("Address line 2")));
$fv->stringLength('address2', 0, 100, sprintf(_("%s must be %d-to-%d characters in length."), _("Address line 2"), 0, 100));
$fv->notEmpty('city', sprintf(_("%s cannot be blank."), _("City")));
$fv->stringLength('city', 0, 50, sprintf(_("%s must be %d-to-%d characters in length."), _("City"), 0, 50));
$fv->notEmpty('state', sprintf(_("%s cannot be blank."), _("State/Province/Region")));
$fv->stringLength('state', 0, 30, sprintf(_("%s must be %d-to-%d characters in length."), _("State/Province/Region"), 0, 30));
$fv->notEmpty('zip', sprintf(_("%s cannot be blank."), _("ZIP/Postal code")));
$fv->stringLength('zip', 0, 15, sprintf(_("%s must be %d-to-%d characters in length."), _("ZIP/Postal code"), 0, 15));
$fv->notEmpty('country', sprintf(_("%s cannot be blank."), _("Country")));
$fv->stringLength('country', 0, 33, sprintf(_("%s must be %d-to-%d characters in length."), _("Country"), 0, 33));
// $fv->notEmpty('url', sprintf(_("%s cannot be blank."), _("Website URL")));
$fv->stringLength('url', 0, 255, sprintf(_("%s must be %d-to-%d characters in length."), _("Website URL"), 0, 255));
// $fv->notEmpty('notes', sprintf(_("%s cannot be blank."), _("Notes")));
$fv->stringLength('notes', 0, 65535, sprintf(_("%s must be %d-to-%d characters in length."), _("Notes"), 0, 65535));
// $fv->notEmpty('available_credit', sprintf(_("%s cannot be blank."), _("Available credit")));
$fv->numericRange('available_credit', -3.40282E+38, 3.40282E+38, sprintf(_("%s must be a number between %f and %f."), _("Available credit"), -3.40282E+38, 3.40282E+38));
$fv->isFloat('available_credit', sprintf(_("%s must be a valid number."), _("Available credit")), true);
// $fv->notEmpty('used_credit', sprintf(_("%s cannot be blank."), _("Used credit")));
$fv->numericRange('used_credit', -3.40282E+38, 3.40282E+38, sprintf(_("%s must be a number between %f and %f."), _("Used credit"), -3.40282E+38, 3.40282E+38));
$fv->isFloat('used_credit', sprintf(_("%s must be a valid number."), _("Used credit")), true);
$fv->notEmpty('recharge_amount', sprintf(_("%s cannot be blank."), _("Recharge amount")));
$fv->numericRange('recharge_amount', -3.40282E+38, 3.40282E+38, sprintf(_("%s must be a number between %f and %f."), _("Recharge amount"), -3.40282E+38, 3.40282E+38));
$fv->isFloat('recharge_amount', sprintf(_("%s must be a valid number."), _("Recharge amount")), true);
return $fv;
}
/*
*
*
* @access public
* @param
* @return
* @author Quinn Comendant
* @version 1.0
* @since 16 Nov 2014 17:45:40
*/
function getAddFields()
{
// Set default values for the reset of the fields.
return Account::merge(array(
'new_op' => 'insert',
'submit_buttons' => array(
array('name' => 'btn_submit', 'value' => _("Add new account"), 'class' => 'small button', 'accesskey' => 's'),
array('name' => 'btn_repeat', 'value' => _("Add & repeat"), 'class' => 'small button secondary', 'accesskey' => 'r'),
array('name' => 'btn_cancel', 'value' => _("Cancel"), 'class' => 'small button secondary', 'accesskey' => 'c'),
),
));
}
/*
*
*
* @access public
* @param
* @return
* @author Quinn Comendant
* @version 1.0
* @since 16 Nov 2014 17:45:40
*/
function getEditFields($id)
{
global $lock, $locally_carried_queries;
$db =& DB::getInstance();
$app =& App::getInstance();
$lock->select('account_tbl', 'account_id', $id);
if ($lock->isLocked() && !$lock->isMine()) {
$lock->dieErrorPage();
}
// Get the information for the form.
if (!$frm = Account::get(array('account_id' => $id))) {
$app->logMsg('Could not find record with account_id: ' . $id, LOG_WARNING, __FILE__, __LINE__);
$app->raiseMsg(sprintf(_("The requested record %s could not be found."), $id), MSG_ERR, __FILE__, __LINE__);
$app->dieBoomerangURL('account', $locally_carried_queries);
}
// Lock this record.
$lock->set('account_tbl', 'account_id', $id, $frm['organization']);
// Set misc values for the form.
return Account::merge(array(
'new_op' => 'update',
'submit_buttons' => array(
array('name' => 'btn_submit', 'value' => _("Save changes"), 'class' => 'small button', 'accesskey' => 's'),
array('name' => 'btn_repeat', 'value' => _("Save & edit next"), 'class' => 'small button secondary', 'accesskey' => 'e'),
array('name' => 'btn_reset', 'value' => _("Reset"), 'class' => 'small button secondary', 'accesskey' => 'r'),
array('name' => 'btn_cancel', 'value' => _("Cancel"), 'class' => 'small button secondary', 'accesskey' => 'c'),
),
), $frm);
}