Attached Files |
link_approve_removes_wrong_images.patch [^] (2,687 bytes) 2011-12-15 07:24
[Show Content]
Index: kernel/db/cat_event_handler.php
===================================================================
--- kernel/db/cat_event_handler.php (revision 14858)
+++ kernel/db/cat_event_handler.php (working copy)
@@ -2464,7 +2464,7 @@
$cloned_ids = $temp_handler->CloneItems($event->Prefix, $event->Special, Array($original_id), null, null, null, true);
$object->Load($cloned_ids[0]);
- $object->SetFieldsFromHash($field_values);
+ $object->SetFieldsFromHash($this->fixClonedUploads($field_values, $event));
// 1a. delete record from CategoryItems (about cloned item) that was automatically created during call of Create method of kCatDBItem
$ci_table = $this->Application->getUnitOption('ci', 'TableName');
@@ -2511,6 +2511,69 @@
}
/**
+ * Patches submit data for it to contain cloned uploaded filenames instead of original ones
+ *
+ * @param Array $field_values
+ * @param kEvent $event
+ * @return Array
+ */
+ function fixClonedUploads($field_values, &$event)
+ {
+ $object =& $event->getObject();
+ /* @var $object kCatDBItem */
+
+ foreach ($field_values as $field => $value) {
+ if ( !is_array($value) ) {
+ continue;
+ }
+
+ $old_file = $value['upload'];
+ $new_file = $object->GetDBField($field);
+
+ if ( $new_file == $old_file ) {
+ continue;
+ }
+
+ if ( isset($value['tmp_deleted']) && $value['tmp_deleted'] ) {
+ // replace original deleted image with cloned deleted image
+ $old_files = explode('|', $old_file);
+ $deleted = explode('|', $value['tmp_deleted']);
+
+ $var_name = $this->_getPendingActionVariableName($event);
+ $schedule = $this->Application->RecallVar($var_name);
+ $schedule = $schedule ? unserialize($schedule) : Array ();
+
+ foreach ($old_files as $old_file) {
+ $deleted_pos = array_search($old_file, $deleted);
+
+ if ( $deleted_pos === false ) {
+ continue;
+ }
+
+ $deleted[$deleted_pos] = $new_file;
+
+ // update file delete queue as well
+ foreach ($schedule as $index => $data) {
+ if ( $data['action'] == 'delete' ) {
+ if ( preg_match('/(.*)' . preg_quote($old_file, '/') . '$/', $data['file'], $regs) ) {
+ $schedule[$index]['file'] = $regs[1] . $new_file;
+ }
+ }
+ }
+ }
+
+ $field_values[$field]['tmp_deleted'] = implode('|', $deleted);
+ $this->Application->StoreVar($var_name, serialize($schedule));
+ }
+
+ // replace original image with cloned image
+ $field_values[$field]['upload'] = $new_file;
+ }
+
+ return $field_values;
+ }
+
+ /**
* Sets next template to one required for front-end after adding/modifying item
*
* @param kEvent $event
link_approval_core_1142.patch [^] (1,988 bytes) 2011-12-20 11:11
[Show Content]
Index: kernel/db/cat_dbitem.php
===================================================================
--- kernel/db/cat_dbitem.php (revision 14888)
+++ kernel/db/cat_dbitem.php (working copy)
@@ -519,7 +519,10 @@
$temp_handler->DeleteItems($this->Prefix, $this->Special, Array ($original_id));
+ $this->raiseEvent('OnAfterDeleteOriginal', null, Array('new_resource_id' => $this->GetDBField('ResourceId') ));
+
$this->SetDBField('OrgId', 0);
+
return $this->ChangeStatus(STATUS_ACTIVE, true);
}
Index: kernel/db/cat_event_handler.php
===================================================================
--- kernel/db/cat_event_handler.php (revision 14888)
+++ kernel/db/cat_event_handler.php (working copy)
@@ -27,6 +27,7 @@
'OnSaveSettings' => Array ('self' => 'add|edit|advanced:import'),
'OnResetSettings' => Array ('self' => 'add|edit|advanced:import'),
'OnBeforeDeleteOriginal' => Array ('self' => 'edit|advanced:approve'),
+ 'OnAfterDeleteOriginal' => Array ('self' => 'edit|advanced:approve'),
'OnCopy' => Array ('self' => true),
'OnDownloadFile' => Array ('self' => 'view'),
@@ -2271,6 +2272,16 @@
}
/**
+ * Occures after original item of item in pending editing got deleted (for hooking only)
+ *
+ * @param kEvent $event
+ */
+ function OnAfterDeleteOriginal(&$event)
+ {
+
+ }
+
+ /**
* Occurs before an item has been cloned
* Id of newly created item is passed as event' 'id' param
*
Index: units/helpers/file_helper.php
===================================================================
--- units/helpers/file_helper.php (revision 14888)
+++ units/helpers/file_helper.php (working copy)
@@ -428,6 +428,9 @@
$parts = pathinfo($name);
$ext = '.' . $parts['extension'];
$filename = $parts['filename'];
+ if (preg_match('/(.*)_[\d]+$/', $filename, $matches) && $matches[1]) {
+ $filename = $matches[1];
+ }
$new_name = $filename . $ext;
if ( $parts['dirname'] != '.' ) {
link_approval_modules_1142.patch [^] (2,337 bytes) 2011-12-20 11:11
[Show Content]
Index: units/links/links_event_handler.php
===================================================================
--- units/links/links_event_handler.php (revision 14888)
+++ units/links/links_event_handler.php (working copy)
@@ -525,4 +525,58 @@
$this->Application->setUnitOption($event->MasterEvent->Prefix, 'Clones', $clones);
}
}
+
+ /**
+ * Occures before original item of item in pending editing got deleted (for hooking only)
+ *
+ * @param kEvent $event
+ */
+ public function OnBeforeDeleteOriginal(&$event)
+ {
+ parent::OnBeforeDeleteOriginal($event);
+
+ $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
+ $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName');
+
+ $sql = 'SELECT ResourceId FROM '.$table_name.' WHERE '.$id_field.'='.$event->getEventParam('original_id');
+ $original_resource_id = $this->Conn->GetOne($sql);
+
+ $this->Application->SetVar('original_resource_id', $original_resource_id);
+ }
+
+ /**
+ * Changes item resource id in one field
+ *
+ * @param string $table_name
+ * @param string $resource_id_field
+ * @param string $original_link_resource_id
+ * @param string $new_link_resource_id
+ */
+ private function changeResourceId($table_name, $resource_id_field, $original_link_resource_id, $new_link_resource_id)
+ {
+ $sql = 'UPDATE '.TABLE_PREFIX.$table_name.' SET
+ '.$resource_id_field.' = '.$new_link_resource_id.'
+ WHERE '.$resource_id_field.' = '.$original_link_resource_id;
+ $this->Conn->Query($sql);
+ }
+
+
+ /**
+ * Occures before original item of item in pending editing got deleted (for hooking only)
+ *
+ * @param kEvent $event
+ */
+ public function OnAfterDeleteOriginal(&$event)
+ {
+ parent::OnAfterDeleteOriginal($event);
+
+ $original_link_resource_id = $this->Application->GetVar('original_resource_id');
+ $new_link_resource_id = $event->getEventParam('new_resource_id');
+
+ $this->changeResourceId('Listings', 'ItemResourceId', $original_link_resource_id, $new_link_resource_id);
+ $this->changeResourceId('Relationship', 'SourceId', $original_link_resource_id, $new_link_resource_id);
+ $this->changeResourceId('Relationship', 'TargetId', $original_link_resource_id, $new_link_resource_id);
+ }
+
+
}
\ No newline at end of file
advanced_unique_filename_check.patch [^] (1,618 bytes) 2011-12-21 08:21
[Show Content]
Index: units/helpers/file_helper.php
===================================================================
--- units/helpers/file_helper.php (revision 14888)
+++ units/helpers/file_helper.php (working copy)
@@ -428,25 +428,31 @@
$parts = pathinfo($name);
$ext = '.' . $parts['extension'];
$filename = $parts['filename'];
+ $path = rtrim($path, '/');
+
+ $original_checked = false;
$new_name = $filename . $ext;
if ( $parts['dirname'] != '.' ) {
- $path = rtrim($path, '/') . '/' . ltrim($parts['dirname'], '/');
+ $path .= '/' . ltrim($parts['dirname'], '/');
}
// make sure target folder always exists, especially for cases,
// when storage engine folder is supplied as a part of $name
$this->CheckFolder($path);
- while ( file_exists($path . '/' . $new_name) || in_array(rtrim($path, '/') . '/' . $new_name, $forbidden_names) ) {
- if ( preg_match('/(' . preg_quote($filename, '/') . '_)([0-9]*)(' . preg_quote($ext, '/') . ')/', $new_name, $regs) ) {
- $new_name = $regs[1] . ($regs[2] + 1) . $regs[3];
+ do {
+ if ( preg_match('/(.*)_([0-9]*)(' . preg_quote($ext, '/') . ')/', $new_name, $regs) ) {
+ $new_name = $regs[1] . '_' . ((int)$regs[2] + 1) . $regs[3];
}
- else {
+ elseif ( $original_checked ) {
$new_name = $filename . '_1' . $ext;
}
- }
+ $original_checked = true;
+ $res = file_exists($path . '/' . $new_name) || in_array($path . '/' . $new_name, $forbidden_names);
+ } while ($res !== false);
+
if ( $parts['dirname'] != '.' ) {
$new_name = $parts['dirname'] . '/' . $new_name;
}
pending_link_approval_core_v2.patch [^] (1,883 bytes) 2011-12-21 09:17
[Show Content]
Index: kernel/db/cat_dbitem.php
===================================================================
--- kernel/db/cat_dbitem.php (revision 14888)
+++ kernel/db/cat_dbitem.php (working copy)
@@ -519,7 +519,9 @@
$temp_handler->DeleteItems($this->Prefix, $this->Special, Array ($original_id));
+ $this->raiseEvent('OnAfterDeleteOriginal', null, Array ('original_id' => $original_id));
$this->SetDBField('OrgId', 0);
+
return $this->ChangeStatus(STATUS_ACTIVE, true);
}
Index: kernel/db/cat_event_handler.php
===================================================================
--- kernel/db/cat_event_handler.php (revision 14888)
+++ kernel/db/cat_event_handler.php (working copy)
@@ -27,6 +27,7 @@
'OnSaveSettings' => Array ('self' => 'add|edit|advanced:import'),
'OnResetSettings' => Array ('self' => 'add|edit|advanced:import'),
'OnBeforeDeleteOriginal' => Array ('self' => 'edit|advanced:approve'),
+ 'OnAfterDeleteOriginal' => Array ('self' => 'edit|advanced:approve'),
'OnCopy' => Array ('self' => true),
'OnDownloadFile' => Array ('self' => 'view'),
@@ -2261,16 +2262,30 @@
}
/**
- * Occures before original item of item in pending editing got deleted (for hooking only)
+ * Occurs before original item of item in pending editing got deleted (for hooking only)
*
* @param kEvent $event
+ * @return void
+ * @access protected
*/
- function OnBeforeDeleteOriginal(&$event)
+ protected function OnBeforeDeleteOriginal(kEvent &$event)
{
}
/**
+ * Occurs after original item of item in pending editing got deleted (for hooking only)
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnAfterDeleteOriginal(kEvent &$event)
+ {
+
+ }
+
+ /**
* Occurs before an item has been cloned
* Id of newly created item is passed as event' 'id' param
*
pending_link_approval_modules_v2.patch [^] (8,765 bytes) 2011-12-21 09:17
[Show Content]
Index: units/links/links_event_handler.php
===================================================================
--- units/links/links_event_handler.php (revision 14888)
+++ units/links/links_event_handler.php (working copy)
@@ -48,35 +48,39 @@
$object =& $event->getObject();
/* @var $object kDBList */
- if (!$this->Application->isAdminUser) {
- $object->addFilter('expire_filter', '(Expire > '.adodb_mktime().' OR Expire IS NULL)');
+ if ( !$this->Application->isAdminUser ) {
+ $object->addFilter('expire_filter', '(Expire > ' . adodb_mktime() . ' OR Expire IS NULL)');
}
- if (substr($event->Special, 0, 10) == 'duplicates') {
+ if ( substr($event->Special, 0, 10) == 'duplicates' ) {
$object->removeFilter('category_filter');
$link_helper =& $this->Application->recallObject('LinkHelper');
/* @var $link_helper LinkHelper */
- $grouping = $link_helper->getGrouping( $event->getPrefixSpecial() );
+ $grouping = $link_helper->getGrouping($event->getPrefixSpecial());
+
switch ($event->Special) {
case 'duplicates':
foreach ($grouping as $group_field) {
- $object->AddGroupByField($object->TableName.'.'.$group_field);
+ $object->AddGroupByField($object->TableName . '.' . $group_field);
}
+
$object->addFilter('has_dupes_filter', 'DupeCount > 1', kDBList::AGGREGATE_FILTER);
break;
case 'duplicates-sub':
- $main_object =& $this->Application->recallObject($event->Prefix.'.duplicates');
+ $main_object =& $this->Application->recallObject($event->Prefix . '.duplicates');
+ /* @var $main_object kDBItem */
+
foreach ($grouping as $field_index => $group_field) {
- $object->addFilter('dupe_filter_'.$field_index, '%1$s.`'.$group_field.'` = '.$this->Conn->qstr($main_object->GetDBField($group_field)) );
+ $object->addFilter('dupe_filter_' . $field_index, '%1$s.`' . $group_field . '` = ' . $this->Conn->qstr($main_object->GetDBField($group_field)));
}
break;
}
- $object->addFilter('primary_filter', TABLE_PREFIX.'CategoryItems.PrimaryCat = 1');
+
+ $object->addFilter('primary_filter', TABLE_PREFIX . 'CategoryItems.PrimaryCat = 1');
}
-
}
/**
@@ -99,32 +103,33 @@
$link_helper =& $this->Application->recallObject('LinkHelper');
/* @var $link_helper LinkHelper */
- $grouping = $link_helper->getGrouping( $event->getPrefixSpecial() );
+ $grouping = $link_helper->getGrouping($event->getPrefixSpecial());
$ids = $this->StoreSelectedIDs($event);
- if (!$ids) {
- return ;
+ if ( !$ids ) {
+ return;
}
// check, that user has not selected multiple links from same group
- $primary_links = Array();
+ $primary_links = Array ();
$id_field = $this->Application->getUnitOption($event->Prefix, 'IDField');
$table_name = $this->Application->getUnitOption($event->Prefix, 'TableName');
$sql = 'SELECT *
- FROM '.$table_name.'
- WHERE '.$id_field.' IN ('.implode(',', $ids).')';
+ FROM ' . $table_name . '
+ WHERE ' . $id_field . ' IN (' . implode(',', $ids) . ')';
$links = $this->Conn->Query($sql, $id_field);
$groping_error = false;
- foreach ($links as $link_id => $link_data) {
+
+ foreach ($links as $link_data) {
$group_key = '';
foreach ($grouping as $grouping_field) {
- $group_key .= 'main_table.`'.$grouping_field.'` = '.$this->Conn->qstr($link_data[$grouping_field]).' AND ';
+ $group_key .= 'main_table.`' . $grouping_field . '` = ' . $this->Conn->qstr($link_data[$grouping_field]) . ' AND ';
}
$group_key = substr($group_key, 0, -5);
- if (isset($primary_links[$group_key])) {
+ if ( isset($primary_links[$group_key]) ) {
$groping_error = true;
break;
}
@@ -133,48 +138,48 @@
}
}
- if (!$groping_error) {
- $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial().'_TempHandler', 'kTempTablesHandler');
+ if ( !$groping_error ) {
+ $temp_handler =& $this->Application->recallObject($event->getPrefixSpecial() . '_TempHandler', 'kTempTablesHandler');
/* @var $temp_handler kTempTablesHandler */
- $categories_sql = 'SELECT main_table.ResourceId, ci.CategoryId, main_table.'.$id_field.'
- FROM '.$table_name.' main_table
- LEFT JOIN '.TABLE_PREFIX.'CategoryItems ci ON main_table.ResourceId = ci.ItemResourceId
- WHERE %s';
+ $categories_sql = ' SELECT main_table.ResourceId, ci.CategoryId, main_table.' . $id_field . '
+ FROM ' . $table_name . ' main_table
+ LEFT JOIN ' . TABLE_PREFIX . 'CategoryItems ci ON main_table.ResourceId = ci.ItemResourceId
+ WHERE %s';
foreach ($primary_links as $group_key => $primary_resource_id) {
- $categories = Array();
- $group_links = Array();
+ $categories = Array ();
+ $group_links = Array ();
$group_categories = $this->Conn->Query(sprintf($categories_sql, $group_key));
+
foreach ($group_categories as $category_data) {
- $group_links[ $category_data['ResourceId'] ] = $category_data[$id_field];
+ $group_links[$category_data['ResourceId']] = $category_data[$id_field];
$categories[$category_data['ResourceId'] == $primary_resource_id ? 'remove' : 'add'][] = $category_data['CategoryId'];
}
+
unset($group_links[$primary_resource_id]);
- $categories = array_unique( array_diff($categories['add'], $categories['remove']) );
- if ($categories) {
+ $categories = array_unique(array_diff($categories['add'], $categories['remove']));
+
+ if ( $categories ) {
// add link to other link categories
$values_sql = '';
foreach ($categories as $category_id) {
- $values_sql .= '('.$category_id.','.$primary_resource_id.',0),';
+ $values_sql .= '(' . $category_id . ',' . $primary_resource_id . ',0),';
}
$values_sql = substr($values_sql, 0, -1);
- $insert_sql = 'INSERT INTO '.TABLE_PREFIX.'CategoryItems (CategoryId,ItemResourceId,PrimaryCat) VALUES '.$values_sql;
+ $insert_sql = 'INSERT INTO ' . TABLE_PREFIX . 'CategoryItems (CategoryId,ItemResourceId,PrimaryCat) VALUES ' . $values_sql;
$this->Conn->Query($insert_sql);
}
// delete all links from group except primary
$temp_handler->DeleteItems($event->Prefix, $event->Special, array_values($group_links));
}
-
-
}
else {
$event->status = kEvent::erFAIL;
$event->redirect = false;
$this->Application->SetVar($event->getPrefixSpecial().'_error', 1);
}
-
}
/**
@@ -525,4 +530,68 @@
$this->Application->setUnitOption($event->MasterEvent->Prefix, 'Clones', $clones);
}
}
+
+ /**
+ * Occurs before original item of item in pending editing got deleted (for hooking only)
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnBeforeDeleteOriginal(kEvent &$event)
+ {
+ parent::OnBeforeDeleteOriginal($event);
+
+ $object =& $event->getObject();
+ /* @var $object kDBItem */
+
+ $link_id = $event->getEventParam('original_id');
+ $new_resource_id = $object->GetDBField('ResourceId');
+
+ $sql = 'SELECT ResourceId
+ FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . '
+ WHERE ' . $this->Application->getUnitOption($event->Prefix, 'IDField') . '=' . $link_id;
+ $old_resource_id = $this->Conn->GetOne($sql);
+
+ $this->Application->SetVar('original_resource_id', $old_resource_id);
+ $this->changeResourceId('rel', 'TargetId', $old_resource_id, $new_resource_id);
+ }
+
+ /**
+ * Occurs after original item of item in pending editing got deleted
+ *
+ * @param kEvent $event
+ * @return void
+ * @access protected
+ */
+ protected function OnAfterDeleteOriginal(kEvent &$event)
+ {
+ parent::OnAfterDeleteOriginal($event);
+
+ $object =& $event->getObject();
+ /* @var $object kDBItem */
+
+ $old_resource_id = $this->Application->GetVar('original_resource_id');
+ $new_resource_id = $object->GetDBField('ResourceId');
+
+ $this->changeResourceId('ls', 'ItemResourceId', $old_resource_id, $new_resource_id);
+ }
+
+ /**
+ * Changes item resource id in one field
+ *
+ * @param string $prefix
+ * @param string $field
+ * @param string $old_resource_id
+ * @param string $new_resource_id
+ * @return void
+ * @access protected
+ */
+ protected function changeResourceId($prefix, $field, $old_resource_id, $new_resource_id)
+ {
+ $fields_hash = Array ($field => $new_resource_id);
+ $table_name = $this->Application->getUnitOption($prefix, 'TableName');
+
+ $this->Conn->doUpdate($fields_hash, $table_name, $field . ' = ' . $old_resource_id);
+ }
}
\ No newline at end of file
|