Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Sat, Jul 19, 5:30 AM

in-portal

Index: trunk/kernel/units/custom_fields/custom_fields_event_handler.php
===================================================================
--- trunk/kernel/units/custom_fields/custom_fields_event_handler.php (revision 4016)
+++ trunk/kernel/units/custom_fields/custom_fields_event_handler.php (revision 4017)
@@ -1,96 +1,112 @@
<?php
class CustomFieldsEventHandler extends InpDBEventHandler {
/**
* Apply any custom changes to list's sql query
*
* @param kEvent $event
* @access protected
* @see OnListBuild
*/
function SetCustomQuery(&$event)
{
$object =& $event->getObject();
$item_type=$this->Application->GetVar('cf_type');
$object->addFilter('itemtype_filter', '%1$s.Type = '.$item_type);
//$object->AddOrderField('DisplayOrder', 'ASC');
}
/**
* Prevents from duplicate item creation
*
* @param kEvent $event
*/
function OnBeforeItemCreate(&$event)
{
$object =& $event->getObject();
$live_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'SELECT COUNT(*)
FROM '.$live_table.'
WHERE FieldName = '.$this->Conn->qstr($object->GetDBField('FieldName')).' AND Type = '.$object->GetDBField('Type');
$found = $this->Conn->GetOne($sql);
if ($found) {
$event->status = erFAIL;
$object->SetError('FieldName', 'duplicate', 'la_error_CustomExists');
}
}
/**
* Occurse after deleting item, id of deleted item
* is stored as 'id' param of event
*
* @param kEvent $event
* @access public
*/
function OnAfterItemDelete(&$event)
{
$object =& $event->getObject();
- $custom_field_id=$event->getEventParam('id');
- $this->Application->SetVar('cf_id', '');
+ $main_prefix = $this->getPrefixByItemType($object->GetDBField('Type'));
- $sql='DELETE FROM '.TABLE_PREFIX.'CustomMetaData WHERE CustomFieldId = '.$custom_field_id;
- $this->Conn->Query($sql);
+ $ml_helper =& $this->Application->recallObject('kMultiLanguageHelper');
+ $ml_helper->deleteField($main_prefix.'-cdata', $event->getEventParam('id'));
+ }
+
+ /**
+ * Get config prefix based on item type
+ *
+ * @param unknown_type $item_type
+ * @return unknown
+ */
+ function getPrefixByItemType($item_type)
+ {
+ $sql = 'SELECT Prefix
+ FROM '.TABLE_PREFIX.'ItemTypes
+ WHERE ItemType = '.$item_type;
+ return $this->Conn->GetOne($sql);
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnSaveCustomField(&$event)
{
+ if ($event->MasterEvent->status != erSUCCESS) {
+ return false;
+ }
+
$object =& $event->getObject();
- $sql = 'SELECT Prefix
- FROM '.TABLE_PREFIX.'ItemTypes
- WHERE ItemType = '.$object->GetDBField('Type');
- $main_prefix = $this->Conn->GetOne($sql);
+ $main_prefix = $this->getPrefixByItemType($object->GetDBField('Type'));
+
+ $this->Application->HandleEvent( new kEvent($main_prefix.'-cdata:OnCreateCustomFields') );
$ml_helper =& $this->Application->recallObject('kMultiLanguageHelper');
$ml_helper->createFields($main_prefix.'-cdata');
}
function OnMassDelete(&$event)
{
parent::OnMassDelete($event);
$event->redirect_params = Array('opener' => 's');
}
/**
* Prepare temp tables for creating new item
* but does not create it. Actual create is
* done in OnPreSaveCreated
*
* @param kEvent $event
*/
function OnPreCreate(&$event)
{
parent::OnPreCreate($event);
$object =& $event->getObject();
$object->SetDBField('Type', $this->Application->GetVar('cf_type'));
}
}
?>
\ No newline at end of file
Property changes on: trunk/kernel/units/custom_fields/custom_fields_event_handler.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.8
\ No newline at end of property
+1.9
\ No newline at end of property
Index: trunk/kernel/units/general/helpers/multilanguage.php
===================================================================
--- trunk/kernel/units/general/helpers/multilanguage.php (revision 4016)
+++ trunk/kernel/units/general/helpers/multilanguage.php (revision 4017)
@@ -1,181 +1,196 @@
<?php
/**
* Performs action on multilingual fields
*
*/
class kMultiLanguageHelper extends kHelper {
var $languageCount = 0;
/**
* Structure of table, that is currently processed
*
* @var Array
*/
var $curStructure = Array();
/**
* Field, to get structure information from
*
* @var string
*/
var $curSourceField = false;
/**
* Fields from config, that are currently used
*
* @var Array
*/
var $curFields = Array();
function kMultiLanguageHelper()
{
parent::kHelper();
$this->languageCount = $this->getLanguageCount();
}
/**
* Returns language count in system (always is divisible by 5)
*
*/
function getLanguageCount()
{
$table_name = $this->Application->getUnitOption('lang', 'TableName');
$languages_count = $this->Conn->GetOne('SELECT COUNT(*) FROM '.$table_name);
return $languages_count + 5 - ( $languages_count % 5 ? ($languages_count % 5) : 5 );
}
function scanTable($mask)
{
$i = 0;
$fields_found = 0;
$fields = array_keys($this->curStructure);
foreach ($fields as $field_name) {
if (preg_match($mask, $field_name)) {
$fields_found++;
}
}
return $fields_found;
}
function readTableStructure($table_name)
{
static $structure_status = Array();
if (!getArrayValue($structure_status, $table_name)) {
$this->curStructure = $this->Conn->Query('DESCRIBE '.$table_name, 'Field');
$structure_status[$table_name] = true;
}
}
/**
* Creates missing multilanguage fields in table by specified prefix
*
* @param string $prefix
*/
function createFields($prefix)
{
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
$this->curFields = $this->Application->getUnitOption($prefix, 'Fields');
if (!($table_name && $this->curFields) ) {
// invalid config found or prefix not found
return true;
}
$sqls = Array();
foreach($this->curFields as $field_name => $field_options)
{
if (getArrayValue($field_options, 'formatter') == 'kMultiLanguage') {
$this->readTableStructure($table_name);
$created_count = $this->getCreatedCount($field_name);
$create_count = $this->languageCount - $created_count;
if ($create_count > 0) {
// `l77_Name` VARCHAR( 255 ) NULL DEFAULT '0';
$field_mask = Array();
$field_mask['name'] = 'l%s_'.$field_name;
$field_mask['null'] = getArrayValue($field_options, 'not_null') ? 'NOT NULL' : 'NULL';
if ($this->curSourceField) {
$default_value = $this->getFieldParam('Default') != 'NULL' ? $this->Conn->qstr($this->getFieldParam('Default')) : $this->getFieldParam('Default');
$field_mask['type'] = $this->getFieldParam('Type');
}
else {
$default_value = is_null($field_options['default']) ? 'NULL' : $this->Conn->qstr($field_options['default']);
$field_mask['type'] = $field_options['db_type'];
}
$field_mask['default'] = 'DEFAULT '.$default_value;
$field_mask = $field_mask['name'].' '.$field_mask['type'].' '.$field_mask['null'].' '.$field_mask['default'];
$sqls[] = 'ALTER TABLE '.$table_name.( $this->generateAlterSQL($field_mask, $created_count + 1, $create_count) );
}
}
}
foreach ($sqls as $sql_query) {
$this->Conn->Query($sql_query);
}
}
+ function deleteField($prefix, $custom_id)
+ {
+ $table_name = $this->Application->getUnitOption($prefix, 'TableName');
+ $sql = 'DESCRIBE '.$table_name.' "l%_cust_'.$custom_id.'"';
+ $fields = $this->Conn->GetCol($sql);
+
+ $sql = 'ALTER TABLE '.$table_name.' ';
+ $sql_template = 'DROP COLUMN %s, ';
+ foreach ($fields as $field_name) {
+ $sql .= sprintf($sql_template, $field_name);
+ }
+ $sql = preg_replace('/(.*), $/', '\\1', $sql);
+ $this->Conn->Query($sql);
+ }
+
/**
* Returns parameter requested of current source field
*
* @param string $param_name
* @return string
*/
function getFieldParam($param_name)
{
return $this->curStructure[$this->curSourceField][$param_name];
}
function getCreatedCount($field_name)
{
$ret = $this->scanTable('/^l[\d]+_'.preg_quote($field_name, '/').'/');
if (!$ret) {
// no multilingual fields at all (but we have such field without language prefix)
$original_found = $this->scanTable('/'.preg_quote($field_name, '/').'/');
$this->curSourceField = $original_found ? $field_name : false;
}
else {
$this->curSourceField = 'l1_'.$field_name;
}
return $ret;
}
/**
* Returns ALTER statement part for adding required fields to table
*
* @param string $field_mask sql mask for creating field with correct definition (type & size)
* @param int $start_index add new fields starting from this index
* @param int $create_count create this much new multilingual field translations
* @return string
*/
function generateAlterSQL($field_mask, $start_index, $create_count)
{
$i_count = $start_index + $create_count;
$ret = ' ';
while ($start_index < $i_count) {
list($prev_field,$type) = explode(' ', sprintf($field_mask, $start_index - 1) );
if (substr($prev_field, 0, 3) == 'l0_') {
$prev_field = substr($prev_field, 3, strlen($prev_field));
if (!$this->curSourceField) {
// get field name before this one
$fields = array_keys($this->curFields);
$prev_field = $fields[array_search($prev_field, $fields) - 1];
if (getArrayValue($this->curFields[$prev_field], 'formatter') == 'kMultiLanguage') {
$prev_field = 'l'.$this->languageCount.'_'.$prev_field;
}
}
}
$ret .= 'ADD COLUMN '.sprintf($field_mask, $start_index).' AFTER `'.$prev_field.'`, ';
$start_index++;
}
return preg_replace('/, $/',';',$ret);
}
}
?>
\ No newline at end of file
Property changes on: trunk/kernel/units/general/helpers/multilanguage.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.1
\ No newline at end of property
+1.2
\ No newline at end of property
Index: trunk/core/units/custom_fields/custom_fields_event_handler.php
===================================================================
--- trunk/core/units/custom_fields/custom_fields_event_handler.php (revision 4016)
+++ trunk/core/units/custom_fields/custom_fields_event_handler.php (revision 4017)
@@ -1,96 +1,112 @@
<?php
class CustomFieldsEventHandler extends InpDBEventHandler {
/**
* Apply any custom changes to list's sql query
*
* @param kEvent $event
* @access protected
* @see OnListBuild
*/
function SetCustomQuery(&$event)
{
$object =& $event->getObject();
$item_type=$this->Application->GetVar('cf_type');
$object->addFilter('itemtype_filter', '%1$s.Type = '.$item_type);
//$object->AddOrderField('DisplayOrder', 'ASC');
}
/**
* Prevents from duplicate item creation
*
* @param kEvent $event
*/
function OnBeforeItemCreate(&$event)
{
$object =& $event->getObject();
$live_table = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'SELECT COUNT(*)
FROM '.$live_table.'
WHERE FieldName = '.$this->Conn->qstr($object->GetDBField('FieldName')).' AND Type = '.$object->GetDBField('Type');
$found = $this->Conn->GetOne($sql);
if ($found) {
$event->status = erFAIL;
$object->SetError('FieldName', 'duplicate', 'la_error_CustomExists');
}
}
/**
* Occurse after deleting item, id of deleted item
* is stored as 'id' param of event
*
* @param kEvent $event
* @access public
*/
function OnAfterItemDelete(&$event)
{
$object =& $event->getObject();
- $custom_field_id=$event->getEventParam('id');
- $this->Application->SetVar('cf_id', '');
+ $main_prefix = $this->getPrefixByItemType($object->GetDBField('Type'));
- $sql='DELETE FROM '.TABLE_PREFIX.'CustomMetaData WHERE CustomFieldId = '.$custom_field_id;
- $this->Conn->Query($sql);
+ $ml_helper =& $this->Application->recallObject('kMultiLanguageHelper');
+ $ml_helper->deleteField($main_prefix.'-cdata', $event->getEventParam('id'));
+ }
+
+ /**
+ * Get config prefix based on item type
+ *
+ * @param unknown_type $item_type
+ * @return unknown
+ */
+ function getPrefixByItemType($item_type)
+ {
+ $sql = 'SELECT Prefix
+ FROM '.TABLE_PREFIX.'ItemTypes
+ WHERE ItemType = '.$item_type;
+ return $this->Conn->GetOne($sql);
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnSaveCustomField(&$event)
{
+ if ($event->MasterEvent->status != erSUCCESS) {
+ return false;
+ }
+
$object =& $event->getObject();
- $sql = 'SELECT Prefix
- FROM '.TABLE_PREFIX.'ItemTypes
- WHERE ItemType = '.$object->GetDBField('Type');
- $main_prefix = $this->Conn->GetOne($sql);
+ $main_prefix = $this->getPrefixByItemType($object->GetDBField('Type'));
+
+ $this->Application->HandleEvent( new kEvent($main_prefix.'-cdata:OnCreateCustomFields') );
$ml_helper =& $this->Application->recallObject('kMultiLanguageHelper');
$ml_helper->createFields($main_prefix.'-cdata');
}
function OnMassDelete(&$event)
{
parent::OnMassDelete($event);
$event->redirect_params = Array('opener' => 's');
}
/**
* Prepare temp tables for creating new item
* but does not create it. Actual create is
* done in OnPreSaveCreated
*
* @param kEvent $event
*/
function OnPreCreate(&$event)
{
parent::OnPreCreate($event);
$object =& $event->getObject();
$object->SetDBField('Type', $this->Application->GetVar('cf_type'));
}
}
?>
\ No newline at end of file
Property changes on: trunk/core/units/custom_fields/custom_fields_event_handler.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.8
\ No newline at end of property
+1.9
\ No newline at end of property
Index: trunk/core/units/general/helpers/multilanguage.php
===================================================================
--- trunk/core/units/general/helpers/multilanguage.php (revision 4016)
+++ trunk/core/units/general/helpers/multilanguage.php (revision 4017)
@@ -1,181 +1,196 @@
<?php
/**
* Performs action on multilingual fields
*
*/
class kMultiLanguageHelper extends kHelper {
var $languageCount = 0;
/**
* Structure of table, that is currently processed
*
* @var Array
*/
var $curStructure = Array();
/**
* Field, to get structure information from
*
* @var string
*/
var $curSourceField = false;
/**
* Fields from config, that are currently used
*
* @var Array
*/
var $curFields = Array();
function kMultiLanguageHelper()
{
parent::kHelper();
$this->languageCount = $this->getLanguageCount();
}
/**
* Returns language count in system (always is divisible by 5)
*
*/
function getLanguageCount()
{
$table_name = $this->Application->getUnitOption('lang', 'TableName');
$languages_count = $this->Conn->GetOne('SELECT COUNT(*) FROM '.$table_name);
return $languages_count + 5 - ( $languages_count % 5 ? ($languages_count % 5) : 5 );
}
function scanTable($mask)
{
$i = 0;
$fields_found = 0;
$fields = array_keys($this->curStructure);
foreach ($fields as $field_name) {
if (preg_match($mask, $field_name)) {
$fields_found++;
}
}
return $fields_found;
}
function readTableStructure($table_name)
{
static $structure_status = Array();
if (!getArrayValue($structure_status, $table_name)) {
$this->curStructure = $this->Conn->Query('DESCRIBE '.$table_name, 'Field');
$structure_status[$table_name] = true;
}
}
/**
* Creates missing multilanguage fields in table by specified prefix
*
* @param string $prefix
*/
function createFields($prefix)
{
$table_name = $this->Application->getUnitOption($prefix, 'TableName');
$this->curFields = $this->Application->getUnitOption($prefix, 'Fields');
if (!($table_name && $this->curFields) ) {
// invalid config found or prefix not found
return true;
}
$sqls = Array();
foreach($this->curFields as $field_name => $field_options)
{
if (getArrayValue($field_options, 'formatter') == 'kMultiLanguage') {
$this->readTableStructure($table_name);
$created_count = $this->getCreatedCount($field_name);
$create_count = $this->languageCount - $created_count;
if ($create_count > 0) {
// `l77_Name` VARCHAR( 255 ) NULL DEFAULT '0';
$field_mask = Array();
$field_mask['name'] = 'l%s_'.$field_name;
$field_mask['null'] = getArrayValue($field_options, 'not_null') ? 'NOT NULL' : 'NULL';
if ($this->curSourceField) {
$default_value = $this->getFieldParam('Default') != 'NULL' ? $this->Conn->qstr($this->getFieldParam('Default')) : $this->getFieldParam('Default');
$field_mask['type'] = $this->getFieldParam('Type');
}
else {
$default_value = is_null($field_options['default']) ? 'NULL' : $this->Conn->qstr($field_options['default']);
$field_mask['type'] = $field_options['db_type'];
}
$field_mask['default'] = 'DEFAULT '.$default_value;
$field_mask = $field_mask['name'].' '.$field_mask['type'].' '.$field_mask['null'].' '.$field_mask['default'];
$sqls[] = 'ALTER TABLE '.$table_name.( $this->generateAlterSQL($field_mask, $created_count + 1, $create_count) );
}
}
}
foreach ($sqls as $sql_query) {
$this->Conn->Query($sql_query);
}
}
+ function deleteField($prefix, $custom_id)
+ {
+ $table_name = $this->Application->getUnitOption($prefix, 'TableName');
+ $sql = 'DESCRIBE '.$table_name.' "l%_cust_'.$custom_id.'"';
+ $fields = $this->Conn->GetCol($sql);
+
+ $sql = 'ALTER TABLE '.$table_name.' ';
+ $sql_template = 'DROP COLUMN %s, ';
+ foreach ($fields as $field_name) {
+ $sql .= sprintf($sql_template, $field_name);
+ }
+ $sql = preg_replace('/(.*), $/', '\\1', $sql);
+ $this->Conn->Query($sql);
+ }
+
/**
* Returns parameter requested of current source field
*
* @param string $param_name
* @return string
*/
function getFieldParam($param_name)
{
return $this->curStructure[$this->curSourceField][$param_name];
}
function getCreatedCount($field_name)
{
$ret = $this->scanTable('/^l[\d]+_'.preg_quote($field_name, '/').'/');
if (!$ret) {
// no multilingual fields at all (but we have such field without language prefix)
$original_found = $this->scanTable('/'.preg_quote($field_name, '/').'/');
$this->curSourceField = $original_found ? $field_name : false;
}
else {
$this->curSourceField = 'l1_'.$field_name;
}
return $ret;
}
/**
* Returns ALTER statement part for adding required fields to table
*
* @param string $field_mask sql mask for creating field with correct definition (type & size)
* @param int $start_index add new fields starting from this index
* @param int $create_count create this much new multilingual field translations
* @return string
*/
function generateAlterSQL($field_mask, $start_index, $create_count)
{
$i_count = $start_index + $create_count;
$ret = ' ';
while ($start_index < $i_count) {
list($prev_field,$type) = explode(' ', sprintf($field_mask, $start_index - 1) );
if (substr($prev_field, 0, 3) == 'l0_') {
$prev_field = substr($prev_field, 3, strlen($prev_field));
if (!$this->curSourceField) {
// get field name before this one
$fields = array_keys($this->curFields);
$prev_field = $fields[array_search($prev_field, $fields) - 1];
if (getArrayValue($this->curFields[$prev_field], 'formatter') == 'kMultiLanguage') {
$prev_field = 'l'.$this->languageCount.'_'.$prev_field;
}
}
}
$ret .= 'ADD COLUMN '.sprintf($field_mask, $start_index).' AFTER `'.$prev_field.'`, ';
$start_index++;
}
return preg_replace('/, $/',';',$ret);
}
}
?>
\ No newline at end of file
Property changes on: trunk/core/units/general/helpers/multilanguage.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.1
\ No newline at end of property
+1.2
\ No newline at end of property

Event Timeline