Array('subitem' => 'add|edit'), ); $this->permMapping = array_merge($this->permMapping, $permissions); } /** * Add new relation * * @param kEvent $event */ function OnAddRelation(&$event) { $this->Application->setUnitOption($event->Prefix,'AutoLoad',false); $object =& $event->getObject(); $table_info = $object->getLinkedInfo(); $main_item_type = $this->Application->getUnitOption($table_info['ParentPrefix'],'ItemType'); $target['id'] = $this->Application->GetVar('TargetId'); $target['type'] = $this->Application->GetVar('TargetType'); $object->SetDBField($table_info['ForeignKey'], $table_info['ParentId']); $object->SetDBField('SourceType', $main_item_type); $object->SetDBField('TargetId', $target['id']); $object->SetDBField('TargetType', $target['type']); // 1. get item info used in relation $configs = $this->extractModulesInfo(); foreach($configs as $prefix => $config_data) { if($config_data['ItemType'] == $target['type']) break; } // this forces prepareOptions of kMultiLanguge formatter to substiture title field (if multilingual) of target item // BUT this is not the solution, find out another way to get correct title field here $target_object =& $this->Application->recallObject($prefix, null, Array('skip_autoload' => true)); $title_field = $this->Application->getUnitOption($prefix, 'TitleField'); $item['name'] = $this->Conn->GetOne('SELECT '.$title_field.' FROM '.$config_data['TableName'].' WHERE ResourceId = '.$target['id']); $item['type'] = $this->Application->Phrase($config_data['TitlePhrase']); $object->SetDBField('ItemName', $item['name']); $object->SetDBField('ItemType', $item['type']); $object->setID(0); $event->redirect = false; // 2. substitute opener $opener_stack = $this->Application->RecallVar('opener_stack'); $opener_stack = $opener_stack ? unserialize($opener_stack) : Array(); array_pop($opener_stack); $return_template = $this->Application->RecallVar('return_template'); $new_level = 'index4.php|'.ltrim($this->Application->BuildEnv($return_template, Array('m_opener'=>'u'),'all'),ENV_VAR_NAME.'='); array_push($opener_stack,$new_level); $this->Application->StoreVar('opener_stack',serialize($opener_stack)); $this->Application->SetVar($event->Prefix_Special.'_SaveEvent','OnCreate'); } /** * Creates needed sql query to load list, * if no query is defined in config for * special requested, then use default * query * * @param kEvent $event * @access protected */ function ListPrepareQuery(&$event) { return $this->BaseQuery($event,'ListSQLs'); } /** * Creates needed sql query to load item, * if no query is defined in config for * special requested, then use default * query * * @param kEvent $event * @access protected */ function ItemPrepareQuery(&$event) { return $this->BaseQuery($event,'ItemSQLs'); } /** * Get item name & type based on relation type & modules installed * * @param kEvent $event * @param string $sql_field */ function BaseQuery(&$event, $sql_field) { $sqls = $this->Application->getUnitOption($event->Prefix,$sql_field); $sql = isset($sqls[$event->Special]) ? $sqls[$event->Special] : $sqls['']; $configs = $this->extractModulesInfo(); // 2. build sql based on information queried $sql_templates['ItemName'] = 'IFNULL(%s.%s,\' \')'; $sql_templates['TableJoin'] = 'LEFT JOIN %1$s ON %1$s.ResourceId = rel.TargetId'; $sql_templates['TargetName'] = 'IF(rel.TargetType = %s, \'%s\', %s)'; $sql_parts = Array(); $sql_parts['TargetName'] = "''"; foreach ($configs as $prefix => $config_data) { // convert TitleField field of kMultiLanguage formatter used for it: begin $title_field = $config_data['TitleField']; $formatter_class = isset($config_data['Fields'][$title_field]['formatter']) ? $config_data['Fields'][$title_field]['formatter'] : ''; if ($formatter_class == 'kMultiLanguage') { $ml_formatter =& $this->Application->recallObject('kMultiLanguage'); $title_field = $ml_formatter->LangFieldName($title_field); } // convert TitleField field of kMultiLanguage formatter used for it: end $sql_parts['ItemName'][] = sprintf($sql_templates['ItemName'], $config_data['TableName'], $title_field); $sql_parts['TableJoin'][] = sprintf($sql_templates['TableJoin'], $config_data['TableName']); $sql_parts['TargetName'] = sprintf( $sql_templates['TargetName'], $config_data['ItemType'], '!'.$config_data['TitlePhrase'].'!', $sql_parts['TargetName']); $sql_parts['TargetName'] = str_replace('rel','%1$s',$sql_parts['TargetName']); } $object =& $event->getObject(); $vars = Array('#ITEM_NAMES#', '#ITEM_TYPES#'); $replacements = Array( implode(', ',$sql_parts['ItemName']), $sql_parts['TargetName'] ); $calculated_fields =& $object->getProperty('CalculatedFields'); foreach ($calculated_fields as $field_name => $field_expression) { $calculated_fields[$field_name] = str_replace($vars, $replacements, $field_expression); } $object->setProperty('CalculatedFields', $calculated_fields); $sql = str_replace('#ITEM_JOIN#', implode(' ',$sql_parts['TableJoin']), $sql); $sql = str_replace('rel.','%1$s.',$sql); return $sql; } /** * Get configs from modules installed * * @return Array * @access private */ function extractModulesInfo() { // get installed modules & their config info // maybe we should leave only prefixes, that have "view" permission $configs = Array(); foreach ($this->Application->ModuleInfo as $module_name => $module_data) { $prefix = $module_data['Var']; $configs[$prefix] = $this->Application->getUnitOptions($prefix); if($configs[$prefix] === false) unset($configs[$prefix]); } return $configs; } /** * Deletes relations to hooked item from other items * * @param kEvent $event */ function OnDeleteForeignRelations(&$event) { $main_object =& $event->MasterEvent->getObject(); $resource_id = $main_object->GetDBField('ResourceId'); $table = $this->Application->getUnitOption($event->Prefix,'TableName'); $sql = 'DELETE FROM '.$table.' WHERE TargetId = '.$resource_id; $this->Conn->Query($sql); } } ?>