From 7aff034e616ba88b9c4c24ecbc23e57d64963720 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Mon, 18 Aug 2014 02:47:02 +0200 Subject: [PATCH 1/3] Removed new item if an error appears when processing contribution. --- controllers/ContributionController.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/controllers/ContributionController.php b/controllers/ContributionController.php index 96e931c1..33020302 100644 --- a/controllers/ContributionController.php +++ b/controllers/ContributionController.php @@ -260,6 +260,7 @@ protected function _processForm($post) $item = update_item($item, $itemMetadata, array(), $fileMetadata); } catch(Omeka_Validator_Exception $e) { $this->flashValidatonErrors($e); + $item->delete(); return false; } catch (Omeka_File_Ingest_InvalidException $e) { // Copying this cruddy hack @@ -268,9 +269,11 @@ protected function _processForm($post) } else { $this->_helper->flashMessenger($e->getMessage()); } + $item->delete(); return false; } catch (Exception $e) { $this->_helper->flashMessenger($e->getMessage()); + $item->delete(); return false; } $this->_addElementTextsToItem($item, $post['Elements']); From 24ef06e138536e88c1c5ee619384144249058777 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Tue, 9 Sep 2014 02:47:02 +0200 Subject: [PATCH 2/3] Added possibility for user to delete a contribution. --- ContributionPlugin.php | 11 ++++++++++- controllers/ContributionController.php | 13 +++++++++++-- helpers/ThemeHelpers.php | 14 ++++++++++++++ models/ContributionContributedItem.php | 15 +++++++++++++-- views/admin/common/contribution-quick-filters.php | 2 ++ views/admin/contributors/show.php | 14 ++++++++------ views/admin/items/browse.php | 4 +++- views/public/contribution/my-contributions.php | 13 +++++++------ 8 files changed, 68 insertions(+), 18 deletions(-) diff --git a/ContributionPlugin.php b/ContributionPlugin.php index 4afff01e..10dabc83 100644 --- a/ContributionPlugin.php +++ b/ContributionPlugin.php @@ -135,6 +135,7 @@ public function hookInstall() `item_id` INT UNSIGNED NOT NULL, `public` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0', `anonymous` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0', + `deleted` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0', PRIMARY KEY (`id`), UNIQUE KEY `item_id` (`item_id`) ) ENGINE=MyISAM;"; @@ -259,6 +260,12 @@ public function hookUpgrade($args) set_option('contribution_open', get_option('contribution_simple')); delete_option('contribution_simple'); } + + if (version_compare($oldVersion, '3.1.3', '<')) { + $db = $this->_db; + $sql = "ALTER TABLE `$db->ContributionContributedItem` ADD COLUMN `deleted` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0' AFTER `anonymous`"; + $db->query($sql); + } } public function hookUninstallMessage() @@ -714,7 +721,9 @@ private function _adminBaseInfo($args) $publicMessage = ''; if (is_allowed($item, 'edit')) { - if ($contributedItem->public) { + if ($contributedItem->deleted) { + $publicMessage = __('This item has been deleted by user. It cannot be made public.'); + } elseif ($contributedItem->public) { $publicMessage = __("This item can be made public."); } else { $publicMessage = __("This item cannot be made public."); diff --git a/controllers/ContributionController.php b/controllers/ContributionController.php index 33020302..5bb1acb8 100644 --- a/controllers/ContributionController.php +++ b/controllers/ContributionController.php @@ -38,16 +38,25 @@ public function myContributionsAction() $contribItem->public = $value; $contribItem->anonymous = $_POST['contribution_anonymous'][$id]; + if ($post['contribution_deleted'][$id]) { + $contribItem->makeDeletedByUser(); + } + if($contribItem->save()) { $this->_helper->flashMessenger( __('Your contributions have been updated.'), 'success'); } else { $this->_helper->flashMessenger($contribItem->getErrors()); } - $contribItems[] = $contribItem; + if (!$contribItem->deleted) { + $contribItems[] = $contribItem; + } } } else { - $contribItems = $contribItemTable->findBy(array('contributor'=>$user->id)); + $contribItems = $contribItemTable->findBy(array( + 'contributor' => $user->id, + 'deleted' => false, + )); } $this->view->contrib_items = $contribItems; } diff --git a/helpers/ThemeHelpers.php b/helpers/ThemeHelpers.php index 962d4b96..324eac87 100644 --- a/helpers/ThemeHelpers.php +++ b/helpers/ThemeHelpers.php @@ -61,3 +61,17 @@ function contribution_contribute_url($actionName = null) return get_view()->url($options, $route, array(), true); } +/** + * Get a URL to the public contribution remove page. + * + * @param Record|integer $contributedItem. + * @return string URL + */ +function contribution_remove_url($contributedItem) +{ + $basePath = get_option('contribution_page_path'); + $string = $basePath ? $basePath : 'contribution'; + $string .= '/remove/'; + $string .= is_object($contributedItem) ? $contributedItem->id : (integer) $contributedItem; + return url($string); +} diff --git a/models/ContributionContributedItem.php b/models/ContributionContributedItem.php index c98276a3..68aebf7a 100644 --- a/models/ContributionContributedItem.php +++ b/models/ContributionContributedItem.php @@ -17,7 +17,8 @@ class ContributionContributedItem extends Omeka_Record_AbstractRecord public $item_id; public $public; public $anonymous; - + public $deleted = 0; + protected $_related = array( 'Item' => 'getItem', 'Contributor' => 'getContributor' @@ -36,7 +37,17 @@ public function makeNotPublic() $item->save(); release_object($item); } - + + /** + * Delete a contributed item. In fact, for security reason, make it private + * and invisible to contributor. + */ + public function makeDeletedByUser() + { + $this->deleted = true; + $this->makeNotPublic(); + } + public function getContributor() { $owner = $this->Item->getOwner(); diff --git a/views/admin/common/contribution-quick-filters.php b/views/admin/common/contribution-quick-filters.php index f8d5c7c4..0abe00e3 100644 --- a/views/admin/common/contribution-quick-filters.php +++ b/views/admin/common/contribution-quick-filters.php @@ -6,6 +6,8 @@
  • +
  • +
  • diff --git a/views/admin/contributors/show.php b/views/admin/contributors/show.php index 0dfaf006..1d75aea2 100644 --- a/views/admin/contributors/show.php +++ b/views/admin/contributors/show.php @@ -29,14 +29,16 @@
    - - Item); ?> + + Item); ?>
    - Item->public) { + deleted) { + $status = __('User Deleted'); + } elseif ($contributedItem->Item->public) { $status = __('Public'); } else { - if($item->public) { + if($contributedItem->public) { $status = __('Needs review'); } else { $status = __('Private contribution'); @@ -45,7 +47,7 @@ ?>

    -

    anonymous ? " | " . __('Anonymous') : ""; ?>

    +

    anonymous ? " | " . __('Anonymous') : ""; ?>

    array('class' => 'admin-thumb panel')), diff --git a/views/admin/items/browse.php b/views/admin/items/browse.php index 02eddb2f..c0580785 100644 --- a/views/admin/items/browse.php +++ b/views/admin/items/browse.php @@ -120,7 +120,9 @@ - + deleted): ?> + + diff --git a/views/public/contribution/my-contributions.php b/views/public/contribution/my-contributions.php index fab13a28..4ba7b75f 100644 --- a/views/public/contribution/my-contributions.php +++ b/views/public/contribution/my-contributions.php @@ -8,6 +8,7 @@ + @@ -15,15 +16,15 @@ Item; ?> - formCheckbox("contribution_public[{$contribItem->id}]", null, array('checked'=>$contribItem->public) ); ?> - - formCheckbox("contribution_anonymous[{$contribItem->id}]", null, array('checked'=>$contribItem->anonymous) ); ?> - + formCheckbox("contribution_public[{$contribItem->id}]", + null, array('checked' => $contribItem->public)); ?> + formCheckbox("contribution_anonymous[{$contribItem->id}]", + null, array('checked' => $contribItem->anonymous)); ?> + formCheckbox("contribution_deleted[{$contribItem->id}]", + null, array('checked' => false)); ?> - - From 030e17eb8bd9ca4a215c71bea22ee1766c3f5512 Mon Sep 17 00:00:00 2001 From: Daniel Berthereau Date: Fri, 19 Sep 2014 02:47:02 +0200 Subject: [PATCH 3/3] Replaced the makeNotPublic() method by the afterSave() hook. --- controllers/ContributionController.php | 15 +++----- models/ContributionContributedItem.php | 50 ++++++++++++++++---------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/controllers/ContributionController.php b/controllers/ContributionController.php index 5bb1acb8..324b7659 100644 --- a/controllers/ContributionController.php +++ b/controllers/ContributionController.php @@ -30,17 +30,9 @@ public function myContributionsAction() if(!empty($_POST)) { foreach($_POST['contribution_public'] as $id=>$value) { $contribItem = $contribItemTable->find($id); - if($value) { - $contribItem->public = true; - } else { - $contribItem->makeNotPublic(); - } - $contribItem->public = $value; - $contribItem->anonymous = $_POST['contribution_anonymous'][$id]; - - if ($post['contribution_deleted'][$id]) { - $contribItem->makeDeletedByUser(); - } + $contribItem->public = (integer) $value; + $contribItem->anonymous = (integer) $_POST['contribution_anonymous'][$id]; + $contribItem->deleted = (integer) $_POST['contribution_deleted'][$id]; if($contribItem->save()) { $this->_helper->flashMessenger( __('Your contributions have been updated.'), 'success'); @@ -48,6 +40,7 @@ public function myContributionsAction() $this->_helper->flashMessenger($contribItem->getErrors()); } + // Clean list for next view. if (!$contribItem->deleted) { $contribItems[] = $contribItem; } diff --git a/models/ContributionContributedItem.php b/models/ContributionContributedItem.php index 68aebf7a..f96fd5f1 100644 --- a/models/ContributionContributedItem.php +++ b/models/ContributionContributedItem.php @@ -29,25 +29,6 @@ public function getItem() return $this->getDb()->getTable('Item')->find($this->item_id); } - public function makeNotPublic() - { - $this->public = false; - $item = $this->Item; - $item->public = false; - $item->save(); - release_object($item); - } - - /** - * Delete a contributed item. In fact, for security reason, make it private - * and invisible to contributor. - */ - public function makeDeletedByUser() - { - $this->deleted = true; - $this->makeNotPublic(); - } - public function getContributor() { $owner = $this->Item->getOwner(); @@ -68,4 +49,35 @@ public function getContributor() } return $owner; } + + /** + * Before-save hook. + * + * @param array $args + */ + protected function beforeSave($args) + { + // Delete a contributed item. In fact, for security reason, make it + // private and invisible to contributor. + if ($this->deleted) { + $this->public = false; + } + } + + /** + * After-save hook. + * + * @param array $args + */ + protected function afterSave($args) + { + if (!$this->public) { + $item = $this->Item; + if ($item->public) { + $item->public = false; + $item->save(false); + } + release_object($item); + } + } }