diff --git a/administrator/controllers/hierarchy.php b/administrator/controllers/hierarchy.php
index 588535c..679afee 100755
--- a/administrator/controllers/hierarchy.php
+++ b/administrator/controllers/hierarchy.php
@@ -81,7 +81,7 @@ public function save($key = null, $urlVar = null)
$data['created_by'] = $jinput->get('created_by', '', 'int');
$data['modified_by'] = $jinput->get('modified_by', '', 'int');
- // To add new or remove manager from the selected managers list.
+ // Get the existing managers of the user
$hierarchysData = $model->getReportsTo($data['user_id']);
$userIDs = array();
@@ -91,9 +91,10 @@ public function save($key = null, $urlVar = null)
$userIDs[] = $hierarchy->reports_to;
}
+ // Get the list of removed managers for the user
$deleteUser = array_diff($userIDs, $data['reports_to']);
- // Delete user from the existing list
+ // Delete records for removed managers for the user
foreach ($deleteUser as $key => $val)
{
$this->hierarchyTableObj->load(array('reports_to' => (int) $val, 'user_id' => (int) $data['user_id']));
@@ -101,6 +102,7 @@ public function save($key = null, $urlVar = null)
$return = $model->delete($id);
}
+ // Save the records for new managers of the user
foreach ($data['reports_to'] as $key => $val)
{
$data['reports_to'] = $val;
diff --git a/administrator/controllers/hierarchys.php b/administrator/controllers/hierarchys.php
index 9e3bcc4..364d59f 100755
--- a/administrator/controllers/hierarchys.php
+++ b/administrator/controllers/hierarchys.php
@@ -133,7 +133,7 @@ public function csvImport()
* Get user id from user email
*
* @param string $email email.
- *
+ *
* @return void
*
* @since 1.0
@@ -159,12 +159,27 @@ public function getUserId($email)
*/
public function remove()
{
- $model = $this->getModel('hierarchys');
+ $model = $this->getModel();
$input = JFactory::getApplication()->input;
$post = $input->post;
- $hierarchyID = $post->get('cid', '', 'ARRAY');
+ $userIds = $post->get('cid', '', 'ARRAY');
+
+ $records = array();
+
+ foreach ($userIds as $userId)
+ {
+ $hierarchyData = $model->getReportsTo($userId);
+ $records = array_merge($records, $hierarchyData);
+ }
+
+ $hierarchyIds = array();
+
+ foreach ($records as $record)
+ {
+ $hierarchyIds[] = $record->id;
+ }
- if ($model->delete($hierarchyID))
+ if ($model->delete($hierarchyIds))
{
$msg = JText::_('COM_HIERARCHY_HIERARCHY_DELETED_SCUSS');
}
diff --git a/administrator/models/hierarchy.php b/administrator/models/hierarchy.php
index 136f241..d0b0f25 100755
--- a/administrator/models/hierarchy.php
+++ b/administrator/models/hierarchy.php
@@ -169,10 +169,24 @@ public function save($data)
$data['created_date'] = $date->toSql(true);
}
+ $isNew = empty($data['id']) ? false : true;
+
+ // On before assigning manager
+ $dispatcher = JDispatcher::getInstance();
+ JPluginHelper::importPlugin("system");
+ JPluginHelper::importPlugin("actionlog");
+ $dispatcher->trigger("hierarchyOnBeforeSaveHierarchy", array($data, $isNew));
+
if (parent::save($data))
{
$id = (int) $this->getState($this->getName() . '.id');
+ // On after assigning manager
+ $dispatcher = JDispatcher::getInstance();
+ JPluginHelper::importPlugin("system");
+ JPluginHelper::importPlugin("actionlog");
+ $dispatcher->trigger("hierarchyOnAfterSaveHierarchy", array($data, $isNew));
+
return $id;
}
@@ -313,9 +327,9 @@ public function getReportsTo($reportsTo)
return false;
}
- $db = JFactory::getDBO();
+ $db = JFactory::getDbo();
$query = $db->getQuery(true);
- $query->select($db->quoteName(array('user_id','reports_to','name','username','email')));
+ $query->select($db->quoteName(array('hu.id', 'user_id','reports_to','name','username','email')));
$query->from($db->quoteName('#__hierarchy_users', 'hu'));
$query->join('INNER', $db->quoteName('#__users', 'u') . ' ON (' . $db->quoteName('u.id') . ' = ' . $db->quoteName('hu.reports_to') . ')');
$query->where($db->quoteName('hu.user_id') . " = " . $db->quote($reportsTo));
@@ -326,7 +340,7 @@ public function getReportsTo($reportsTo)
}
/**
- * Method to get users to manage hierarchy.
+ * Method to get users to manager hierarchy.
*
* @param integer $userId userId
*
@@ -363,4 +377,77 @@ public function getAutoSuggestUsers($userId)
return $allUsers;
}
+
+ /**
+ * Method to delete a row from the database table by primary key value.
+ *
+ * @param array $items An array of primary key value to delete.
+ *
+ * @return int Returns count of success
+ */
+ public function delete($items)
+ {
+ $user = JFactory::getUser();
+ $db = JFactory::getDbo();
+
+ JTable::addIncludePath(JPATH_ROOT . '/administrator/components/com_hierarchy/tables');
+
+ if (is_array($items))
+ {
+ foreach ($items as $id)
+ {
+ $hierarchyTable = JTable::getInstance('Hierarchy', 'HierarchyTable', array('dbo', $db));
+ $hierarchyTable->load(array('id' => $id));
+
+ $data = $hierarchyTable->getProperties();
+
+ // On before removing manager
+ JPluginHelper::importPlugin("system");
+ JPluginHelper::importPlugin("actionlog");
+ $dispatcher = JDispatcher::getInstance();
+ $dispatcher->trigger("hierarchyOnBeforeDeleteHierarchy", array($data));
+
+ if ($hierarchyTable->delete($data['id']))
+ {
+ // On after removing manager
+ JPluginHelper::importPlugin("system");
+ JPluginHelper::importPlugin("actionlog");
+ $dispatcher = JDispatcher::getInstance();
+ $dispatcher->trigger("hierarchyOnAfterDeleteHierarchy", array($data));
+ }
+ else
+ {
+ return false;
+ }
+ }
+ }
+ else
+ {
+ $hierarchyTable = JTable::getInstance('Hierarchy', 'HierarchyTable', array('dbo', $db));
+ $hierarchyTable->load(array('id' => $items));
+
+ $data = $hierarchyTable->getProperties();
+
+ // On before removing manager
+ $dispatcher = JDispatcher::getInstance();
+ JPluginHelper::importPlugin("system");
+ JPluginHelper::importPlugin("actionlog");
+ $dispatcher->trigger("hierarchyOnBeforeDeleteHierarchy", array($data));
+
+ if ($hierarchyTable->delete($data['id']))
+ {
+ // On after removing manager
+ $dispatcher = JDispatcher::getInstance();
+ JPluginHelper::importPlugin("system");
+ JPluginHelper::importPlugin("actionlog");
+ $dispatcher->trigger("hierarchyOnAfterDeleteHierarchy", array($data));
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+ return true;
+ }
}
diff --git a/administrator/models/hierarchys.php b/administrator/models/hierarchys.php
index c5e5808..219cbcb 100644
--- a/administrator/models/hierarchys.php
+++ b/administrator/models/hierarchys.php
@@ -190,37 +190,4 @@ public function getItems()
return $items;
}
-
- /**
- * Delete order
- *
- * @param integer $hierarchyID id of jticketing_order table to delete
- *
- * @return void
- *
- * @since 1.0
- */
- public function delete($hierarchyID)
- {
- $id = implode(',', array_filter($hierarchyID));
-
- if ($id)
- {
- // Delete the order item
- $deleteHierarchy = $this->_db->getQuery(true);
- $deleteHierarchy->delete($this->_db->quoteName('#__hierarchy_users'));
- $deleteHierarchy->where('user_id IN (' . $id . ')');
- $this->_db->setQuery($deleteHierarchy);
- $confirm = $this->_db->execute();
-
- if ($confirm)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
}
diff --git a/administrator/views/hierarchys/tmpl/default.php b/administrator/views/hierarchys/tmpl/default.php
index 4cbdfed..e5815f9 100755
--- a/administrator/views/hierarchys/tmpl/default.php
+++ b/administrator/views/hierarchys/tmpl/default.php
@@ -128,7 +128,7 @@
client && $this->clientId)
{
$clientUrl = '&client=' . $this->client . '&client_id=' . $this->clientId;
diff --git a/plugins/actionlog/hierarchy/hierarchy.php b/plugins/actionlog/hierarchy/hierarchy.php
new file mode 100644
index 0000000..7ec5f07
--- /dev/null
+++ b/plugins/actionlog/hierarchy/hierarchy.php
@@ -0,0 +1,152 @@
+
+ * @copyright Copyright (c) 2009-2018 Techjoomla. All rights reserved.
+ * @license GNU General Public License version 2 or later.
+ */
+
+// No direct access.
+defined('_JEXEC') or die();
+
+JLoader::register('ActionlogsHelper', JPATH_ADMINISTRATOR . '/components/com_actionlogs/helpers/actionlogs.php');
+
+use Joomla\CMS\Plugin\CMSPlugin;
+use Joomla\CMS\Factory;
+use Joomla\CMS\MVC\Model\BaseDatabaseModel;
+
+/**
+ * Hierarchy Actions Logging Plugin.
+ *
+ * @since __DEPLOY_VERSION__
+ */
+class PlgActionlogHierarchy extends CMSPlugin
+{
+ /**
+ * Application object.
+ *
+ * @var JApplicationCms
+ * @since __DEPLOY_VERSION__
+ */
+ protected $app;
+
+ /**
+ * Database object.
+ *
+ * @var JDatabaseDriver
+ * @since __DEPLOY_VERSION__
+ */
+ protected $db;
+
+ /**
+ * Load plugin language file automatically so that it can be used inside component
+ *
+ * @var boolean
+ * @since __DEPLOY_VERSION__
+ */
+ protected $autoloadLanguage = true;
+
+ /**
+ * Proxy for ActionlogsModelUserlog addLog method
+ *
+ * This method adds a record to #__action_logs contains (message_language_key, message, date, context, user)
+ *
+ * @param array $messages The contents of the messages to be logged
+ * @param string $messageLanguageKey The language key of the message
+ * @param string $context The context of the content passed to the plugin
+ * @param int $userId ID of user perform the action, usually ID of current logged in user
+ *
+ * @return void
+ *
+ * @since __DEPLOY_VERSION__
+ */
+ protected function addLog($messages, $messageLanguageKey, $context, $userId = null)
+ {
+ JLoader::register('ActionlogsModelActionlog', JPATH_ADMINISTRATOR . '/components/com_actionlogs/models/actionlog.php');
+
+ /* @var ActionlogsModelActionlog $model */
+ $model = BaseDatabaseModel::getInstance('Actionlog', 'ActionlogsModel');
+ $model->addLog($messages, $messageLanguageKey, $context, $userId);
+ }
+
+ /**
+ * On after manager assigned to the user
+ *
+ * @param ARRAY $data hierarchy data
+ * @param BOOLEAN $isNew flag for new hierarchy
+ *
+ * @return void
+ *
+ * @since __DEPLOY_VERSION__
+ */
+ public function hierarchyOnAfterSaveHierarchy($data, $isNew)
+ {
+ if (!$this->params->get('logActionForAssigningManager', 1) || !$isNew)
+ {
+ return;
+ }
+
+ $option = $this->app->input->getCmd('option');
+ $actor = Factory::getUser();
+ $manager = Factory::getUser($data['reports_to']);
+ $user = Factory::getUser($data['user_id']);
+ $action = 'assignmanager';
+ $userId = $actor->id;
+ $userName = $actor->username;
+
+ $messageLanguageKey = 'PLG_ACTIONLOG_HIERARCHY_ASSIGN_MANAGER';
+
+ $message = array(
+ 'action' => $action,
+ 'actorAccountLink' => 'index.php?option=com_users&task=user.edit&id=' . $userId,
+ 'actorName' => $userName,
+ 'managerForUser' => $manager->username,
+ 'managerForUserAccountLink' => 'index.php?option=com_users&task=user.edit&id=' . $manager->id,
+ 'user' => $user->username,
+ 'userAccountLink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id
+ );
+
+ $this->addLog(array($message), $messageLanguageKey, $option, $userId);
+ }
+
+ /**
+ * On after manager removed for the user
+ *
+ * @param ARRAY $data hierarchy data
+ *
+ * @return void
+ *
+ * @since __DEPLOY_VERSION__
+ */
+ public function hierarchyOnAfterDeleteHierarchy($data)
+ {
+ if (!$this->params->get('logActionForRemoveManager', 1) || empty($data['id']))
+ {
+ return;
+ }
+
+ $option = $this->app->input->getCmd('option');
+ $actor = Factory::getUser();
+ $manager = Factory::getUser($data['reports_to']);
+ $user = Factory::getUser($data['user_id']);
+ $action = 'assignmanager';
+ $userId = $actor->id;
+ $userName = $actor->username;
+
+ $messageLanguageKey = 'PLG_ACTIONLOG_HIERARCHY_REMOVE_MANAGER';
+
+ $message = array(
+ 'action' => $action,
+ 'actorAccountLink' => 'index.php?option=com_users&task=user.edit&id=' . $userId,
+ 'actorName' => $userName,
+ 'managerForUser' => $manager->username,
+ 'managerForUserAccountLink' => 'index.php?option=com_users&task=user.edit&id=' . $manager->id,
+ 'user' => $user->username,
+ 'userAccountLink' => 'index.php?option=com_users&task=user.edit&id=' . $user->id
+ );
+
+ $this->addLog(array($message), $messageLanguageKey, $option, $userId);
+ }
+}
diff --git a/plugins/actionlog/hierarchy/hierarchy.xml b/plugins/actionlog/hierarchy/hierarchy.xml
new file mode 100644
index 0000000..46a76fd
--- /dev/null
+++ b/plugins/actionlog/hierarchy/hierarchy.xml
@@ -0,0 +1,34 @@
+
+