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/Trigger.inc.php';
/********************************************************************
* CONFIG
********************************************************************/
// Titles and navigation header.
$nav->add(_("Triggers"), null);
// Instantiate a sorting object with the default sort and order. Add SQL for each column.
$so = new SortOrder('trigger_tbl.keyword', 'ASC');
$so->asc_widget = '';
$so->desc_widget = '';
$so->setColumn('trigger_tbl.trigger_id', 'trigger_tbl.trigger_id ASC', 'trigger_tbl.trigger_id DESC');
$so->setColumn('trigger_tbl.virtual_number', 'trigger_tbl.virtual_number ASC', 'trigger_tbl.virtual_number DESC');
$so->setColumn('trigger_tbl.keyword', 'trigger_tbl.keyword ASC', 'trigger_tbl.keyword DESC');
$so->setColumn('trigger_tbl.action', 'trigger_tbl.action ASC', 'trigger_tbl.action DESC');
$so->setColumn('trigger_tbl.action_options', 'trigger_tbl.action_options ASC', 'trigger_tbl.action_options DESC');
$so->setColumn('trigger_tbl.added_datetime', 'trigger_tbl.added_datetime ASC', 'trigger_tbl.added_datetime DESC');
$so->setColumn('trigger_tbl.added_by_user_id', 'trigger_tbl.added_by_user_id ASC', 'trigger_tbl.added_by_user_id DESC');
$so->setColumn('trigger_tbl.modified_datetime', 'trigger_tbl.modified_datetime ASC', 'trigger_tbl.modified_datetime DESC');
$so->setColumn('trigger_tbl.modified_by_user_id', 'trigger_tbl.modified_by_user_id ASC', 'trigger_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_action',
));
// 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'], 'trigger');
}
if (getFormData('break_list_cache', false)) {
// Remove any stale cached list data.
$cache->delete('trigger list');
}
// What action to take.
switch (getFormData('op')) {
case 'add' :
// Bounce the user if they don't have permission to create a record.
Trigger::requireAllow('create');
// Initialize variables for the form template.
$frm = getAddFields();
$nav->add(_("Add Trigger"));
$main_template = 'trigger_form.inc.html';
break;
case 'edit' :
// Bounce the user if they don't have permission to update a record.
Trigger::requireAllow('update');
// Initialize variables for the form template.
$frm = getEditFields(getFormData('trigger_id'));
$nav->add(_("Edit Trigger"));
$main_template = 'trigger_form.inc.html';
break;
case 'del' :
$app->requireValidCSRFToken();
// Bounce the user if they don't have permission to delete the specified record.
Trigger::requireAllow('delete', Trigger::get(array('trigger_id' => getFormData('trigger_id')), 1));
if ($del_row = Trigger::get(array('trigger_id' => getFormData('trigger_id')), 1)) {
Trigger::delete(getFormData('trigger_id'));
$app->raiseMsg(sprintf(_("The trigger with keyword %s has been deleted."), $del_row['keyword']), MSG_SUCCESS, __FILE__, __LINE__);
}
if ($app->validBoomerangURL('trigger')) {
// Display boomerang page.
$app->dieBoomerangURL('trigger', $locally_carried_queries);
}
// Display default page.
$app->dieURL($_SERVER['PHP_SELF'], $locally_carried_queries);
break;
case 'insert' :
$app->requireValidCSRFToken();
if (getFormData('account_id')) {
// Bounce the user if they don't have permission to create a record under the specified account_id.
Trigger::requireAllow('create', array(array('account_id' => getFormData('account_id'))));
} else {
// Bounce the user if they don't have permission to create a record at all.
Trigger::requireAllow('create');
}
if (getFormdata('btn_cancel', false)) {
if ($app->validBoomerangURL('trigger')) {
// Display boomerang page.
$app->dieBoomerangURL('trigger', $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 Trigger"));
$main_template = 'trigger_form.inc.html';
} else {
$trigger_id = Trigger::insert(getFormData());
$app->raiseMsg(sprintf(_("The trigger with keyword %s has been added."), getFormData('keyword')), 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('trigger')) {
// Display boomerang page.
$app->dieBoomerangURL('trigger', $locally_carried_queries);
}
// Display default page.
$app->dieURL($_SERVER['PHP_SELF'], $locally_carried_queries);
}
break;
case 'update' :
$app->requireValidCSRFToken();
if (getFormData('account_id')) {
// Bounce the user if they don't have permission to update the specified record or don't have permission to assign this record to the specified account_id.
$check_rows = array_merge(Trigger::get(array('trigger_id' => getFormData('trigger_id')), 1), array(array('account_id' => getFormData('account_id'))));
Trigger::requireAllow('update', $check_rows);
} else {
// Bounce the user if they don't have permission to update the specified record.
Trigger::requireAllow('update', Trigger::get(array('trigger_id' => getFormData('trigger_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&trigger_id=' . getFormData('trigger_id'), $locally_carried_queries);
}
if (getFormdata('btn_cancel', false)) {
// Remove lock
$lock->select('trigger_tbl', 'trigger_id', getFormData('trigger_id'));
$lock->remove();
if ($app->validBoomerangURL('trigger')) {
// Display boomerang page.
$app->dieBoomerangURL('trigger', $locally_carried_queries);
}
// Display default page.
$app->dieURL($_SERVER['PHP_SELF'], $locally_carried_queries);
}
$fv = validateInput($fv);
if ($fv->anyErrors()) {
$frm = getEditFields(getFormData('trigger_id'));
$frm = array_merge($frm, getFormData());
$nav->add(_("Edit Trigger"));
$main_template = 'trigger_form.inc.html';
} else {
Trigger::update(getFormData());
$app->raiseMsg(sprintf(_("The trigger with keyword %s has been updated."), getFormData('keyword')), MSG_SUCCESS, __FILE__, __LINE__);
if (getFormdata('btn_repeat', false)) {
// Display edit function with next available ID.
$qid = $db->query("SELECT trigger_id FROM trigger_tbl WHERE trigger_id > '" . $db->escapeString(getFormData('trigger_id')) . "' ORDER BY trigger_id ASC LIMIT 1");
if (list($next_id) = mysql_fetch_row($qid)) {
$app->dieURL($_SERVER['PHP_SELF'] . '?op=edit&trigger_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('trigger')) {
// Display boomerang page.
$app->dieBoomerangURL('trigger', $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.
Trigger::requireAllow('read');
// If the user does not have access to 'any' record, limit by their account_id.
$where_clause = '';
if (!$acl->check('user_id:' . $auth->get('user_id'), 'trigger:read', 'any')) {
$where_clause = "WHERE trigger_tbl.account_id = '" . $db->escapeString($auth->get('account_id')) . "'";
}
$list = Trigger::getPaginatedList($where_clause);
$main_template = 'trigger_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('trigger_id', 0, 16777215, sprintf(_("%s must be a number between %d and %d."), _("Trigger ID"), 0, 16777215));
$fv->isInteger('trigger_id', sprintf(_("%s must be an integer."), _("Trigger ID")));
$fv->notEmpty('keyword', sprintf(_("%s cannot be blank."), _("Keyword")));
$fv->stringLength('keyword', 0, 255, sprintf(_("%s must be %d-to-%d characters in length."), _("Keyword"), 0, 255));
$fv->notEmpty('action', sprintf(_("%s cannot be blank."), _("Action")));
$fv->stringLength('action', 0, 255, sprintf(_("%s has an invalid selection."), _("Action")));
$fv->notEmpty('action_options', sprintf(_("%s cannot be blank."), _("Action options")));
$fv->stringLength('action_options', 0, 65535, sprintf(_("%s must be %d-to-%d characters in length."), _("Action options"), 0, 65535));
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 Trigger::merge(array(
'new_op' => 'insert',
'submit_buttons' => array(
array('name' => 'btn_submit', 'value' => _("Add Trigger"), '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('trigger_tbl', 'trigger_id', $id);
if ($lock->isLocked() && !$lock->isMine()) {
$lock->dieErrorPage();
}
// Get the information for the form.
if (!$frm = Trigger::get(array('trigger_id' => $id))) {
$app->logMsg('Could not find record with trigger_id: ' . $id, LOG_WARNING, __FILE__, __LINE__);
$app->raiseMsg(sprintf(_("The requested record %s could not be found."), $id), MSG_ERR, __FILE__, __LINE__);
$app->dieBoomerangURL('trigger', $locally_carried_queries);
}
// Lock this record.
$lock->set('trigger_tbl', 'trigger_id', $id, $frm['keyword']);
// Set misc values for the form.
return Trigger::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);
}