Applications should include all of their core, common functions in a lib/App.php file. Here is Nag's:
<?php // $Horde: hordeweb/papers/oscon2001-horde_tutorial/37_nagbase.xml.html,v 1.1 2001/12/05 16:48:06 chuck Exp $
/** @const NAG_NAME The sitename. */ define('NAG_NAME', 'Task List Manager');
require_once NAG_BASE . '/lib/version.php';
// Actions /** @const NAG_ADD_TASK Add a new task. */ define('NAG_ADD_TASK', 100); /** @const NAG_DELETE_TASKS Delete tasks. */ define('NAG_DELETE_TASKS', 101); /** @const NAG_MODIFY_TASK Modify a task. */ define('NAG_MODIFY_TASK', 102); /** @const NAG_SAVE_TASK Save a task to storage. */ define('NAG_SAVE_TASK', 103); /** @const NAG_SEARCH_TASKS Search the task list. */ define('NAG_SEARCH_TASKS', 104);
// Sort types /** @const SORT_NAME Sort by task name. */ define('SORT_NAME', 0); /** @const SORT_ADDED Sort by creation date. */ define('SORT_ADDED', 1); /** @const SORT_DUE Sort by due date. */ define('SORT_DUE', 2); /** @const SORT_PRIORITY Sort by priority. */ define('SORT_PRIORITY', 3); /** @const SORT_ASCEND Sort in ascending order. */ define('SORT_ASCEND', 0); /** @const SORT_DESCEND Sort in descending order. */ define('SORT_DESCEND', 1);
// Priority types /** @const PRIORITY_HIGH High priority task. */ define('PRIORITY_HIGH', 0); /** @const PRIORITY_MED Medium priority task. */ define('PRIORITY_MED', 10); /** @const PRIORITY_LOW Low priority task. */ define('PRIORITY_LOW', 20);
/** * Array for mapping priority constants to their string representations. * @var array $priority_map */ $priority_map = array( PRIORITY_HIGH => _("High"), PRIORITY_MED => _("Medium"), PRIORITY_LOW => _("Low") );
/* Set the umask. */ if (isset($conf['umask'])) { umask($conf['umask']); }
/** * Nag Base Class. * * @author Jon Parise <jon@horde.org> * @version $Revision: 1.1 $ * @package nag */ class Nag {
/** * Retrieves the current user's task list from storage. * This function will also sort the resulting list, if requested. * * @param $storage The current storage object. * @param $criteria (optional) A set of flags specifying the set of * tasks to return. (TASK_ADDED, TASK_MODIFIED, * TASK_DELETED) * @param $sortby (optional) The field by which to sort. * (SORT_NAME, SORT_ADDED, SORT_DUE) * @param $sortdir (optional) The direction by which to sort. * (SORT_ASC, SORT_DESC) * * @return array Returns a list of the requested tasks. * * @see Nag_Storage::listTasks */ function listTasks($storage, $criteria = 0, $sortby = SORT_ADDED, $sortdir = SORT_ASCEND) { /* Retrieve the task list for storage. */ $tasks = $storage->listTasks($criteria);
/* Sort the task list. */ $sort_functions = array( SORT_PRIORITY => 'ByPriority', SORT_NAME => 'ByName', SORT_ADDED => 'ByAdded', SORT_DUE => 'ByDue' );
/* Sort the array if we have a sort function defined for this field. */ if (isset($sort_functions[$sortby])) { $prefix = ($sortdir == SORT_DESCEND) ? '_rsort' : '_sort'; uasort($tasks, $prefix . $sort_functions[$sortby]); }
return $tasks; }
/** * Generates the HTML for a day selection widget. * * @param string $name The name of the widget. * @param int $timestamp (optional) The timestamp to select by default. * @param string $javascript (optional) Any onchange, onblur, etc. javascript to include. * * @return string The HTML <select> widget. */ function buildDayWidget($name, $timestamp = null, $javascript = null) { $selected = (isset($timestamp)) ? date('d', $timestamp) : null;
$html = "<select name=\"$name\""; if ($javascript != null) { $html .= ' ' . $javascript; } $html .= '>'; for ($day = 1; $day <= 31; $day++) { $html .= "<option value=\"$day\""; $html .= ($day == $selected) ? ' selected>' : '>'; $html .= "$day</option>"; } $html .= "</select>\n";
return $html; }
/** * Generates the HTML for a month selection widget. * * @param string $name The name of the widget. * @param int $timestamp (optional) The timestamp to select by default. * @param string $javascript (optional) Any onchange, onblur, etc. javascript to include. * * @return string The HTML <select> widget. */ function buildMonthWidget($name, $timestamp = null, $javascript = null) { $months = array( 1 => _("January"), 2 => _("February"), 3 => _("March"), 4 => _("April"), 5 => _("May"), 6 => _("June"), 7 => _("July"), 8 => _("August"), 9 => _("September"), 10 => _("October"), 11 => _("November"), 12 => _("December") );
$selected = (isset($timestamp)) ? date('m', $timestamp) : null;
$html = "<select name=\"$name\""; if ($javascript != null) { $html .= ' ' . $javascript; } $html .= '>'; for ($month = 1; $month <= 12; $month++) { $html .= "<option value=\"$month\""; $html .= ($month == $selected) ? ' selected>' : '>'; $html .= $months[$month] . '</option>'; } $html .= "</select>\n";
return $html; }
/** * Generates the HTML for a year selection widget. * * @param int $name The name of the widget. * @param int $years The number of years into the future to include. * @param string $timestamp (optional) The timestamp to select by default. * @param string $javascript (optional) Any onchange, onblur, etc. javascript to include. * * @return string The HTML <select> widget. */ function buildYearWidget($name, $years, $timestamp = null, $javascript = null) { $selected = (isset($timestamp)) ? date('Y', $timestamp) : null;
$html = "<select name=\"$name\""; if ($javascript != null) { $html .= ' ' . $javascript; } $html .= '>'; for ($year = date('Y'); $year < (date('Y') + $years); $year++) { $html .= "<option value=\"$year\""; $html .= ($year == $selected) ? ' selected>' : '>'; $html .= "$year</option>"; } $html .= "</select>\n";
return $html; }
/** * Generates the HTML for an hour selection widget. * * @param strin $name The name of the widget. * @param int $timestamp (optional) The timestamp to select by default. * @param string $javascript (optional) Any onchange, onblur, etc. javascript to include. * * @return string The HTML <select> widget. */ function buildHourWidget($name, $timestamp = null, $javascript = null) { $selected = (isset($timestamp)) ? date('H', $timestamp) : null;
$html = "<select name=\"$name\""; if ($javascript != null) { $html .= ' ' . $javascript; } $html .= '>'; for ($hour = 0; $hour <= 23; $hour++) { $html .= "<option value=\"$hour\""; $html .= ($hour == $selected) ? ' selected>' : '>'; $html .= sprintf("%02d", $hour) . '</option>'; } $html .= "</select>\n";
return $html; }
/** * Generates the HTML for a minute selection widget. * * @param string $name The name of the widget. * @param int $increment (optional) The increment between minutes. * @param int $timestamp (optional) The timestamp to select by default. * @param string $javascript (optional) Any onchange, onblur, etc. javascript to include. * * @return string The HTML <select> widget. */ function buildMinuteWidget($name, $increment = 1, $timestamp = null, $javascript = null) { $selected = (isset($timestamp)) ? date('i', $timestamp) : null;
$html = "<select name=\"$name\""; if ($javascript != null) { $html .= ' ' . $javascript; } $html .= '>'; for ($minute = 0; $minute <= 60; $minute += $increment) { $html .= "<option value=\"$minute\""; $html .= ($minute == $selected) ? ' selected>' : '>'; $html .= sprintf("%02d", $minute) . '</option>'; } $html .= "</select>\n";
return $html; }
/** * Builds the HTML for a task list selection widget. * * @param $name The name of the widget. * @param $tasks The array of tasks. * @param $selected (optional) The default selected item in the list. * @param $none (optional) Include a 'None' item in the task list. * * @return string The HTML <select> widget. */ function buildTaskListWidget($name, $tasks, $selected = -1, $none = false) { $html = "<select name=\"$name\">";
if ($none) { $html .= '<option value="-1">' . _("None") . '</option>'; }
ksort($tasks, SORT_NUMERIC); foreach ($tasks as $id => $task) { $html .= "<option value=\"$id\""; $html .= ($id == $selected) ? ' selected>' : '>'; $html .= htmlspecialchars($task['name']) . '</option>'; } $html .= "</select>\n";
return $html; }
/** * Builds the HTML for a priority selection widget. * * @param $name The name of the widget. * @param $selected (optional) The default selected priority. * * @return string The HTML <select> widget. */ function buildPriorityWidget($name, $selected = -1) { global $priority_map;
$html = "<select name=\"$name\">";
foreach ($priority_map as $priority => $name) { $html .= "<option value=\"$priority\""; $html .= ($priority == $selected) ? ' selected>' : '>'; $html .= $name . '</option>'; } $html .= "</select>\n";
return $html; }
/** * Formats the given Unix-style date string. * * @param $unixdate The Unix-style date value to format. * * @return string The formatted due date string. */ function formatDate($unixdate = '') { global $conf;
if (empty($unixdate)) { return _("None"); }
$s = strftime($conf['view']['date_format'], $unixdate); $s .= ' ' . _("at") . ' '; $s .= strftime($conf['view']['time_format'], $unixdate);
return $s; }
/** * Returns the string representation of the given priority. * * @param $priority The priority value. * * @return string The string representation of $priority. */ function formatPriority($priority) { global $priority_map;
if (in_array($priority, array_keys($priority_map))) { if ($priority == PRIORITY_HIGH) { return Horde::img('high.gif') . ' ' . $priority_map[$priority]; } elseif ($priority == PRIORITY_LOW) { return Horde::img('low.gif') . ' '. $priority_map[$priority]; } else { return $priority_map[$priority]; } }
return _("Unknown"); } }
function _sortByPriority($a, $b) { if ($a['priority'] == $b['priority']) return 0; return ($a['priority'] > $b['priority']) ? -1 : 1; }
function _rsortByPriority($a, $b) { if ($a['priority'] == $b['priority']) return 0; return ($a['priority'] < $b['priority']) ? -1 : 1; }
function _sortByName($a, $b) { return strcmp($a['name'], $b['name']); }
function _rsortByName($a, $b) { return strcmp($b['name'], $a['name']); }
function _sortByAdded($a, $b) { if ($a['added'] == $b['added']) return 0; return ($a['added'] > $b['added']) ? -1 : 1; }
function _rsortByAdded($a, $b) { if ($a['added'] == $b['added']) return 0; return ($a['added'] < $b['added']) ? -1 : 1; }
function _sortByDue($a, $b) { if ($a['due'] == $b['due']) return 0; return ($a['due'] > $b['due']) ? -1 : 1; }
function _rsortByDue($a, $b) { if ($a['due'] == $b['due']) return 0; return ($a['due'] < $b['due']) ? -1 : 1; }
?>
|