Page Menu
Home
In-Portal Phabricator
Search
Configure Global Search
Log In
Files
F776277
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
Fri, Feb 7, 1:03 AM
Size
4 KB
Mime Type
text/x-diff
Expires
Sun, Feb 9, 1:03 AM (1 d, 3 h)
Engine
blob
Format
Raw Data
Handle
558726
Attached To
rINP In-Portal
in-portal
View Options
Index: branches/RC/core/units/general/helpers/controls/edit_picker_helper.php
===================================================================
--- branches/RC/core/units/general/helpers/controls/edit_picker_helper.php (revision 10527)
+++ branches/RC/core/units/general/helpers/controls/edit_picker_helper.php (revision 10528)
@@ -1,69 +1,129 @@
<?php
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 ;
}
$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 = $object->IDField.' '.$filter_type.' ('.implode(',', $selected_items).')';
}
else {
$filter_clause = ($event->Special == 'selected') ? 'FALSE' : 'TRUE';
}
$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();
$selected_items = array_unique($this->Conn->GetCol($sql));
$object->SetDBField($store_field, $selected_items ? '|'.implode('|', $selected_items).'|' : '');
}
+
+ /**
+ * Saves value to subitem'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();
+ $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();
+ $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) {
+ $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();
+
+ if ($object->IsTempTable()) {
+ $object->setTempID();
+ }
+ }
+ }
+ }
}
?>
\ No newline at end of file
Property changes on: branches/RC/core/units/general/helpers/controls/edit_picker_helper.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.1.8.1
\ No newline at end of property
+1.1.8.2
\ No newline at end of property
Event Timeline
Log In to Comment