Page MenuHomeIn-Portal Phabricator

favorites_eh.php
No OneTemporary

File Metadata

Created
Wed, Nov 18, 4:53 PM

favorites_eh.php

<?php
/**
* @version $Id: favorites_eh.php 16519 2017-01-20 20:21:46Z alex $
* @package In-Portal
* @copyright Copyright (C) 1997 - 2009 Intechnic. All rights reserved.
* @license GNU/GPL
* In-Portal is Open Source software.
* This means that this software may have been modified pursuant
* the GNU General Public License, and as distributed it includes
* or is derivative of works licensed under the GNU General Public License
* or other free or open source software licenses.
* See http://www.in-portal.org/license for copyright notices and details.
*/
defined('FULL_PATH') or die('restricted access!');
class FavoritesEventHandler extends kDBEventHandler {
/**
* Allows to override standard permission mapping
*
* @return void
* @access protected
* @see kEventHandler::$permMapping
*/
protected function mapPermissions()
{
parent::mapPermissions();
$permissions = Array (
'OnFavoriteToggle' => Array ('subitem' => true),
);
$this->permMapping = array_merge($this->permMapping, $permissions);
}
/**
* Adds/removes item from favorites
*
* @param kEvent $event
*/
function OnFavoriteToggle($event)
{
$config = $event->getUnitConfig();
$parent_prefix = $config->getParentPrefix();
/** @var kDBItem $parent_object */
$parent_object = $this->Application->recallObject($parent_prefix);
if ( !$parent_object->isLoaded() || !$this->Application->CheckPermission('FAVORITES', 0, $parent_object->GetDBField('ParentPath')) ) {
$event->status = kEvent::erPERM_FAIL;
return;
}
$user_id = $this->Application->RecallVar('user_id');
$sql = 'SELECT FavoriteId
FROM ' . $config->getTableName() . '
WHERE (PortalUserId = ' . $user_id . ') AND (ResourceId = ' . $parent_object->GetDBField('ResourceId') . ')';
$favorite_id = $this->Conn->GetOne($sql);
/** @var kDBItem $object */
$object = $event->getObject(Array ('skip_autoload' => true));
if ( $favorite_id ) {
$object->Delete($favorite_id);
}
else {
$object->Create();
}
$event->SetRedirectParam('pass', 'm,' . $parent_prefix);
}
/**
* Prepares Favorite record fields
*
* @param kEvent $event
* @return void
* @access protected
*/
protected function OnBeforeItemCreate(kEvent $event)
{
parent::OnBeforeItemCreate($event);
/** @var kDBItem $object */
$object = $event->getObject();
$user_id = $this->Application->RecallVar('user_id');
$object->SetDBField('PortalUserId', $user_id);
$parent_prefix = $event->getUnitConfig()->getParentPrefix();
/** @var kDBItem $parent_object */
$parent_object = $this->Application->recallObject($parent_prefix);
$object->SetDBField('ResourceId', $parent_object->GetDBField('ResourceId'));
$object->SetDBField('ItemTypeId', $this->Application->getUnitConfig($parent_prefix)->getItemType());
}
/**
* [HOOK] Deletes favorite record to item, that is being deleted
*
* @param kEvent $event
*/
function OnDeleteFavoriteItem($event)
{
$main_object = $event->MasterEvent->getObject();
/* @var $main_object kDBItem */
$sql = 'DELETE FROM ' . $event->getUnitConfig()->getTableName() . '
WHERE ResourceId = ' . $main_object->GetDBField('ResourceId');
$this->Conn->Query($sql);
}
}

Event Timeline