PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/administrator/components/com_multisites/models/templates.php

https://github.com/sherdog/GitWitty
PHP | 364 lines | 212 code | 50 blank | 102 comment | 50 complexity | 36b324dbe22b84fe2bff20c504ed9b92 MD5 | raw file
  1. <?php
  2. /**
  3. * @file templates.php
  4. * @version 1.2.0 RC4
  5. * @author Edwin CHERONT (e.cheront@jms2win.com)
  6. * Edwin2Win sprlu (www.jms2win.com)
  7. * @copyright Joomla Multi Sites
  8. * Single Joomla! 1.5.x installation using multiple configuration (One for each 'slave' sites).
  9. * (C) 2008-2009 Edwin2Win sprlu - all right reserved.
  10. * @license This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version 2
  13. * of the License, or (at your option) any later version.
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * A full text version of the GNU GPL version 2 can be found in the LICENSE.php file.
  22. * @par History:
  23. * - V1.1.0 01-OCT-2008: Initial version
  24. * - V1.1.8 24-DEC-2008: Add a parameter in save to allow reset all field before save in aim
  25. * to perform an update of all the record. (This allow to remove fields)
  26. * - V1.2.0 RC4 10-JUL-2009: Add a check that the template is correctly written.
  27. * Otherwise, set an error message
  28. */
  29. // Check to ensure this file is included in Joomla!
  30. defined('_JEXEC') or die( 'Restricted access' );
  31. jimport( 'joomla.application.component.model' );
  32. jimport('joomla.filesystem.file');
  33. require_once( JPATH_COMPONENT_ADMINISTRATOR .DS. 'classes' .DS. 'site.php');
  34. require_once( JPATH_COMPONENT_ADMINISTRATOR .DS. 'classes' .DS. 'template.php');
  35. require_once( JPATH_COMPONENT_ADMINISTRATOR .DS. 'classes' .DS. 'utils.php');
  36. if ( !defined( 'JPATH_MULTISITES')) {
  37. define( 'JPATH_MULTISITES', JPATH_ROOT.DS.'multisites');
  38. }
  39. // ===========================================================
  40. // MultisitesModelTemplates class
  41. // ===========================================================
  42. /**
  43. * @brief Is used to manage the 'Template websites'.
  44. */
  45. class MultisitesModelTemplates extends JModel
  46. {
  47. // Private members
  48. var $_modelName = 'templates';
  49. var $_template = null;
  50. var $_countAll = 0;
  51. //------------ getTemplateFilename ---------------
  52. /**
  53. * @return Return the template configuration file name.
  54. */
  55. function getTemplateFilename()
  56. {
  57. $filename = JPATH_MULTISITES .DS. 'config_templates.php';
  58. return $filename;
  59. }
  60. //------------ getTemplates ---------------
  61. /**
  62. * @brief Return a list of Templates.
  63. *
  64. * The templates can be filtered on :
  65. * - DB 'host' server name
  66. * - DB name
  67. */
  68. function &getTemplates()
  69. {
  70. // Extract the filtering selection
  71. $filters = $this->getState( 'filters');
  72. if ( !is_null($filters)) {
  73. // If there is a filtering on DB server
  74. if ( isset($filters['hosts']) && $filters['host'] != '[unselected]') {
  75. $filter_host = $filters['host'];
  76. }
  77. // If there is a filtering on DB name
  78. if ( $filters['db'] != '[unselected]') {
  79. $filter_db = $filters['db'];
  80. }
  81. }
  82. /* Read the collection of templates */
  83. $rows = array();
  84. $filename = $this->getTemplateFilename();
  85. @include( $filename);
  86. $loadExtraInfo = false;
  87. if ( isset( $templates)) {
  88. // If there is a filter
  89. if ( !empty( $filter_host) || !empty( $filter_db)) {
  90. foreach( $templates as $key => $template) {
  91. $site = new Site();
  92. $site->load( $template['fromSiteID']);
  93. $template['fromHost'] = $site->host;
  94. $template['fromDB'] = $site->db;
  95. $template['fromPrefix'] = $site->dbprefix;
  96. if ( !empty( $filter_host) && !empty( $filter_db)){
  97. if ( $template['fromHost'] == $filter_host
  98. && $template['fromDB'] == $filter_db
  99. )
  100. {
  101. $rows[$key] = $template;
  102. }
  103. }
  104. if ( !empty( $filter_host) && empty( $filter_db)){
  105. if ( $template['fromHost'] == $filter_host)
  106. {
  107. $rows[$key] = $template;
  108. }
  109. }
  110. else if ( empty( $filter_host) && !empty( $filter_db)){
  111. if ( $template['fromDB'] == $filter_db)
  112. {
  113. $rows[$key] = $template;
  114. }
  115. }
  116. }
  117. }
  118. else {
  119. $rows = $templates;
  120. $loadExtraInfo = true;
  121. }
  122. }
  123. $this->_countAll = count( $rows);
  124. // Fill the ID with the key
  125. foreach( $rows as $key => $row) {
  126. $rows[$key]['id'] = $key;
  127. }
  128. // If the user request ordering records
  129. if ( !is_null($filters)) {
  130. if ( !empty( $filters['order'])) {
  131. $colname = $filters['order'];
  132. $sortedrows = array();
  133. $i = 0;
  134. foreach( $rows as $row){
  135. $colValue = isset( $row[$colname]) ? $row[$colname] : '';
  136. $key = $colValue . '.' . substr( "00".strval($i++), -3);
  137. $sortedrows[$key] = $row;
  138. }
  139. // If requested a REVERSE ordering (descending)
  140. if ( !empty( $filters['order_Dir']) && $filters['order_Dir'] =='desc') {
  141. krsort($sortedrows);
  142. $rows = $sortedrows;
  143. }
  144. // Ascending order
  145. else {
  146. ksort($sortedrows);
  147. $rows = $sortedrows;
  148. }
  149. }
  150. }
  151. // If there is a limits specified
  152. if ( !is_null($filters)) {
  153. // If there is a limit specified (not ALL records)
  154. if ( $filters['limit'] > 0) {
  155. // slice out elements based on limits
  156. $rows = array_slice( $rows, $filters['limitstart'], $filters['limit'] );
  157. }
  158. }
  159. // For each row, compute the additional information (fromDB, fromPrefix)
  160. if ( $loadExtraInfo) {
  161. foreach( $rows as $key => $row) {
  162. $site = new Site();
  163. $site->load( $row['fromSiteID']);
  164. $rows[$key]['fromDB'] = $site->db;
  165. $rows[$key]['fromPrefix'] = $site->dbprefix;
  166. }
  167. }
  168. return $rows;
  169. }
  170. //------------ getCountAll ---------------
  171. /**
  172. * @return the total number of records.
  173. */
  174. function getCountAll()
  175. {
  176. return $this->_countAll;
  177. }
  178. //------------ setFilters ---------------
  179. function setFilters( &$filters)
  180. {
  181. $this->setState( 'filters', $filters);
  182. }
  183. //------------ removeFilters ---------------
  184. function removeFilters()
  185. {
  186. $this->setState( 'filters', null);
  187. }
  188. //------------ getCurrentRecord ---------------
  189. /**
  190. * @brief Return a single record Template coresponding to the id. Null when does not exists.
  191. */
  192. function getCurrentRecord()
  193. {
  194. if ($this->_template == null) {
  195. $this->_template = new Jms2WinTemplate();
  196. if ($id = JRequest::getVar('id', false, '', 'string')) {
  197. $this->_template->load($id);
  198. }
  199. }
  200. return $this->_template;
  201. }
  202. //------------ getNewRecord ---------------
  203. /**
  204. * @brief Return a new record template.
  205. */
  206. function getNewRecord()
  207. {
  208. if ($this->_template == null) {
  209. $this->_template = new Jms2WinTemplate();
  210. }
  211. return $this->_template;
  212. }
  213. //------------ write ---------------
  214. /**
  215. * Write the template array into its configuration template file
  216. * @return boolean
  217. */
  218. function write( $templates)
  219. {
  220. $filename = $this->getTemplateFilename();
  221. // Write the new template file
  222. $config = "<?php\n";
  223. $config .= "if( !defined( '_JEXEC' )) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' ); \n\n";
  224. $config .= '$templates = array();'
  225. . "\n";
  226. foreach( $templates as $key => $values) {
  227. $config .= "\$templates['$key'] = array(\n";
  228. $leadingSpaces = ' ';
  229. $sep2 = $leadingSpaces;
  230. foreach( $values as $key2 => $value) {
  231. if ( is_array( $value)) {
  232. $config .= $sep2 . "'$key2' => array( " . MultisitesUtils::CnvArray2Str( $leadingSpaces.' ', $value) . ")";
  233. }
  234. else {
  235. $config .= $sep2 . "'$key2' => '" . addslashes($value) ."'";
  236. }
  237. $sep2 = ",\n" . $leadingSpaces;
  238. }
  239. $config .= ");\n";
  240. }
  241. if ( !JFile::write( $filename, $config)) {
  242. $this->setError( JText::sprintf( 'TEMPLATE_WRITE_ERR', $filename) );
  243. return false;
  244. }
  245. return true;
  246. }
  247. //------------ save ---------------
  248. /**
  249. * Save the template into the template collection and store the result on disk.
  250. * @param reset Flag that indicate if the record must be reset before save (unset the key ID)
  251. * @return boolean
  252. */
  253. function save( $enteredvalues, $reset=false)
  254. {
  255. // Read the current template configuration file
  256. $templates = array();
  257. $filename = $this->getTemplateFilename();
  258. @include( $filename);
  259. if ( $reset && !empty( $enteredvalues['id'])) {
  260. unset( $templates[ $enteredvalues['id']]);
  261. }
  262. // Update the template content
  263. foreach( $enteredvalues as $key => $value) {
  264. if ( strstr('*id*isnew*', $key)) {}
  265. else {
  266. $templates[ $enteredvalues['id']][$key] = $value;
  267. }
  268. }
  269. ksort( $templates);
  270. return $this->write( $templates);
  271. }
  272. //------------ canDelete ---------------
  273. /**
  274. * Checks if the template can be deleted
  275. * @return boolean
  276. */
  277. function canDelete()
  278. {
  279. // Return TRUE If the template exists
  280. $template = new Jms2WinTemplate();
  281. $id = JRequest::getString('id');
  282. if (!empty( $id)) {
  283. if ( !$template->load($id)) {
  284. $this->setError( JText::_( 'TEMPLATE_NOT_FOUND' ) );
  285. return false;
  286. }
  287. return true;
  288. }
  289. return false;
  290. }
  291. //------------ delete ---------------
  292. /**
  293. * Deletes the template from the template collection.
  294. * @return boolean
  295. */
  296. function delete()
  297. {
  298. $id = JRequest::getString('id');
  299. if ( empty( $id)) {
  300. return false;
  301. }
  302. // Read the current template configuration file
  303. $templates = array();
  304. $filename = $this->getTemplateFilename();
  305. @include( $filename);
  306. // If there is something in the templates collection for this id,
  307. if ( isset( $templates[ $id])) {
  308. // Delete it
  309. unset( $templates[ $id]);
  310. return $this->write( $templates);
  311. }
  312. return true;
  313. }
  314. } // End class