Benutzer:Elkuku/Proyektz/EVI2/AppEVI
Inhaltsverzeichnis |
[Bearbeiten] app evi
Dies ist die Standard Applikation welche aufgerufen wird, wenn keine andere Applikation (Modul) angegeben wurde. Bei Ihrem Aufruf werden alle "installierten" Module aufgezeigt, vergleichbar mit den Quickicons im Joomla! Backend.
Die Applikation übernimmt ebenfalls das Installieren neuer Module, und es besteht die Möglichkeit ein "Hello World" Modul zu erstellen, welches die Basis für eigene Module bietet (ähnlich dem EasyCreator).
[Bearbeiten] evi.php - Helper Klasse
Die Funktion getModules() der Helper Klasse EVI liest alle Unterverzeichnisse des Ordners
app ein, und erstellt daraus die Liste der installierten Module.
Es werden hier auch die Informationen der XML Datei ausgelesen.
class EVI { /** * Gets the installed modules * * @return array */ public static function getModules() { static $modules = array(); if( $modules ) { return $modules; } $apps = JFolder::folders(JPATH_EVI.DS.'apps'); $doc = JFactory::getDocument(); $sizes = array(16, 32, 48); foreach ($apps as $a) { if( $a == 'evi' || $a == 'login' || $a == 'blank' ) { continue; } $app = new stdClass(); $app->folder = $a; $app->name = ucfirst($a); $app->version = '?'; foreach ($sizes as $size) { //-- Hinzufügen der CSS informationen für die icons $fName = JURI::root(true).'/evi/apps/'.$app->folder.'/assets/images/appicons/icon-'.$size.'-'.$app->folder.'.png'; $doc->addStyleDeclaration('.icon-'.$size.'-'.$app->folder.'{ background-image: url('.$fName.'); }'); }//foreach $xmlfiles = JFolder::files(JPATH_EVI.DS.'apps'.DS.$a, '.xml$', 1, true); if (!empty($xmlfiles)) { foreach ($xmlfiles as $file) { //-- Is it a valid joomla installation manifest file? if ($data = JApplicationHelper::parseXMLInstallFile($file)) { foreach($data as $key => $value) { $app->$key = $value; }//foreach } }//foreach } $modules[] = $app; }//foreach return $modules; }//function
[Bearbeiten] install.php - Helper Klasse
Abb. 2.2 Die Datei installer.php der Helper Klasse eviInstaller
/** * Module Class to manage * EVI Modules */ class eviInstaller {
[Bearbeiten] upload()
In dieser Funktion wird ein Paket (zip Datei) mit Hilfe des Joomla! Installers und eines speziellen Adapters hochgeladen und installiert.
/** * Upload a plugin package * @access public */ function upload() { global $mainframe; //get the package $package = $this->_getPackageFromUpload(); // Was the package unpacked? if (!$package) { return false; } // Get an installer instance jimport('joomla.installer.installer'); $installer =& JInstaller::getInstance(); //set the adapter JLoader::import('module', JPATH_COMPONENT.DS.'helpers'.DS.'adapter'); $adapter = new eviModule($installer); $installer->setAdapter('evimodule', $adapter ); // Install the package if (!$installer->install($package['dir'])) { // There was an error installing the package $result = false; } else { // Package installed sucessfully $result = true; } // Cleanup the install files if (!is_file($package['packagefile'])) { $config =& JFactory::getConfig(); $package['packagefile'] = $config->getValue('config.tmp_path').DS.$package['packagefile']; } JInstallerHelper::cleanupInstall($package['packagefile'], $package['extractdir']); return $result; }// function
[Bearbeiten] create()
Diese Funktion erstellt ein neues Modul.
/** * Create a new module from a name given by request * @return unknown_type */ function create() { jimport('joomla.filesystem.file'); $mTitle = JRequest::getVar('module_title'); $mName = JRequest::getCmd('module_name'); //-- Clean up the name - abit $mName = strtolower($mName); $mName = str_replace(' ', '_', $mName); if( ! $mTitle || ! $mName ) { JError::raiseWarning(100, JText::_('Missing arguments')); return false; } $app = JFactory::getApplication(); $tmpPath = $app->getCfg('tmp_path'); $tmpPath .= DS.uniqid($mName); if( ! JFolder::create($tmpPath)) { jError::raiseWarning(100, JText::_('Unable to create temp folder')); return false; } $basePath = JPATH_EVI.DS.'apps'; $fList = JFolder::files($basePath.DS.'blank', '.', true, true); foreach ($fList as $fName) { $fPath = str_replace(JPATH_EVI.DS.'apps'.DS.'blank'.DS, '', $fName); $fPath = str_replace('__EVI_MOD__', strtolower($mName), $fPath); $cPath = ''; if(strpos($fPath, DS)) { //-- File is inside a subdir $cPath = str_replace(DS.JFile::getName($fPath), '', $fPath); if($cPath != 'language') { if( ! JFolder::create($tmpPath.DS.$cPath)) { jError::raiseWarning(100, JText::_('Unable to create language folder')); return false; } } } if( $cPath == 'language' ) { $f = JFile::getName($fPath); $tag = substr($f, 0, 5); $langDir = JPATH_EVI.DS.'language'; if( JFolder::exists($langDir.DS.$tag)) { if( ! JFile::copy($fName, $langDir.DS.$tag.DS.$f)) { jError::raiseWarning(100, JText::_('Unable to copy language file')); return false; } $fContents = JFile::read($langDir.DS.$tag.DS.$f); $fContents = preg_replace(array('/__EVI_MOD__/', '/__EVI_MOD_NAME__/'), array($mName, $mTitle), $fContents); if( ! JFile::write($langDir.DS.$tag.DS.$f, $fContents)) { JError::raiseWarning(100, JText::_('Unable to write file')); } continue; } } if( ! JFile::copy($fName, $tmpPath.DS.$fPath)) { JError::raiseWarning(100, JText::_('Unable to copy file')); return false; } switch( JFile::getExt($fPath) ) { case 'php': case 'css': case 'js': $fContents = JFile::read($tmpPath.DS.$fPath); $fContents = preg_replace(array('/__EVI_MOD__/', '/__EVI_MOD_NAME__/'), array($mName, $mTitle), $fContents); if( ! JFile::write($tmpPath.DS.$fPath, $fContents)) { JError::raiseWarning(100, JText::_('Unable to write file')); } break; }//switch }//foreach if( ! JFolder::move($tmpPath, $basePath.DS.$mName)) { JError::raiseWarning(100, JText::_('Unable to move finished package')); } return true; }//function
[Bearbeiten] getPackageFromUpload()
Diese interne Funktion übernimmt den upload der Datei.
/** * @param string The class name for the installer */ private function getPackageFromUpload() { // Get the uploaded file information $userfile = JRequest::getVar('install_package', null, 'files', 'array' ); // Make sure that file uploads are enabled in php if (!(bool) ini_get('file_uploads')) { JError::raiseWarning('SOME_ERROR_CODE', JText::_('WARNINSTALLFILE')); return false; } // Make sure that zlib is loaded so that the package can be unpacked if (!extension_loaded('zlib')) { JError::raiseWarning('SOME_ERROR_CODE', JText::_('WARNINSTALLZLIB')); return false; } // If there is no uploaded file, we have a problem... if (!is_array($userfile) ) { JError::raiseWarning('SOME_ERROR_CODE', JText::_('No file selected')); return false; } // Check if there was a problem uploading the file. if ( $userfile['error'] || $userfile['size'] < 1 ) { JError::raiseWarning('SOME_ERROR_CODE', JText::_('WARNINSTALLUPLOADERROR')); return false; } // Build the appropriate paths $config =& JFactory::getConfig(); $tmp_dest = $config->getValue('config.tmp_path').DS.$userfile['name']; $tmp_src = $userfile['tmp_name']; // Move uploaded file jimport('joomla.filesystem.file'); $uploaded = JFile::upload($tmp_src, $tmp_dest); // Unpack the downloaded package file jimport('joomla.installer.helper'); $package = JInstallerHelper::unpack($tmp_dest); return $package; }//function
[Bearbeiten] module.php - Installer Adapter
Abb. 2.2 Die Datei installer.php der Helper Klasse eviInstaller
Dies ist der Adapter, der die Logik für die Installation unserer Module enthält. Er ist einem standard Joomla! installer adapter nachempfunden und leicht abgeändert (siehe Kommentare im Quellcode)
/** * Module installer for EVI2 Modules */ class eviModule extends JObject { /** * Constructor * * @access protected * @param object $parent Parent object [JInstaller instance] * @return void * @since 1.5 */ function __construct(&$parent) { $this->parent =& $parent; }//function /** * Custom install method for EasyToolbar2 Plugins * * @access public * @return boolean True on success */ function install() { $db =& $this->parent->getDBO(); $manifest =& $this->parent->getManifest(); $this->manifest =& $manifest->document; $root =& $manifest->document; $basePath = JPATH_EVI.DS.'apps'; // Get the Module name $name =& $this->manifest->getElementByPath('name'); $name = JFilterInput::clean($name->data(), 'cmd'); $this->set('name', $name); $appName =& $this->manifest->getElementByPath('appname'); if(is_a($appName, 'JSimpleXMLElement')) { $appName = JFilterInput::clean($appName->data(), 'cmd'); } if( ! $appName ) { $this->parent->abort(JText::_('Install').': '.JText::_('No AppName set')); return false; } // get the plugin folder name from filename.php $filesElement =& $root->getElementByPath('files'); // Set the module installation paths $this->parent->setPath('extension_evi', $basePath.DS.$appName); // If the directory does not exist, lets create it $created = false; if (!file_exists($this->parent->getPath('extension_evi'))) { if (!$created = JFolder::create($this->parent->getPath('extension_evi'))) { $this->parent->abort(JText::_('Plugin').' '.JText::_('Install').': '.JText::_('Failed to create directory').' "'.$this->parent->getPath('extension_site').'"'); return false; } } /* * If we created the directory and will want to remove it if we * have to roll back the installation, lets add it to the installation * step stack */ if ($created) { $this->parent->pushStep(array ('type' => 'folder', 'path' => $this->parent->getPath('extension_evi'))); } /** * // Copy all the necessary files */ //-- SET cid to 4 for EVI if ($this->parent->parseFiles($filesElement, 4) === false) { // Install failed, rollback changes $this->parent->abort(); return false; } /* * Copy language files */ //-- SET cid to 4 for EVI $this->parseLanguages($this->manifest->getElementByPath('languages'), 4, $basePath); return true; }//function /** * Method to parse through a languages element of the installation manifest and take appropriate * action. * * @access public * @param object $element The xml node to process * @param string $destination The destination folder * @return boolean True on success * @since 1.5 */ function parseLanguages( $element, $cid, $destination=null ) { // Initialize variables $copyfiles = array (); // Get the client info $client =& JApplicationHelper::getClientInfo($cid); if( ! $destination ) { jimport('joomla.application.helper'); } if (!is_a($element, 'JSimpleXMLElement') || !count($element->children())) { // Either the tag does not exist or has no children therefore we return zero files processed. return 0; } // Get the array of file nodes to process $files = $element->children(); if (count($files) == 0) { // No files to process return 0; } /* * Here we set the folder we are going to copy the files to. * * 'languages' Files are copied to $destination/language/ folder */ $destination = $client->path.DS.'language'; /* * Here we set the folder we are going to copy the files from. * * Does the element have a folder attribute? * * If so this indicates that the files are in a subdirectory of the source * folder and we should append the folder attribute to the source path when * copying files. */ if ($folder = $element->attributes('folder')) { //NiK:changed to $this->parent $source = $this->parent->getPath('source').DS.$folder; } else { //NiK:changed to $this->parent $source = $this->parent->getPath('source'); } // Process each file in the $files array (children of $tagName). foreach ($files as $file) { /* * Language files go in a subfolder based on the language code, ie. * * <language tag="en-US">en-US.mycomponent.ini</language> * * would go in the en-US subdirectory of the language folder. * * We will only install language files where a core language pack * already exists. * !! EDIT :: we will override this behavior and install * the language even if it not exists */ if ($file->attributes('tag') != '') { $path['src'] = $source.DS.$file->data(); $path['dest'] = $destination.DS.$file->attributes('tag').DS.basename($file->data()); } else { $path['src'] = $source.DS.$file->data(); $path['dest'] = $destination.DS.$file->data(); } /* * Before we can add a file to the copyfiles array we need to ensure * that the folder we are copying our file to exits and if it doesn't, * we need to create it. */ if (basename($path['dest']) != $path['dest']) { $newdir = dirname($path['dest']); if (!JFolder::create($newdir)) { JError::raiseWarning(1, 'JInstaller::install: '.JText::_('Failed to create directory').' "'.$newdir.'"'); return false; } } // Add the file to the copyfiles array $copyfiles[] = $path; }//foreach //NiK:changed to $this->parent return $this->parent->copyFiles($copyfiles); }// function /** * Custom uninstall method * * @access public * @param string $tag The tag of the language to uninstall * @param int $clientId The id of the client (unused) * @return mixed Return value for uninstall method in component uninstall file * @todo uninstall plugin */ function uninstall($tag, $clientId) { $path = trim($tag); if (!JFolder::exists($path)) { JError::raiseWarning(100, JText::_('Language').' '.JText::_('Uninstall').': '.JText::_('Language path is empty, cannot uninstall files')); return false; } if (!JFolder::delete($path)) { JError::raiseWarning(100, JText::_('Language').' '.JText::_('Uninstall').': '.JText::_('Unable to remove language directory')); return false; } return true; }// function }// class