1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
<?php
/* --------------------------------------------------------------
AdminFavoritesAjaxController.php 2016-07-07
Gambio GmbH
http://www.gambio.de
Copyright (c) 2016 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
/**
* Class AdminFavoritesAjaxController
*
* This class handles the ajax requests for the favorites menu box from the admin menu.
*
* @category System
* @package AdminHttpViewControllers
* @extends AdminHttpViewController
*/
class AdminFavoritesAjaxController extends AdminHttpViewController
{
/**
* Database connection.
*
* @var CI_DB_query_builder
*/
protected $db;
/**
* @var string Link key.
*
* Unique link key of the menu item.
*/
protected $linkKey;
/**
* @var int Customer ID.
*/
protected $customerId;
/**
* @var string Table name of the favorites table.
*/
protected $adminFavoritesTable;
public function init()
{
$this->db = StaticGXCoreLoader::getDatabaseQueryBuilder();
$this->customerId = $_SESSION['customer_id'];
// TODO: Use _getQueryParameter. The method had a bug which wouldn't return the value of the GET parameter.
// That is why we have to use the global variable. When this is fixed, it should be changed.
$this->linkKey = addslashes($_GET['link_key']);
$this->adminFavoritesTable = 'gm_admin_favorites';
}
/**
* Callback method
*
* This method inserts a menu item to the favorites table.
*
* @throws AuthenticationException If the customer has no admin privileges.
*
* @throws InvalidArgumentException If the link_key argument from the get request is not valid.
*
* @return HttpControllerResponse 'success' if deletion was successfully else 'error'
*/
public function actionAddMenuItem()
{
if(!$this->_isAdmin())
{
throw new AuthenticationException('No admin privileges. Please contact the administrator.');
}
$successStatus = $this->_addMenuItemToFavorites();
if($successStatus)
{
return MainFactory::create('HttpControllerResponse', 'success');
}
else
{
return MainFactory::create('HttpControllerResponse', 'error');
}
}
/**
* Callback method
*
* This method deletes a menu item from the favorites table.
*
* @throws AuthenticationException If the customer has no admin privileges.
*
* @throws InvalidArgumentException If the link_key argument from the get request is not valid.
*
* @return HttpControllerResponse 'success' if deletion was successfully else 'error'
*/
public function actionRemoveMenuItem()
{
if(!$this->_isAdmin())
{
throw new AuthenticationException('No admin privileges. Please contact the administrator.');
}
$successStatus = $this->_removeMenuItemFromFavorites();
if($successStatus)
{
return MainFactory::create('HttpControllerResponse', 'success');
}
else
{
return MainFactory::create('HttpControllerResponse', 'error');
}
}
/**
* Callback method for the default action
*
* @return HttpControllerResponse
*/
public function actionDefault()
{
return MainFactory::create('HttpControllerResponse', array());
}
/**
* Check if the customer is the admin.
*
* @return bool Is the customer the admin?
*/
protected function _isAdmin()
{
try
{
$this->validateCurrentAdminStatus();
return true;
}
catch(LogicException $exception)
{
return false;
}
}
/**
* Adds an entry to the favorites table.
*
* @throws InvalidArgumentException If the link key is not valid.
*
* @return bool true on success, else false will be returned.
*/
protected function _addMenuItemToFavorites()
{
$conditions = array('customers_id' => $this->customerId, 'link_key' => $this->linkKey);
try
{
// We have to delete a possible item first in order to prevent duplication.
// We cannot use a REPLACE INTO statement, because the table has no primary key.
$this->_removeMenuItemFromFavorites();
$this->db->insert($this->adminFavoritesTable, $conditions);
// Success
return true;
}
catch(Exception $e)
{
// Error
return false;
}
}
/**
* Removes an entry from the favorites database table.
*
* @throws InvalidArgumentException If the link key is not valid.
*
* @return bool true on success, else false will be returned.
*/
protected function _removeMenuItemFromFavorites()
{
$conditions = array('link_key' => $this->linkKey, 'customers_id' => $this->customerId);
try
{
$this->db->delete($this->adminFavoritesTable, $conditions);
// Success
return true;
}
catch(Exception $e)
{
// Error
return false;
}
}
}