Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/installer/adapters/module.php

Documentation is available at module.php

  1. <?php
  2. /**
  3.  * @version        $Id: module.php 6138 2007-01-02 03:44:18Z eddiea $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    Installer
  6.  * @copyright    Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant
  9.  *  to the GNU General Public License, and as distributed it includes or
  10.  *  is derivative of works licensed under the GNU General Public License or
  11.  *  other free or open source software licenses.
  12.  *  See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. /**
  19.  * Module installer
  20.  *
  21.  * @package        Joomla.Framework
  22.  * @subpackage    Installer
  23.  * @since        1.5
  24.  */
  25. class JInstallerModule extends JObject
  26. {
  27.     /**
  28.      * Constructor
  29.      *
  30.      * @access    protected
  31.      * @param    object    $parent    Parent object [JInstaller instance]
  32.      * @return    void 
  33.      * @since    1.5
  34.      */
  35.     function __construct(&$parent)
  36.     {
  37.         $this->parent =$parent;
  38.     }
  39.  
  40.     /**
  41.      * Custom install method
  42.      *
  43.      * @access    public
  44.      * @return    boolean    True on success
  45.      * @since    1.5
  46.      */
  47.     function install()
  48.     {
  49.         // Get a database connector object
  50.         $db =$this->parent->getDBO();
  51.  
  52.         // Get the extension manifest object
  53.         $manifest =$this->parent->getManifest();
  54.         $this->manifest =$manifest->document;
  55.  
  56.         /**
  57.          * ---------------------------------------------------------------------------------------------
  58.          * Manifest Document Setup Section
  59.          * ---------------------------------------------------------------------------------------------
  60.          */
  61.  
  62.         // Set the component name
  63.         $name =$this->manifest->getElementByPath('name');
  64.         $this->set('name'$name->data());
  65.  
  66.         // Get the component description
  67.         $description $this->manifest->getElementByPath('description');
  68.         if (is_a($description'JSimpleXMLElement')) {
  69.             $this->parent->set('message'$this->get('name').'<p>'.$description->data().'</p>');
  70.         else {
  71.             $this->parent->set('message'$this->get('name'));
  72.         }
  73.  
  74.         /**
  75.          * ---------------------------------------------------------------------------------------------
  76.          * Target Application Section
  77.          * ---------------------------------------------------------------------------------------------
  78.          */
  79.  
  80.         // Get the target application
  81.         if ($cname $this->manifest->attributes('client')) {
  82.             // Attempt to map the client to a base path
  83.             jimport('joomla.application.helper');
  84.             $client JApplicationHelper::getClientInfo($cnametrue);
  85.             if ($client === false{
  86.                 $this->parent->abort('Module Install: '.JText::_('Unknown client type').' ['.$client->name.']');
  87.                 return false;
  88.             }
  89.             $basePath $client->path;
  90.             $clientId $client->id;
  91.         else {
  92.             // No client attribute was found so we assume the site as the client
  93.             $cname 'site';
  94.             $basePath JPATH_SITE;
  95.             $clientId 0;
  96.         }
  97.  
  98.         // Set the installation path
  99.         $element =$this->manifest->getElementByPath('files');
  100.         if (is_a($element'JSimpleXMLElement'&& count($element->children())) {
  101.             $files =$element->children();
  102.             foreach ($files as $file{
  103.                 if ($file->attributes('module')) {
  104.                     $mname $file->attributes('module');
  105.                     break;
  106.                 }
  107.             }
  108.         }
  109.         if (!empty ($mname)) {
  110.             $this->parent->setPath('extension_root'$basePath.DS.'modules'.DS.$mname);
  111.         else {
  112.             $this->parent->abort('Module Install: '.JText::_('No module file specified'));
  113.             return false;
  114.         }
  115.  
  116.         /**
  117.          * ---------------------------------------------------------------------------------------------
  118.          * Filesystem Processing Section
  119.          * ---------------------------------------------------------------------------------------------
  120.          */
  121.  
  122.         /*
  123.          * If the module directory already exists, then we will assume that the
  124.          * module is already installed or another module is using that
  125.          * directory.
  126.          */
  127.         if (file_exists($this->parent->getPath('extension_root'))) {
  128.             $this->parent->abort('Module Install: '.JText::_('Another module is already using directory').': "'.$this->parent->getPath('extension_root').'"');
  129.             return false;
  130.         }
  131.  
  132.         // If the module directory does not exist, lets create it
  133.         $created false;
  134.         if (!file_exists($this->parent->getPath('extension_root'))) {
  135.             if (!$created JFolder::create($this->parent->getPath('extension_root'))) {
  136.                 $this->parent->abort('Module Install: '.JText::_('Failed to create directory').': "'.$this->parent->getPath('extension_root').'"');
  137.                 return false;
  138.             }
  139.         }
  140.  
  141.         /*
  142.          * Since we created the module directory and will want to remove it if
  143.          * we have to roll back the installation, lets add it to the
  144.          * installation step stack
  145.          */
  146.         if ($created{
  147.             $this->parent->pushStep(array ('type' => 'folder''path' => $this->parent->getPath('extension_root')));
  148.         }
  149.  
  150.         // Copy all necessary files
  151.         if ($this->parent->parseFiles($element-1=== false{
  152.             // Install failed, roll back changes
  153.             $this->parent->abort();
  154.             return false;
  155.         }
  156.  
  157.         // Parse optional tags
  158.         $this->parent->parseMedia($this->manifest->getElementByPath('media')$clientId);
  159.         $this->parent->parseLanguages($this->manifest->getElementByPath('languages')$clientId);
  160.  
  161.         // Parse deprecated tags
  162.         $this->parent->parseFiles($this->manifest->getElementByPath('images')-1);
  163.  
  164.         /**
  165.          * ---------------------------------------------------------------------------------------------
  166.          * Database Processing Section
  167.          * ---------------------------------------------------------------------------------------------
  168.          */
  169.  
  170.         // Check to see if a module by the same name is already installed
  171.         $query 'SELECT `id`' .
  172.                 ' FROM `#__modules` ' .
  173.                 ' WHERE module = '.$db->Quote($mname.
  174.                 ' AND client_id = '.(int)$clientId;
  175.         $db->setQuery($query);
  176.         if (!$db->Query()) {
  177.             // Install failed, roll back changes
  178.             $this->parent->abort('Module Install: '.$db->stderr(true));
  179.             return false;
  180.         }
  181.         $id $db->loadResult();
  182.  
  183.         // Was there a module already installed with the same name?
  184.         if ($id{
  185.             // Install failed, roll back changes
  186.             $this->parent->abort('Module Install: '.JText::_('Module').' "'.$mname.'" '.JText::_('already exists!'));
  187.             return false;
  188.         else {
  189.             $row JTable::getInstance('module');
  190.             $row->title $this->get('name');
  191.             $row->ordering $row->getNextOrder"position='left'" );
  192.             $row->position 'left';
  193.             $row->showtitle 1;
  194.             $row->iscore 0;
  195.             $row->access $clientId == 0;
  196.             $row->client_id $clientId;
  197.             $row->module $mname;
  198.             $row->params $this->parent->getParams();
  199.  
  200.             if (!$row->store()) {
  201.                 // Install failed, roll back changes
  202.                 $this->parent->abort('Module Install: '.$db->stderr(true));
  203.                 return false;
  204.             }
  205.  
  206.             // Since we have created a module item, we add it to the installation step stack
  207.             // so that if we have to rollback the changes we can undo it.
  208.             $this->parent->pushStep(array ('type' => 'module''id' => $row->id));
  209.  
  210.             // Clean up possible garbage first
  211.             $query 'DELETE FROM #__modules_menu WHERE moduleid = '.(int) $row->id;
  212.             $db->setQuery$query );
  213.             if (!$db->query()) {
  214.                 // Install failed, roll back changes
  215.                 $this->parent->abort('Module Install: '.$db->stderr(true));
  216.                 return false;
  217.             }
  218.  
  219.             // Time to create a menu entry for the module
  220.             $query 'INSERT INTO `#__modules_menu` ' .
  221.                     ' VALUES ('.(int) $row->id.', 0 )';
  222.             $db->setQuery($query);
  223.             if (!$db->query()) {
  224.                 // Install failed, roll back changes
  225.                 $this->parent->abort('Module Install: '.$db->stderr(true));
  226.                 return false;
  227.             }
  228.  
  229.             /*
  230.              * Since we have created a menu item, we add it to the installation step stack
  231.              * so that if we have to rollback the changes we can undo it.
  232.              */
  233.             $this->parent->pushStep(array ('type' => 'menu''id' => $db->insertid()));
  234.         }
  235.  
  236.         /**
  237.          * ---------------------------------------------------------------------------------------------
  238.          * Finalization and Cleanup Section
  239.          * ---------------------------------------------------------------------------------------------
  240.          */
  241.  
  242.         // Lastly, we will copy the manifest file to its appropriate place.
  243.         if (!$this->parent->copyManifest(-1)) {
  244.             // Install failed, rollback changes
  245.             $this->parent->abort('Module Install: '.JText::_('Could not copy setup file'));
  246.             return false;
  247.         }
  248.         return true;
  249.     }
  250.  
  251.     /**
  252.      * Custom uninstall method
  253.      *
  254.      * @access    public
  255.      * @param    int        $id            The id of the module to uninstall
  256.      * @param    int        $clientId    The id of the client (unused)
  257.      * @return    boolean    True on success
  258.      * @since    1.5
  259.      */
  260.     function uninstall$id$clientId )
  261.     {
  262.         // Initialize variables
  263.         $row    null;
  264.         $retval true;
  265.         $db        =$this->parent->getDBO();
  266.  
  267.         // First order of business will be to load the module object table from the database.
  268.         // This should give us the necessary information to proceed.
  269.         $row JTable::getInstance('module');
  270.         $row->load((int) $id);
  271.  
  272.         // Is the module we are trying to uninstall a core one?
  273.         // Because that is not a good idea...
  274.         if ($row->iscore{
  275.             JError::raiseWarning(100'Module Uninstall: '.JText::sprintf('WARNCOREMODULE'$row->name)."<br />".JText::_('WARNCOREMODULE2'));
  276.             return false;
  277.         }
  278.  
  279.         // Get the extension root path
  280.         jimport('joomla.application.helper');
  281.         $client JApplicationHelper::getClientInfo($row->client_id);
  282.         if ($client === false{
  283.             $this->parent->abort('Module Uninstall: '.JText::_('Unknown client type').' ['.$row->client_id.']');
  284.             return false;
  285.         }
  286.         $this->parent->setPath('extension_root'$client->path.DS.'modules'.DS.$row->module);
  287.  
  288.         // Get the package manifest objecct
  289.         $this->parent->setPath('source'$this->parent->getPath('extension_root'));
  290.         $manifest =$this->parent->getManifest();
  291.         if (!is_a($manifest'JSimpleXML')) {
  292.             // Make sure we delete the folders
  293.             JFolder::delete($this->parent->getPath('extension_root'));
  294.             JError::raiseWarning(100'Module Uninstall: Package manifest file invalid or not found');
  295.             return false;
  296.         }
  297.  
  298.         // Remove other files
  299.         $root =$manifest->document;
  300.         $this->parent->removeFiles($root->getElementByPath('media'));
  301.         $this->parent->removeFiles($root->getElementByPath('administration/media')1);
  302.         $this->parent->removeFiles($root->getElementByPath('languages'));
  303.         $this->parent->removeFiles($root->getElementByPath('administration/languages')1);
  304.  
  305.         // Lets delete all the module copies for the type we are uninstalling
  306.         $query 'SELECT `id`' .
  307.                 ' FROM `#__modules`' .
  308.                 ' WHERE module = '.$db->Quote($row->module.
  309.                 ' AND client_id = '.(int)$row->client_id;
  310.         $db->setQuery($query);
  311.         $modules $db->loadResultArray();
  312.  
  313.         // Do we have any module copies?
  314.         if (count($modules)) {
  315.             $modID implode(','$modules);
  316.             $query 'DELETE' .
  317.                     ' FROM #__modules_menu' .
  318.                     ' WHERE moduleid IN ("'.$modID.'")';
  319.             $db->setQuery($query);
  320.             if (!$db->query()) {
  321.                 JError::raiseWarning(100'Module Uninstall: '.$db->stderr(true));
  322.                 $retval false;
  323.             }
  324.         }
  325.  
  326.         // Now we will no longer need the module object, so lets delete it and free up memory
  327.         $row->delete($row->id);
  328.         unset ($row);
  329.  
  330.         // Remove the installation folder
  331.         if (!JFolder::delete($this->parent->getPath('extension_root'))) {
  332.             // JFolder should raise an error
  333.             $retval false;
  334.         }
  335.         return $retval;
  336.     }
  337.  
  338.     /**
  339.      * Custom rollback method
  340.      *     - Roll back the menu item
  341.      *
  342.      * @access    public
  343.      * @param    array    $arg    Installation step to rollback
  344.      * @return    boolean    True on success
  345.      * @since    1.5
  346.      */
  347.     function _rollback_menu($arg)
  348.     {
  349.         // Get database connector object
  350.         $db =$this->parent->getDBO();
  351.  
  352.         // Remove the entry from the #__modules_menu table
  353.         $query 'DELETE' .
  354.                 ' FROM `#__modules_menu`' .
  355.                 ' WHERE moduleid='.(int)$arg['id'];
  356.         $db->setQuery($query);
  357.         return ($db->query(!== false);
  358.     }
  359.  
  360.     /**
  361.      * Custom rollback method
  362.      *     - Roll back the module item
  363.      *
  364.      * @access    public
  365.      * @param    array    $arg    Installation step to rollback
  366.      * @return    boolean    True on success
  367.      * @since    1.5
  368.      */
  369.     function _rollback_module($arg)
  370.     {
  371.         // Get database connector object
  372.         $db =$this->parent->getDBO();
  373.  
  374.         // Remove the entry from the #__modules table
  375.         $query 'DELETE' .
  376.                 ' FROM `#__modules`' .
  377.                 ' WHERE id='.(int)$arg['id'];
  378.         $db->setQuery($query);
  379.         return ($db->query(!== false);
  380.     }
  381. }
  382. ?>

Documentation generated on Mon, 05 Mar 2007 21:11:13 +0000 by phpDocumentor 1.3.1