Admin Specific
From Triangle Wiki
IntroductionData Grid To DosIn order of priority please
You don't have to use our datagrid, you can just stick with the parent class. The base class I wrote is designed to work with quickform and to output our admin, it's not designed for other uses. You'd need to write a different renderer if you want to use datagrids in another context
Bugs
Multi-language supportThe admin uses PEAR_Translation2 to provide multi-language strings based on user preferences. It automatically detects the preferred language when reaching the login page based on browser locale, but language can be changed at any time. There is a translation management admin panel that parses the entire source code, searching for calls to the translation object's get() method. These are saved into a transition table called language_strings. It then compares these with the existing translations in the Database, and prints a form with the missing translations. A translator can enter the missing translations in the text areas. To speed things up, a cache of the missing translations is saved in the session, and refreshed only when the user presses the update button, at which point the parsing restarts, and the session cache is updated. When a translation is entered using this widget, the corresponding translation in the session array is also removed, giving instant feedback. The table for translation entry has 4 main columns: [selected_language_string] [target_language] [page] [translations] The first column is dynamic, based on which language is selected in the session. It provides the translated version of the string missing in the target language. If that translated version doesn't exist, it outputs the translation code instead, giving a less useful indication of the target translation. The Page column refers to page-specific translations. Most translations can have this set to null, but if a different page is specified, the translation may differ from the top-level, null-page translation (so 'login' could be translated differently according to which page is loaded). Form and TableThe admin framework generates a form for data entry and editing and a paginated table with in-built filters for each database table. To create this basic scaffold, all you need to do is create a new page in the modules section, under a specific module, and include this code: (example taken from the countries table) $admin->authPage('admin');
$settings['show_actions_column'] = true;
$settings['show_calendar'] = false;
$settings['show_filters'] = true;
$settings['show_form'] = true;
$settings['convert_rows'] = true;
$settings['table'] = TBL_ADMIN_COUNTRIES;
$settings['table_id'] = 'country_id';
$settings['element'] = 'Countries';
$settings['icons'] = array('edit', 'delete');
$settings['datetime_format'] = 'UK';
$settings['records_per_page'] = 20;
$dg =& new Structures_DataGrid_Base_new($settings, null, 'Admin');
echo $admin->showMessage($msg . $dg->msg);
echo $dg->output(true);
A number of settings can be changed, including showing the form. If the form isn't shown, the application will consist only of a paginated, filtered table. When the form is shown, it is hidden by default, and you bring it up by either
Sometimes you will want to link directly to this page for data entry, in which case you may want the form to be shown by default. Simply append &show_form to the URL, and the form will be shown. NavigationThe navigation system is now drawn from the users_options table. A single user may have many seperate inline sites within the admin, this is all controlled by adding the following lines into the Options table.
Add as many menu elements as the user needs. The files holding navigation set up now use a html-free method for creating links, incorporating translation features. A complex example that includes conditonal navigation elements is given here, for the Chinasavvy navigation: $boxes = array(
'entry_forms' => array(
'enquiry' => 'admin.php?d=chinasavvy'
. '&p=form_enquiry',
'supplier_inbound' => 'admin.php?'
. 'd=chinasavvy&p=form_inbound'
)
);
// If the user is logged in as an Admin,
// then show the extended navigation
if (isset($_SESSION['admin_is_admin']) &&
$_SESSION['admin_is_admin'] == 1) {
$boxes['entry_forms']['customer_outbound'] =
'admin.php?d=chinasavvy&p=form_outbound';
$boxes['reports'] = array(
'enquiry_reports' => 'admin.php?
. 'd=chinasavvy&p=report_enquiry',
'supplier_reports' => 'admin.php?
. 'd=chinasavvy&p=report_inbound',
'customer_reports' => 'admin.php?
. 'd=chinasavvy&p=report_outbound',
);
$boxes['admin'] = array(
'staff_management' => 'admin.php?
. 'd=users&p=admin_users',
'client_management' => 'admin.php?
. 'd=users&p=admin_users
. '&option=is_admin&value=0'
);
$boxes['categories'] = array(
'category_admin' => 'admin.php?
. 'd=chinasavvy&p=admin_category',
'subcategory_admin' => 'admin.php?
. 'd=chinasavvy&p=admin_subcategory'
);
} elseif (isset($_SESSION['admin_is_admin']) &&
$_SESSION['admin_is_admin'] == 2) {
$boxes['assignments'] = array(
'assigned_enquiries' => 'admin.php?
. 'd=chinasavvy&p=report_enquiry'
);
}
echo $admin->showCompleteNav($boxes);
DataObject common class: InterThis class extends the DB_DataObject class and adds a number of methods that can be used by all our DataObjects. It also overrides some of the parent class' methods such as insert() and update(), and adds a replace() method to make use of MySQL's replace statement. One of its most important features is the setupFields() method, which prepares the DO by assigning it an array of Field objects, which are mapped to the fields of the Database table. These Field objects contain additional information that is used to create CRUD forms and datagrid tables. The setupField() method can be overridden by any DataObject that extends the Inter class, and is used to setup additional validation rules, display options and other specific settings. Following is a synopsis of the types of changes that can be made, using the DO_Country class as an example. function setupFields()
{
// Set default attributes here. Default size is 55
$_SESSION['static_field']->attr['html_attributes']['size'] = 55;
// Invoke parent's construct method, sets up all fields.
// This MUST be invoked before any of the following code!
parent::setupFields();
}
The overrideFields method takes three arguments, the first and third can be either strings or arrays. The first argument is one or more fields for which you want to override an attribute. The second is the attribute you wish to change. The third is either the value to assign to the attribute, and will be assigned to all the fields given in the first argument, or if an array is given, it must match the number of fields given in the first argument, and each of its values will be assigned respectively to each of the fields. $this->overrideFields(array('country_iso', 'currency'),
'required', true);
// Set Rule text
$this->overrideFields(
array('country',
'country_iso',
'currency'),
'required_msg',
array('Please enter a Country name',
'Please enter a country ISO',
'Please enter a currency'));
// Set whether they should display in the table
$this->overrideFields(
array('currency_sub_unit',
'full_name',
'currency',
'capital',
'adjective',
'continent',
'citizen',
'dial_code',
'idd',
'ndd'),
'showInTable',
false);
// Default elements
$this->overrideFields('capital', 'default', 'Paris');
// Any variation in the type default is text
$this->overrideFields('citizen', 'type', 'textarea_counter');
$this->overrideFields('citizen', 'quickform_attributes',
array('max_chars' => 160));
Additional fields can be setup also. The following comes from the DO_Users class // Add additional site-specific fields if set up
if (isset($_SESSION['user_options'])) {
foreach ($_SESSION['user_options'] as $option) {
if (!is_array($option)) {
continue;
} else {
$this->fields[] = new Structures_DataGrid_Field(
$this->translateOptions($option));
}
}
}
// Add the details field (column)
$this->fields[] = new Structures_DataGrid_Field(
array('name' => 'details',
'info' => $tr->get('details'),
'label' => $tr->get('details'),
'showInForm' => false,
'data_function' => 'DO_Users::getDetails()'
)
);
// Add the user_type form field
$user_types = $this->getUserTypes();
if (!empty($user_types)) {
$this->fields[] = new Structures_DataGrid_Field(
array('name' => 'user_type',
'info' => $tr->get('user_type'),
'label' => $tr->get('user_type'),
'showInTable' => false,
'type' => 'select',
'options' => $user_types,
'default' => 'none'
)
);
}
return $this->fields;
}
The 'data_function' attribute of the Field object is used to populate a DataGrid column with different data than the default value from the row in the database. You can set up any method in any object, but it must meet the following criteria:
Following is an example from the DO_Users class: /**
* Callback function for getting the drop-down of details for this User.
*
* @param array $params
* @return string The HTML code for the drop-down menu.
*/
function getDetails($params)
{
global $admin, $tr;
extract($params);
$html = '
<select onchange="window.location = \'admin.php?d=users&p=\'
+ this.value + \'&user_id=' . $record['user_id'] . '\';">
<option selected="selected">' . $tr->get('select') . '</option>
<option value="users_addresses">' . $tr->get('addresses') . '</option>
<option value="users_contacts">' . $tr->get('contacts') . '</option>';
if ($_SESSION['admin_is_admin'] == 1) {
$html .= '<option value="users_options">' .
$tr->get('options') . '</option>';
}
$html .= '</select>';
return $html;
}
External Links |
Modules |
Categories: Admin | PHP | Programming

