Page MenuHomeIn-Portal Phabricator

in-portal
No OneTemporary

File Metadata

Created
Mon, Mar 10, 5:58 AM

in-portal

Index: branches/RC/core/kernel/db/db_event_handler.php
===================================================================
--- branches/RC/core/kernel/db/db_event_handler.php (revision 10620)
+++ branches/RC/core/kernel/db/db_event_handler.php (revision 10621)
@@ -1,2322 +1,2342 @@
<?php
define('EH_CUSTOM_PROCESSING_BEFORE',1);
define('EH_CUSTOM_PROCESSING_AFTER',2);
/**
* Note:
* 1. When adressing variables from submit containing
* Prefix_Special as part of their name use
* $event->getPrefixSpecial(true) instead of
* $event->Prefix_Special as usual. This is due PHP
* is converting "." symbols in variable names during
* submit info "_". $event->getPrefixSpecial optional
* 1st parameter returns correct corrent Prefix_Special
* for variables beeing submitted such way (e.g. variable
* name that will be converted by PHP: "users.read_only_id"
* will be submitted as "users_read_only_id".
*
* 2. When using $this->Application-LinkVar on variables submitted
* from form which contain $Prefix_Special then note 1st item. Example:
* LinkVar($event->getPrefixSpecial(true).'_varname',$event->Prefix_Special.'_varname')
*
*/
/**
* EventHandler that is used to process
* any database related events
*
*/
class kDBEventHandler extends kEventHandler {
/**
* Description
*
* @var kDBConnection
* @access public
*/
var $Conn;
/**
* Adds ability to address db connection
*
* @return kDBEventHandler
* @access public
*/
function kDBEventHandler()
{
parent::kBase();
$this->Conn =& $this->Application->GetADODBConnection();
}
/**
* Checks permissions of user
*
* @param kEvent $event
*/
function CheckPermission(&$event)
{
if (!$this->Application->IsAdmin()) {
$allow_events = Array('OnSearch', 'OnSearchReset', 'OnNew');
if (in_array($event->Name, $allow_events)) {
// allow search on front
return true;
}
}
$section = $event->getSection();
if (!preg_match('/^CATEGORY:(.*)/', $section)) {
// only if not category item events
if ((substr($event->Name, 0, 9) == 'OnPreSave') || ($event->Name == 'OnSave')) {
if ($this->isNewItemCreate($event)) {
return $this->Application->CheckPermission($section.'.add', 1);
}
else {
return $this->Application->CheckPermission($section.'.add', 1) || $this->Application->CheckPermission($section.'.edit', 1);
}
}
}
if ($event->Name == 'OnPreCreate') {
// save category_id before item create (for item category selector not to destroy permission checking category)
$this->Application->LinkVar('m_cat_id');
}
return parent::CheckPermission($event);
}
/**
* Allows to override standart permission mapping
*
*/
function mapPermissions()
{
parent::mapPermissions();
$permissions = Array(
'OnLoad' => Array('self' => 'view', 'subitem' => 'view'),
'OnItemBuild' => Array('self' => 'view', 'subitem' => 'view'),
'OnBuild' => Array('self' => true),
'OnNew' => Array('self' => 'add', 'subitem' => 'add|edit'),
'OnCreate' => Array('self' => 'add', 'subitem' => 'add|edit'),
'OnUpdate' => Array('self' => 'edit', 'subitem' => 'add|edit'),
'OnSetPrimary' => Array('self' => 'add|edit', 'subitem' => 'add|edit'),
'OnDelete' => Array('self' => 'delete', 'subitem' => 'add|edit'),
'OnMassDelete' => Array('self' => 'delete', 'subitem' => 'add|edit'),
'OnMassClone' => Array('self' => 'add', 'subitem' => 'add|edit'),
'OnCut' => array('self'=>'edit', 'subitem' => 'edit'),
'OnCopy' => array('self'=>'edit', 'subitem' => 'edit'),
'OnPaste' => array('self'=>'edit', 'subitem' => 'edit'),
'OnSelectItems' => Array('self' => 'add|edit', 'subitem' => 'add|edit'),
'OnProcessSelected' => Array('self' => 'add|edit', 'subitem' => 'add|edit'),
'OnSelectUser' => Array('self' => 'add|edit', 'subitem' => 'add|edit'),
'OnMassApprove' => Array('self' => 'advanced:approve|edit', 'subitem' => 'advanced:approve|add|edit'),
'OnMassDecline' => Array('self' => 'advanced:decline|edit', 'subitem' => 'advanced:decline|add|edit'),
'OnMassMoveUp' => Array('self' => 'advanced:move_up|edit', 'subitem' => 'advanced:move_up|add|edit'),
'OnMassMoveDown' => Array('self' => 'advanced:move_down|edit', 'subitem' => 'advanced:move_down|add|edit'),
'OnPreCreate' => Array('self' => 'add|add.pending', 'subitem' => 'edit|edit.pending'),
'OnEdit' => Array('self' => 'edit|edit.pending', 'subitem' => 'edit|edit.pending'),
'OnExport' => Array('self' => 'view|advanced:export'),
'OnExportBegin' => Array('self' => 'view|advanced:export'),
'OnExportProgress' => Array('self' => 'view|advanced:export'),
'OnSetAutoRefreshInterval' => Array ('self' => true, 'subitem' => true),
'OnAutoRefreshToggle' => Array ('self' => true, 'subitem' => true),
// theese event do not harm, but just in case check them too :)
'OnCancelEdit' => Array('self' => true, 'subitem' => true),
'OnCancel' => Array('self' => true, 'subitem' => true),
'OnReset' => Array('self' => true, 'subitem' => true),
'OnSetSorting' => Array('self' => true, 'subitem' => true),
'OnSetSortingDirect' => Array('self' => true, 'subitem' => true),
'OnSetFilter' => Array('self' => true, 'subitem' => true),
'OnApplyFilters' => Array('self' => true, 'subitem' => true),
'OnRemoveFilters' => Array('self' => true, 'subitem' => true),
'OnSetFilterPattern' => Array('self' => true, 'subitem' => true),
'OnSetPerPage' => Array('self' => true, 'subitem' => true),
'OnSearch' => Array('self' => true, 'subitem' => true),
'OnSearchReset' => Array('self' => true, 'subitem' => true),
'OnGoBack' => Array('self' => true, 'subitem' => true),
// it checks permission itself since flash uploader does not send cookies
'OnUploadFile' => Array('self'=>true, 'subitem'=>true),
'OnViewFile' => Array('self'=>true, 'subitem'=>true),
'OnSaveWidths' => Array('self'=>true, 'subitem'=>true),
'OnValidateMInputFields' => Array('self'=>'view'),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
function mapEvents()
{
$events_map = Array(
'OnRemoveFilters' => 'FilterAction',
'OnApplyFilters' => 'FilterAction',
'OnMassApprove'=>'iterateItems',
'OnMassDecline'=>'iterateItems',
'OnMassMoveUp'=>'iterateItems',
'OnMassMoveDown'=>'iterateItems',
);
$this->eventMethods = array_merge($this->eventMethods, $events_map);
}
/**
* Returns ID of current item to be edited
* by checking ID passed in get/post as prefix_id
* or by looking at first from selected ids, stored.
* Returned id is also stored in Session in case
* it was explicitly passed as get/post
*
* @param kEvent $event
* @return int
*/
function getPassedID(&$event)
{
if ($event->getEventParam('raise_warnings') === false) {
$event->setEventParam('raise_warnings', 1);
}
if (preg_match('/^auto-(.*)/', $event->Special, $regs) && $this->Application->prefixRegistred($regs[1])) {
// <inp2:lang.auto-phrase_Field name="DateFormat"/> - returns field DateFormat value from language (LanguageId is extracted from current phrase object)
$main_object =& $this->Application->recallObject($regs[1]);
/* @var $main_object kDBItem */
$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
return $main_object->GetDBField($id_field);
}
// 1. get id from post (used in admin)
$ret = $this->Application->GetVar($event->getPrefixSpecial(true).'_id');
if ($ret) {
return $ret;
}
// 2. get id from env (used in front)
$ret = $this->Application->GetVar($event->getPrefixSpecial().'_id');
if ($ret) {
return $ret;
}
// recall selected ids array and use the first one
$ids = $this->Application->GetVar($event->getPrefixSpecial().'_selected_ids');
if ($ids != '') {
$ids = explode(',',$ids);
if ($ids) {
$ret = array_shift($ids);
}
}
else { // if selected ids are not yet stored
$this->StoreSelectedIDs($event);
return $this->Application->GetVar($event->getPrefixSpecial(true).'_id'); // StoreSelectedIDs sets this variable
}
return $ret;
}
/**
* Prepares and stores selected_ids string
* in Session and Application Variables
* by getting all checked ids from grid plus
* id passed in get/post as prefix_id
*
* @param kEvent $event
* @param Array $ids
*
* @return Array ids stored
*/
function StoreSelectedIDs(&$event, $ids = null)
{
$wid = $this->Application->GetTopmostWid($event->Prefix);
$session_name = rtrim($event->getPrefixSpecial().'_selected_ids_'.$wid, '_');
if (isset($ids)) {
// save ids directly if they given
$this->Application->StoreVar($session_name, implode(',', $ids));
return $ids;
}
$ret = Array();
// May be we don't need this part: ?
$passed = $this->Application->GetVar($event->getPrefixSpecial(true).'_id');
if($passed !== false && $passed != '')
{
array_push($ret, $passed);
}
$ids = Array();
// get selected ids from post & save them to session
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if($items_info)
{
$id_field = $this->Application->getUnitOption($event->Prefix,'IDField');
foreach($items_info as $id => $field_values)
{
if( getArrayValue($field_values,$id_field) ) array_push($ids,$id);
}
//$ids=array_keys($items_info);
}
$ret = array_unique(array_merge($ret, $ids));
$this->Application->SetVar($event->getPrefixSpecial().'_selected_ids', implode(',',$ret));
$this->Application->LinkVar($event->getPrefixSpecial().'_selected_ids', $session_name);
// This is critical - otherwise getPassedID will return last ID stored in session! (not exactly true)
// this smells... needs to be refactored
$first_id = getArrayValue($ret,0);
if (($first_id === false) && ($event->getEventParam('raise_warnings') == 1)) {
if ($this->Application->isDebugMode()) {
$this->Application->Debugger->appendTrace();
}
trigger_error('Requested ID for prefix <b>'.$event->getPrefixSpecial().'</b> <span class="debug_error">not passed</span>',E_USER_NOTICE);
}
$this->Application->SetVar($event->getPrefixSpecial(true).'_id', $first_id);
return $ret;
}
/**
* Returns stored selected ids as an array
*
* @param kEvent $event
* @param bool $from_session return ids from session (written, when editing was started)
* @return array
*/
function getSelectedIDs(&$event, $from_session = false)
{
if ($from_session) {
$wid = $this->Application->GetTopmostWid($event->Prefix);
$var_name = rtrim($event->getPrefixSpecial().'_selected_ids_'.$wid, '_');
$ret = $this->Application->RecallVar($var_name);
}
else {
$ret = $this->Application->GetVar($event->getPrefixSpecial().'_selected_ids');
}
return explode(',', $ret);
}
/**
* Returs associative array of submitted fields for current item
* Could be used while creating/editing single item -
* meaning on any edit form, except grid edit
*
* @param kEvent $event
*/
function getSubmittedFields(&$event)
{
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
$field_values = $items_info ? array_shift($items_info) : Array();
return $field_values;
}
/**
* Removes any information about current/selected ids
* from Application variables and Session
*
* @param kEvent $event
*/
function clearSelectedIDs(&$event)
{
$prefix_special = $event->getPrefixSpecial();
$ids = implode(',', $this->getSelectedIDs($event, true));
$event->setEventParam('ids', $ids);
$wid = $this->Application->GetTopmostWid($event->Prefix);
$session_name = rtrim($prefix_special.'_selected_ids_'.$wid, '_');
$this->Application->RemoveVar($session_name);
$this->Application->SetVar($prefix_special.'_selected_ids', '');
$this->Application->SetVar($prefix_special.'_id', ''); // $event->getPrefixSpecial(true).'_id' too may be
}
/*function SetSaveEvent(&$event)
{
$this->Application->SetVar($event->Prefix_Special.'_SaveEvent','OnUpdate');
$this->Application->LinkVar($event->Prefix_Special.'_SaveEvent');
}*/
/**
* Common builder part for Item & List
*
* @param kDBBase $object
* @param kEvent $event
* @access private
*/
function dbBuild(&$object, &$event)
{
$object->Configure( $event->getEventParam('populate_ml_fields') || $this->Application->getUnitOption($event->Prefix, 'PopulateMlFields') );
$this->PrepareObject($object, $event);
// force live table if specified or is original item
$live_table = $event->getEventParam('live_table') || $event->Special == 'original';
if( $this->UseTempTables($event) && !$live_table )
{
$object->SwitchToTemp();
}
// This strange constuction creates hidden field for storing event name in form submit
// It pass SaveEvent to next screen, otherwise after unsuccsefull create it will try to update rather than create
$current_event = $this->Application->GetVar($event->Prefix_Special.'_event');
// $this->Application->setEvent($event->Prefix_Special, $current_event);
$this->Application->setEvent($event->Prefix_Special, '');
$save_event = $this->UseTempTables($event) && $this->Application->GetTopmostPrefix($event->Prefix) == $event->Prefix ? 'OnSave' : 'OnUpdate';
$this->Application->SetVar($event->Prefix_Special.'_SaveEvent',$save_event);
}
/**
* Checks, that currently loaded item is allowed for viewing (non permission-based)
*
* @param kEvent $event
* @return bool
*/
function checkItemStatus(&$event)
{
$status_fields = $this->Application->getUnitOption($event->Prefix,'StatusField');
if (!$status_fields) {
return true;
}
$status_field = array_shift($status_fields);
if ($status_field == 'Status' || $status_field == 'Enabled') {
$object =& $event->getObject();
if (!$object->isLoaded()) {
return true;
}
return $object->GetDBField($status_field) == STATUS_ACTIVE;
}
return true;
}
/**
* Builds item (loads if needed)
*
* @param kEvent $event
* @access protected
*/
function OnItemBuild(&$event)
{
$object =& $event->getObject();
$this->dbBuild($object,$event);
$sql = $this->ItemPrepareQuery($event);
$sql = $this->Application->ReplaceLanguageTags($sql);
$object->setSelectSQL($sql);
// 2. loads if allowed
$auto_load = $this->Application->getUnitOption($event->Prefix,'AutoLoad');
$skip_autload = $event->getEventParam('skip_autoload');
if ($auto_load && !$skip_autload) {
$perm_status = true;
$user_id = $this->Application->RecallVar('user_id');
$event->setEventParam('top_prefix', $this->Application->GetTopmostPrefix($event->Prefix, true));
$status_checked = false;
if ($user_id == -1 || $this->CheckPermission($event)) {
// don't autoload item, when user doesn't have view permission
$this->LoadItem($event);
$status_checked = true;
if ($user_id != -1 && !$this->Application->IsAdmin() && !$this->checkItemStatus($event)) {
$perm_status = false;
}
}
else {
$perm_status = false;
}
if (!$perm_status) {
// when no permission to view item -> redirect to no pemrission template
if ($this->Application->isDebugMode()) {
$this->Application->Debugger->appendTrace();
}
trigger_error('ItemLoad Permission Failed for prefix ['.$event->getPrefixSpecial().'] in <strong>'.($status_checked ? 'checkItemStatus' : 'CheckPermission').'</strong>', E_USER_WARNING);
$next_template = $this->Application->IsAdmin() ? 'no_permission' : $this->Application->ConfigValue('NoPermissionTemplate');
$this->Application->Redirect($next_template, Array('next_template' => $this->Application->GetVar('t')));
}
}
$actions =& $this->Application->recallObject('kActions');
$actions->Set($event->Prefix_Special.'_GoTab', '');
$actions->Set($event->Prefix_Special.'_GoId', '');
}
/**
* Build subtables array from configs
*
* @param kEvent $event
*/
function OnTempHandlerBuild(&$event)
{
$object =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
/* @var $object kTempTablesHandler */
$object->BuildTables( $event->Prefix, $this->getSelectedIDs($event) );
}
/**
* Enter description here...
*
* @param kEvent $event
* @return unknown
*/
function UseTempTables(&$event)
{
$object = &$event->getObject();
$top_prefix = $this->Application->GetTopmostPrefix($event->Prefix);
$var_names = Array (
$top_prefix,
rtrim($top_prefix.'_'.$event->Special, '_'),
rtrim($top_prefix.'.'.$event->Special, '.'),
);
$var_names = array_unique($var_names);
$temp_mode = false;
foreach ($var_names as $var_name) {
$value = $this->Application->GetVar($var_name.'_mode');
if (substr($value, 0, 1) == 't') {
$temp_mode = true;
break;
}
}
return $temp_mode;
}
/**
* Returns table prefix from event (temp or live)
*
* @param kEvent $event
* @return string
* @todo Needed? Should be refactored (by Alex)
*/
function TablePrefix(&$event)
{
return $this->UseTempTables($event) ? $this->Application->GetTempTablePrefix('prefix:'.$event->Prefix).TABLE_PREFIX : TABLE_PREFIX;
}
/**
* Load item if id is available
*
* @param kEvent $event
*/
function LoadItem(&$event)
{
$object =& $event->getObject();
$id = $this->getPassedID($event);
if ($object->isLoaded() && ($object->GetID() == $id)) {
// object is already loaded by same id
return ;
}
if ($object->Load($id)) {
$actions =& $this->Application->recallObject('kActions');
$actions->Set($event->Prefix_Special.'_id', $object->GetID() );
}
else {
$object->setID($id);
}
}
/**
* Builds list
*
* @param kEvent $event
* @access protected
*/
function OnListBuild(&$event)
{
$object =& $event->getObject();
/* @var $object kDBList */
$this->dbBuild($object,$event);
$sql = $this->ListPrepareQuery($event);
$sql = $this->Application->ReplaceLanguageTags($sql);
$object->setSelectSQL($sql);
$object->Counted = false; // when requery="1" should re-count records too!
$object->ClearOrderFields(); // prevents duplicate order fields, when using requery="1"
$object->linkToParent( $this->getMainSpecial($event) );
$this->AddFilters($event);
$this->SetCustomQuery($event); // new!, use this for dynamic queries based on specials for ex.
$this->SetPagination($event);
$this->SetSorting($event);
// $object->CalculateTotals(); // Now called in getTotals to avoid extra query
$actions =& $this->Application->recallObject('kActions');
$actions->Set('remove_specials['.$event->Prefix_Special.']', '0');
$actions->Set($event->Prefix_Special.'_GoTab', '');
}
/**
* Get's special of main item for linking with subitem
*
* @param kEvent $event
* @return string
*/
function getMainSpecial(&$event)
{
$special = $event->getEventParam('main_special');
if($special === false || $special == '$main_special')
{
$special = $event->Special;
}
return $special;
}
/**
* Apply any custom changes to list's sql query
*
* @param kEvent $event
* @access protected
* @see OnListBuild
*/
function SetCustomQuery(&$event)
{
}
/**
* Set's new perpage for grid
*
* @param kEvent $event
*/
function OnSetPerPage(&$event)
{
$per_page = $this->Application->GetVar($event->getPrefixSpecial(true).'_PerPage');
$this->Application->StoreVar($event->getPrefixSpecial().'_PerPage', $per_page);
$view_name = $this->Application->RecallVar($event->getPrefixSpecial().'_current_view');
$this->Application->StorePersistentVar($event->getPrefixSpecial().'_PerPage.'.$view_name, $per_page);
}
/**
* Set's correct page for list
* based on data provided with event
*
* @param kEvent $event
* @access private
* @see OnListBuild
*/
function SetPagination(&$event)
{
// get PerPage (forced -> session -> config -> 10)
$per_page = $this->getPerPage($event);
$object =& $event->getObject();
$object->SetPerPage($per_page);
$this->Application->StoreVarDefault($event->getPrefixSpecial().'_Page', 1);
$page = $this->Application->GetVar($event->getPrefixSpecial().'_Page');
if (!$page) {
$page = $this->Application->GetVar($event->getPrefixSpecial(true).'_Page');
}
if (!$page) {
$page = $this->Application->RecallVar($event->getPrefixSpecial().'_Page');
}
else {
$this->Application->StoreVar($event->getPrefixSpecial().'_Page', $page);
}
if( !$event->getEventParam('skip_counting') )
{
$pages = $object->GetTotalPages();
if($page > $pages)
{
$this->Application->StoreVar($event->getPrefixSpecial().'_Page', 1);
$page = 1;
}
}
/*$per_page = $event->getEventParam('per_page');
if ($per_page == 'list_next') {
$cur_page = $page;
$cur_per_page = $per_page;
$object->SetPerPage(1);
$object =& $this->Application->recallObject($event->Prefix);
$cur_item_index = $object->CurrentIndex;
$page = ($cur_page-1) * $cur_per_page + $cur_item_index + 1;
$object->SetPerPage(1);
}*/
$object->SetPage($page);
}
/**
* Returns current per-page setting for list
*
* @param kEvent $event
* @return int
*/
function getPerPage(&$event)
{
// 1. per-page is passed as tag parameter to PrintList, InitList, etc.
$per_page = $event->getEventParam('per_page');
/*if ($per_page == 'list_next') {
$per_page = '';
}*/
// 2. per-page variable name is store into config variable
$config_mapping = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping');
if ($config_mapping) {
switch ( $per_page ){
case 'short_list' :
$per_page = $this->Application->ConfigValue($config_mapping['ShortListPerPage']);
break;
case 'default' :
$per_page = $this->Application->ConfigValue($config_mapping['PerPage']);
break;
}
}
if (!$per_page) {
// per-page is stored to persistent session
$view_name = $this->Application->RecallVar($event->getPrefixSpecial().'_current_view');
$storage_prefix = $event->getEventParam('same_special') ? $event->Prefix : $event->getPrefixSpecial();
$per_page = $this->Application->RecallPersistentVar($storage_prefix.'_PerPage.'.$view_name, '_USE_DEFAULT_USER_DATA_');
if (!$per_page) {
// per-page is stored to current session
$per_page = $this->Application->RecallVar($storage_prefix.'_PerPage');
}
if (!$per_page) {
if ($config_mapping) {
if (!isset($config_mapping['PerPage'])) {
trigger_error('Incorrect mapping of <span class="debug_error">PerPage</span> key in config for prefix <b>'.$event->Prefix.'</b>', E_USER_WARNING);
}
$per_page = $this->Application->ConfigValue($config_mapping['PerPage']);
}
if (!$per_page) {
// none of checked above per-page locations are useful, then try default value
$per_page = 10;
}
}
}
return $per_page;
}
/**
* Set's correct sorting for list
* based on data provided with event
*
* @param kEvent $event
* @access private
* @see OnListBuild
*/
function SetSorting(&$event)
{
$event->setPseudoClass('_List');
$object =& $event->getObject();
$storage_prefix = $event->getEventParam('same_special') ? $event->Prefix : $event->Prefix_Special;
$cur_sort1 = $this->Application->RecallVar($storage_prefix.'_Sort1');
$cur_sort1_dir = $this->Application->RecallVar($storage_prefix.'_Sort1_Dir');
$cur_sort2 = $this->Application->RecallVar($storage_prefix.'_Sort2');
$cur_sort2_dir = $this->Application->RecallVar($storage_prefix.'_Sort2_Dir');
$sorting_configs = $this->Application->getUnitOption($event->Prefix, 'ConfigMapping');
$list_sortings = $this->Application->getUnitOption($event->Prefix, 'ListSortings');
$sorting_prefix = getArrayValue($list_sortings, $event->Special) ? $event->Special : '';
$tag_sort_by = $event->getEventParam('sort_by');
if ($tag_sort_by) {
if ($tag_sort_by == 'random') {
$by = 'RAND()';
$dir = '';
}
else {
list($by, $dir) = explode(',', $tag_sort_by);
}
$object->AddOrderField($by, $dir);
}
if ($sorting_configs && isset ($sorting_configs['DefaultSorting1Field'])){
$list_sortings[$sorting_prefix]['Sorting'] = Array(
$this->Application->ConfigValue($sorting_configs['DefaultSorting1Field']) => $this->Application->ConfigValue($sorting_configs['DefaultSorting1Dir']),
$this->Application->ConfigValue($sorting_configs['DefaultSorting2Field']) => $this->Application->ConfigValue($sorting_configs['DefaultSorting2Dir']),
);
}
// Use default if not specified
if ( !$cur_sort1 || !$cur_sort1_dir)
{
if ( $sorting = getArrayValue($list_sortings, $sorting_prefix, 'Sorting') ) {
reset($sorting);
$cur_sort1 = key($sorting);
$cur_sort1_dir = current($sorting);
if (next($sorting)) {
$cur_sort2 = key($sorting);
$cur_sort2_dir = current($sorting);
}
}
}
if ( $forced_sorting = getArrayValue($list_sortings, $sorting_prefix, 'ForcedSorting') ) {
foreach ($forced_sorting as $field => $dir) {
$object->AddOrderField($field, $dir);
}
}
if($cur_sort1 != '' && $cur_sort1_dir != '')
{
$object->AddOrderField($cur_sort1, $cur_sort1_dir);
}
if($cur_sort2 != '' && $cur_sort2_dir != '')
{
$object->AddOrderField($cur_sort2, $cur_sort2_dir);
}
}
/**
* Add filters found in session
*
* @param kEvent $event
*/
function AddFilters(&$event)
{
$object =& $event->getObject();
$edit_mark = rtrim($this->Application->GetSID().'_'.$this->Application->GetTopmostWid($event->Prefix), '_');
// add search filter
$filter_data = $this->Application->RecallVar($event->getPrefixSpecial().'_search_filter');
if ($filter_data) {
$filter_data = unserialize($filter_data);
foreach ($filter_data as $filter_field => $filter_params) {
$filter_type = ($filter_params['type'] == 'having') ? HAVING_FILTER : WHERE_FILTER;
$filter_value = str_replace(EDIT_MARK, $edit_mark, $filter_params['value']);
$object->addFilter($filter_field, $filter_value, $filter_type, FLT_SEARCH);
}
}
// add custom filter
$view_name = $this->Application->RecallVar($event->getPrefixSpecial().'_current_view');
$custom_filters = $this->Application->RecallPersistentVar($event->getPrefixSpecial().'_custom_filter.'.$view_name);
if ($custom_filters) {
$grid_name = $event->getEventParam('grid');
$custom_filters = unserialize($custom_filters);
if (isset($custom_filters[$grid_name])) {
foreach ($custom_filters[$grid_name] as $field_name => $field_options) {
list ($filter_type, $field_options) = each($field_options);
if (isset($field_options['value']) && $field_options['value']) {
$filter_type = ($field_options['sql_filter_type'] == 'having') ? HAVING_FILTER : WHERE_FILTER;
$filter_value = str_replace(EDIT_MARK, $edit_mark, $field_options['value']);
$object->addFilter($field_name, $filter_value, $filter_type, FLT_CUSTOM);
}
}
}
}
$view_filter = $this->Application->RecallVar($event->getPrefixSpecial().'_view_filter');
if($view_filter)
{
$view_filter = unserialize($view_filter);
$temp_filter =& $this->Application->makeClass('kMultipleFilter');
$filter_menu = $this->Application->getUnitOption($event->Prefix,'FilterMenu');
$group_key = 0; $group_count = count($filter_menu['Groups']);
while($group_key < $group_count)
{
$group_info = $filter_menu['Groups'][$group_key];
$temp_filter->setType( constant('FLT_TYPE_'.$group_info['mode']) );
$temp_filter->clearFilters();
foreach ($group_info['filters'] as $flt_id)
{
$sql_key = getArrayValue($view_filter,$flt_id) ? 'on_sql' : 'off_sql';
if ($filter_menu['Filters'][$flt_id][$sql_key] != '')
{
$temp_filter->addFilter('view_filter_'.$flt_id, $filter_menu['Filters'][$flt_id][$sql_key]);
}
}
$object->addFilter('view_group_'.$group_key, $temp_filter, $group_info['type'] , FLT_VIEW);
$group_key++;
}
}
}
/**
* Set's new sorting for list
*
* @param kEvent $event
* @access protected
*/
function OnSetSorting(&$event)
{
$cur_sort1 = $this->Application->RecallVar($event->Prefix_Special.'_Sort1');
$cur_sort1_dir = $this->Application->RecallVar($event->Prefix_Special.'_Sort1_Dir');
$use_double_sorting = $this->Application->ConfigValue('UseDoubleSorting') !== false ? $this->Application->ConfigValue('UseDoubleSorting') : true;
if ($use_double_sorting) {
$cur_sort2 = $this->Application->RecallVar($event->Prefix_Special.'_Sort2');
$cur_sort2_dir = $this->Application->RecallVar($event->Prefix_Special.'_Sort2_Dir');
}
$passed_sort1 = $this->Application->GetVar($event->getPrefixSpecial(true).'_Sort1');
if ($cur_sort1 == $passed_sort1) {
$cur_sort1_dir = $cur_sort1_dir == 'asc' ? 'desc' : 'asc';
}
else {
if ($use_double_sorting) {
$cur_sort2 = $cur_sort1;
$cur_sort2_dir = $cur_sort1_dir;
}
$cur_sort1 = $passed_sort1;
$cur_sort1_dir = 'asc';
}
$this->Application->StoreVar($event->Prefix_Special.'_Sort1', $cur_sort1);
$this->Application->StoreVar($event->Prefix_Special.'_Sort1_Dir', $cur_sort1_dir);
if ($use_double_sorting) {
$this->Application->StoreVar($event->Prefix_Special.'_Sort2', $cur_sort2);
$this->Application->StoreVar($event->Prefix_Special.'_Sort2_Dir', $cur_sort2_dir);
}
}
/**
* Set sorting directly to session (used for category item sorting (front-end), grid sorting (admin, view menu)
*
* @param kEvent $event
*/
function OnSetSortingDirect(&$event)
{
$combined = $this->Application->GetVar($event->Prefix.'_CombinedSorting');
if ($combined) {
list($field, $dir) = explode('|', $combined);
$this->Application->StoreVar($event->Prefix.'_Sort1', $field);
$this->Application->StoreVar($event->Prefix.'_Sort1_Dir', $dir);
return ;
}
$field_pos = $this->Application->GetVar($event->Prefix.'_SortPos');
$this->Application->LinkVar($event->Prefix.'_Sort'.$field_pos, $event->Prefix.'_Sort'.$field_pos);
$this->Application->LinkVar($event->Prefix.'_Sort'.$field_pos.'_Dir', $event->Prefix.'_Sort'.$field_pos.'_Dir');
}
/**
* Reset grid sorting to default (from config)
*
* @param kEvent $event
*/
function OnResetSorting(&$event)
{
$this->Application->RemoveVar($event->Prefix_Special.'_Sort1');
$this->Application->RemoveVar($event->Prefix_Special.'_Sort1_Dir');
$this->Application->RemoveVar($event->Prefix_Special.'_Sort2');
$this->Application->RemoveVar($event->Prefix_Special.'_Sort2_Dir');
}
/**
* Sets grid refresh interval
*
* @param kEvent $event
*/
function OnSetAutoRefreshInterval(&$event)
{
$refresh_interval = $this->Application->GetVar('refresh_interval');
$view_name = $this->Application->RecallVar($event->getPrefixSpecial().'_current_view');
$this->Application->StorePersistentVar($event->getPrefixSpecial().'_refresh_interval.'.$view_name, $refresh_interval);
}
/**
* Changes auto-refresh state for grid
*
* @param kEvent $event
*/
function OnAutoRefreshToggle(&$event)
{
$refresh_intervals = $this->Application->ConfigValue('AutoRefreshIntervals');
if (!$refresh_intervals) {
return ;
}
$view_name = $this->Application->RecallVar($event->getPrefixSpecial().'_current_view');
$auto_refresh = $this->Application->RecallPersistentVar($event->getPrefixSpecial().'_auto_refresh.'.$view_name);
if ($auto_refresh === false) {
$refresh_intervals = explode(',', $refresh_intervals);
$this->Application->StorePersistentVar($event->getPrefixSpecial().'_refresh_interval.'.$view_name, $refresh_intervals[0]);
}
$this->Application->StorePersistentVar($event->getPrefixSpecial().'_auto_refresh.'.$view_name, $auto_refresh ? 0 : 1);
}
/**
* 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)
{
$sqls = $this->Application->getUnitOption($event->Prefix, 'ItemSQLs', Array ());
$special = array_key_exists($event->Special, $sqls) ? $event->Special : '';
if (!array_key_exists($special, $sqls)) {
// preferred special not found in ItemSQLs -> use analog from ListSQLs
return $this->ListPrepareQuery($event);
}
return $sqls[$special];
}
/**
* 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)
{
$sqls = $this->Application->getUnitOption($event->Prefix, 'ListSQLs', Array ());
return $sqls[ array_key_exists($event->Special, $sqls) ? $event->Special : '' ];
}
/**
* Apply custom processing to item
*
* @param kEvent $event
*/
function customProcessing(&$event, $type)
{
}
/* Edit Events mostly used in Admin */
/**
* Creates new kDBItem
*
* @param kEvent $event
* @access protected
*/
function OnCreate(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
/* @var $object kDBItem */
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if ($items_info) {
list($id,$field_values) = each($items_info);
$object->SetFieldsFromHash($field_values);
}
$this->customProcessing($event,'before');
//look at kDBItem' Create for ForceCreateId description, it's rarely used and is NOT set by default
if( $object->Create($event->getEventParam('ForceCreateId')) )
{
if( $object->IsTempTable() ) $object->setTempID();
$this->customProcessing($event,'after');
$event->status=erSUCCESS;
$event->redirect_params = Array('opener'=>'u');
}
else
{
$event->status = erFAIL;
$event->redirect = false;
$this->Application->SetVar($event->Prefix_Special.'_SaveEvent','OnCreate');
$object->setID($id);
}
}
/**
* Updates kDBItem
*
* @param kEvent $event
* @access protected
*/
function OnUpdate(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if($items_info)
{
foreach($items_info as $id => $field_values)
{
$object->Load($id);
$object->SetFieldsFromHash($field_values);
$this->customProcessing($event, 'before');
if( $object->Update($id) )
{
$this->customProcessing($event, 'after');
$event->status=erSUCCESS;
}
else
{
$event->status=erFAIL;
$event->redirect=false;
break;
}
}
}
$event->redirect_params = Array('opener'=>'u');
}
/**
* Delete's kDBItem object
*
* @param kEvent $event
* @access protected
*/
function OnDelete(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
return;
}
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
/* @var $temp kTempTablesHandler */
$temp->DeleteItems($event->Prefix, $event->Special, Array($this->getPassedID($event)));
}
/**
* Prepares new kDBItem object
*
* @param kEvent $event
* @access protected
*/
function OnNew(&$event)
{
$object =& $event->getObject( Array('skip_autoload' => true) );
/* @var $object kDBItem */
$object->Clear(0);
$this->Application->SetVar($event->Prefix_Special.'_SaveEvent', 'OnCreate');
$table_info = $object->getLinkedInfo();
$object->SetDBField($table_info['ForeignKey'], $table_info['ParentId']);
$event->redirect = false;
}
/**
* Cancel's kDBItem Editing/Creation
*
* @param kEvent $event
* @access protected
*/
function OnCancel(&$event)
{
$object =& $event->getObject(Array('skip_autoload' => true));
$items_info = $this->Application->GetVar($event->getPrefixSpecial(true));
if ($items_info) {
$delete_ids = Array();
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
foreach ($items_info as $id => $field_values) {
$object->Load($id);
// record created for using with selector (e.g. Reviews->Select User), and not validated => Delete it
if ($object->isLoaded() && !$object->Validate() && ($id <= 0) ) {
$delete_ids[] = $id;
}
}
if ($delete_ids) {
$temp->DeleteItems($event->Prefix, $event->Special, $delete_ids);
}
}
$event->redirect_params = Array('opener'=>'u');
}
/**
* Deletes all selected items.
* Automatically recurse into sub-items using temp handler, and deletes sub-items
* by calling its Delete method if sub-item has AutoDelete set to true in its config file
*
* @param kEvent $event
*/
function OnMassDelete(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
return;
}
$event->status=erSUCCESS;
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
$ids = $this->StoreSelectedIDs($event);
$event->setEventParam('ids', $ids);
$this->customProcessing($event, 'before');
$ids = $event->getEventParam('ids');
if($ids)
{
$temp->DeleteItems($event->Prefix, $event->Special, $ids);
}
$this->clearSelectedIDs($event);
}
/**
* Sets window id (of first opened edit window) to temp mark in uls
*
* @param kEvent $event
*/
function setTempWindowID(&$event)
{
$mode = $this->Application->GetVar($event->Prefix.'_mode');
if ($mode == 't') {
$wid = $this->Application->GetVar('m_wid');
$this->Application->SetVar($event->Prefix.'_mode', 't'.$wid);
}
}
/**
* Prepare temp tables and populate it
* with items selected in the grid
*
* @param kEvent $event
*/
function OnEdit(&$event)
{
$this->setTempWindowID($event);
$this->StoreSelectedIDs($event);
$var_name = $event->getPrefixSpecial().'_file_pending_actions'.$this->Application->GetVar('m_wid');
$this->Application->RemoveVar($var_name);
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
/* @var $temp kTempTablesHandler */
$temp->PrepareEdit();
$event->redirect=false;
}
/**
* Saves content of temp table into live and
* redirects to event' default redirect (normally grid template)
*
* @param kEvent $event
*/
function OnSave(&$event)
{
$event->CallSubEvent('OnPreSave');
if ($event->status == erSUCCESS) {
$skip_master = false;
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
$changes_var_name = $this->Prefix.'_changes_'.$this->Application->GetTopmostWid($this->Prefix);
if (!$this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
$live_ids = $temp->SaveEdit($event->getEventParam('master_ids') ? $event->getEventParam('master_ids') : Array());
// Deleteing files scheduled for delete
$var_name = $event->getPrefixSpecial().'_file_pending_actions'.$this->Application->GetVar('m_wid');
$schedule = $this->Application->RecallVar($var_name);
$schedule = $schedule ? unserialize($schedule) : array();
foreach ($schedule as $data) {
if ($data['action'] == 'delete') {
unlink($data['file']);
}
}
if ($live_ids) {
// ensure, that newly created item ids are avalable as if they were selected from grid
// NOTE: only works if main item has subitems !!!
$this->StoreSelectedIDs($event, $live_ids);
}
$this->SaveLoggedChanges($changes_var_name);
}
else {
$this->Application->RemoveVar($changes_var_name);
}
$this->clearSelectedIDs($event);
$event->redirect_params = Array('opener' => 'u');
$this->Application->RemoveVar($event->getPrefixSpecial().'_modified');
// all temp tables are deleted here => all after hooks should think, that it's live mode now
$this->Application->SetVar($event->Prefix.'_mode', '');
}
}
function SaveLoggedChanges($changes_var_name)
{
$ses_log_id = $this->Application->RecallVar('_SessionLogId_');
if (!$ses_log_id) {
return ;
}
$changes = $this->Application->RecallVar($changes_var_name);
$changes = $changes ? unserialize($changes) : Array ();
if (!$changes) {
return ;
}
$add_fields = Array (
'PortalUserId' => $this->Application->RecallVar('user_id'),
'SessionLogId' => $ses_log_id,
);
$changelog_table = $this->Application->getUnitOption('change-log', 'TableName');
$sessionlog_table = $this->Application->getUnitOption('session-log', 'TableName');
foreach ($changes as $rec) {
$this->Conn->doInsert(array_merge($rec, $add_fields), $changelog_table);
}
$sql = 'UPDATE '.$sessionlog_table.'
SET AffectedItems = AffectedItems + '.count($changes).'
WHERE SessionLogId = '.$ses_log_id;
$this->Conn->Query($sql);
$this->Application->RemoveVar($changes_var_name);
}
/**
* Cancels edit
* Removes all temp tables and clears selected ids
*
* @param kEvent $event
*/
function OnCancelEdit(&$event)
{
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
$temp->CancelEdit();
$this->clearSelectedIDs($event);
$event->redirect_params = Array('opener'=>'u');
$this->Application->RemoveVar($event->getPrefixSpecial().'_modified');
}
/**
* Allows to determine if we are creating new item or editing already created item
*
* @param kEvent $event
* @return bool
*/
function isNewItemCreate(&$event)
{
$event->setEventParam('raise_warnings', 0);
$object =& $event->getObject();
return !$object->IsLoaded();
// $item_id = $this->getPassedID($event);
// return ($item_id == '') ? true : false;
}
/**
* Saves edited item into temp table
* If there is no id, new item is created in temp table
*
* @param kEvent $event
*/
function OnPreSave(&$event)
{
//$event->redirect = false;
// if there is no id - it means we need to create an item
if (is_object($event->MasterEvent)) {
$event->MasterEvent->setEventParam('IsNew',false);
}
if ($this->isNewItemCreate($event)) {
$event->CallSubEvent('OnPreSaveCreated');
if (is_object($event->MasterEvent)) {
$event->MasterEvent->setEventParam('IsNew',true);
}
return;
}
$object =& $event->getObject( Array('skip_autoload' => true) );
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if ($items_info) {
foreach ($items_info as $id => $field_values) {
$object->SetDefaultValues();
$object->Load($id);
$object->SetFieldsFromHash($field_values);
$this->customProcessing($event, 'before');
if( $object->Update($id) )
{
$this->customProcessing($event, 'after');
$event->status=erSUCCESS;
}
else {
$event->status = erFAIL;
$event->redirect = false;
break;
}
}
}
}
/**
* Saves edited item in temp table and loads
* item with passed id in current template
* Used in Prev/Next buttons
*
* @param kEvent $event
*/
function OnPreSaveAndGo(&$event)
{
$event->CallSubEvent('OnPreSave');
if ($event->status == erSUCCESS) {
$event->SetRedirectParam($event->getPrefixSpecial(true).'_id', $this->Application->GetVar($event->Prefix_Special.'_GoId'));
}
}
/**
* Saves edited item in temp table and goes
* to passed tabs, by redirecting to it with OnPreSave event
*
* @param kEvent $event
*/
function OnPreSaveAndGoToTab(&$event)
{
$event->CallSubEvent('OnPreSave');
if ($event->status==erSUCCESS) {
$event->redirect=$this->Application->GetVar($event->getPrefixSpecial(true).'_GoTab');
}
}
/**
* Saves editable list and goes to passed tab,
* by redirecting to it with empty event
*
* @param kEvent $event
*/
function OnUpdateAndGoToTab(&$event)
{
$event->setPseudoClass('_List');
$event->CallSubEvent('OnUpdate');
if ($event->status==erSUCCESS) {
$event->redirect=$this->Application->GetVar($event->getPrefixSpecial(true).'_GoTab');
}
}
/**
* Prepare temp tables for creating new item
* but does not create it. Actual create is
* done in OnPreSaveCreated
*
* @param kEvent $event
*/
function OnPreCreate(&$event)
{
$this->setTempWindowID($event);
$this->clearSelectedIDs($event);
$object =& $event->getObject( Array('skip_autoload' => true) );
$temp =& $this->Application->recallObject($event->Prefix.'_TempHandler', 'kTempTablesHandler');
$temp->PrepareEdit();
$object->setID(0);
$this->Application->SetVar($event->getPrefixSpecial().'_id',0);
$this->Application->SetVar($event->getPrefixSpecial().'_PreCreate', 1);
$event->redirect=false;
}
/**
* Creates a new item in temp table and
* stores item id in App vars and Session on succsess
*
* @param kEvent $event
*/
function OnPreSaveCreated(&$event)
{
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if($items_info) $field_values = array_shift($items_info);
$object =& $event->getObject( Array('skip_autoload' => true) );
$object->SetFieldsFromHash($field_values);
$this->customProcessing($event, 'before');
if( $object->Create() )
{
$this->customProcessing($event, 'after');
$event->redirect_params[$event->getPrefixSpecial(true).'_id'] = $object->GetId();
$event->status=erSUCCESS;
}
else
{
$event->status=erFAIL;
$event->redirect=false;
$object->setID(0);
}
}
function OnReset(&$event)
{
//do nothing - should reset :)
if ($this->isNewItemCreate($event)) {
// just reset id to 0 in case it was create
$object =& $event->getObject( Array('skip_autoload' => true) );
$object->setID(0);
$this->Application->SetVar($event->getPrefixSpecial().'_id',0);
}
}
/**
* Apply same processing to each item beeing selected in grid
*
* @param kEvent $event
* @access private
*/
function iterateItems(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
return;
}
$object =& $event->getObject( Array('skip_autoload' => true) );
$ids = $this->StoreSelectedIDs($event);
if ($ids) {
$status_field = array_shift( $this->Application->getUnitOption($event->Prefix,'StatusField') );
foreach ($ids as $id) {
$object->Load($id);
switch ($event->Name) {
case 'OnMassApprove':
$object->SetDBField($status_field, 1);
break;
case 'OnMassDecline':
$object->SetDBField($status_field, 0);
break;
case 'OnMassMoveUp':
$object->SetDBField('Priority', $object->GetDBField('Priority') + 1);
break;
case 'OnMassMoveDown':
$object->SetDBField('Priority', $object->GetDBField('Priority') - 1);
break;
}
if ($object->Update()) {
$event->status = erSUCCESS;
}
else {
$event->status = erFAIL;
$event->redirect = false;
break;
}
}
}
$this->clearSelectedIDs($event);
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnMassClone(&$event)
{
if ($this->Application->CheckPermission('SYSTEM_ACCESS.READONLY', 1)) {
return;
}
$event->status = erSUCCESS;
$temp =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
$ids = $this->StoreSelectedIDs($event);
if ($ids) {
$temp->CloneItems($event->Prefix, $event->Special, $ids);
}
$this->clearSelectedIDs($event);
}
function check_array($records, $field, $value)
{
foreach ($records as $record) {
if ($record[$field] == $value) {
return true;
}
}
return false;
}
function OnPreSavePopup(&$event)
{
$object =& $event->getObject();
$this->RemoveRequiredFields($object);
$event->CallSubEvent('OnPreSave');
$this->finalizePopup($event);
}
/* End of Edit events */
// III. Events that allow to put some code before and after Update,Load,Create and Delete methods of item
/**
* Occurse before loading item, 'id' parameter
* allows to get id of item beeing loaded
*
* @param kEvent $event
* @access public
*/
function OnBeforeItemLoad(&$event)
{
}
/**
* Occurse after loading item, 'id' parameter
* allows to get id of item that was loaded
*
* @param kEvent $event
* @access public
*/
function OnAfterItemLoad(&$event)
{
}
/**
* Occurse before creating item
*
* @param kEvent $event
* @access public
*/
function OnBeforeItemCreate(&$event)
{
}
/**
* Occurse after creating item
*
* @param kEvent $event
* @access public
*/
function OnAfterItemCreate(&$event)
{
}
/**
* Occurse before updating item
*
* @param kEvent $event
* @access public
*/
function OnBeforeItemUpdate(&$event)
{
}
/**
* Occurse after updating item
*
* @param kEvent $event
* @access public
*/
function OnAfterItemUpdate(&$event)
{
}
/**
* Occurse before deleting item, id of item beeing
* deleted is stored as 'id' event param
*
* @param kEvent $event
* @access public
*/
function OnBeforeItemDelete(&$event)
{
}
/**
* Occurse after deleting item, id of deleted item
* is stored as 'id' param of event
*
* @param kEvent $event
* @access public
*/
function OnAfterItemDelete(&$event)
{
}
/**
* Occurs after successful item validation
*
* @param kEvent $event
*/
function OnAfterItemValidate(&$event)
{
}
/**
* Occures after an item has been copied to temp
* Id of copied item is passed as event' 'id' param
*
* @param kEvent $event
*/
function OnAfterCopyToTemp(&$event)
{
}
/**
* Occures before an item is deleted from live table when copying from temp
* (temp handler deleted all items from live and then copy over all items from temp)
* Id of item being deleted is passed as event' 'id' param
*
* @param kEvent $event
*/
function OnBeforeDeleteFromLive(&$event)
{
}
/**
* Occures before an item is copied to live table (after all foreign keys have been updated)
* Id of item being copied is passed as event' 'id' param
*
* @param kEvent $event
*/
function OnBeforeCopyToLive(&$event)
{
}
/**
* !!! NOT FULLY IMPLEMENTED - SEE TEMP HANDLER COMMENTS (search by event name)!!!
* Occures after an item has been copied to live table
* Id of copied item is passed as event' 'id' param
*
* @param kEvent $event
*/
function OnAfterCopyToLive(&$event)
{
}
/**
* Occures before an item is cloneded
* Id of ORIGINAL item is passed as event' 'id' param
* Do not call object' Update method in this event, just set needed fields!
*
* @param kEvent $event
*/
function OnBeforeClone(&$event)
{
}
/**
* Occures after an item has been cloned
* Id of newly created item is passed as event' 'id' param
*
* @param kEvent $event
*/
function OnAfterClone(&$event)
{
}
/**
* Ensures that popup will be closed automatically
* and parent window will be refreshed with template
* passed
*
* @param kEvent $event
* @access public
*/
function finalizePopup(&$event)
{
$event->SetRedirectParam('opener', 'u');
/*return ;
// 2. substitute opener
$opener_stack = $this->Application->RecallVar('opener_stack');
$opener_stack = $opener_stack ? unserialize($opener_stack) : Array();
//array_pop($opener_stack);
$t = $this->Application->RecallVar('return_template');
$this->Application->RemoveVar('return_template');
// restore original "m" prefix all params, that have values before opening selector
$return_m = $this->Application->RecallVar('return_m');
$this->Application->RemoveVar('return_m');
$this->Application->HttpQuery->parseEnvPart($return_m);
$pass_events = $event->getEventParam('pass_events');
$redirect_params = array_merge_recursive2($event->redirect_params, Array('m_opener' => 'u', '__URLENCODE__' => 1));
$new_level = 'index.php|'.ltrim($this->Application->BuildEnv($t, $redirect_params, 'all', $pass_events), ENV_VAR_NAME.'=');
array_push($opener_stack, $new_level);
$this->Application->StoreVar('opener_stack', serialize($opener_stack));*/
}
/**
* Create search filters based on search query
*
* @param kEvent $event
* @access protected
*/
function OnSearch(&$event)
{
$event->setPseudoClass('_List');
$search_helper =& $this->Application->recallObject('SearchHelper');
$search_helper->performSearch($event);
}
/**
* Clear search keywords
*
* @param kEvent $event
* @access protected
*/
function OnSearchReset(&$event)
{
$search_helper =& $this->Application->recallObject('SearchHelper');
$search_helper->resetSearch($event);
}
/**
* Set's new filter value (filter_id meaning from config)
*
* @param kEvent $event
*/
function OnSetFilter(&$event)
{
$filter_id = $this->Application->GetVar('filter_id');
$filter_value = $this->Application->GetVar('filter_value');
$view_filter = $this->Application->RecallVar($event->getPrefixSpecial().'_view_filter');
$view_filter = $view_filter ? unserialize($view_filter) : Array();
$view_filter[$filter_id] = $filter_value;
$this->Application->StoreVar( $event->getPrefixSpecial().'_view_filter', serialize($view_filter) );
}
function OnSetFilterPattern(&$event)
{
$filters = $this->Application->GetVar($event->getPrefixSpecial(true).'_filters');
if (!$filters) return ;
$view_filter = $this->Application->RecallVar($event->getPrefixSpecial().'_view_filter');
$view_filter = $view_filter ? unserialize($view_filter) : Array();
$filters = explode(',', $filters);
foreach ($filters as $a_filter) {
list($id, $value) = explode('=', $a_filter);
$view_filter[$id] = $value;
}
$this->Application->StoreVar( $event->getPrefixSpecial().'_view_filter', serialize($view_filter) );
$event->redirect = false;
}
/**
* Add/Remove all filters applied to list from "View" menu
*
* @param kEvent $event
*/
function FilterAction(&$event)
{
$view_filter = Array();
$filter_menu = $this->Application->getUnitOption($event->Prefix,'FilterMenu');
switch ($event->Name)
{
case 'OnRemoveFilters':
$filter_value = 1;
break;
case 'OnApplyFilters':
$filter_value = 0;
break;
}
foreach($filter_menu['Filters'] as $filter_key => $filter_params)
{
if(!$filter_params) continue;
$view_filter[$filter_key] = $filter_value;
}
$this->Application->StoreVar( $event->getPrefixSpecial().'_view_filter', serialize($view_filter) );
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnPreSaveAndOpenTranslator(&$event)
{
$this->Application->SetVar('allow_translation', true);
$object =& $event->getObject();
$this->RemoveRequiredFields($object);
$event->CallSubEvent('OnPreSave');
if ($event->status == erSUCCESS) {
$resource_id = $this->Application->GetVar('translator_resource_id');
if ($resource_id) {
$t_prefixes = explode(',', $this->Application->GetVar('translator_prefixes'));
$cdata =& $this->Application->recallObject($t_prefixes[1], null, Array('skip_autoload' => true));
$cdata->Load($resource_id, 'ResourceId');
if (!$cdata->isLoaded()) {
$cdata->SetDBField('ResourceId', $resource_id);
$cdata->Create();
}
$this->Application->SetVar($cdata->getPrefixSpecial().'_id', $cdata->GetID());
}
$event->redirect = $this->Application->GetVar('translator_t');
$event->redirect_params = Array('pass'=>'all,trans,'.$this->Application->GetVar('translator_prefixes'),
$event->getPrefixSpecial(true).'_id' => $object->GetID(),
'trans_event' => 'OnLoad',
'trans_prefix' => $this->Application->GetVar('translator_prefixes'),
'trans_field' => $this->Application->GetVar('translator_field'),
'trans_multi_line' => $this->Application->GetVar('translator_multi_line'),
);
// 1. SAVE LAST TEMPLATE TO SESSION (really needed here, because of tweaky redirect)
$last_template = $this->Application->RecallVar('last_template');
preg_match('/index4\.php\|'.$this->Application->GetSID().'-(.*):/U', $last_template, $rets);
$this->Application->StoreVar('return_template', $this->Application->GetVar('t'));
}
}
function RemoveRequiredFields(&$object)
{
// making all field non-required to achieve successful presave
foreach($object->Fields as $field => $options)
{
if(isset($options['required']))
{
unset($object->Fields[$field]['required']);
}
}
}
/**
* Saves selected user in needed field
*
* @param kEvent $event
*/
function OnSelectUser(&$event)
{
$items_info = $this->Application->GetVar('u');
if ($items_info) {
$user_id = array_shift( array_keys($items_info) );
$object =& $event->getObject();
$this->RemoveRequiredFields($object);
$is_new = !$object->isLoaded();
$is_main = substr($this->Application->GetVar($event->Prefix.'_mode'), 0, 1) == 't';
if ($is_new) {
$new_event = $is_main ? 'OnPreCreate' : 'OnNew';
$event->CallSubEvent($new_event);
$event->redirect = true;
}
$object->SetDBField($this->Application->RecallVar('dst_field'), $user_id);
if ($is_new) {
$object->Create();
if (!$is_main && $object->IsTempTable()) {
$object->setTempID();
}
}
else {
$object->Update();
}
}
$event->SetRedirectParam($event->getPrefixSpecial().'_id', $object->GetID());
$this->finalizePopup($event);
}
/** EXPORT RELATED **/
/**
* Shows export dialog
*
* @param kEvent $event
*/
function OnExport(&$event)
{
$this->StoreSelectedIDs($event);
$selected_ids = $this->getSelectedIDs($event);
if (implode(',', $selected_ids) == '') {
// K4 fix when no ids found bad selected ids array is formed
$selected_ids = false;
}
$this->Application->StoreVar($event->Prefix.'_export_ids', $selected_ids ? implode(',', $selected_ids) : '' );
$export_t = $this->Application->GetVar('export_template');
$this->Application->LinkVar('export_finish_t');
$this->Application->LinkVar('export_progress_t');
$this->Application->StoreVar('export_oroginal_special', $event->Special);
$export_helper =& $this->Application->recallObject('CatItemExportHelper');
$event->redirect = $export_t ? $export_t : $export_helper->getModuleFolder($event).'/export';
list($index_file, $env) = explode('|', $this->Application->RecallVar('last_template'));
$finish_url = $this->Application->BaseURL('/admin').$index_file.'?'.ENV_VAR_NAME.'='.$env;
$this->Application->StoreVar('export_finish_url', $finish_url);
$redirect_params = Array(
$this->Prefix.'.export_event' => 'OnNew',
'pass' => 'all,'.$this->Prefix.'.export');
$event->setRedirectParams($redirect_params);
}
/**
* Apply some special processing to
* object beeing recalled before using
* it in other events that call prepareObject
*
* @param Object $object
* @param kEvent $event
* @access protected
*/
function prepareObject(&$object, &$event)
{
if ($event->Special == 'export' || $event->Special == 'import')
{
$export_helper =& $this->Application->recallObject('CatItemExportHelper');
$export_helper->prepareExportColumns($event);
}
}
/**
* Returns specific to each item type columns only
*
* @param kEvent $event
* @return Array
*/
function getCustomExportColumns(&$event)
{
return Array();
}
/**
* Export form validation & processing
*
* @param kEvent $event
*/
function OnExportBegin(&$event)
{
$export_helper =& $this->Application->recallObject('CatItemExportHelper');
/* @var $export_helper kCatDBItemExportHelper */
$export_helper->OnExportBegin($event);
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnExportCancel(&$event)
{
$this->OnGoBack($event);
}
/**
* Allows configuring export options
*
* @param kEvent $event
*/
function OnBeforeExportBegin(&$event)
{
}
function OnDeleteExportPreset(&$event)
{
$object =& $event->GetObject();
$items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) );
if($items_info)
{
list($id,$field_values) = each($items_info);
$preset_key = $field_values['ExportPresets'];
$export_settings = $this->Application->RecallPersistentVar('export_settings');
if (!$export_settings) return ;
$export_settings = unserialize($export_settings);
if (!isset($export_settings[$event->Prefix])) return ;
$to_delete = '';
$export_presets = array(''=>'');
foreach ($export_settings[$event->Prefix] as $key => $val) {
if (implode('|', $val['ExportColumns']) == $preset_key) {
$to_delete = $key;
break;
}
}
if ($to_delete) {
unset($export_settings[$event->Prefix][$to_delete]);
$this->Application->StorePersistentVar('export_settings', serialize($export_settings));
}
}
}
/**
* Saves changes & changes language
*
* @param kEvent $event
*/
function OnPreSaveAndChangeLanguage(&$event)
{
$event->CallSubEvent('OnPreSave');
if ($event->status == erSUCCESS) {
$this->Application->SetVar('m_lang', $this->Application->GetVar('language'));
}
}
+ /**
+ * Used to save files uploaded via swfuploader
+ *
+ * @param kEvent $event
+ */
function OnUploadFile(&$event)
{
+ $event->status = erSTOP;
+
// Flash uploader does NOT send correct cookies, so we need to make our own check
$cookie_name = 'adm_'.$this->Application->ConfigValue('SessionCookieName');
$this->Application->HttpQuery->Cookie['cookies_on'] = 1;
$this->Application->HttpQuery->Cookie[$cookie_name] = $this->Application->GetVar('flashsid');
$this->Application->HttpQuery->Cookie[$cookie_name.'_live'] = $this->Application->GetVar('flashsid'); // this prevents session from auto-expiring when KeepSessionOnBrowserClose & FireFox is used
$admin_ses =& $this->Application->recallObject('Session.admin');
/* @var $admin_ses Session */
- $user = $admin_ses->RecallVar('user_id');
-
$perm_helper =& $this->Application->recallObject('PermissionsHelper');
/* @var $perm_helper kPermissionsHelper */
$section = $event->getSection();
+ $user_id = $admin_ses->RecallVar('user_id');
- if (!$perm_helper->CheckUserPermission($user, $section.'.add') && !$perm_helper->CheckUserPermission($user, $section.'.edit')) {
+ if (!$perm_helper->CheckUserPermission($user_id, $section.'.add') && !$perm_helper->CheckUserPermission($user_id, $section.'.edit')) {
$event->status = erPERM_FAIL;
+ // 403 Forbidden
header('HTTP/1.0 403 You don\'t have permissions to upload');
exit;
- return;
+ return ;
}
if (!$cookie_name) {
$cookie_name = 'sid';
}
$value = $this->Application->GetVar('Filedata');
if (!$value) {
- return ;
+ $event->status = erFAIL;
+ // 413 Request Entity Too Large (when uploaded file was to large for web server to accept)
+ header('HTTP/1.0 413 File size exceeds allowed limit');
+ exit;
}
$tmp_path = defined('WRITEABLE') ? WRITEABLE.'/tmp/' : FULL_PATH.'/kernel/cache/';
$fname = $value['name'];
$id = $this->Application->GetVar('id');
- if ($id) $fname = $id.'_'.$fname;
+ if ($id) {
+ $fname = $id.'_'.$fname;
+ }
if (!is_writable($tmp_path)) {
+ $event->status = erFAIL;
+ // 500 Internal Server Error
header('HTTP/1.0 500 Write permissions not set on the server');
exit;
}
move_uploaded_file($value['tmp_name'], $tmp_path.$fname);
- exit;
+
+ die("Flash requires that we output something or it won't fire the uploadSuccess event");
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnDeleteFile(&$event)
{
- if (strpos($this->Application->GetVar('file'), '../') !== false) return ;
- $object =& $event->getObject(array('skip_autoload'=>true));
+ $event->status = erSTOP;
+
+ if (strpos($this->Application->GetVar('file'), '../') !== false) {
+ return ;
+ }
+
+ $object =& $event->getObject( Array ('skip_autoload' => true)) ;
$options = $object->GetFieldOptions($this->Application->GetVar('field'));
$var_name = $event->getPrefixSpecial().'_file_pending_actions'.$this->Application->GetVar('m_wid');
$schedule = $this->Application->RecallVar($var_name);
$schedule = $schedule ? unserialize($schedule) : array();
$schedule[] = array('action'=>'delete', 'file'=>$path = FULL_PATH.$options['upload_dir'].$this->Application->GetVar('file'));
$this->Application->StoreVar($var_name, serialize($schedule));
$this->Application->Session->SaveData();
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnViewFile(&$event)
{
if (strpos($this->Application->GetVar('file'), '../') !== false) return ;
if ($this->Application->GetVar('tmp')) {
$path = (defined('WRITEABLE') ? WRITEABLE.'/tmp/' : FULL_PATH.'/kernel/cache/').$this->Application->GetVar('id').'_'.$this->Application->GetVar('file');
}
else {
$object =& $event->getObject(array('skip_autoload'=>true));
$options = $object->GetFieldOptions($this->Application->GetVar('field'));
$path = FULL_PATH.$options['upload_dir'].$this->Application->GetVar('file');
}
$type = mime_content_type($path);
header('Content-Length: '.filesize($path));
header('Content-Type: '.$type);
safeDefine('DBG_SKIP_REPORTING',1);
readfile($path);
exit();
}
/**
* Validates MInput control fields
*
* @param kEvent $event
*/
function OnValidateMInputFields(&$event)
{
$minput_helper =& $this->Application->recallObject('MInputHelper');
/* @var $minput_helper MInputHelper */
$minput_helper->OnValidateMInputFields($event);
}
/**
* Returns auto-complete values for ajax-dropdown
*
* @param kEvent $event
*/
function OnSuggestValues(&$event)
{
$this->Application->XMLHeader();
$field = $this->Application->GetVar('field');
$cur_value = $this->Application->GetVar('cur_value');
if (!$field || !$cur_value) {
exit;
}
$object =& $event->getObject();
$sql = 'SELECT DISTINCT '.$field.'
FROM '.$object->TableName.'
WHERE '.$field.' LIKE '.$this->Conn->qstr($cur_value.'%').'
ORDER BY '.$field.'
LIMIT 0,20';
$data = $this->Conn->GetCol($sql);
echo '<suggestions>';
foreach ($data as $item) {
echo '<item>'.$item.'</item>';
}
echo '</suggestions>';
$event->status = erSTOP;
}
/**
* Enter description here...
*
* @param kEvent $event
*/
function OnSaveWidths(&$event)
{
safeDefine('DBG_SKIP_REPORTING', 1);
$lang =& $this->Application->recallObject('lang.current');
// header('Content-type: text/xml; charset='.$lang->GetDBField('Charset'));
$picker_helper =& $this->Application->RecallObject('ColumnPickerHelper');
/* @var $picker_helper kColumnPickerHelper */
$picker_helper->PreparePicker($event->getPrefixSpecial(), $this->Application->GetVar('grid_name'));
$picker_helper->SaveWidths($event->getPrefixSpecial(), $this->Application->GetVar('widths'));
exit;
}
/**
* Called from CSV import script after item fields
* are set and validated, but before actual item create/update.
* If event status is erSUCCESS, line will be imported,
* else it will not be imported but added to skipped lines
* and displayed in the end of import.
* Event status is preset from import script.
*
* @param kEvent $event
*/
function OnBeforeCSVLineImport(&$event)
{
// abstract, for hooking
}
}
?>
\ No newline at end of file
Property changes on: branches/RC/core/kernel/db/db_event_handler.php
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.99.2.22
\ No newline at end of property
+1.99.2.23
\ No newline at end of property
Index: branches/RC/core/admin_templates/swfupload.swf
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Property changes on: branches/RC/core/admin_templates/swfupload.swf
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.2
\ No newline at end of property
+1.2.4.1
\ No newline at end of property
Index: branches/RC/core/admin_templates/js/uploader.js
===================================================================
--- branches/RC/core/admin_templates/js/uploader.js (revision 10620)
+++ branches/RC/core/admin_templates/js/uploader.js (revision 10621)
@@ -1,451 +1,6 @@
-function Uploader(id, params)
-{
- this.id = id;
- if (params.multiple.length == 0) params.multiple = 1;
- this.params = params;
- this.files_count = 0;
- this.files = new Array();
- this.deleted = new Array()
+var $uploader_scripts = [
+ '<script type="text/javascript" src="js/uploader/upload_manager.js"></script>',
+ '<script type="text/javascript" src="js/uploader/uploader.js"></script>'
+];
- this.uploadURL = params.uploadURL;
- this.deleteURL = params.deleteURL;
-}
-
-Uploader.prototype.Init = function()
-{
- var holder = document.createElement('DIV');
- document.body.appendChild(holder);
-
- document.getElementById($form_name).style.display = 'block';
-
- // moving out progress div to overcome loosing of flash object after setting opacity
- this.div = document.getElementById(this.id+'_progress');
- var clone = this.div.cloneNode(true);
- this.div.parentNode.removeChild(this.div)
- this.div = document.body.appendChild(clone);
- this.IconPath = this.params.IconPath ? this.params.IconPath : '../cmseditor/editor/images';
-
- this.filename = document.getElementById(this.id+'_progress_filename');
- this.progress = document.getElementById(this.id+'_progress_progress');
- this.elapsed = document.getElementById(this.id+'_progress_elapsed');
- this.remaining = document.getElementById(this.id+'_progress_remaining');
- this.percent = document.getElementById(this.id+'_percent');
- this.done = document.getElementById(this.id+'_done');
- this.total = 0;
- this.uploaded = 0;
-
- this.flash_id = UploadsManager.NextFlashId();
- this.swf = new SWFObject('swfupload.swf', this.flash_id, "0", "0", "8", "#000000");
- this.swf.setAttribute('style', '');
- this.swf.addVariable("uploadScript", '');
- this.swf.addVariable("maxFiles", escape(this.params.multiple));
- this.swf.addVariable("allowedFilesize", escape(this.params.allowedFilesize));
- this.swf.addVariable("allowedFiletypes", escape(this.params.allowedFiletypes));
- this.swf.addVariable("allowedFiletypesDescription", escape(this.params.allowedFiletypesDescription));
-
- this.swf.addVariable("uploadFileQueuedCallback", escape('UploadsManager.FileSelected'));
- this.swf.addVariable("uploadFileStartCallback", escape('UploadsManager.UploadFileStart'));
- this.swf.addVariable("uploadProgressCallback", escape('UploadsManager.UploadProgress'));
- this.swf.addVariable("uploadFileCompleteCallback", escape('UploadsManager.UploadFileComplete'));
- this.swf.addVariable("uploadFileCancelCallback", escape('UploadsManager.FileCancelled'));
- this.swf.addVariable("uploadQueueCompleteCallback", escape('UploadsManager.UploadQueueComplete'));
- this.swf.addVariable("uploadFileErrorCallback", escape('UploadsManager.UploadError'));
- this.swf.addVariable("autoUpload", escape('false'));
-// this.swf.addVariable("flashLoadedCallback", 'flashLoadedCallback');
-
- this.swf.addVariable("uploadScriptCallback", escape('UploadsManager.GetUploadScript'));
- this.swf.addVariable("uploaderId", escape(this.id));
- this.swf.write(holder);
-
- this.flash = document.getElementById(this.flash_id);
-/* if (this.flash != null) {
- if(this.flash.PercentLoaded() == 100) {
- alert('done movie: '+this.flash.PercentLoaded());
- }
- }
- else {
- alert('this.flash is null')
- }*/
- if (this.params.urls != '') {
- var urls = this.params.urls.split('|');
- var names = this.params.names.split('|');
- var sizes = this.params.sizes.split('|');
- for (var i in urls) {
- var a_file = {
- id : names[i],
- name : names[i],
- url : urls[i],
- size: sizes[i],
- uploaded : 1
- }
- this.files.push(a_file)
- this.files_count++;
- }
- this.UpdateInfo();
- }
-}
-
-Uploader.prototype.UpdateInfo = function()
-{
- var o = '';
- for (var f in this.files) {
- this.files[f].name.match(/\.([^.]*)$/);
- var ext = RegExp.$1;
- var icon = ext.match(/^(ai|avi|bmp|cs|dll|doc|dot|exe|fla|gif|htm|html|jpg|js|mdb|mp3|pdf|ppt|rdp|swf|swt|txt|vsd|xls|xml|zip)$/) ? ext : 'default.icon';
- o += '<img src="' + this.IconPath + '/' + icon+'.gif" style="position: relative; top: 2px;">&nbsp;';
- if (isset(this.files[f].uploaded)) {
- o += '<a href="'+this.files[f].url+'" target="_new">'+this.files[f].name + '</a> ('+this.FormatSize(this.files[f].size)+')&nbsp;[<a href="javascript:UploadsManager.DeleteFile(\''+this.id+'\', \''+this.files[f].name+'\')">Delete</a>]<br/>';
- }
- else {
- o += this.files[f].name + ' ('+this.FormatSize(this.files[f].size)+')&nbsp;[<a href="javascript:UploadsManager.CancelFile(\''+this.files[f].uploader_id+'\', \''+this.files[f].id+'\')">Delete</a>]<br/>';
- }
- }
- document.getElementById(this.id+'_queueinfo').innerHTML = o;
- this.PrepareFiles()
-}
-
-Uploader.prototype.RemoveFile = function(file)
-{
- var n_files = new Array();
- var count=0;
- this.total=0;
- for (var f in this.files) {
- if (this.files[f].id != file.id && this.files[f].name != file.id ) {
- n_files.push(this.files[f]);
- if (!isset(this.files[f].uploaded)) {
- this.total += file.size
- }
- count++;
- }
- }
- this.files = n_files;
- this.files_count = count;
- this.UpdateInfo();
-}
-
-Uploader.prototype.GetUploadScript = function(file)
-{
-// document.getElementById(this.id+'[my_tmp]').value = file.id;
- return this.uploadURL.replace('#ID#', file.id).replace('#FIELD#', this.params.field).replace('#SID#', this.params.flashsid);
-}
-
-Uploader.prototype.PrepareFiles = function()
-{
- var ids = '';
- var names = '';
- for (var f in this.files) {
- if (isset(this.files[f].uploaded) && !isset(this.files[f].temp)) continue;
- ids += this.files[f].id + '|'
- names += this.files[f].name + '|'
- }
- ids = ids.replace(/\|$/, '', ids);
- names = names.replace(/\|$/, '', names);
- document.getElementById(this.id+'[tmp_ids]').value = ids;
- document.getElementById(this.id+'[tmp_names]').value = names;
- document.getElementById(this.id+'[tmp_deleted]').value = this.deleted.join('|');
-}
-
-
-Uploader.prototype.HasQueue = function()
-{
- for (var f in this.files) {
- if (isset(this.files[f].uploaded)) continue;
- return true;
- }
- return false;
-}
-
-Uploader.prototype.StartUpload = function()
-{
- if (!this.HasQueue()) return;
-
- Request.setOpacity(30, UploadsManager.getFromContainer());
-
- if (!document.all) {
- var $winW = window.innerWidth;
- var $winH = window.innerHeight;
- }
- else {
- var $winW = window.document.body.offsetWidth;
- var $winH = window.document.body.offsetHeight;
- }
-
- var left = Math.round(($winW - 350)/2)+'px';
- var top = Math.round(($winH - 110)/2)+'px';
-
- this.div.style.top = top;
- this.div.style.left = left;
- this.div.style.display = 'block';
- Request.setOpacity(100, this.div);
-
- this.StartTime = this.GetMicroTime();
- this.ProgressPercent = 0; // progress percent
- this.ProgressTime = new Array();
-
- this.uploaded = 0;
- this.total = 0;
- for (var f in this.files) {
- if (isset(this.files[f].uploaded)) continue;
- this.total += this.files[f].size;
- }
-
- document.getElementById(this.flash_id).upload();
-}
-
-Uploader.prototype.GetMicroTime = function() {
- var $now = new Date();
- return Math.round($now.getTime() / 1000); // because miliseconds are returned too
-}
-Uploader.prototype.GetEstimatedTime = function() {
- return Math.ceil((100 - this.ProgressPercent) * this.ProgressTime / this.ProgressPercent);
-}
-
-Uploader.prototype.FormatTime = function ($seconds) {
- $seconds = parseInt($seconds);
-
- var $minutes = Math.floor($seconds / 60);
- if ($minutes < 10) $minutes = '0' + $minutes;
- $seconds = $seconds % 60;
- if ($seconds < 10) $seconds = '0' + $seconds;
-
- return $minutes + ':' + $seconds;
-}
-
-Uploader.prototype.FormatSize = function (bytes) {
- var kb = Math.round(bytes / 1024);
- if (kb < 1024) return kb+'Kb';
- var mb = Math.round(kb / 1024 * 100)/100;
- return mb+'Mb';
-}
-
-Uploader.prototype.UploadFileStart = function(file, position, queuelength)
-{
- this.filename.innerHTML = file.name;
-}
-
-Uploader.prototype.UploadProgress = function(file, bytesLoaded)
-{
- this.cur_file_uploaded = bytesLoaded;
- var uploaded = this.uploaded+this.cur_file_uploaded;
- this.ProgressTime = this.GetMicroTime() - this.StartTime;
-
- var speed = 0;
- if (this.ProgressTime > 0) {
- speed = Math.round(uploaded/this.ProgressTime*100)/100;
- }
-
- this.progress.innerHTML = this.FormatSize(uploaded)+' / '+this.FormatSize(this.total) + ' ('+this.FormatSize(speed)+'/s)';
- this.ProgressPercent = Math.round(uploaded/this.total*100);
- this.done.style.width = this.ProgressPercent+'%';
- this.percent.innerHTML = this.ProgressPercent+'%';
-
- this.elapsed.innerHTML = this.FormatTime(this.ProgressTime );
- this.remaining.innerHTML = this.FormatTime( this.GetEstimatedTime() );
-}
-
-Uploader.prototype.UploadFileComplete = function(file)
-{
- this.uploaded += this.cur_file_uploaded;
- for (var f in this.files) {
- if (this.files[f].id == file.id) {
- this.files[f].uploaded = 1;
- this.files[f].temp = 1;
- this.files[f].url = this.params.tmp_url.replace('#ID#', file.id).replace('#FILE#', file.name).replace('#FIELD#', this.params.field);
- }
- }
- this.UpdateInfo();
-}
-
-
-// MANAGER
-
-
-function UploadsManager() {
- // hooking to standard toolbar select button to peform auto-upload when Save is clicked
- if (isset(a_toolbar)) {
- if (a_toolbar.ButtonExists('select')) {
- var old_onclick = a_toolbar.Buttons['select'].onClick;
- a_toolbar.Buttons['select'].onClick = function() {
- UploadsManager.UploadAll(function() {old_onclick()});
- }
- }
- }
- addLoadEvent(function() {UploadsManager.InitAll()});
-}
-
-UploadsManager = new UploadsManager();
-UploadsManager.Uploaders = new Object();
-UploadsManager.nextId = 0;
-UploadsManager.formContainerId = '';
-
-UploadsManager.NextFlashId = function()
-{
- this.nextId++;
- return 'uploaderflash'+this.nextId;
-}
-
-UploadsManager.AddUploader = function(id, params )
-{
- this.Uploaders[id] = new Uploader(id, params);
-}
-
-UploadsManager.getFromContainer = function()
-{
- if (UploadsManager.formContainerId) {
- return document.getElementById(UploadsManager.formContainerId);
- }
-
- return Form.Div;
-}
-
-UploadsManager.InitAll = function()
-{
- for (var i in this.Uploaders) {
- this.Uploaders[i].Init();
- }
-}
-
-UploadsManager.FileSelected = function(file)
-{
- var upl = this.Uploaders[file.uploader_id]
- if (upl.files_count >= upl.params.multiple) {
- if (upl.params.multiple > 1) {
- alert('too many files');
- upl.flash.cancelFile(file.id);
- }
- else {
- upl.files_count++;
- upl.files.push(file);
- upl.total += file.size;
- if (upl.files[0].uploaded) {
- UploadsManager.DeleteFile(file.uploader_id, upl.files[0].name, true);
- }
- else {
- upl.flash.cancelFile(upl.files[0].id);
- }
- }
- }
- else {
- upl.files_count++;
- upl.files.push(file);
- upl.total += file.size;
- }
- upl.UpdateInfo();
-}
-
-UploadsManager.FileCancelled = function(file)
-{
- this.Uploaders[file.uploader_id].RemoveFile(file);
-}
-
-UploadsManager.GetUploadScript = function(file)
-{
- return this.Uploaders[file.uploader_id].GetUploadScript(file);
-}
-
-UploadsManager.UploadError = function(errno, file, msg)
-{
- this.Uploaders[file.uploader_id].RemoveFile(file);
-
- if (errno == -10) {
- switch (msg.toString()) {
- case '403':
- msg = 'You don\'t have permission to upload';
- break;
- case '500':
- msg = 'Write permissions not set on the server, please contact server administrator';
- break;
- }
- }
- alert('Error: '+msg+'\nOccured on file '+file.name);
-}
-
-UploadsManager.DeleteFile = function(mov, fname, confirmed)
-{
- if (!confirmed && !confirm('Are you sure you want to delete this file?')) return;
- Request.makeRequest(
- this.Uploaders[mov].deleteURL.replace('#FILE#', fname).replace('#FIELD#', this.Uploaders[mov].params.field),
- false, '',
- function(req, fname, upl) {
- upl.RemoveFile({id:fname})
- upl.deleted.push(fname);
- upl.UpdateInfo();
- },
- function(req, fname, upl) {alert('Error while deleting file')},
- fname, this.Uploaders[mov]
- );
-}
-
-
-UploadsManager.Browse = function(id)
-{
- this.Uploaders[id].flash.browse()
-}
-
-UploadsManager.StartUpload = function(id)
-{
- this.uploadCancelled = false;
- this.Uploaders[id].StartUpload();
-}
-
-UploadsManager.CancelFile = function(id, file_id)
-{
- this.Uploaders[id].flash.cancelFile(file_id)
-}
-
-UploadsManager.UploadAll = function(onAllUploaded)
-{
- if (!this.HasQueue()) {
- onAllUploaded();
- return;
- }
- this.uploadCancelled = false;
- for (var i in this.Uploaders) {
- this.Uploaders[i].StartUpload();
- }
- this.OnAllUploaded = onAllUploaded;
-}
-
-UploadsManager.UploadFileStart = function(file, position, queuelength)
-{
- this.Uploaders[file.uploader_id].UploadFileStart(file, position, queuelength);
-}
-
-UploadsManager.UploadProgress = function(file, bytesLoaded)
-{
- this.Uploaders[file.uploader_id].UploadProgress(file, bytesLoaded);
-}
-
-UploadsManager.UploadFileComplete = function(file)
-{
- this.Uploaders[file.uploader_id].UploadFileComplete(file);
-}
-
-UploadsManager.HasQueue = function()
-{
- var has_queue = false;
- for (var i in this.Uploaders) {
- var tmp = this.Uploaders[i].HasQueue()
- has_queue = has_queue || tmp;
- }
- return has_queue;
-}
-
-UploadsManager.UploadQueueComplete = function(file)
-{
- Request.setOpacity(100, UploadsManager.getFromContainer());
- var all_done = true;
- for (var i in this.Uploaders) {
- this.Uploaders[i].div.style.display='none';
- all_done == all_done && !this.Uploaders[i].HasQueue();
- }
- if (all_done && isset(this.OnAllUploaded) && !this.uploadCancelled) {
- this.OnAllUploaded();
- }
-}
-
-UploadsManager.CancelUpload = function(id)
-{
- this.Uploaders[id].flash.cancelQueue();
- this.uploadCancelled = true;
-}
\ No newline at end of file
+document.write($uploader_scripts.join(''));
\ No newline at end of file
Property changes on: branches/RC/core/admin_templates/js/uploader.js
___________________________________________________________________
Modified: cvs2svn:cvs-rev
## -1 +1 ##
-1.3.4.2
\ No newline at end of property
+1.3.4.3
\ No newline at end of property
Index: branches/RC/core/admin_templates/js/uploader/uploader.js
===================================================================
--- branches/RC/core/admin_templates/js/uploader/uploader.js (nonexistent)
+++ branches/RC/core/admin_templates/js/uploader/uploader.js (revision 10621)
@@ -0,0 +1,327 @@
+// this js class name is hardcoded in flash object :(
+var SWFUpload = function () {};
+SWFUpload.instances = {};
+
+function Uploader(id, params) {
+ this.id = id;
+
+ // normalize params
+ if (isNaN(parseInt(params.multiple))) {
+ // ensure that maximal file number is greather then zero
+ params.multiple = 1;
+ }
+
+ params.allowedFilesize = this._normalizeFilesize(params.allowedFilesize);
+
+ // set params to uploader
+ this._eventQueue = [];
+
+ this.params = params;
+ this.files_count = 0;
+ this.files = new Array();
+ this.deleted = new Array()
+
+ this.uploadURL = params.uploadURL;
+ this.deleteURL = params.deleteURL;
+}
+
+/* ==== Private methods ==== */
+Uploader.prototype._normalizeFilesize = function($file_size) {
+ var $normalize_size = parseInt($file_size);
+ if (isNaN($normalize_size)) {
+ return $file_size;
+ }
+
+ // in kilobytes (flash doesn't recognize numbers, that are longer, then 9 digits)
+ return $normalize_size / 1024;
+}
+
+Uploader.prototype._prepareFiles = function() {
+ var ids = '';
+ var names = '';
+ for (var f in this.files) {
+ if (isset(this.files[f].uploaded) && !isset(this.files[f].temp)) {
+ continue;
+ }
+
+ ids += this.files[f].id + '|'
+ names += this.files[f].name + '|'
+ }
+
+ ids = ids.replace(/\|$/, '', ids);
+ names = names.replace(/\|$/, '', names);
+ document.getElementById(this.id+'[tmp_ids]').value = ids;
+ document.getElementById(this.id+'[tmp_names]').value = names;
+ document.getElementById(this.id+'[tmp_deleted]').value = this.deleted.join('|');
+}
+
+Uploader.prototype._getMicroTime = function() {
+ var $now = new Date();
+ return Math.round($now.getTime() / 1000); // because miliseconds are returned too
+}
+
+Uploader.prototype._getEstimatedTime = function() {
+ return Math.ceil((100 - this.ProgressPercent) * this.ProgressTime / this.ProgressPercent);
+}
+
+Uploader.prototype._formatTime = function ($seconds) {
+ $seconds = parseInt($seconds);
+
+ var $minutes = Math.floor($seconds / 60);
+ if ($minutes < 10) $minutes = '0' + $minutes;
+ $seconds = $seconds % 60;
+ if ($seconds < 10) $seconds = '0' + $seconds;
+
+ return $minutes + ':' + $seconds;
+}
+
+Uploader.prototype._formatSize = function (bytes) {
+ var kb = Math.round(bytes / 1024);
+ if (kb < 1024) return kb+'Kb';
+ var mb = Math.round(kb / 1024 * 100)/100;
+ return mb+'Mb';
+}
+
+Uploader.prototype._executeNextEvent = function () {
+ var f = this._eventQueue ? this._eventQueue.shift() : null;
+ if (typeof(f) === 'function') {
+ f.apply(this);
+ }
+};
+
+/* ==== Public methods ==== */
+Uploader.prototype.init = function() {
+ var holder = document.createElement('DIV');
+ document.body.appendChild(holder);
+
+ document.getElementById($form_name).style.display = 'block';
+
+ // moving out progress div to overcome loosing of flash object after setting opacity
+ this.div = document.getElementById(this.id+'_progress');
+ var clone = this.div.cloneNode(true);
+ this.div.parentNode.removeChild(this.div)
+ this.div = document.body.appendChild(clone);
+ this.IconPath = this.params.IconPath ? this.params.IconPath : '../cmseditor/editor/images';
+
+ this.filename = document.getElementById(this.id+'_progress_filename');
+ this.progress = document.getElementById(this.id+'_progress_progress');
+ this.elapsed = document.getElementById(this.id+'_progress_elapsed');
+ this.remaining = document.getElementById(this.id+'_progress_remaining');
+ this.percent = document.getElementById(this.id+'_percent');
+ this.done = document.getElementById(this.id+'_done');
+ this.total = 0;
+ this.uploaded = 0;
+
+ // initialize flash object
+ this.flash_id = UploadsManager._nextFlashId();
+
+ // add callbacks for every event, because none of callbacks will work in other case (see swfupload documentation)
+ SWFUpload.instances[this.flash_id] = this;
+ SWFUpload.instances[this.flash_id].flashReady = UploadsManager.onHandleEverything;
+ SWFUpload.instances[this.flash_id].fileDialogStart = UploadsManager.onHandleEverything;
+ SWFUpload.instances[this.flash_id].fileQueued = UploadsManager.onFileQueued;
+ SWFUpload.instances[this.flash_id].fileQueueError = UploadsManager.onFileQueueError;
+ SWFUpload.instances[this.flash_id].fileDialogComplete = UploadsManager.onHandleEverything;
+
+ SWFUpload.instances[this.flash_id].uploadStart = UploadsManager.onUploadStart;
+ SWFUpload.instances[this.flash_id].uploadProgress = UploadsManager.onUploadProgress;
+ SWFUpload.instances[this.flash_id].uploadError = UploadsManager.onUploadError;
+ SWFUpload.instances[this.flash_id].uploadSuccess = UploadsManager.onHandleEverything;
+ SWFUpload.instances[this.flash_id].uploadComplete = UploadsManager.onUploadComplete;
+ SWFUpload.instances[this.flash_id].debug = UploadsManager.onDebug;
+
+ this.swf = new SWFObject('swfupload.swf', this.flash_id, '0', '0', '8', '#FFFFFF');
+ this.swf.setAttribute('style', '');
+
+ this.swf.addVariable('movieName', escape(this.flash_id));
+ this.swf.addVariable('fileUploadLimit', escape(this.params.multiple));
+ this.swf.addVariable('fileSizeLimit', escape(this.params.allowedFilesize)); // in kilobytes
+ this.swf.addVariable('fileTypes', escape(this.params.allowedFiletypes));
+ this.swf.addVariable('fileTypesDescription', escape(this.params.allowedFiletypesDescription));
+ this.swf.addVariable('uploadURL', escape(this.params.uploadURL));
+
+ if (UploadsManager._debugMode) {
+ this.swf.addVariable('debugEnabled', escape('true')); // flash var
+ }
+
+ this.swf.write(holder);
+
+ this.flash = document.getElementById(this.flash_id);
+ /*if (this.flash != null) {
+ if(this.flash.PercentLoaded() == 100) {
+ alert('done movie: '+this.flash.PercentLoaded());
+ }
+ }
+ else {
+ alert('this.flash is null')
+ }*/
+
+ if (this.params.urls != '') {
+ var urls = this.params.urls.split('|');
+ var names = this.params.names.split('|');
+ var sizes = this.params.sizes.split('|');
+ for (var i in urls) {
+ var a_file = {
+ id : names[i],
+ name : names[i],
+ url : urls[i],
+ size: sizes[i],
+ uploaded : 1
+ }
+ this.files.push(a_file)
+ this.files_count++;
+ }
+ this.updateInfo();
+ }
+}
+
+Uploader.prototype.updateInfo = function() {
+ var o = '';
+ for (var f in this.files) {
+ this.files[f].name.match(/\.([^.]*)$/);
+ var ext = RegExp.$1;
+ var icon = ext.match(/^(ai|avi|bmp|cs|dll|doc|dot|exe|fla|gif|htm|html|jpg|js|mdb|mp3|pdf|ppt|rdp|swf|swt|txt|vsd|xls|xml|zip)$/) ? ext : 'default.icon';
+ o += '<img src="' + this.IconPath + '/' + icon+'.gif" style="position: relative; top: 2px;">&nbsp;';
+ if (isset(this.files[f].uploaded)) {
+ o += '<a href="'+this.files[f].url+'" target="_new">'+this.files[f].name + '</a> ('+this._formatSize(this.files[f].size)+')&nbsp;[<a href="javascript:UploadsManager.DeleteFile(\''+this.id+'\', \''+this.files[f].name+'\')">Delete</a>]<br/>';
+ }
+ else {
+ o += this.files[f].name + ' ('+this._formatSize(this.files[f].size)+')&nbsp;[<a href="javascript:UploadsManager.CancelFile(\'' + UploadsManager._getUploader(this.files[f]).id + '\', \''+this.files[f].id+'\')">Delete</a>]<br/>';
+ }
+ }
+ document.getElementById(this.id+'_queueinfo').innerHTML = o;
+ this._prepareFiles()
+}
+
+Uploader.prototype.removeFile = function (file) {
+ var n_files = new Array();
+ var count = 0;
+ this.total = 0;
+
+ for (var f in this.files) {
+ if (this.files[f].id != file.id && this.files[f].name != file.id) {
+ n_files.push(this.files[f]);
+ if (!isset(this.files[f].uploaded)) {
+ this.total += file.size;
+ }
+ count++;
+ }
+ }
+
+ this.files = n_files;
+ this.files_count = count;
+ this.updateInfo();
+}
+
+Uploader.prototype.hasQueue = function() {
+ for (var f in this.files) {
+ if (isset(this.files[f].uploaded)) {
+ continue;
+ }
+
+ return true;
+ }
+
+ return false;
+}
+
+Uploader.prototype.startUpload = function() {
+ if (!this.hasQueue()) {
+ return;
+ }
+
+ Request.setOpacity(30, UploadsManager._getFromContainer());
+
+ if (!document.all) {
+ var $winW = window.innerWidth;
+ var $winH = window.innerHeight;
+ }
+ else {
+ var $winW = window.document.body.offsetWidth;
+ var $winH = window.document.body.offsetHeight;
+ }
+
+ var left = Math.round(($winW - 350)/2)+'px';
+ var top = Math.round(($winH - 110)/2)+'px';
+
+ this.div.style.top = top;
+ this.div.style.left = left;
+ this.div.style.display = 'block';
+ Request.setOpacity(100, this.div);
+
+ this.StartTime = this._getMicroTime();
+ this.ProgressPercent = 0; // progress percent
+ this.ProgressTime = new Array();
+
+ this.uploaded = 0;
+ this.total = 0;
+ for (var f in this.files) {
+ if (isset(this.files[f].uploaded)) continue;
+ this.total += this.files[f].size;
+ }
+
+ this.flash.StartUpload();
+}
+
+Uploader.prototype.UploadFileStart = function(file) {
+ this.filename.innerHTML = file.name;
+
+ // we can prevent user from adding any files here :)
+ this.flash.ReturnUploadStart(true);
+}
+
+Uploader.prototype.UploadProgress = function(file, bytesLoaded, bytesTotal) {
+ this.cur_file_uploaded = bytesLoaded;
+ var uploaded = this.uploaded+this.cur_file_uploaded;
+ this.ProgressTime = this._getMicroTime() - this.StartTime;
+
+ var speed = 0;
+ if (this.ProgressTime > 0) {
+ speed = Math.round(uploaded/this.ProgressTime*100)/100;
+ }
+
+ this.progress.innerHTML = this._formatSize(uploaded)+' / '+this._formatSize(this.total) + ' ('+this._formatSize(speed)+'/s)';
+ this.ProgressPercent = Math.round(uploaded/this.total*100);
+ this.done.style.width = this.ProgressPercent+'%';
+ this.percent.innerHTML = this.ProgressPercent+'%';
+
+ this.elapsed.innerHTML = this._formatTime(this.ProgressTime );
+ this.remaining.innerHTML = this._formatTime( this._getEstimatedTime() );
+}
+
+Uploader.prototype.UploadFileComplete = function(file) {
+ // in use
+ this.uploaded += this.cur_file_uploaded;
+ for (var f in this.files) {
+ if (this.files[f].id == file.id) {
+ this.files[f].uploaded = 1;
+ this.files[f].temp = 1;
+ this.files[f].url = this.params.tmp_url.replace('#ID#', file.id).replace('#FILE#', file.name).replace('#FIELD#', this.params.field);
+ }
+ }
+ this.updateInfo();
+
+ // upload next file in queue
+ var $stats = this.flash.GetStats();
+ if ($stats.files_queued > 0 && !UploadsManager.uploadCancelled) {
+ this.flash.StartUpload();
+ } else if (!UploadsManager.uploadCancelled) {
+ // all files in queue are uploaded
+ UploadsManager.UploadQueueComplete();
+ }
+}
+
+Uploader.prototype.queueEvent = function (function_body) {
+ // Warning: Don't call this.debug inside here or you'll create an infinite loop
+ var self = this;
+
+ // Queue the event
+ this._eventQueue.push(function_body);
+
+ // Execute the next queued event
+ setTimeout(
+ function () {
+ self._executeNextEvent();
+ }, 0
+ );
+};
\ No newline at end of file
Property changes on: branches/RC/core/admin_templates/js/uploader/uploader.js
___________________________________________________________________
Added: cvs2svn:cvs-rev
## -0,0 +1 ##
+1.1.2.1
\ No newline at end of property
Index: branches/RC/core/admin_templates/js/uploader/upload_manager.js
===================================================================
--- branches/RC/core/admin_templates/js/uploader/upload_manager.js (nonexistent)
+++ branches/RC/core/admin_templates/js/uploader/upload_manager.js (revision 10621)
@@ -0,0 +1,367 @@
+function UploadsManager() {
+ // hooking to standard toolbar select button to peform auto-upload when Save is clicked
+ if (isset(a_toolbar)) {
+ if (a_toolbar.ButtonExists('select')) {
+ var old_onclick = a_toolbar.Buttons['select'].onClick;
+ a_toolbar.Buttons['select'].onClick = function() {
+ UploadsManager.UploadAll(function() {old_onclick()});
+ }
+ }
+ }
+
+ addLoadEvent(
+ function() {
+ UploadsManager._initAll();
+ }
+ );
+}
+
+UploadsManager = new UploadsManager();
+
+UploadsManager._nextId = 0;
+UploadsManager._debugMode = false;
+UploadsManager.formContainerId = '';
+UploadsManager._Uploaders = new Object();
+
+/* ==== Private methods ==== */
+UploadsManager._nextFlashId = function() {
+ this._nextId++;
+ return 'uploaderflash' + this._nextId;
+}
+
+UploadsManager._getFromContainer = function() {
+ if (UploadsManager.formContainerId) {
+ return document.getElementById(UploadsManager.formContainerId);
+ }
+
+ return Form.Div;
+}
+
+UploadsManager._initAll = function() {
+ for (var i in this._Uploaders) {
+ this._Uploaders[i].init();
+ }
+}
+
+UploadsManager._hasQueue = function() {
+ var has_queue = false;
+
+ for (var i in this._Uploaders) {
+ var tmp = this._Uploaders[i].hasQueue();
+ has_queue = has_queue || tmp;
+ }
+
+ return has_queue;
+}
+
+UploadsManager._getUploader = function (file) {
+ var $flash_id = file.id.match(/(.*)_[\d]+/) ? RegExp.$1 : file.id;
+
+ for (var $uploader_index in this._Uploaders) {
+ if (this._Uploaders[$uploader_index].flash_id == $flash_id) {
+ return this._Uploaders[$uploader_index];
+ }
+ }
+
+ return null;
+}
+
+/* ==== Public methods ==== */
+UploadsManager.AddUploader = function(id, params) {
+ this._Uploaders[id] = new Uploader(id, params);
+}
+
+UploadsManager.DeleteFile = function(uploader_id, fname, confirmed) {
+ if (!confirmed && !confirm('Are you sure you want to delete this file?')) {
+ return;
+ }
+
+ var $uploader = this._Uploaders[uploader_id];
+
+ Request.makeRequest(
+ $uploader.deleteURL.replace('#FILE#', fname).replace('#FIELD#', $uploader.params.field),
+ false, '',
+ function(req, fname, $uploader) {
+ $uploader.removeFile({id:fname})
+ $uploader.deleted.push(fname);
+ $uploader.updateInfo();
+ },
+
+ function(req, fname, $uploader) {
+ alert('Error while deleting file');
+ },
+ fname, $uploader
+ );
+}
+
+UploadsManager.Browse = function(id) {
+ if (parseInt(this._Uploaders[id].params.multiple) > 1) {
+ this._Uploaders[id].flash.SelectFiles();
+ } else {
+ this._Uploaders[id].flash.SelectFile();
+ }
+}
+
+UploadsManager.StartUpload = function(id) {
+ this.uploadCancelled = false;
+ this._Uploaders[id].startUpload();
+}
+
+UploadsManager.CancelFile = function(id, file_id) {
+ this._Uploaders[id].flash.CancelUpload(file_id);
+}
+
+UploadsManager.UploadAll = function(onAllUploaded) {
+ if (!this._hasQueue()) {
+ onAllUploaded();
+ return ;
+ }
+
+ this.uploadCancelled = false;
+ for (var i in this._Uploaders) {
+ // could raise problems, when simultanious uploads from diffrent uploaders are made
+ this._Uploaders[i].startUpload();
+ }
+
+ this.OnAllUploaded = onAllUploaded;
+}
+
+UploadsManager.UploadQueueComplete = function() {
+ Request.setOpacity(100, UploadsManager._getFromContainer());
+ var all_done = true;
+ for (var i in this._Uploaders) {
+ this._Uploaders[i].div.style.display = 'none';
+ all_done == all_done && !this._Uploaders[i].hasQueue();
+ }
+ if (all_done && isset(this.OnAllUploaded) && !this.uploadCancelled) {
+ this.OnAllUploaded();
+ }
+}
+
+UploadsManager.CancelUpload = function(id) {
+ var $flash = this._Uploaders[id].flash;
+
+ $flash.StopUpload();
+
+ var $stats = $flash.GetStats();
+ while ($stats.files_queued > 0) {
+ $flash.CancelUpload();
+ $stats = $flash.GetStats();
+ }
+
+ this.uploadCancelled = true;
+}
+
+UploadsManager.setDebugMode = function ($enabled) {
+ /*for (var $uploader_index in this._Uploaders) {
+ this._Uploaders[$uploader_index].flash.SetDebugEnabled($enabled);
+ }*/
+
+ this._debugMode = $enabled;
+}
+
+
+/* ==== Flash event handlers ==== */
+UploadsManager.onHandleEverything = function () {
+ if (UploadsManager._debugMode) {
+ console.log('default swf handler');
+ }
+}
+
+UploadsManager.onUploadStart = function(file) {
+ var $uploader = UploadsManager._getUploader(file);
+
+ $uploader.queueEvent(
+ function() {
+ this.UploadFileStart(file);
+ }
+ );
+}
+
+UploadsManager.onUploadProgress = function(file, bytesLoaded, bytesTotal) {
+ var $uploader = UploadsManager._getUploader(file);
+
+ $uploader.queueEvent(
+ function() {
+ this.UploadProgress(file, bytesLoaded, bytesTotal);
+ }
+ );
+}
+
+UploadsManager.onUploadComplete = function(file) {
+ var $uploader = UploadsManager._getUploader(file);
+
+ $uploader.queueEvent(
+ function() {
+ this.UploadFileComplete(file);
+ }
+ );
+}
+
+UploadsManager.onFileQueued = function(file) {
+ var $uploader = UploadsManager._getUploader(file);
+
+ $uploader.queueEvent(
+ function() {
+ this.flash.AddFileParam(file.id, 'field', this.params.field);
+ this.flash.AddFileParam(file.id, 'id', file.id);
+ this.flash.AddFileParam(file.id, 'flashsid', this.params.flashsid);
+
+ if (this.files_count >= this.params.multiple) {
+ // new file can exceed allowed file number
+ if (this.params.multiple > 1) {
+ // it definetly exceed it
+ UploadsManager.onFileQueueError(file, -100, this.params.multiple);
+ this.flash.CancelUpload(file.id);
+ }
+ else {
+ // delete file added
+ this.files_count++;
+ this.files.push(file);
+ this.total += file.size;
+ if (this.files[0].uploaded) {
+ UploadsManager.DeleteFile(UploadsManager._getUploader(file).id, this.files[0].name, true);
+ }
+ else {
+ this.flash.CancelUpload(this.files[0].id);
+ }
+ }
+ }
+ else {
+ // new file will not exceed allowed file number
+ this.files_count++;
+ this.files.push(file);
+ this.total += file.size;
+ }
+ this.updateInfo();
+ }
+ )
+}
+
+UploadsManager.onUploadError = function(file, errorCode, message) {
+ var $uploader = UploadsManager._getUploader(file);
+
+ $uploader.queueEvent(
+ function() {
+ this.removeFile(file);
+
+ switch (errorCode) {
+ case -200:
+ // HTTP Error
+ message = parseInt(message); // HTTP Error Code
+ switch (message) {
+ case 403:
+ message = "You don't have permission to upload";
+ break;
+ case 500:
+ message = 'Write permissions not set on the server, please contact server administrator';
+ break;
+ }
+
+ if (isNaN(message)) {
+ // message is processed
+ alert('Error: ' + message + "\n" + 'Occured on file ' + file.name);
+ return ;
+ }
+ break;
+
+ case -280:
+ // File Cancelled
+ return ;
+ break;
+
+ case -290:
+ // Upload Stopped
+ UploadsManager.UploadQueueComplete();
+ return ;
+ break;
+ }
+
+ // all not processed error messages go here
+ alert('Error [' + errorCode + ']: ' + message + "\n" + 'Occured on file ' + file.name);
+ }
+ );
+}
+
+UploadsManager.onFileQueueError = function(file, errorCode, message) {
+ switch (errorCode) {
+ case -100:
+ // maximal allowed file count reached
+ alert('Error: Files count exceeds allowed limit' + "\n" + 'Occured on file ' + file.name);
+ return ;
+ break;
+
+ case -110:
+ // maximal allowed filesize reached
+ alert('Error: File size exceeds allowed limit' + "\n" + 'Occured on file ' + file.name);
+ return ;
+ break;
+
+ case -130:
+ // maximal allowed filesize reached
+ alert('Error: File is not an allowed file type.' + "\n" + 'Occured on file ' + file.name);
+ return ;
+ break;
+ }
+
+ // all not processed error messages go here
+ alert('Error [' + errorCode + ']: ' + message + "\n" + 'Occured on file ' + file.name);
+}
+
+UploadsManager.onDebug = function (message) {
+ if (!UploadsManager._debugMode) {
+ return ;
+ }
+
+ var exceptionMessage, exceptionValues = [];
+
+ // Check for an exception object and print it nicely
+ if (typeof(message) === 'object' && typeof(message.name) === 'string' && typeof(message.message) === 'string') {
+ for (var key in message) {
+ if (message.hasOwnProperty(key)) {
+ exceptionValues.push(key + ': ' + message[key]);
+ }
+ }
+ exceptionMessage = exceptionValues.join("\n") || '';
+ exceptionValues = exceptionMessage.split("\n");
+ exceptionMessage = 'EXCEPTION: ' + exceptionValues.join("\nEXCEPTION: ");
+
+ console.log(exceptionMessage);
+ } else {
+ console.log(message);
+ }
+};
+
+if (!window.console || !console.firebug) {
+ // emulate FireBug Console in other browsers to see flash debug messages
+ window.console = {};
+ window.console.log = function (message) {
+ var console, documentForm;
+
+ try {
+ console = document.getElementById('SWFUpload_Console');
+
+ if (!console) {
+ documentForm = document.createElement('form');
+ document.getElementsByTagName('body')[0].appendChild(documentForm);
+
+ console = document.createElement('textarea');
+ console.id = 'SWFUpload_Console';
+ console.style.fontFamily = 'monospace';
+ console.setAttribute('wrap', 'off');
+ console.wrap = 'off';
+ console.style.overflow = 'auto';
+ console.style.width = '700px';
+ console.style.height = '350px';
+ console.style.margin = '5px';
+ documentForm.appendChild(console);
+ }
+
+ console.value += message + "\n";
+
+ console.scrollTop = console.scrollHeight - console.clientHeight;
+ } catch (ex) {
+ alert('Exception: ' + ex.name + ' Message: ' + ex.message);
+ }
+ };
+}
\ No newline at end of file
Property changes on: branches/RC/core/admin_templates/js/uploader/upload_manager.js
___________________________________________________________________
Added: cvs2svn:cvs-rev
## -0,0 +1 ##
+1.1.2.1
\ No newline at end of property

Event Timeline