[ Index ]

PHP Cross Reference of Joomla 2.5.4 DE

title

Body

[close]

/libraries/joomla/installer/adapters/ -> template.php (source)

   1  <?php
   2  /**
   3   * @package     Joomla.Platform
   4   * @subpackage  Installer
   5   *
   6   * @copyright   Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  defined('JPATH_PLATFORM') or die;
  11  
  12  jimport('joomla.installer.extension');
  13  jimport('joomla.base.adapterinstance');
  14  
  15  /**
  16   * Template installer
  17   *
  18   * @package     Joomla.Platform
  19   * @subpackage  Installer
  20   * @since       11.1
  21   */
  22  class JInstallerTemplate extends JAdapterInstance
  23  {
  24      protected $name = null;
  25  
  26      protected $element = null;
  27  
  28      protected $route = 'install';
  29  
  30      /**
  31       * Custom loadLanguage method
  32       *
  33       * @param   string  $path  The path where to find language files.
  34       *
  35       * @return  JInstallerTemplate
  36       *
  37       * @since   11.1
  38       */
  39  	public function loadLanguage($path = null)
  40      {
  41          $source = $this->parent->getPath('source');
  42  
  43          if (!$source)
  44          {
  45              $this->parent
  46                  ->setPath(
  47                  'source',
  48                  ($this->parent->extension->client_id ? JPATH_ADMINISTRATOR : JPATH_SITE) . '/templates/' . $this->parent->extension->element
  49              );
  50          }
  51  
  52          $clientId = isset($this->parent->extension) ? $this->parent->extension->client_id : 0;
  53          $this->manifest = $this->parent->getManifest();
  54          $name = strtolower(JFilterInput::getInstance()->clean((string) $this->manifest->name, 'cmd'));
  55          $client = (string) $this->manifest->attributes()->client;
  56  
  57          // Load administrator language if not set.
  58          if (!$client)
  59          {
  60              $client = 'ADMINISTRATOR';
  61          }
  62  
  63          $extension = "tpl_$name";
  64          $lang = JFactory::getLanguage();
  65          $source = $path ? $path : ($this->parent->extension->client_id ? JPATH_ADMINISTRATOR : JPATH_SITE) . '/templates/' . $name;
  66          $lang->load($extension . '.sys', $source, null, false, false)
  67              || $lang->load($extension . '.sys', constant('JPATH_' . strtoupper($client)), null, false, false)
  68              || $lang->load($extension . '.sys', $source, $lang->getDefault(), false, false)
  69              || $lang->load($extension . '.sys', constant('JPATH_' . strtoupper($client)), $lang->getDefault(), false, false);
  70      }
  71  
  72      /**
  73       * Custom install method
  74       *
  75       * @return  boolean  True on success
  76       *
  77       * @since   11.1
  78       */
  79  	public function install()
  80      {
  81          // Get a database connector object
  82          $db = $this->parent->getDbo();
  83  
  84          $lang = JFactory::getLanguage();
  85          $xml = $this->parent->getManifest();
  86  
  87          // Get the client application target
  88          if ($cname = (string) $xml->attributes()->client)
  89          {
  90              // Attempt to map the client to a base path
  91              $client = JApplicationHelper::getClientInfo($cname, true);
  92              if ($client === false)
  93              {
  94                  $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_UNKNOWN_CLIENT', $cname));
  95                  return false;
  96              }
  97              $basePath = $client->path;
  98              $clientId = $client->id;
  99          }
 100          else
 101          {
 102              // No client attribute was found so we assume the site as the client
 103              $cname = 'site';
 104              $basePath = JPATH_SITE;
 105              $clientId = 0;
 106          }
 107  
 108          // Set the extension's name
 109          $name = JFilterInput::getInstance()->clean((string) $xml->name, 'cmd');
 110  
 111          $element = strtolower(str_replace(" ", "_", $name));
 112          $this->set('name', $name);
 113          $this->set('element', $element);
 114  
 115          // Check to see if a template by the same name is already installed.
 116          $query = $db->getQuery(true);
 117          $query->select($query->qn('extension_id'))->from($query->qn('#__extensions'));
 118          $query->where($query->qn('type') . ' = ' . $query->q('template'));
 119          $query->where($query->qn('element') . ' = ' . $query->q($element));
 120          $db->setQuery($query);
 121  
 122          try
 123          {
 124              $id = $db->loadResult();
 125          }
 126          catch (JDatabaseException $e)
 127          {
 128              // Install failed, roll back changes
 129              $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_ROLLBACK'), $e->getMessage());
 130              return false;
 131          }
 132  
 133          // Legacy error handling switch based on the JError::$legacy switch.
 134          // @deprecated  12.1
 135          if (JError::$legacy && $db->getErrorNum())
 136          {
 137              // Install failed, roll back changes
 138              $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_ROLLBACK', $db->stderr(true)));
 139              return false;
 140          }
 141  
 142          // Set the template root path
 143          $this->parent->setPath('extension_root', $basePath . '/templates/' . $element);
 144  
 145          // if it's on the fs...
 146          if (file_exists($this->parent->getPath('extension_root')) && (!$this->parent->getOverwrite() || $this->parent->getUpgrade()))
 147          {
 148              $updateElement = $xml->update;
 149              // Upgrade manually set or
 150              // Update function available or
 151              // Update tag detected
 152              if ($this->parent->getUpgrade() || ($this->parent->manifestClass && method_exists($this->parent->manifestClass, 'update'))
 153                  || $updateElement)
 154              {
 155                  // Force this one
 156                  $this->parent->setOverwrite(true);
 157                  $this->parent->setUpgrade(true);
 158                  if ($id)
 159                  {
 160                      // if there is a matching extension mark this as an update; semantics really
 161                      $this->route = 'update';
 162                  }
 163              }
 164              elseif (!$this->parent->getOverwrite())
 165              {
 166                  // Overwrite is not set
 167                  // If we didn't have overwrite set, find an update function or find an update tag so let's call it safe
 168                  $this->parent
 169                      ->abort(
 170                      JText::sprintf(
 171                          'JLIB_INSTALLER_ABORT_TPL_INSTALL_ANOTHER_TEMPLATE_USING_DIRECTORY', JText::_('JLIB_INSTALLER_' . $this->route),
 172                          $this->parent->getPath('extension_root')
 173                      )
 174                  );
 175                  return false;
 176              }
 177          }
 178  
 179          /*
 180           * If the template directory already exists, then we will assume that the template is already
 181           * installed or another template is using that directory.
 182           */
 183          if (file_exists($this->parent->getPath('extension_root')) && !$this->parent->getOverwrite())
 184          {
 185              JError::raiseWarning(
 186                  100,
 187                  JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_ANOTHER_TEMPLATE_USING_DIRECTORY', $this->parent->getPath('extension_root'))
 188              );
 189              return false;
 190          }
 191  
 192          // If the template directory does not exist, let's create it
 193          $created = false;
 194          if (!file_exists($this->parent->getPath('extension_root')))
 195          {
 196              if (!$created = JFolder::create($this->parent->getPath('extension_root')))
 197              {
 198                  $this->parent
 199                      ->abort(JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_FAILED_CREATE_DIRECTORY', $this->parent->getPath('extension_root')));
 200  
 201                  return false;
 202              }
 203          }
 204  
 205          // If we created the template directory and will want to remove it if we have to roll back
 206          // the installation, let's add it to the installation step stack
 207          if ($created)
 208          {
 209              $this->parent->pushStep(array('type' => 'folder', 'path' => $this->parent->getPath('extension_root')));
 210          }
 211  
 212          // Copy all the necessary files
 213          if ($this->parent->parseFiles($xml->files, -1) === false)
 214          {
 215              // Install failed, rollback changes
 216              $this->parent->abort();
 217  
 218              return false;
 219          }
 220  
 221          if ($this->parent->parseFiles($xml->images, -1) === false)
 222          {
 223              // Install failed, rollback changes
 224              $this->parent->abort();
 225  
 226              return false;
 227          }
 228  
 229          if ($this->parent->parseFiles($xml->css, -1) === false)
 230          {
 231              // Install failed, rollback changes
 232              $this->parent->abort();
 233  
 234              return false;
 235          }
 236  
 237          // Parse optional tags
 238          $this->parent->parseMedia($xml->media);
 239          $this->parent->parseLanguages($xml->languages, $clientId);
 240  
 241          // Get the template description
 242          $this->parent->set('message', JText::_((string) $xml->description));
 243  
 244          // Lastly, we will copy the manifest file to its appropriate place.
 245          if (!$this->parent->copyManifest(-1))
 246          {
 247              // Install failed, rollback changes
 248              $this->parent->abort(JText::_('JLIB_INSTALLER_ABORT_TPL_INSTALL_COPY_SETUP'));
 249  
 250              return false;
 251          }
 252  
 253          // Extension Registration
 254  
 255          $row = JTable::getInstance('extension');
 256  
 257          if ($this->route == 'update' && $id)
 258          {
 259              $row->load($id);
 260          }
 261          else
 262          {
 263              $row->type = 'template';
 264              $row->element = $this->get('element');
 265              // There is no folder for templates
 266              $row->folder = '';
 267              $row->enabled = 1;
 268              $row->protected = 0;
 269              $row->access = 1;
 270              $row->client_id = $clientId;
 271              $row->params = $this->parent->getParams();
 272              $row->custom_data = ''; // custom data
 273          }
 274          $row->name = $this->get('name'); // name might change in an update
 275          $row->manifest_cache = $this->parent->generateManifestCache();
 276  
 277          if (!$row->store())
 278          {
 279              // Install failed, roll back changes
 280              $this->parent->abort(JText::sprintf('JLIB_INSTALLER_ABORT_TPL_INSTALL_ROLLBACK', $db->stderr(true)));
 281  
 282              return false;
 283          }
 284  
 285          if ($this->route == 'install')
 286          {
 287              //insert record in #__template_styles
 288              $query = $db->getQuery(true);
 289              $query->insert($db->quoteName('#__template_styles'));
 290              $debug = $lang->setDebug(false);
 291              $columns = array($db->quoteName('template'),
 292                  $db->quoteName('client_id'),
 293                  $db->quoteName('home'),
 294                  $db->quoteName('title'),
 295                  $db->quoteName('params')
 296              );
 297              $query->columns($columns);
 298              $query->values(
 299                  $db->Quote($row->element)
 300                  . ',' . $db->Quote($clientId)
 301                  . ',' . $db->Quote(0)
 302                  . ',' . $db->Quote(JText::sprintf('JLIB_INSTALLER_DEFAULT_STYLE', JText::_($this->get('name'))))
 303                  . ',' . $db->Quote($row->params)
 304              );
 305              $lang->setDebug($debug);
 306              $db->setQuery($query);
 307              // There is a chance this could fail but we don't care...
 308              $db->query();
 309          }
 310  
 311          return $row->get('extension_id');
 312      }
 313  
 314      /**
 315       * Custom update method for components
 316       *
 317       * @return  boolean  True on success
 318       *
 319       * @since   11.1
 320       */
 321  	public function update()
 322      {
 323          return $this->install();
 324      }
 325  
 326      /**
 327       * Custom uninstall method
 328       *
 329       * @param   integer  $id  The extension ID
 330       *
 331       * @return  boolean  True on success
 332       *
 333       * @since   11.1
 334       */
 335  	public function uninstall($id)
 336      {
 337          // Initialise variables.
 338          $retval = true;
 339  
 340          // First order of business will be to load the template object table from the database.
 341          // This should give us the necessary information to proceed.
 342          $row = JTable::getInstance('extension');
 343  
 344          if (!$row->load((int) $id) || !strlen($row->element))
 345          {
 346              JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_ERRORUNKOWNEXTENSION'));
 347              return false;
 348          }
 349  
 350          // Is the template we are trying to uninstall a core one?
 351          // Because that is not a good idea...
 352          if ($row->protected)
 353          {
 354              JError::raiseWarning(100, JText::sprintf('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_WARNCORETEMPLATE', $row->name));
 355              return false;
 356          }
 357  
 358          $name = $row->element;
 359          $clientId = $row->client_id;
 360  
 361          // For a template the id will be the template name which represents the subfolder of the templates folder that the template resides in.
 362          if (!$name)
 363          {
 364              JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_ID_EMPTY'));
 365  
 366              return false;
 367          }
 368  
 369          // Deny remove default template
 370          $db = $this->parent->getDbo();
 371          $query = 'SELECT COUNT(*) FROM #__template_styles' . ' WHERE home = 1 AND template = ' . $db->Quote($name);
 372          $db->setQuery($query);
 373  
 374          if ($db->loadResult() != 0)
 375          {
 376              JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DEFAULT'));
 377  
 378              return false;
 379          }
 380  
 381          // Get the template root path
 382          $client = JApplicationHelper::getClientInfo($clientId);
 383  
 384          if (!$client)
 385          {
 386              JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_CLIENT'));
 387              return false;
 388          }
 389  
 390          $this->parent->setPath('extension_root', $client->path . '/templates/' . strtolower($name));
 391          $this->parent->setPath('source', $this->parent->getPath('extension_root'));
 392  
 393          // We do findManifest to avoid problem when uninstalling a list of extensions: getManifest cache its manifest file
 394          $this->parent->findManifest();
 395          $manifest = $this->parent->getManifest();
 396          if (!($manifest instanceof JXMLElement))
 397          {
 398              // Kill the extension entry
 399              $row->delete($row->extension_id);
 400              unset($row);
 401              // Make sure we delete the folders
 402              JFolder::delete($this->parent->getPath('extension_root'));
 403              JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_NOTFOUND_MANIFEST'));
 404  
 405              return false;
 406          }
 407  
 408          // Remove files
 409          $this->parent->removeFiles($manifest->media);
 410          $this->parent->removeFiles($manifest->languages, $clientId);
 411  
 412          // Delete the template directory
 413          if (JFolder::exists($this->parent->getPath('extension_root')))
 414          {
 415              $retval = JFolder::delete($this->parent->getPath('extension_root'));
 416          }
 417          else
 418          {
 419              JError::raiseWarning(100, JText::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DIRECTORY'));
 420              $retval = false;
 421          }
 422  
 423          // Set menu that assigned to the template back to default template
 424          $query = 'UPDATE #__menu INNER JOIN #__template_styles' . ' ON #__template_styles.id = #__menu.template_style_id'
 425              . ' SET #__menu.template_style_id = 0' . ' WHERE #__template_styles.template = ' . $db->Quote(strtolower($name))
 426              . ' AND #__template_styles.client_id = ' . $db->Quote($clientId);
 427          $db->setQuery($query);
 428          $db->Query();
 429  
 430          $query = 'DELETE FROM #__template_styles' . ' WHERE template = ' . $db->Quote($name) . ' AND client_id = ' . $db->Quote($clientId);
 431          $db->setQuery($query);
 432          $db->Query();
 433  
 434          $row->delete($row->extension_id);
 435          unset($row);
 436  
 437          return $retval;
 438      }
 439  
 440      /**
 441       * Discover existing but uninstalled templates
 442       *
 443       * @return  array  JExtensionTable list
 444       */
 445  	public function discover()
 446      {
 447          $results = array();
 448          $site_list = JFolder::folders(JPATH_SITE . '/templates');
 449          $admin_list = JFolder::folders(JPATH_ADMINISTRATOR . '/templates');
 450          $site_info = JApplicationHelper::getClientInfo('site', true);
 451          $admin_info = JApplicationHelper::getClientInfo('administrator', true);
 452  
 453          foreach ($site_list as $template)
 454          {
 455              if ($template == 'system')
 456              {
 457                  continue;
 458  
 459                  // Ignore special system template
 460              }
 461              $manifest_details = JApplicationHelper::parseXMLInstallFile(JPATH_SITE . "/templates/$template/templateDetails.xml");
 462              $extension = JTable::getInstance('extension');
 463              $extension->set('type', 'template');
 464              $extension->set('client_id', $site_info->id);
 465              $extension->set('element', $template);
 466              $extension->set('name', $template);
 467              $extension->set('state', -1);
 468              $extension->set('manifest_cache', json_encode($manifest_details));
 469              $results[] = $extension;
 470          }
 471  
 472          foreach ($admin_list as $template)
 473          {
 474              if ($template == 'system')
 475              {
 476                  continue;
 477  
 478                  // Ignore special system template
 479              }
 480  
 481              $manifest_details = JApplicationHelper::parseXMLInstallFile(JPATH_ADMINISTRATOR . "/templates/$template/templateDetails.xml");
 482              $extension = JTable::getInstance('extension');
 483              $extension->set('type', 'template');
 484              $extension->set('client_id', $admin_info->id);
 485              $extension->set('element', $template);
 486              $extension->set('name', $template);
 487              $extension->set('state', -1);
 488              $extension->set('manifest_cache', json_encode($manifest_details));
 489              $results[] = $extension;
 490          }
 491  
 492          return $results;
 493      }
 494  
 495      /**
 496       * Discover_install
 497       * Perform an install for a discovered extension
 498       *
 499       * @return boolean
 500       *
 501       * @since 11.1
 502       */
 503  	public function discover_install()
 504      {
 505          // Templates are one of the easiest
 506          // If its not in the extensions table we just add it
 507          $client = JApplicationHelper::getClientInfo($this->parent->extension->client_id);
 508          $manifestPath = $client->path . '/templates/' . $this->parent->extension->element . '/templateDetails.xml';
 509          $this->parent->manifest = $this->parent->isManifest($manifestPath);
 510          $description = (string) $this->parent->manifest->description;
 511  
 512          if ($description)
 513          {
 514              $this->parent->set('message', JText::_($description));
 515          }
 516          else
 517          {
 518              $this->parent->set('message', '');
 519          }
 520  
 521          $this->parent->setPath('manifest', $manifestPath);
 522          $manifest_details = JApplicationHelper::parseXMLInstallFile($this->parent->getPath('manifest'));
 523          $this->parent->extension->manifest_cache = json_encode($manifest_details);
 524          $this->parent->extension->state = 0;
 525          $this->parent->extension->name = $manifest_details['name'];
 526          $this->parent->extension->enabled = 1;
 527  
 528          $data = new JObject;
 529  
 530          foreach ($manifest_details as $key => $value)
 531          {
 532              $data->set($key, $value);
 533          }
 534  
 535          $this->parent->extension->params = $this->parent->getParams();
 536  
 537          if ($this->parent->extension->store())
 538          {
 539              //insert record in #__template_styles
 540              $db = $this->parent->getDbo();
 541              $query = $db->getQuery(true);
 542              $query->insert($db->quoteName('#__template_styles'));
 543              $debug = $lang->setDebug(false);
 544              $columns = array($db->quoteName('template'),
 545                  $db->quoteName('client_id'),
 546                  $db->quoteName('home'),
 547                  $db->quoteName('title'),
 548                  $db->quoteName('params')
 549              );
 550              $query->columns($columns);
 551              $query->values(
 552                  $db->Quote($this->parent->extension->element)
 553                  . ',' . $db->Quote($this->parent->extension->client_id)
 554                  . ',' . $db->Quote(0)
 555                  . ',' . $db->Quote(JText::sprintf('JLIB_INSTALLER_DEFAULT_STYLE', $this->parent->extension->name))
 556                  . ',' . $db->Quote($this->parent->extension->params)
 557              );
 558              $lang->setDebug($debug);
 559              $db->setQuery($query);
 560              $db->query();
 561  
 562              return $this->parent->extension->get('extension_id');
 563          }
 564          else
 565          {
 566              JError::raiseWarning(101, JText::_('JLIB_INSTALLER_ERROR_TPL_DISCOVER_STORE_DETAILS'));
 567  
 568              return false;
 569          }
 570      }
 571  
 572      /**
 573       * Refreshes the extension table cache
 574       *
 575       * @return  boolean  Result of operation, true if updated, false on failure
 576       *
 577       * @since   11.1
 578       */
 579  	public function refreshManifestCache()
 580      {
 581          // Need to find to find where the XML file is since we don't store this normally.
 582          $client = JApplicationHelper::getClientInfo($this->parent->extension->client_id);
 583          $manifestPath = $client->path . '/templates/' . $this->parent->extension->element . '/templateDetails.xml';
 584          $this->parent->manifest = $this->parent->isManifest($manifestPath);
 585          $this->parent->setPath('manifest', $manifestPath);
 586  
 587          $manifest_details = JApplicationHelper::parseXMLInstallFile($this->parent->getPath('manifest'));
 588          $this->parent->extension->manifest_cache = json_encode($manifest_details);
 589          $this->parent->extension->name = $manifest_details['name'];
 590  
 591          try
 592          {
 593              return $this->parent->extension->store();
 594          }
 595          catch (JException $e)
 596          {
 597              JError::raiseWarning(101, JText::_('JLIB_INSTALLER_ERROR_TPL_REFRESH_MANIFEST_CACHE'));
 598              return false;
 599          }
 600      }
 601  }


Generated: Tue Apr 3 11:40:28 2012 Cross-referenced by PHPXref 0.7.1