Skip to content

Commit

Permalink
Replaced the makeNotPublic() method by the afterSave() hook.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-KM committed Dec 18, 2016
1 parent 24ef06e commit 030e17e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
15 changes: 4 additions & 11 deletions controllers/ContributionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,17 @@ 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');
} else {
$this->_helper->flashMessenger($contribItem->getErrors());
}

// Clean list for next view.
if (!$contribItem->deleted) {
$contribItems[] = $contribItem;
}
Expand Down
50 changes: 31 additions & 19 deletions models/ContributionContributedItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
}
}

0 comments on commit 030e17e

Please sign in to comment.