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 @@ + + + plg_actionlog_hierarchy + __DEPLOY_VERSION__ + __RELEASE_DATE__ + Techjoomla + extensions@techjoomla.com + https://techjoomla.com + Copyright (c) 2009-2018 Techjoomla. All rights reserved. + http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL + PLG_ACTIONLOG_HIERARCHY_XML_DESCRIPTION + + hierarchy.php + index.html + + + en-GB/en-GB.plg_actionlog_hierarchy.ini + en-GB/en-GB.plg_actionlog_hierarchy.sys.ini + + + +
+ + + + + + + + +
+
+
+
diff --git a/plugins/actionlog/hierarchy/index.html b/plugins/actionlog/hierarchy/index.html new file mode 100644 index 0000000..2efb97f --- /dev/null +++ b/plugins/actionlog/hierarchy/index.html @@ -0,0 +1 @@ + diff --git a/plugins/actionlog/hierarchy/language/en-GB/en-GB.plg_actionlog_hierarchy.ini b/plugins/actionlog/hierarchy/language/en-GB/en-GB.plg_actionlog_hierarchy.ini new file mode 100644 index 0000000..3a73bef --- /dev/null +++ b/plugins/actionlog/hierarchy/language/en-GB/en-GB.plg_actionlog_hierarchy.ini @@ -0,0 +1,18 @@ +; @package Hierarchy +; @subpackage Plg_Actionlog_Hierarchy +; @copyright Copyright © 2009-2018 TechJoomla. All rights reserved. +; @license GNU General Public License version 2, or later +; Note: All ini files need to be saved as UTF-8 + +PLG_ACTIONLOG_HIERARCHY="Actionlog - Hierarchy" +PLG_ACTIONLOG_HIERARCHY_XML_DESCRIPTION="Hierarchy actionlog plugin" + +; params +PLG_ACTIONLOG_HIERARCHY_LOG_ACTION_ASSIGN_MANAGER_LBL="Log action for assigning a manager to a user?" +PLG_ACTIONLOG_HIERARCHY_LOG_ACTION_ASSIGN_MANAGER_DESC="Log action when a manager is assigned to a user" +PLG_ACTIONLOG_HIERARCHY_LOG_ACTION_REMOVE_MANAGER_LBL="Log action for removing a manager of the user?" +PLG_ACTIONLOG_HIERARCHY_LOG_ACTION_REMOVE_MANAGER_DESC="Log action when a manager of user is removed" + +; Event actions +PLG_ACTIONLOG_HIERARCHY_ASSIGN_MANAGER="User {actorName} assigned {managerForUser} as manager to {user}" +PLG_ACTIONLOG_HIERARCHY_REMOVE_MANAGER="User {actorName} removed {managerForUser} as manager to {user}" diff --git a/plugins/actionlog/hierarchy/language/en-GB/en-GB.plg_actionlog_hierarchy.sys.ini b/plugins/actionlog/hierarchy/language/en-GB/en-GB.plg_actionlog_hierarchy.sys.ini new file mode 100644 index 0000000..23919e2 --- /dev/null +++ b/plugins/actionlog/hierarchy/language/en-GB/en-GB.plg_actionlog_hierarchy.sys.ini @@ -0,0 +1,8 @@ +; @package Hierarchy +; @subpackage Plg_Actionlog_Hierarchy +; @copyright Copyright © 2009-2018 TechJoomla. All rights reserved. +; @license GNU General Public License version 2, or later +; Note: All ini files need to be saved as UTF-8 + +PLG_ACTIONLOG_HIERARCHY="Actionlog - Hierarchy" +PLG_ACTIONLOG_HIERARCHY_XML_DESCRIPTION="Hierarchy actionlog plugin" diff --git a/plugins/privacy/hierarchy/hierarchy.php b/plugins/privacy/hierarchy/hierarchy.php new file mode 100644 index 0000000..3694374 --- /dev/null +++ b/plugins/privacy/hierarchy/hierarchy.php @@ -0,0 +1,169 @@ + + * @copyright Copyright (C) 2009 - 2018 Techjoomla. All rights reserved. + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +// No direct access. +defined('_JEXEC') or die(); + +JLoader::register('PrivacyPlugin', JPATH_ADMINISTRATOR . '/components/com_privacy/helpers/plugin.php'); +JLoader::register('PrivacyRemovalStatus', JPATH_ADMINISTRATOR . '/components/com_privacy/helpers/removal/status.php'); + +use Joomla\CMS\User\User; +use Joomla\CMS\Factory; +use Joomla\CMS\Language\Text; +use Joomla\CMS\MVC\Model\BaseDatabaseModel; + +/** + * Hierarchy Privacy Plugin. + * + * @since __DEPLOY_VERSION__ + */ +class PlgPrivacyHierarchy extends PrivacyPlugin +{ + /** + * Load the language file on instantiation. + * + * @var boolean + * + * @since __DEPLOY_VERSION__ + */ + protected $autoloadLanguage = true; + + /** + * Database object + * + * @var JDatabaseDriver + * @since __DEPLOY_VERSION__ + */ + protected $db; + + /** + * Processes an export request for Hierarchy user data + * + * This event will collect data for the following tables: + * + * - #__hierarchy_users + * + * @param PrivacyTableRequest $request The request record being processed + * @param JUser $user The user account associated with this request if available + * + * @return PrivacyExportDomain[] + * + * @since __DEPLOY_VERSION__ + */ + public function onPrivacyExportRequest(PrivacyTableRequest $request, JUser $user = null) + { + if (!$user) + { + return array(); + } + + /** @var JTableUser $user */ + $userTable = User::getTable(); + $userTable->load($user->id); + + $domains = array(); + $domains[] = $this->createHierarchyUsers($userTable); + + return $domains; + } + + /** + * Create the domain for the Hierarchy user role + * + * @param JTableUser $user The JTableUser object to process + * + * @return PrivacyExportDomain + * + * @since __DEPLOY_VERSION__ + */ + private function createHierarchyUsers(JTableUser $user) + { + $domain = $this->createDomain('Hierarchy Users', 'Users hierarchy details'); + + $query = $this->db->getQuery(true); + $query->select($this->db->quoteName(array('id', 'user_id', 'reports_to', 'context', 'context_id', 'created_by', 'modified_by'))); + $query->from($this->db->quoteName('#__hierarchy_users')); + $query->where('(' . $this->db->qn('user_id') . '=' . $user->id . ' OR ' . $this->db->qn('reports_to') . + '=' . $user->id . ' OR ' . $this->db->qn('created_by') . '=' . $user->id . ' OR ' . + $this->db->qn('modified_by') . '=' . $user->id . ')' + ); + + $list = $this->db->setQuery($query)->loadAssocList(); + + if (!empty($list)) + { + foreach ($list as $data) + { + $domain->addItem($this->createItemFromArray($data, $data['id'])); + } + } + + return $domain; + } + + /** + * Performs validation to determine if the data associated with a remove information request can be processed + * + * This event will not allow a super user account to be removed + * + * @param PrivacyTableRequest $request The request record being processed + * @param JUser $user The user account associated with this request if available + * + * @return PrivacyRemovalStatus + * + * @since __DEPLOY_VERSION__ + */ + public function onPrivacyCanRemoveData(PrivacyTableRequest $request, JUser $user = null) + { + $status = new PrivacyRemovalStatus; + + if (!$user->id) + { + return $status; + } + } + + /** + * Removes the data associated with a remove information request + * + * This event will pseudoanonymise the user account + * + * @param PrivacyTableRequest $request The request record being processed + * @param JUser $user The user account associated with this request if available + * + * @return void + * + * @since __DEPLOY_VERSION__ + */ + public function onPrivacyRemoveData(PrivacyTableRequest $request, JUser $user = null) + { + // This plugin only processes data for registered user accounts + if (!$user) + { + return; + } + + // If there was an error loading the user do nothing here + if ($user->guest) + { + return; + } + + $db = $this->db; + + // 1. Delete data from #__kart_customer_address + $query = $db->getQuery(true) + ->delete($db->quoteName('#__hierarchy_users')) + ->where('(' . $db->qn('user_id') . '=' . $user->id . ' OR ' . $db->qn('reports_to') . '=' . $user->id . ')'); + + $db->setQuery($query); + $db->execute(); + } +} diff --git a/plugins/privacy/hierarchy/hierarchy.xml b/plugins/privacy/hierarchy/hierarchy.xml new file mode 100644 index 0000000..a282fd8 --- /dev/null +++ b/plugins/privacy/hierarchy/hierarchy.xml @@ -0,0 +1,25 @@ + + + plg_privacy_hierarchy + __DEPLOY_VERSION__ + __RELEASE_DATE__ + Techjoomla + extensions@techjoomla.com + https://techjoomla.com + Copyright (c) 2009-2018 Techjoomla. All rights reserved. + http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL + PLG_PRIVACY_HIERARCHY_XML_DESCRIPTION + + hierarchy.php + index.html + + + en-GB/en-GB.plg_privacy_hierarchy.ini + en-GB/en-GB.plg_privacy_hierarchy.sys.ini + + + +
+ + + diff --git a/plugins/privacy/hierarchy/index.html b/plugins/privacy/hierarchy/index.html new file mode 100644 index 0000000..2efb97f --- /dev/null +++ b/plugins/privacy/hierarchy/index.html @@ -0,0 +1 @@ + diff --git a/plugins/privacy/hierarchy/language/en-GB/en-GB.plg_privacy_hierarchy.ini b/plugins/privacy/hierarchy/language/en-GB/en-GB.plg_privacy_hierarchy.ini new file mode 100644 index 0000000..2b3cc38 --- /dev/null +++ b/plugins/privacy/hierarchy/language/en-GB/en-GB.plg_privacy_hierarchy.ini @@ -0,0 +1,8 @@ +; @package Hierarchy +; @subpackage Plg_Privacy_Hierarchy +; @copyright Copyright © 2009-2018 TechJoomla. All rights reserved. +; @license GNU General Public License version 2, or later +; Note: All ini files need to be saved as UTF-8 + +PLG_PRIVACY_HIERARCHY="Privacy - Hierarchy" +PLG_PRIVACY_HIERARCHY_XML_DESCRIPTION="Hierarchy privacy plugin" diff --git a/plugins/privacy/hierarchy/language/en-GB/en-GB.plg_privacy_hierarchy.sys.ini b/plugins/privacy/hierarchy/language/en-GB/en-GB.plg_privacy_hierarchy.sys.ini new file mode 100644 index 0000000..2b3cc38 --- /dev/null +++ b/plugins/privacy/hierarchy/language/en-GB/en-GB.plg_privacy_hierarchy.sys.ini @@ -0,0 +1,8 @@ +; @package Hierarchy +; @subpackage Plg_Privacy_Hierarchy +; @copyright Copyright © 2009-2018 TechJoomla. All rights reserved. +; @license GNU General Public License version 2, or later +; Note: All ini files need to be saved as UTF-8 + +PLG_PRIVACY_HIERARCHY="Privacy - Hierarchy" +PLG_PRIVACY_HIERARCHY_XML_DESCRIPTION="Hierarchy privacy plugin" diff --git a/script.hierarchy.php b/script.hierarchy.php index 2bf06ff..cea054e 100755 --- a/script.hierarchy.php +++ b/script.hierarchy.php @@ -26,6 +26,28 @@ class Com_HierarchyInstallerScript /** @var array The list of extra modules and plugins to install */ private $oldversion = ""; + private $installation_queue = array( + 'plugins'=>array( + 'privacy'=>array( + 'hierarchy'=>1, + ), + 'actionlog'=>array( + 'hierarchy'=>1, + ) + ) + ); + + private $uninstall_queue = array( + 'plugins'=>array( + 'privacy'=>array( + 'hierarchy'=>1 + ), + 'actionlog'=>array( + 'hierarchy'=>1 + ) + ) + ); + /** * method to run before an install/update/uninstall method * @@ -111,4 +133,156 @@ public function installSqlFiles($parent) } } } + + /** + * Runs after install, update or discover_update + * + * @param string $type install, update or discover_update + * @param JInstaller $parent + * + */ + public function postflight($type, $parent) + { + // Install subextensions + $status = $this->_installSubextensions($parent); + } + + /** + * Installs subextensions (modules, plugins) bundled with the main extension + * + * @param JInstaller $parent + * @return JObject The subextension installation status + */ + private function _installSubextensions($parent) + { + $src = $parent->getParent()->getPath('source'); + $db = JFactory::getDbo(); + + $status = new JObject(); + $status->modules = array(); + + // Plugins installation + if (count($this->installation_queue['plugins'])) + { + foreach ($this->installation_queue['plugins'] as $folder => $plugins) + { + if (count($plugins)) + { + foreach ($plugins as $plugin => $published) + { + $path = "$src/plugins/$folder/$plugin"; + + if (!is_dir($path)) + { + $path = "$src/plugins/$folder/plg_$plugin"; + } + + if (!is_dir($path)) + { + $path = "$src/plugins/$plugin"; + } + + if (!is_dir($path)) + { + $path = "$src/plugins/plg_$plugin"; + } + + if (!is_dir($path)) continue; + + // Was the plugin already installed? + $query = $db->getQuery(true) + ->select('COUNT(*)') + ->from($db->qn('#__extensions')) + ->where('( ' . ($db->qn('name') . ' = ' . $db->q($plugin)) . ' OR ' . ($db->qn('element') . ' = ' . $db->q($plugin)) . ' )') + ->where($db->qn('folder') . ' = ' . $db->q($folder)); + $db->setQuery($query); + $count = $db->loadResult(); + + $installer = new JInstaller; + $result = $installer->install($path); + + $status->plugins[] = array('name'=>$plugin, 'group'=>$folder, 'result'=>$result, 'status'=>$published); + + if ($published && !$count) + { + $query = $db->getQuery(true) + ->update($db->qn('#__extensions')) + ->set($db->qn('enabled') . ' = ' . $db->q('1')) + ->where('( ' . ($db->qn('name') . ' = ' . $db->q($plugin)) . ' OR ' . ($db->qn('element') . ' = ' . $db->q($plugin)) . ' )') + ->where($db->qn('folder') . ' = ' . $db->q($folder)); + $db->setQuery($query); + $db->execute(); + } + } + } + } + } + + return $status; + } + + /** + * Runs on uninstallation + * + * @param JInstaller $parent + */ + function uninstall($parent) + { + // Uninstall subextensions + $status = $this->_uninstallSubextensions($parent); + } + + /** + * Uninstalls subextensions (modules, plugins) bundled with the main extension + * + * @param JInstaller $parent + * @return JObject The subextension uninstallation status + */ + private function _uninstallSubextensions($parent) + { + jimport('joomla.installer.installer'); + + $db = JFactory::getDbo(); + + $status = new JObject(); + $status->modules = array(); + $status->plugins = array(); + + $src = $parent->getParent()->getPath('source'); + + // Plugins uninstallation + if (count($this->uninstall_queue['plugins'])) + { + foreach ($this->uninstall_queue['plugins'] as $folder => $plugins) + { + if (count($plugins)) + { + foreach ($plugins as $plugin => $published) + { + $sql = $db->getQuery(true) + ->select($db->qn('extension_id')) + ->from($db->qn('#__extensions')) + ->where($db->qn('type') . ' = ' . $db->q('plugin')) + ->where($db->qn('element') . ' = ' . $db->q($plugin)) + ->where($db->qn('folder') . ' = ' . $db->q($folder)); + $db->setQuery($sql); + $id = $db->loadResult(); + + if ($id) + { + $installer = new JInstaller; + $result = $installer->uninstall('plugin', $id); + $status->plugins[] = array( + 'name'=>'plg_' . $plugin, + 'group'=>$folder, + 'result'=>$result + ); + } + } + } + } + } + + return $status; + } }