Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F1097282
in-portal
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Tue, Aug 12, 4:34 PM
Size
13 KB
Mime Type
text/x-diff
Expires
Thu, Aug 14, 4:34 PM (9 h, 13 m)
Engine
blob
Format
Raw Data
Handle
710678
Attached To
rINP In-Portal
in-portal
View Options
Index: branches/5.2.x/core/units/helpers/controls/minput_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/controls/minput_helper.php (revision 15231)
+++ branches/5.2.x/core/units/helpers/controls/minput_helper.php (revision 15232)
@@ -1,218 +1,218 @@
<?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 MInputHelper extends kHelper {
/**
* Returns table for given prefix
*
* @param string $prefix
* @param bool $temp
* @return string
* @access protected
*/
protected function getTable($prefix, $temp = false)
{
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
return $temp ? $this->Application->GetTempName($table_name, 'prefix:' . $prefix) : $table_name;
}
function prepareMInputXML($records, $use_fields)
{
$xml = '';
foreach ($records as $record) {
$xml .= '<record>';
foreach ($record as $field_name => $field_value) {
if (!in_array($field_name, $use_fields)) {
continue;
}
$xml .= '<field name="' . $field_name . '">' . htmlspecialchars($field_value) . '</field>';
}
$xml .= '</record>';
}
return $xml ? '<records>'.$xml.'</records>' : '';
}
/**
* Returns validation errors in XML format
*
* @param kDBItem $object
* @param Array $fields_hash
* @return string
*/
function prepareErrorsXML(&$object, $fields_hash)
{
$xml = '';
$errors = Array ();
foreach ($fields_hash as $field_name => $field_value) {
if (!$object->ValidateField($field_name)) {
$field_options = $object->GetFieldOptions($field_name);
$error_field = array_key_exists('error_field', $field_options) ? $field_options['error_field'] : $field_name;
$errors[$error_field] = '<field name="'.$error_field.'">'.$object->GetErrorMsg($error_field, false).'</field>';
}
}
return '<errors>'.implode('', $errors).'</errors>';
}
/**
* Validates MInput control fields
*
* @param kEvent $event
*/
function OnValidateMInputFields($event)
{
$object = $event->getObject();
/* @var $object kDBItem */
$items_info = $this->Application->GetVar($event->getPrefixSpecial(true));
if ($items_info) {
list ($id, $field_values) = each($items_info);
foreach ($field_values as $field_name => $field_value) {
$object->SetField($field_name, $field_value);
}
$event_mapping = Array (
'AddRecord' => 'OnBeforeItemCreate',
'SaveRecord' => 'OnBeforeItemUpdate',
);
$request_type = $this->Application->GetVar('request_type');
if (array_key_exists($request_type, $event_mapping)) {
$event->CallSubEvent($event_mapping[$request_type]);
}
echo $this->prepareErrorsXML($object, $field_values);
}
$event->status = kEvent::erSTOP;
}
function parseMInputXML($xml)
{
$records = Array ();
$records_node = simplexml_load_string($xml);
if ( $records_node === false ) {
return false;
}
foreach ($records_node as $record_node) {
$record = Array ();
foreach ($record_node as $field_node) {
$record[(string)$field_node['name']] = (string)$field_node;
}
$records[] = $record;
}
return $records;
}
/**
* Loads selected values from sub_prefix to main item virtual field.
* Called from OnAfterItemLoad of main prefix.
*
* @param kEvent $event
* @param string $store_field main item's field name, to store values into
* @param string $sub_prefix prefix used to store info about selected items
* @param Array $use_fields fields, used in value string building
*/
function LoadValues($event, $store_field, $sub_prefix, $use_fields)
{
$object = $event->getObject();
/* @var $object kDBItem */
$sub_item = $this->Application->recallObject($sub_prefix, null, Array('skip_autoload' => true));
/* @var $sub_item kDBItem */
$foreign_key = $this->Application->getUnitOption($sub_prefix, 'ForeignKey');
$sql = 'SELECT *
FROM '.$this->getTable($sub_prefix, $object->IsTempTable()).'
WHERE '.$foreign_key.' = '.$object->GetID();
$selected_items = $this->Conn->Query($sql);
$field_names = array_keys( $sub_item->GetFieldValues() );
foreach ($selected_items as $key => $fields_hash) {
$sub_item->Clear();
$sub_item->SetDBFieldsFromHash($fields_hash);
// to fill *_date and *_time fields from main date fields
$sub_item->UpdateFormattersSubFields();
foreach ($field_names as $field) {
$field_options = $sub_item->GetFieldOptions($field);
$formatter = array_key_exists('formatter', $field_options) ? $field_options['formatter'] : false;
if ($formatter == 'kDateFormatter') {
$selected_items[$key][$field] = $sub_item->GetField($field, $field_options['input_format']);
}
else {
$selected_items[$key][$field] = $sub_item->GetDBField($field);
}
}
}
$object->SetDBField($store_field, $this->prepareMInputXML($selected_items, $use_fields));
}
/**
* Saves data from minput control to subitem table (used from subitem hook)
*
* @param kEvent $sub_event
* @param string $store_field
*/
function SaveValues(&$sub_event, $store_field)
{
- $main_object =& $sub_event->MasterEvent->getObject();
+ $main_object = $sub_event->MasterEvent->getObject();
/* @var $main_object kDBItem */
$affected_field = $main_object->GetDBField($store_field);
$object = $this->Application->recallObject($sub_event->getPrefixSpecial(), null, Array ('skip_autoload' => true));
/* @var $object kDBItem */
$sub_table = $object->TableName;
$foreign_key = $this->Application->getUnitOption($sub_event->Prefix, 'ForeignKey');
$sql = 'DELETE FROM '.$sub_table.'
WHERE '.$foreign_key.' = '.$main_object->GetID();
$this->Conn->Query($sql);
if ($affected_field) {
$records = $this->parseMInputXML($affected_field);
$main_id = $main_object->GetID();
foreach ($records as $fields_hash) {
$object->Clear();
$fields_hash[$foreign_key] = $main_id;
$object->SetDBFieldsFromHash($fields_hash);
$object->Create();
}
}
}
}
\ No newline at end of file
Index: branches/5.2.x/core/units/helpers/controls/edit_picker_helper.php
===================================================================
--- branches/5.2.x/core/units/helpers/controls/edit_picker_helper.php (revision 15231)
+++ branches/5.2.x/core/units/helpers/controls/edit_picker_helper.php (revision 15232)
@@ -1,186 +1,186 @@
<?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 EditPickerHelper extends kHelper {
function getTable($prefix, $temp=false)
{
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
return $temp ? $this->Application->GetTempName($table_name, 'prefix:'.$prefix) : $table_name;
}
/**
* Applies filter for multiple lists in inp_edit_picker control.
* Called from SetCustomQuery of prefix, that contains all available items.
*
* @param kEvent $event
* @param string $storage_field main item's field name, where values are located
*/
function applyFilter($event, $storage_field)
{
if ($event->Special != 'selected' && $event->Special != 'available') {
return ;
}
if ($storage_field != $event->getEventParam('link_to_field')) {
return ;
}
$object = $event->getObject();
/* @var $object kDBList */
$main_object = $this->Application->recallObject($event->getEventParam('link_to_prefix'));
/* @var $main_object kDBItem */
$selected_items = $main_object->GetDBField($storage_field);
if ($selected_items) {
$filter_type = $event->Special == 'selected' ? 'IN' : 'NOT IN';
$selected_items = explode('|', substr($selected_items, 1, -1));
$filter_clause = '%1$s.' . $object->IDField.' '.$filter_type.' ('.implode(',', $selected_items).')';
}
else {
$filter_clause = ($event->Special == 'selected') ? 'FALSE' : 'TRUE';
}
$constrain = $this->_getConstrain($main_object, $storage_field, 'filter');
if ($constrain) {
$filter_clause .= ' AND (' . $constrain . ')';
}
$object->addFilter('edit_picker_filter', $filter_clause);
}
/**
* Loads selected values from sub_prefix to main item virtual field.
* Called from OnAfterItemLoad of main prefix.
*
* @param kEvent $event
* @param string $store_field main item's field name, to store values into
* @param string $source_field prefix and it's field used to store info about selected items (format: prefix.field)
*/
function LoadValues($event, $store_field, $source_field)
{
$object = $event->getObject();
/* @var $object kDBItem */
list ($sub_prefix, $sub_prefix_field) = explode('.', $source_field);
$foreign_key = $this->Application->getUnitOption($sub_prefix, 'ForeignKey');
$sql = 'SELECT '.$sub_prefix_field.'
FROM '.$this->getTable($sub_prefix, $object->IsTempTable()).'
WHERE '.$foreign_key.' = '.$object->GetID();
$constrain = $this->_getConstrain($object, $store_field, 'load');
if ($constrain) {
$sql .= ' AND (' . $sub_prefix_field . ' IN (' . $constrain . '))';
}
$selected_items = array_unique($this->Conn->GetCol($sql));
$object->SetDBField($store_field, $selected_items ? '|'.implode('|', $selected_items).'|' : '');
}
/**
* Saves value to sub-item's table
*
* @param kEvent $sub_event
* @param string $store_field main item's field name, to get values from
* @param string $sub_prefix_field check already existing records by this field
*/
function SaveValues(&$sub_event, $store_field, $sub_prefix_field)
{
- $main_object =& $sub_event->MasterEvent->getObject();
+ $main_object = $sub_event->MasterEvent->getObject();
/* @var $main_object kDBItem */
$affected_field = $main_object->GetDBField($store_field);
$object = $this->Application->recallObject($sub_event->getPrefixSpecial(), null, Array('skip_autoload' => true));
/* @var $object kDBItem */
$sub_table = $object->TableName;
$foreign_key = $this->Application->getUnitOption($sub_event->Prefix, 'ForeignKey');
// 1. get previous values from db
$sql = 'SELECT ' . $sub_prefix_field . '
FROM ' . $sub_table . '
WHERE '.$foreign_key.' = '.$main_object->GetID();
$constrain = $this->_getConstrain($main_object, $store_field, 'save');
if ($constrain) {
$sql .= ' AND (' . $sub_prefix_field . ' IN (' . $constrain . '))';
}
$old_values = $this->Conn->GetCol($sql);
// 2. get new values from form
$new_values = $affected_field ? explode('|', substr($affected_field, 1, -1)) : Array ();
$records_to_add = array_diff($new_values, $old_values);
$records_to_delete = array_diff($old_values, $new_values);
if ($records_to_delete && $main_object->isLoaded()) {
$where_clause = Array (
$foreign_key . ' = ' . $main_object->GetID(),
$sub_prefix_field . ' IN (' . implode(',', $records_to_delete) . ')',
);
$sql = 'SELECT ' . $object->IDField . '
FROM ' . $sub_table . '
WHERE (' . implode(') AND (', $where_clause) . ')';
$delete_ids = $this->Conn->GetCol($sql);
foreach ($delete_ids as $delete_id) {
$object->Delete($delete_id);
}
}
if ($records_to_add) {
$main_id = $main_object->GetID();
foreach ($records_to_add as $add_id) {
$object->Clear();
$object->SetDBField($foreign_key, $main_id);
$object->SetDBField($sub_prefix_field, $add_id);
$object->Create();
}
}
}
/**
* Returns constrain for picker options query
*
* @param kDBItem $object
* @param string $store_field
* @param string $mode
* @return bool|string
*/
function _getConstrain(&$object, $store_field, $mode = 'filter')
{
$field_options = $object->GetFieldOptions($store_field);
$constrain = array_key_exists('option_constrain', $field_options) ? $field_options['option_constrain']
: false;
if ( $mode == 'filter' ) {
// filter on edit form
return $constrain;
}
elseif ( $constrain ) {
// load or save
return sprintf($field_options['options_sql'], $field_options['option_key_field']);
}
return false;
}
}
\ No newline at end of file
Event Timeline
Log In to Comment