PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/contentmanager/code/trunk/administrator/components/com_contentmanager/models/setup.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 341 lines | 201 code | 50 blank | 90 comment | 24 complexity | 4e9ddc9cb22aadbfd9251dd40bb3a4f7 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: setup.php 160 2009-07-09 00:06:09Z eddieajau $
  4. * @copyright Copyright (C) 2009 New Life in IT Pty Ltd. All rights reserved.
  5. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  6. * @link http://www.theartofjoomla.com
  7. */
  8. // no direct access
  9. defined('_JEXEC') or die;
  10. // Include dependancies
  11. jimport('joomla.application.component.model');
  12. jimport('joomla.filesystem.file');
  13. /**
  14. * Setup Model
  15. *
  16. * @package TAOJ.ContentManager
  17. * @subpackage com_contentmanager
  18. */
  19. class ContentManagerModelSetup extends JModel
  20. {
  21. /**
  22. * Method to enable the JXtended Libraries plugin.
  23. *
  24. * @return boolean True on success.
  25. */
  26. public function enableLibraries()
  27. {
  28. // Check if the plugin file is present.
  29. if (!file_exists(JPATH_ROOT.'/plugins/system/jxtended.php')) {
  30. $this->setError(JText::_('JX_Libraries_Missing'));
  31. return false;
  32. }
  33. // Get the database object.
  34. $db = &$this->_db;
  35. // Get the plugin information from the database.
  36. $db->setQuery(
  37. 'SELECT `id`, `published`' .
  38. ' FROM `#__plugins`' .
  39. ' WHERE `folder` = "system"' .
  40. ' AND `element` = "jxtended"'
  41. );
  42. $plugin = $db->loadObject();
  43. // Check for a database error.
  44. if ($db->getErrorNum()) {
  45. $this->setError($db->getErrorMsg());
  46. return false;
  47. }
  48. // Check to see if the plugin is installed.
  49. if (empty($plugin))
  50. {
  51. // Create a plugin row to for installation.
  52. $row = & JTable::getInstance('plugin');
  53. $row->name = JText::_('System - JXtended');
  54. $row->ordering = 0;
  55. $row->folder = 'system';
  56. $row->iscore = 0;
  57. $row->access = 0;
  58. $row->client_id = 0;
  59. $row->element = 'jxtended';
  60. $row->published = 1;
  61. $row->params = '';
  62. // Attempt to install the plugin.
  63. if (!$row->store())
  64. {
  65. // Install failed, set the error and return false.
  66. $this->setError(JText::_('JX_Unable_To_Install_Plugin'));
  67. return false;
  68. }
  69. return true;
  70. }
  71. // Check to see if the plugin is published.
  72. if (!$plugin->published)
  73. {
  74. // Attempt to publish the plugin.
  75. $db->setQuery(
  76. 'UPDATE `#__plugins`' .
  77. ' SET `published` = 1' .
  78. ' WHERE `folder` = "system"' .
  79. ' AND `element` = "jxtended"'
  80. );
  81. $db->query();
  82. // Check for a database error.
  83. if ($db->getErrorNum()) {
  84. $this->setError($db->getErrorMsg());
  85. return false;
  86. }
  87. }
  88. return true;
  89. }
  90. /**
  91. * Method to manually install the extension
  92. *
  93. * @return boolean True on success.
  94. */
  95. public function install()
  96. {
  97. // Get the current component version information.
  98. $version = new ContentManagerVersion();
  99. $current = $version->version.'.'.$version->subversion.$version->status;
  100. // Get the database object.
  101. $db = &$this->_db;
  102. // Get the number of relevant rows in the components table.
  103. $db->setQuery(
  104. 'SELECT COUNT(id)' .
  105. ' FROM `#__components`' .
  106. ' WHERE `option` = "com_contentmanager"'
  107. );
  108. $installed = $db->loadResult();
  109. // Check for a database error.
  110. if ($db->getErrorNum()) {
  111. $this->setError($db->getErrorMsg());
  112. return false;
  113. }
  114. // Check to see if the component is installed.
  115. if ($installed > 0) {
  116. $this->setError(JText::_('TAOJ_Setup_Already_Installed'));
  117. return false;
  118. }
  119. // Attempt to add the necessary rows to the components table.
  120. $db->setQuery(
  121. 'INSERT INTO `#__components` VALUES' .
  122. ' (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)'
  123. );
  124. $db->query();
  125. // Check for a database error.
  126. if ($db->getErrorNum()) {
  127. $this->setError($db->getErrorMsg());
  128. return false;
  129. }
  130. // Verify the schema file.
  131. $file = JPATH_ADMINISTRATOR.'/components/com_contentmanager/install/installsql.mysql.utf8.php';
  132. if (!JFile::exists($file)) {
  133. $this->setError(JText::_('TAOJ_Setup_Schema_File_Missing'));
  134. return false;
  135. }
  136. // Set the SQL from the schema file.
  137. $db->setQuery(JFile::read($file));
  138. // Attempt to import the component schema.
  139. $return = $db->queryBatch(false);
  140. // Check for a database error.
  141. if ($db->getErrorNum()) {
  142. $this->setError($db->getErrorMsg());
  143. return false;
  144. }
  145. // Attempt to insert the manual install entry into the component version table.
  146. $db->setQuery(
  147. 'INSERT IGNORE INTO `#__taoj` (`extension`,`version`,`log`)' .
  148. ' VALUES ('.$db->quote('com_contentmanager').','.$db->Quote($current).', '.$db->Quote('TAOJ_Setup_Manual_Install').')'
  149. );
  150. $db->query();
  151. // Check for a database error.
  152. if ($db->getErrorNum()) {
  153. $this->setError($db->getErrorMsg());
  154. return false;
  155. }
  156. return true;
  157. }
  158. /**
  159. * Method to run necessary database upgrade scripts
  160. *
  161. * @return boolean True on success.
  162. */
  163. public function upgrade()
  164. {
  165. // Get the component upgrade information.
  166. $version = new ContentManagerVersion();
  167. $upgrades = $version->getUpgrades();
  168. // If there are upgrades to process, attempt to process them.
  169. if (is_array($upgrades) && count($upgrades))
  170. {
  171. // Sort the upgrades, lowest version first.
  172. uksort($upgrades, 'version_compare');
  173. // Get the database object.
  174. $db = &$this->_db;
  175. // Get the number of relevant rows in the components table.
  176. $db->setQuery(
  177. 'SELECT COUNT(id)' .
  178. ' FROM `#__components`' .
  179. ' WHERE `option` = "com_contentmanager"'
  180. );
  181. $installed = $db->loadResult();
  182. // Check for a database error.
  183. if ($db->getErrorNum()) {
  184. $this->setError($db->getErrorMsg());
  185. return false;
  186. }
  187. // Check to see if the component is installed.
  188. if ($installed < 1) {
  189. $this->setError(JText::_('TAOJ_Setup_Not_Installed'));
  190. return false;
  191. }
  192. foreach ($upgrades as $upgradeVersion => $file)
  193. {
  194. $file = JPATH_COMPONENT.DS.'install'.DS.$file;
  195. if (JFile::exists($file))
  196. {
  197. // Set the upgrade SQL from the file.
  198. $db->setQuery(JFile::read($file));
  199. // Execute the upgrade SQL.
  200. $return = $db->queryBatch(false);
  201. // Check for a database error.
  202. if ($db->getErrorNum()) {
  203. $this->setError(JText::sprintf('TAOJ_Setup_Database_Upgrade_Failed', $db->getErrorMsg()));
  204. return false;
  205. }
  206. // Upgrade was successful, attempt to log it to the versions table.
  207. $db->setQuery(
  208. 'INSERT INTO `#__taoj` (`extension`,`version`,`log`) VALUES' .
  209. ' ('.$db->quote('com_contentmanager').','.$db->Quote($upgradeVersion).', '.$db->Quote(JText::sprintf('TAOJ_Setup_Database_Upgrade_Version', $upgradeVersion)).')'
  210. );
  211. $db->query();
  212. // Check for a database error.
  213. if ($db->getErrorNum()) {
  214. $this->setError(JText::sprintf('TAOJ_Setup_Database_Upgrade_Failed', $db->getErrorMsg()));
  215. return false;
  216. }
  217. }
  218. }
  219. }
  220. return true;
  221. }
  222. /**
  223. * Initialiases the ACL
  224. */
  225. public function initAcl()
  226. {
  227. // Require the Acl API class
  228. require_once dirname(dirname(__FILE__)).DS.'libraries'.DS.'jxtended.php';
  229. jximport2('jxtended.acl.acladmin');
  230. // We must check for, and possibly create, the zero-user (Public, ARO value = 0)
  231. // otherwise Public access won't work properly
  232. if (!JxAclAdmin::getUser(0))
  233. {
  234. // Create the User
  235. $userId = JxAclAdmin::registerUser('Public User', 0);
  236. if (JError::isError($userId)) {
  237. //return JError::raiseWarning(500, $result->getMessage());
  238. }
  239. // Now we need to map that user to the Public Group
  240. if ($group = JxAclAdmin::getGroupForUsers('Public Frontend')) {
  241. $result = JxAclAdmin::registerUserInGroups($userId, $group->id);
  242. if (JError::isError($result)) {
  243. $this->setError($result->getError());
  244. return false;
  245. }
  246. }
  247. else {
  248. $this->setError(JText::_('Cannot find the public users group'));
  249. return false;
  250. }
  251. }
  252. // Register the sections
  253. // @todo Think about not throwing an error if the section exists??
  254. JxAclAdmin::registerSectionForRules('ContentManager', 'com_contentmanager');
  255. JxAclAdmin::registerSectionForActions('ContentManager', 'com_contentmanager');
  256. JxAclAdmin::registerSectionForAssets('ContentManager', 'com_contentmanager');
  257. // Register Actions
  258. // Type 1 - A user in a group can do "this"
  259. JxAclAdmin::registerAction(1, 'com_contentmanager', 'Access', 'access');
  260. // Type 2 - A user in a group can do "this" to an asset
  261. JxAclAdmin::registerAction(2, 'com_contentmanager', 'Create Articles', 'create.article');
  262. JxAclAdmin::registerAction(2, 'com_contentmanager', 'Edit Articles', 'edit.article');
  263. JxAclAdmin::registerAction(2, 'com_contentmanager', 'Publish Articles', 'publish.article');
  264. JxAclAdmin::registerAction(2, 'com_contentmanager', 'Trash Articles', 'trash.article');
  265. JxAclAdmin::registerAction(2, 'com_contentmanager', 'Edit Article Parameters', 'edit.article.parameters');
  266. JxAclAdmin::registerAction(2, 'com_contentmanager', 'Edit Article Author', 'edit.article.author', 'Edit the author related details for an Article.');
  267. JxAclAdmin::registerAction(2, 'com_contentmanager', 'Edit Article Access', 'edit.article.access', 'Edit the access level for an Article.');
  268. //
  269. // Now we are ready to add some rules
  270. //
  271. $result = JxAclAdmin::registerRule(
  272. // The rule type
  273. 1,
  274. // The rule section
  275. 'com_contentmanager',
  276. // The rule name
  277. 'com_contentmanager.access',
  278. // The title of the rule
  279. 'Content Manager - Access',
  280. // Applies to User Groups
  281. array('Administrator', 'Super Administrator'),
  282. // The Actions attached to the rule
  283. array('com_contentmanager' => array('access')),
  284. // Applies to Assets
  285. array(),
  286. // Applies to Asset Groups
  287. array()
  288. );
  289. return true;
  290. }
  291. }