/contentmanager/code/trunk/administrator/components/com_contentmanager/models/setup.php
PHP | 341 lines | 201 code | 50 blank | 90 comment | 24 complexity | 4e9ddc9cb22aadbfd9251dd40bb3a4f7 MD5 | raw file
- <?php
- /**
- * @version $Id: setup.php 160 2009-07-09 00:06:09Z eddieajau $
- * @copyright Copyright (C) 2009 New Life in IT Pty Ltd. All rights reserved.
- * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
- * @link http://www.theartofjoomla.com
- */
- // no direct access
- defined('_JEXEC') or die;
- // Include dependancies
- jimport('joomla.application.component.model');
- jimport('joomla.filesystem.file');
- /**
- * Setup Model
- *
- * @package TAOJ.ContentManager
- * @subpackage com_contentmanager
- */
- class ContentManagerModelSetup extends JModel
- {
- /**
- * Method to enable the JXtended Libraries plugin.
- *
- * @return boolean True on success.
- */
- public function enableLibraries()
- {
- // Check if the plugin file is present.
- if (!file_exists(JPATH_ROOT.'/plugins/system/jxtended.php')) {
- $this->setError(JText::_('JX_Libraries_Missing'));
- return false;
- }
- // Get the database object.
- $db = &$this->_db;
- // Get the plugin information from the database.
- $db->setQuery(
- 'SELECT `id`, `published`' .
- ' FROM `#__plugins`' .
- ' WHERE `folder` = "system"' .
- ' AND `element` = "jxtended"'
- );
- $plugin = $db->loadObject();
- // Check for a database error.
- if ($db->getErrorNum()) {
- $this->setError($db->getErrorMsg());
- return false;
- }
- // Check to see if the plugin is installed.
- if (empty($plugin))
- {
- // Create a plugin row to for installation.
- $row = & JTable::getInstance('plugin');
- $row->name = JText::_('System - JXtended');
- $row->ordering = 0;
- $row->folder = 'system';
- $row->iscore = 0;
- $row->access = 0;
- $row->client_id = 0;
- $row->element = 'jxtended';
- $row->published = 1;
- $row->params = '';
- // Attempt to install the plugin.
- if (!$row->store())
- {
- // Install failed, set the error and return false.
- $this->setError(JText::_('JX_Unable_To_Install_Plugin'));
- return false;
- }
- return true;
- }
- // Check to see if the plugin is published.
- if (!$plugin->published)
- {
- // Attempt to publish the plugin.
- $db->setQuery(
- 'UPDATE `#__plugins`' .
- ' SET `published` = 1' .
- ' WHERE `folder` = "system"' .
- ' AND `element` = "jxtended"'
- );
- $db->query();
- // Check for a database error.
- if ($db->getErrorNum()) {
- $this->setError($db->getErrorMsg());
- return false;
- }
- }
- return true;
- }
- /**
- * Method to manually install the extension
- *
- * @return boolean True on success.
- */
- public function install()
- {
- // Get the current component version information.
- $version = new ContentManagerVersion();
- $current = $version->version.'.'.$version->subversion.$version->status;
- // Get the database object.
- $db = &$this->_db;
- // Get the number of relevant rows in the components table.
- $db->setQuery(
- 'SELECT COUNT(id)' .
- ' FROM `#__components`' .
- ' WHERE `option` = "com_contentmanager"'
- );
- $installed = $db->loadResult();
- // Check for a database error.
- if ($db->getErrorNum()) {
- $this->setError($db->getErrorMsg());
- return false;
- }
- // Check to see if the component is installed.
- if ($installed > 0) {
- $this->setError(JText::_('TAOJ_Setup_Already_Installed'));
- return false;
- }
- // Attempt to add the necessary rows to the components table.
- $db->setQuery(
- 'INSERT INTO `#__components` VALUES' .
- ' (0, "Content Manager", "option=com_contentmanager", 0, 0, "option=com_contentmanager", "Content Manager", "com_contentmanager", 0, "components/com_contentmanager/media/images/taoj_logo16x16.png", 0, "", 1)'
- );
- $db->query();
- // Check for a database error.
- if ($db->getErrorNum()) {
- $this->setError($db->getErrorMsg());
- return false;
- }
- // Verify the schema file.
- $file = JPATH_ADMINISTRATOR.'/components/com_contentmanager/install/installsql.mysql.utf8.php';
- if (!JFile::exists($file)) {
- $this->setError(JText::_('TAOJ_Setup_Schema_File_Missing'));
- return false;
- }
- // Set the SQL from the schema file.
- $db->setQuery(JFile::read($file));
- // Attempt to import the component schema.
- $return = $db->queryBatch(false);
- // Check for a database error.
- if ($db->getErrorNum()) {
- $this->setError($db->getErrorMsg());
- return false;
- }
- // Attempt to insert the manual install entry into the component version table.
- $db->setQuery(
- 'INSERT IGNORE INTO `#__taoj` (`extension`,`version`,`log`)' .
- ' VALUES ('.$db->quote('com_contentmanager').','.$db->Quote($current).', '.$db->Quote('TAOJ_Setup_Manual_Install').')'
- );
- $db->query();
- // Check for a database error.
- if ($db->getErrorNum()) {
- $this->setError($db->getErrorMsg());
- return false;
- }
- return true;
- }
- /**
- * Method to run necessary database upgrade scripts
- *
- * @return boolean True on success.
- */
- public function upgrade()
- {
- // Get the component upgrade information.
- $version = new ContentManagerVersion();
- $upgrades = $version->getUpgrades();
- // If there are upgrades to process, attempt to process them.
- if (is_array($upgrades) && count($upgrades))
- {
- // Sort the upgrades, lowest version first.
- uksort($upgrades, 'version_compare');
- // Get the database object.
- $db = &$this->_db;
- // Get the number of relevant rows in the components table.
- $db->setQuery(
- 'SELECT COUNT(id)' .
- ' FROM `#__components`' .
- ' WHERE `option` = "com_contentmanager"'
- );
- $installed = $db->loadResult();
- // Check for a database error.
- if ($db->getErrorNum()) {
- $this->setError($db->getErrorMsg());
- return false;
- }
- // Check to see if the component is installed.
- if ($installed < 1) {
- $this->setError(JText::_('TAOJ_Setup_Not_Installed'));
- return false;
- }
- foreach ($upgrades as $upgradeVersion => $file)
- {
- $file = JPATH_COMPONENT.DS.'install'.DS.$file;
- if (JFile::exists($file))
- {
- // Set the upgrade SQL from the file.
- $db->setQuery(JFile::read($file));
- // Execute the upgrade SQL.
- $return = $db->queryBatch(false);
- // Check for a database error.
- if ($db->getErrorNum()) {
- $this->setError(JText::sprintf('TAOJ_Setup_Database_Upgrade_Failed', $db->getErrorMsg()));
- return false;
- }
- // Upgrade was successful, attempt to log it to the versions table.
- $db->setQuery(
- 'INSERT INTO `#__taoj` (`extension`,`version`,`log`) VALUES' .
- ' ('.$db->quote('com_contentmanager').','.$db->Quote($upgradeVersion).', '.$db->Quote(JText::sprintf('TAOJ_Setup_Database_Upgrade_Version', $upgradeVersion)).')'
- );
- $db->query();
- // Check for a database error.
- if ($db->getErrorNum()) {
- $this->setError(JText::sprintf('TAOJ_Setup_Database_Upgrade_Failed', $db->getErrorMsg()));
- return false;
- }
- }
- }
- }
- return true;
- }
- /**
- * Initialiases the ACL
- */
- public function initAcl()
- {
- // Require the Acl API class
- require_once dirname(dirname(__FILE__)).DS.'libraries'.DS.'jxtended.php';
- jximport2('jxtended.acl.acladmin');
- // We must check for, and possibly create, the zero-user (Public, ARO value = 0)
- // otherwise Public access won't work properly
- if (!JxAclAdmin::getUser(0))
- {
- // Create the User
- $userId = JxAclAdmin::registerUser('Public User', 0);
- if (JError::isError($userId)) {
- //return JError::raiseWarning(500, $result->getMessage());
- }
- // Now we need to map that user to the Public Group
- if ($group = JxAclAdmin::getGroupForUsers('Public Frontend')) {
- $result = JxAclAdmin::registerUserInGroups($userId, $group->id);
- if (JError::isError($result)) {
- $this->setError($result->getError());
- return false;
- }
- }
- else {
- $this->setError(JText::_('Cannot find the public users group'));
- return false;
- }
- }
- // Register the sections
- // @todo Think about not throwing an error if the section exists??
- JxAclAdmin::registerSectionForRules('ContentManager', 'com_contentmanager');
- JxAclAdmin::registerSectionForActions('ContentManager', 'com_contentmanager');
- JxAclAdmin::registerSectionForAssets('ContentManager', 'com_contentmanager');
- // Register Actions
- // Type 1 - A user in a group can do "this"
- JxAclAdmin::registerAction(1, 'com_contentmanager', 'Access', 'access');
- // Type 2 - A user in a group can do "this" to an asset
- JxAclAdmin::registerAction(2, 'com_contentmanager', 'Create Articles', 'create.article');
- JxAclAdmin::registerAction(2, 'com_contentmanager', 'Edit Articles', 'edit.article');
- JxAclAdmin::registerAction(2, 'com_contentmanager', 'Publish Articles', 'publish.article');
- JxAclAdmin::registerAction(2, 'com_contentmanager', 'Trash Articles', 'trash.article');
- JxAclAdmin::registerAction(2, 'com_contentmanager', 'Edit Article Parameters', 'edit.article.parameters');
- JxAclAdmin::registerAction(2, 'com_contentmanager', 'Edit Article Author', 'edit.article.author', 'Edit the author related details for an Article.');
- JxAclAdmin::registerAction(2, 'com_contentmanager', 'Edit Article Access', 'edit.article.access', 'Edit the access level for an Article.');
- //
- // Now we are ready to add some rules
- //
- $result = JxAclAdmin::registerRule(
- // The rule type
- 1,
- // The rule section
- 'com_contentmanager',
- // The rule name
- 'com_contentmanager.access',
- // The title of the rule
- 'Content Manager - Access',
- // Applies to User Groups
- array('Administrator', 'Super Administrator'),
- // The Actions attached to the rule
- array('com_contentmanager' => array('access')),
- // Applies to Assets
- array(),
- // Applies to Asset Groups
- array()
- );
- return true;
- }
- }