Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Sat, Feb 22, 3:50 AM

in-portal

Index: branches/5.1.x/core/kernel/utility/formatters/date_formatter.php
===================================================================
--- branches/5.1.x/core/kernel/utility/formatters/date_formatter.php (revision 14129)
+++ branches/5.1.x/core/kernel/utility/formatters/date_formatter.php (revision 14130)
@@ -1,452 +1,459 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
class kDateFormatter extends kFormatter {
/**
* Current Language
*
* @var LanguagesItem
*/
var $language = null;
function kDateFormatter()
{
parent::kBase();
$this->language =& $this->Application->recallObject('lang.current');
}
/**
* Sets mixed format (date + time) for field if not set directly
*
* @param Array $field_options options of field
* @param Array $format separate formats for date & time
* @param string $type destination key in field_options to store mixed format
*/
function SetMixedFormat(&$field_options, &$format, $type)
{
if (!isset($field_options[$type])) {
// default value is date+sepatator+time
$field_options[$type] = '_regional_DateTimeFormat';
}
if ($field_options[$type] == '_regional_DateTimeFormat') {
$field_options[$type] = $format['date'].$field_options['date_time_separator'].$format['time'];
}
else if(preg_match('/_regional_(.*)/', $field_options[$type], $regs)) {
$field_options[$type] = $this->language->GetDBField($regs[1]);
}
$format['mixed'] = $field_options[$type];
}
/**
* Returns separate formats for date,time,combined for input & display formats
*
* @param Array $field_options options of field
* @param string $type type of requested information = {mixed,date,time}
* @return Array display & input formats
*/
function GetSeparateFormats(&$field_options, $type)
{
if ($type == 'mixed') {
if (!isset($field_options['date_time_separator'])) $field_options['date_time_separator'] = ' ';
$display_format = Array ();
$input_format = Array ();
list ($display_format['date'], $input_format['date']) = $this->GetSeparateFormats($field_options, 'date');
list ($display_format['time'], $input_format['time']) = $this->GetSeparateFormats($field_options, 'time');
$this->SetMixedFormat($field_options, $display_format, 'format');
$this->SetMixedFormat($field_options, $input_format, 'input_format');
return Array ($display_format, $input_format);
}
else {
// 1. set display format
if (isset($field_options[$type.'_format'])) {
$format = $field_options[$type.'_format'];
}
else {
$format = $this->language->GetDBField(ucfirst($type).'Format');
}
// 2. set input format
if (isset($field_options['input_'.$type.'_format'])) {
$input_format = $field_options['input_'.$type.'_format'];
}
else {
$input_format = $this->language->GetDBField('Input'.ucfirst($type).'Format');
}
return Array ($format, $input_format);
}
}
function PrepareOptions($field_name, &$field_options, &$object)
{
list ($display_format, $input_format) = $this->GetSeparateFormats($field_options, 'mixed');
$field_options['sub_fields'] = Array('date' => $field_name.'_date', 'time' => $field_name.'_time');
if (!isset($field_options['use_timezone'])) {
// apply timezone from server
$field_options['use_timezone'] = true;
}
$add_fields = Array();
// 1. add DATE virtual field
$opts = Array('master_field' => $field_name, 'formatter' => 'kDateFormatter', 'format' => $display_format['date'], 'input_format' => $input_format['date']);
$copy_options = Array ('default', 'required', 'use_timezone', 'error_msgs');
foreach ($copy_options as $copy_option) {
if (array_key_exists($copy_option, $field_options) ) {
$opts[$copy_option] = $field_options[$copy_option];
}
}
$add_fields[$field_name.'_date'] = $opts;
// 2. add TIME virtual field
$opts['format'] = $display_format['time'];
$opts['input_format'] = $input_format['time'];
$add_fields[$field_name.'_time'] = $opts;
$filter_type = getArrayValue($field_options, 'filter_type');
if($filter_type == 'range')
{
$opts['format'] = $field_options['format'];
$add_fields[$field_name.'_rangefrom'] = $opts;
$add_fields[$field_name.'_rangeto'] = $opts;
}
if ( !isset($object->VirtualFields[$field_name]) ) {
// adding caluclated field to format date directly in the query
if ( !isset($object->CalculatedFields) || !is_array($object->CalculatedFields) ) {
$object->CalculatedFields = Array();
}
// $object->CalculatedFields[$field_name.'_formatted'] = 'FROM_UNIXTIME('.'`%1$s`.'.$field_name.', \''.$this->SQLFormat($field_options['format']).'\')';
$object->CalculatedFields[$field_name.'_date'] = '%1$s.'.$field_name;
$object->CalculatedFields[$field_name.'_time'] = '%1$s.'.$field_name;
// $opts['format'] = $field_options['format'];
// $opts['required'] = 0;
// unset($opts['master_field']);
// $add_fields[$field_name.'_formatted'] = $opts;
}
$add_fields = array_merge_recursive2($add_fields, $object->VirtualFields);
$object->setVirtualFields($add_fields);
}
function UpdateSubFields($field, $value, &$options, &$object)
{
if ( $sub_fields = getArrayValue($options, 'sub_fields') ) {
if( isset($value) && $value )
{
$object->SetDBField( $sub_fields['date'], $value );
$object->SetDBField( $sub_fields['time'], $value );
}
}
}
function UpdateMasterFields($field, $value, &$options, &$object)
{
// when in master field - set own value from sub_fields
if ( $sub_fields = getArrayValue($options, 'sub_fields') ) {
// if date is not empty, but time is empty - set time to 0, otherwise master field fomratter will complain
// when we have only date field on form, we need time hidden field always empty, don't ask me why!
if ($object->GetDBField($sub_fields['date']) != '' && $object->GetDBField($sub_fields['time']) == '') {
$empty_time = getArrayValue($options,'empty_time');
if ($empty_time === false) {
$empty_time = adodb_mktime(0, 0, 0);
}
$object->SetDBField($sub_fields['time'], $empty_time);
}
elseif ($object->GetDBField($sub_fields['time']) != '' && $object->GetDBField($sub_fields['date']) == '') {
$empty_date = getArrayValue($options,'empty_date');
if ($empty_date === false) {
$empty_date = adodb_mktime(0, 0, 0, 1, 1, 1970);
}
$object->SetDBField($sub_fields['date'], $empty_date);
}
$input_format['date'] = $object->Fields[ $sub_fields['date'] ]['input_format'];
$input_format['time'] = $object->Fields[ $sub_fields['time'] ]['input_format'];
$object->SetField($field, $object->GetField($sub_fields['date'], $input_format['date']).$options['date_time_separator'].$object->GetField($sub_fields['time'], $input_format['time']));
}
// when in one of sub_fields - call update for master_field to update its value from sub_fields [are you following ? :) ]
elseif ($master_field = getArrayValue($options, 'master_field') ) {
$opt = $object->GetFieldOptions($master_field);
$this->UpdateMasterFields($master_field, null, $opt, $object);
}
}
//function Format($value, $options, &$errors)
function Format($value, $field_name, &$object, $format=null)
{
if ( is_null($value) ) return '';
if ( !is_numeric($value) ) {
return $value; // for leaving badly formatted date on the form
}
settype($value, 'int');
if ( !is_int($value) ) {
return $value;
}
$options = $object->GetFieldOptions($field_name);
if ( isset($format) ) {
$options['format'] = $format;
}
if (preg_match('/_regional_(.*)/', $options['format'], $regs)) {
// when such type of format is given directly to kDBBase::GetField
$options['format'] = $this->language->GetDBField($regs[1]);
}
if (!$options['use_timezone']) {
return adodb_gmdate($options['format'], $value);
}
- return adodb_date($options['format'], $value);
+ $format = $options['format'];
+ $dt_separator = getArrayValue($options, 'date_time_separator');
+
+ if ($dt_separator) {
+ $format = trim($format, $dt_separator);
+ }
+
+ return adodb_date($format, $value);
}
function HumanFormat($format)
{
$patterns = Array('/m/',
'/n/',
'/d/',
'/j/',
'/y/',
'/Y/',
'/h|H/',
'/g|G/',
'/i/',
'/s/',
'/a|A/');
$replace = Array( 'mm',
'm',
'dd',
'd',
'yy',
'yyyy',
'hh',
'h',
'mm',
'ss',
'AM');
$res = preg_replace($patterns, $replace, $format);
return $res;
}
function SQLFormat($format)
{
$mapping = Array(
'/%/' => '%%',
'/(?<!%)a/' => '%p', // Lowercase Ante meridiem and Post meridiem => MySQL provides only uppercase
'/(?<!%)A/' => '%p', // Uppercase Ante meridiem and Post meridiem
'/(?<!%)d/' => '%d', // Day of the month, 2 digits with leading zeros
'/(?<!%)D/' => '%a', // A textual representation of a day, three letters
'/(?<!%)F/' => '%M', // A full textual representation of a month, such as January or March
'/(?<!%)g/' => '%l', // 12-hour format of an hour without leading zeros
'/(?<!%)G/' => '%k', // 24-hour format of an hour without leading zeros
'/(?<!%)h/' => '%h', // 12-hour format of an hour with leading zeros
'/(?<!%)H/' => '%H', // 24-hour format of an hour with leading zeros
'/(?<!%)i/' => '%i', // Minutes with leading zeros
'/(?<!%)I/' => 'N/A', // Whether or not the date is in daylights savings time
'/(?<!%)S/' => 'N/A', // English ordinal suffix for the day of the month, 2 characters, see below
'/jS/' => '%D', // MySQL can't return separate suffix, but could return date with suffix
'/(?<!%)j/' => '%e', // Day of the month without leading zeros
'/(?<!%)l/' => '%W', // A full textual representation of the day of the week
'/(?<!%)L/' => 'N/A', // Whether it's a leap year
'/(?<!%)m/' => '%m', // Numeric representation of a month, with leading zeros
'/(?<!%)M/' => '%b', // A short textual representation of a month, three letters
'/(?<!%)n/' => '%c', // Numeric representation of a month, without leading zeros
'/(?<!%)O/' => 'N/A', // Difference to Greenwich time (GMT) in hours
'/(?<!%)r/' => 'N/A', // RFC 2822 formatted date
'/(?<!%)s/' => '%s', // Seconds, with leading zeros
// S and jS moved before j - see above
'/(?<!%)t/' => 'N/A', // Number of days in the given month
'/(?<!%)T/' => 'N/A', // Timezone setting of this machine
'/(?<!%)U/' => 'N/A', // Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)
'/(?<!%)w/' => '%w', // Numeric representation of the day of the week
'/(?<!%)W/' => '%v', // ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)
'/(?<!%)Y/' => '%Y', // A full numeric representation of a year, 4 digits
'/(?<!%)y/' => '%y', // A two digit representation of a year
'/(?<!%)z/' => 'N/A', // The day of the year (starting from 0) => MySQL starts from 1
'/(?<!%)Z/' => 'N/A', // Timezone offset in seconds. The offset for timezones west of UTC is always negative, and for those east of UTC is always positive.
);
$patterns = array_keys($mapping);
$replacements = array_values($mapping);
$res = preg_replace($patterns, $replacements, $format);
return $res;
}
/**
* Converts formatted date+time to timestamp and validates format
*
* @param mixed $value
* @param string $field_name
* @param kDBItem $object
* @return string
*/
function Parse($value, $field_name, &$object)
{
$options = $object->GetFieldOptions($field_name);
$dt_separator = getArrayValue($options,'date_time_separator');
if($dt_separator) $value = trim($value, $dt_separator);
if($value == '') return NULL;
//return strtotime($value);
$format = $options['input_format'];
if ($dt_separator) $format = trim($format, $dt_separator);
$error_params = Array( $this->HumanFormat($format), adodb_date($format), 'value' => $value );
$hour = 0;
$minute = 0;
$second = 0;
$month = 1;
$day = 1;
$year = 1970;
$patterns['n'] = '([0-9]{1,2})';
$patterns['m'] = '([0-9]{1,2})';
$patterns['d'] = '([0-9]{1,2})';
$patterns['j'] = '([0-9]{1,2})';
$patterns['Y'] = '([0-9]{4})';
$patterns['y'] = '([0-9]{2})';
$patterns['G'] = '([0-9]{1,2})';
$patterns['g'] = '([0-9]{1,2})';
$patterns['H'] = '([0-9]{2})';
$patterns['h'] = '([0-9]{2})';
$patterns['i'] = '([0-9]{2})';
$patterns['s'] = '([0-9]{2})';
$patterns['a'] = '(am|pm)';
$patterns['A'] = '(AM|PM)';
$holders_mask = '/' . preg_replace('/[a-zA-Z]{1}/i', '([a-zA-Z]{1})', preg_quote($format, '/')) . '/';
if (!preg_match($holders_mask, $format, $holders)) {
$object->SetError($field_name, 'bad_date_format', null, $error_params);
return $value;
}
$values_mask = '/^' . preg_quote($format, '/') . '$/';
foreach ($patterns as $key => $val) {
$values_mask = str_replace($key, $val, $values_mask);
}
if (!preg_match($values_mask, $value, $values)) {
$object->SetError($field_name, 'bad_date_format', null, $error_params);
return $value;
}
for ($i = 1; $i < count($holders); $i++) {
switch ($holders[$i]) {
case 'n':
case 'm':
$month = $values[$i];
$month = preg_replace('/^0{1}/', '', $month);
break;
case 'd':
$day = $values[$i];
$day = preg_replace('/^0{1}/', '', $day);
break;
case 'Y':
$year = $values[$i];
break;
case 'y':
$year = $values[$i] >= 70 ? 1900 + $values[$i] : 2000 + $values[$i];
break;
case 'H':
case 'h':
case 'G':
case 'g':
$hour = $values[$i];
$hour = preg_replace('/^0{1}/', '', $hour);
break;
case 'i':
$minute = $values[$i];
$minute = preg_replace('/^0{1}/', '', $minute);
break;
case 's':
$second = $values[$i];
$second = preg_replace('/^0{1}/', '', $second);
break;
case 'a':
case 'A':
if ($hour <= 12) { // if AM/PM used with 24-hour - could happen :)
if ($values[$i] == 'pm' || $values[$i] == 'PM') {
$hour += 12;
if ($hour == 24) $hour = 12;
}
elseif ($values[$i] == 'am' || $values[$i] == 'AM') {
if ($hour == 12) $hour = 0;
}
}
break;
}
}
//echo "day: $day, month: $month, year: $year, hour: $hour, minute: $minute<br>";
/*if (!($year >= 1970 && $year <= 2037)) {
$object->SetError($field_name, 'bad_date_format', null, $error_params);
return $value;
}*/
if (!($month >= 1 && $month <= 12)) {
$object->SetError($field_name, 'bad_date_format', null, $error_params);
return $value;
}
$months_days = Array ( 1 => 31,2 => 28, 3 => 31, 4 => 30,5 => 31,6 => 30, 7 => 31, 8 => 31,9 => 30,10 => 31,11 => 30,12 => 31);
if ($year % 4 == 0) $months_days[2] = 29;
if (!($day >=1 && $day <= $months_days[$month])) {
$object->SetError($field_name, 'bad_date_format', null, $error_params);
return $value;
}
if (!($hour >=0 && $hour <= 23)) {
$object->SetError($field_name, 'bad_date_format', null, $error_params);
return $value;
}
if (!($minute >=0 && $minute <= 59)) {
$object->SetError($field_name, 'bad_date_format', null, $error_params);
return $value;
}
if (!($second >=0 && $second <= 59)) {
$object->SetError($field_name, 'bad_date_format', null, $error_params);
return $value;
}
if (!$options['use_timezone']) {
return adodb_gmmktime($hour, $minute, $second, $month, $day, $year);
}
return adodb_mktime($hour, $minute, $second, $month, $day, $year);
}
function GetSample($field, &$options, &$object)
{
return $this->Format( adodb_mktime(), $field, $object, $options['input_format']);
}
}
\ No newline at end of file
Index: branches/5.1.x/core/units/custom_data/custom_data_event_handler.php
===================================================================
--- branches/5.1.x/core/units/custom_data/custom_data_event_handler.php (revision 14129)
+++ branches/5.1.x/core/units/custom_data/custom_data_event_handler.php (revision 14130)
@@ -1,213 +1,219 @@
<?php
/**
* @version $Id$
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class CustomDataEventHandler extends kDBEventHandler {
/**
* [HOOK] Allows to apply custom fields functionality to specific config
* When main item is created, then cdata config is cloned
*
* @param kEvent $event
*/
function OnDefineCustomFields(&$event)
{
// 1. clone customdata table
$clones = $this->Application->getUnitOption('cdata', 'Clones');
$clones[$event->MasterEvent->Prefix.'-cdata'] = Array (
'ParentPrefix' => $event->MasterEvent->Prefix,
'TableName' => $this->Application->getUnitOption($event->MasterEvent->Prefix, 'TableName').'CustomData',
);
$this->Application->setUnitOption('cdata', 'Clones', $clones);
// 2. add custom field information to main item
$this->createCustomFields($event->MasterEvent->Prefix);
}
function scanCustomFields($prefix)
{
static $custom_fields = Array ();
if (defined('IS_INSTALL') && IS_INSTALL && !$this->Application->TableFound('CustomField')) {
return false;
}
if (!$prefix) {
// prefix not specified
return false;
}
$item_type = $this->Application->getUnitOption($prefix, 'ItemType');
if (!$item_type) {
// no main config of such type
return false;
}
$no_caching = (defined('IS_INSTALL') && IS_INSTALL) || (defined('CUSTOM_FIELD_ADDED') && CUSTOM_FIELD_ADDED);
if (!$custom_fields || $no_caching) {
// query all custom fields at once -> saves 4 sqls queries
if ($no_caching) {
$all_custom_fields = $this->getCustomFields();
}
else {
$cache_key = 'all_custom_fields[%CfSerial%][%ModSerial%]';
$all_custom_fields = $this->Application->getCache($cache_key, false);
if ($all_custom_fields === false) {
$this->Conn->nextQueryCachable = true;
$all_custom_fields = $this->getCustomFields();
$this->Application->setCache($cache_key, $all_custom_fields);
}
}
foreach ($all_custom_fields as $custom_field_id => $custom_field_data) {
$cf_type = $custom_field_data['Type'];
if (!array_key_exists($cf_type, $custom_fields)) {
$custom_fields[$cf_type] = Array ();
}
$custom_fields[$cf_type][$custom_field_id] = $custom_field_data;
}
}
return array_key_exists($item_type, $custom_fields) ? $custom_fields[$item_type] : false;
}
/**
* Returns sorted list of all custom fields
*
* @return Array
*/
function getCustomFields()
{
$sql = 'SELECT *
FROM '.TABLE_PREFIX.'CustomField';
$ret = $this->Conn->Query($sql, 'CustomFieldId');
ksort($ret);
return $ret;
}
/**
* Fills cloned cdata config with data from it's parent
*
* @param kEvent $event
*/
function OnAfterConfigRead(&$event)
{
$main_prefix = $this->Application->getUnitOption($event->Prefix, 'ParentPrefix');
if (!$main_prefix) {
return false;
}
$custom_fields = $this->scanCustomFields($main_prefix);
if (!$custom_fields) {
return false;
}
// 2. create fields (for customdata item)
$fields = $this->Application->getUnitOption($event->Prefix, 'Fields', Array());
$field_options = Array('type' => 'string', 'formatter' => 'kMultiLanguage', 'db_type' => 'text', 'default' => '');
foreach ($custom_fields as $custom_id => $custom_params) {
if (isset($fields['cust_'.$custom_id])) continue;
$fields['cust_'.$custom_id] = $field_options;
$fields['cust_'.$custom_id]['force_primary'] = !$custom_params['MultiLingual'];
}
$this->Application->setUnitOption($event->Prefix, 'Fields', $fields);
}
/**
* Creates "cust_<custom_name>" virtual fields for main item
*
* @param string $prefix
*/
function createCustomFields($prefix)
{
$custom_fields = $this->scanCustomFields($prefix);
if (!$custom_fields) {
return false;
}
$calculated_fields = Array();
$virtual_fields = $this->Application->getUnitOption($prefix, 'VirtualFields', Array());
$cf_helper =& $this->Application->recallObject('InpCustomFieldsHelper');
$ml_formatter =& $this->Application->recallObject('kMultiLanguage');
/* @var $ml_formatter kMultiLanguage */
$is_install = defined('IS_INSTALL') && IS_INSTALL;
foreach ($custom_fields as $custom_id => $custom_params) {
$custom_name = $custom_params['FieldName'];
$field_options = Array('type' => 'string', 'default' => $custom_params['DefaultValue']);
// raises warnings during 4.3.9 -> 5.0.0 upgrade, no fatal sqls though
if ($custom_params['IsRequired']) {
$field_options['required'] = 1;
}
switch ($custom_params['ElementType']) {
case 'date':
+ unset($field_options['options']);
+ $field_options['formatter'] = 'kDateFormatter';
+ $field_options['input_time_format'] = '';
+ $field_options['time_format'] = '';
+ break;
+
case 'datetime':
unset($field_options['options']);
$field_options['formatter'] = 'kDateFormatter';
break;
case 'select':
case 'multiselect':
case 'radio':
if ($custom_params['ValueList']) {
// $is_install check prevents 335 bad phrase sql errors on upgrade to 5.1.0
$field_options['options'] = $is_install ? Array () : $cf_helper->GetValuesHash($custom_params['ValueList']);
$field_options['formatter'] = 'kOptionsFormatter';
$field_options['multiple'] = $custom_params['ElementType'] == 'multiselect';
}
break;
default:
if ($custom_params['MultiLingual']) {
$calculated_fields['cust_'.$custom_name.'_Primary'] = 'cust.'.$ml_formatter->LangFieldName('cust_'.$custom_id, true);
$virtual_fields['cust_'.$custom_name.'_Primary'] = $field_options;
$field_options['master_field'] = 'cust_'.$custom_name.'_Primary';
$field_options['formatter'] = 'kCustomFieldFormatter';
}
break;
}
$calculated_fields['cust_'.$custom_name] = 'cust.'.$ml_formatter->LangFieldName('cust_'.$custom_id, !$custom_params['MultiLingual']);
if (!isset($virtual_fields['cust_'.$custom_name])) {
$virtual_fields['cust_'.$custom_name] = Array();
}
$virtual_fields['cust_'.$custom_name] = array_merge_recursive2($field_options, $virtual_fields['cust_'.$custom_name]);
$custom_fields[$custom_id] = $custom_name;
}
$config_calculated_fields = $this->Application->getUnitOption($prefix, 'CalculatedFields', Array());
foreach ($config_calculated_fields as $special => $special_fields) {
if ($special == '-virtual') {
continue;
}
$config_calculated_fields[$special] = array_merge_recursive2($config_calculated_fields[$special], $calculated_fields);
}
$this->Application->setUnitOption($prefix, 'CalculatedFields', $config_calculated_fields);
$this->Application->setUnitOption($prefix, 'CustomFields', $custom_fields);
$this->Application->setUnitOption($prefix, 'VirtualFields', $virtual_fields);
}
}
\ No newline at end of file

Event Timeline