Modul: IncludeFile zum Einbinden von Dateien

Gesperrt
xmurrix
Beiträge: 3215
Registriert: Do 21. Okt 2004, 11:08
Wohnort: Augsburg
Hat sich bedankt: 4 Mal
Danksagung erhalten: 17 Mal
Kontaktdaten:

Modul: IncludeFile zum Einbinden von Dateien

Beitrag von xmurrix »

Hallo zusammen,

habe ein neues Modul "IncludeFile" zusammengestrickt, mit dem verschiedene Dateitypen eingebunden werden können.

Beschreibung:
Über das Modulinput kann eine Datei ausgewählt werden, die dann im Output eingebunden wird. Das Modul untertützt JavaScript-. CSS-, PHP- und HTML-Dateien.

JavaScript- und CSS-Dateien werden über das entsprechende HTML-Tag ausgegeben, PHP- und HTML-Dateien hingegen werden mit der Funktion cInclude inkludiert.

Installation:
Das Modul kann über den Import des Modulcodes und der deutschen Übersetzung installiert werden.
Zusätzlich sind folgende Mandantenvariablen anzulegen:

Code: Alles auswählen

Typ   Name           Wert
dir   include.css    css/
dir   include.js     js/
dir   include.php    includes/additional/
dir   include.html   includes/additional/
Die Werte für die Variablen sind Ordner im Clientverzeichnis (relativ zum cms-Pfad), welche einzubindenede Dateien enthalten. Diese Ordner sollten vorhanden sein oder ggf. angelegt werden.
Ordner "css/" und "js/" sind bei der Standardinstallation mit dabei, "includes/additional/" hingegen ist anzulegen.

Download:
modIncludeFile1.0.zip

Grüße
xmurrix
stese
Beiträge: 1040
Registriert: Fr 3. Dez 2004, 17:47
Wohnort: München
Kontaktdaten:

Beitrag von stese »

kannst du die felder vll. auch posten? falls in ein zwei jahren das file auf dem server mal nicht zu finden ist ;) ausserdem schaue ich mir gern den code vorher an bevor ich ein fremdes modul einsetze
xmurrix
Beiträge: 3215
Registriert: Do 21. Okt 2004, 11:08
Wohnort: Augsburg
Hat sich bedankt: 4 Mal
Danksagung erhalten: 17 Mal
Kontaktdaten:

Beitrag von xmurrix »

stese hat geschrieben:kannst du die felder vll. auch posten? falls in ein zwei jahren das file auf dem server mal nicht zu finden ist ;) ausserdem schaue ich mir gern den code vorher an bevor ich ein fremdes modul einsetze
Ist verständlich, hier die Felder...

Beschreibung:

Code: Alles auswählen

Modul zum Einbinden einer Datei per Auswahl. Unterstützt werden JS-, CSS-, PHP- 
und HTML-Dateien. Damit ist es möglich, über die Artikel-/Kategorie-Konfiguration 
eine Datei zusätzlich einzubinden. Ausgewählte JS-/CSS-Dateien werden mit 
entsprechendem HTML-Tag, PHP-/-HTML-Dateien per cInclude eingebunden.

Autor:   Murat Purc, murat@purc.de
Version: 1.0

Wichtig:
Unter "Administration -> Mandanten -> Mandanteneinstellungen" sind folgende 
Variablen einzufügen:

Typ   Name           Wert
dir   include.css    css/
dir   include.js     js/
dir   include.php    includes/additional/
dir   include.html   includes/additional/

Die Werte für die Variablen sind Ordner im Clientverzeichnis (relativ zum cms-Pfad), 
welche einzubindenede Dateien enthalten. Diese Ordner sollten vorhanden sein oder 
ggf. angelegt werden.
Ordner "css/" und "js/" sind bei der Standardinstallation mit dabei, 
"includes/additional/" hingegen ist anzulegen.
Modulinput:

Code: Alles auswählen

?><?php
// input IncludeFile

cInclude('classes', 'contenido/class.client.php');

/**
* Class to generate a selectbox used to select a file 2 include.
*
* @category  Moduleinput
* @author    Murat Purc, info@purc.de
* @copyright o-a c, 2006
* @date      09.08.2006
*/
class cModIncludeFile{

    /**
    * Prefix for found files, used for composing option data
    * @var  string  $_sPrefix
    */
    var $_sPrefix = "&nbsp;&nbsp;-&nbsp;";


    /**
    * Constructor, sets some properties
    */
    function cModIncludeFile(){
        global $cfgClient, $client;

        $this->_sPath = $cfgClient[$client]['path']['frontend'];

        $oApiClient = new cApiClient($client);

        $this->_aDirs['css']['name']  = mi18n("Style sheet file");
        $this->_aDirs['css']['dir']   = $oApiClient->getProperty('dir', 'include.css');
        $this->_aDirs['js']['name']   = mi18n("JavaScript file");
        $this->_aDirs['js']['dir']    = $oApiClient->getProperty('dir', 'include.js');
        $this->_aDirs['php']['name']  = mi18n("PHP file");
        $this->_aDirs['php']['dir']   = $oApiClient->getProperty('dir', 'include.php');
        $this->_aDirs['php']['ext']   = 'php|php3|php4|php5|inc';
        $this->_aDirs['html']['name'] = mi18n("HTML file");
        $this->_aDirs['html']['dir']  = $oApiClient->getProperty('dir', 'include.html');
        $this->_aDirs['html']['ext']  = 'htm|html';

        $this->_sIncFile = "CMS_VALUE[0]";
    } // function cModIncludeFile()


    /**
    * Main function to get all select options.
    * @return   string   Composed html options
    */
    function generateIncOptions(){
        $ret = '<option value="">'.mi18n("--- Select file ---").'</option>'."\n";
        foreach ($this->_aDirs as $ext => $item) {
            if (isset($item['ext'])) {
                $ext = $item['ext'];
            }
            $ret .= $this->_generateOptions($item, $ext);
        }
        return $ret;
    }


    /**
    * Creates options for a specific extention.
    * @param    array    $aItem   Include type array, see $this->_aDirs
    * @param    string   $ext     Allowed extentions
    * @return   string            Composed html options
    * @access   private
    */
    function _generateOptions($aItem, $ext) {
        $aFiles = $this->_readDir($this->_sPath.$aItem['dir'], $ext);
        if (!is_array($aFiles)) {
            return '';
        }
        $ret .= '<option class="dir" value="">'.$aItem['name'].':</option>'."\n";
        foreach ($aFiles as $pos => $file) {
            $val  = $aItem['dir'].$file;
            $sel  = ($val == $this->_sIncFile) ? ' selected="selected"' : '';
            $ret .= '<option value="'.$val.'"'.$sel.'>'.$this->_sPrefix.$file.'</option>'."\n";
        }
        return $ret;
    } // function _generateOptions()


    /**
    * Returns all found files inside a dir
    * @param    string   $dir         Directory to read
    * @param    string   $allowedExt  Extentions of files to get (restriction for returning all files)
    * @return   mixed                 Array of found files or false
    * @access   private
    */
    function _readDir($dir, $allowedExt=null) {
        $hDir = opendir($dir);
        while ($sFile = readdir($hDir)) {
            $bIsEmpty = ($sFile == '.' || $sFile == '..') ? true : false;
            $bIsDir  = (is_dir($dir.$sFile) && !$bIsEmpty) ? true : false;
            if (!$bIsDir && !$bIsEmpty) {
                if ($allowedExt == null) {
                    $aFiles[] = $sFile;
                } else {
                    $tmp  = explode('.', $sFile);
                    $sExt = strtolower($tmp[count($tmp)-1]);
                    if (strpos($allowedExt, $sExt) !== false) {
                        $aFiles[] = $sFile;
                    }
                }
            }
        }
        closedir($hDir);
#       print "\n<!--\ndir: $dir\n";var_dump($aFiles);echo "\n-->\n";
        return (is_array($aFiles)) ? $aFiles : false;
    } // function _readDir()


} // class cModIncludeFile


$oMIF = new cModIncludeFile();

?>
<!-- MOD INCLUDEFILE -->
<style type="text/css">
option.dir{font-size:11px;font-weight:bold;font-style:italic;color:blue;border-bottom:1px blue dashed;}
</style>
<table cellspacing="0" cellpadding="3" border="0">
<tr><td valign="top" class="text_medium"><?php print mi18n("Select file to include"); ?></td></tr>
<tr><td><select name="CMS_VAR[0]" class="text_medium">
    <?php echo $oMIF->generateIncOptions(); ?>
</select><br>
</td></tr>
</table>
<!-- /MOD INCLUDEFILE -->
<?php

unset($oMIF);
Moduloutput:

Code: Alles auswählen

<?php
// output IncludeFile


if (!class_exists('cModIncludeFile')) {

/**
* Class to include a selected file. Provides following filetypes:
* - JS and CSS, inclusion using corresponding HTML-Tag
* - PHP and HTML, inclusion using Contenido cInclude-Function
*
* @category  Moduleoutput
* @author    Murat Purc, info@purc.de
* @copyright o-a c, 2006
* @date      09.08.2006
*/
class cModIncludeFile{

    /**
    * Template for script tag output
    * @var  string  $_sJSTagTpl
    */
    var $_sJSTagTpl  = '<script src="%s" type="text/javascript"></script>';

    /**
    * Template for css tag output
    * @var  string  $_sCSSTagTpl
    */
    var $_sCSSTagTpl = '<link rel="stylesheet" type="text/css" href="%s" media="all" />';


    /**
    * Constructor, sets some properties
    */
    function cModIncludeFile(){
        $this->_sIncFile = "CMS_VALUE[0]";

        $this->_aExt['js']   = 'js';
        $this->_aExt['css']  = 'css';
        $this->_aExt['php']  = 'php|php3|php4|php5|inc';
        $this->_aExt['html'] = 'htm|html';
    }


    /**
    * Main function to include desired file. Check file extention and
    * prints out a html-tag ord includes file using cInclude-Function.
    */
    function generateOutput(){
        if ($this->_sIncFile == '') {
            return;
        }

        $tmp  = explode('.', $this->_sIncFile);
        $sExt = strtolower($tmp[count($tmp)-1]);

        foreach($this->_aExt as $ext => $allowedExt) {
            if (strpos($allowedExt, $sExt) !== false) {
                $sExt = $ext;
                break;
            }
        }

        switch ($sExt) {
            case 'js':
                // selected file is a javascript file put out the script tag
                print sprintf($this->_sJSTagTpl, $this->_sIncFile); break;
            case 'css':
                // selected file is a css file put out the css tag
                print sprintf($this->_sCSSTagTpl, $this->_sIncFile); break;
            case 'php':
                // selected file is a php file, include file and pray that the user done right
                cInclude('frontend', $this->_sIncFile); break;
            case 'html':
                // selected file is a html file
                cInclude('frontend', $this->_sIncFile); break;
            default:
                break;
        }
    } // function generateOutput()

} // class cModIncludeFile

} // if (!class_exists('cModIncludeFile'))


$oMIF = new cModIncludeFile();
$oMIF->generateOutput();
#print "\n<!--\ndir: $dir\n";print_r($oMIF);print "\n-->\n";
unset($oMIF);

?>
Übersetzung (XML-Export):

Code: Alles auswählen

<?xml version="1.0" encoding="ISO-8859-1"?>
<module><translation origin-language-id="1" origin-language-name="deutsch"><string><original>Style sheet file</original>
<translation>Style Sheet Datei</translation>
</string>
<string><original>JavaScript file</original>
<translation>JavaScript Datei</translation>
</string>
<string><original>PHP file</original>
<translation>PHP Datei</translation>
</string>
<string><original>HTML file</original>
<translation>HTML Datei</translation>
</string>
<string><original>--- Select file ---</original>
<translation>--- Datei ausw&auml;hlen ---</translation>
</string>
<string><original>Select file to include</original>
<translation>Einzubindende Datei ausw&auml;hlen</translation>
</string>
</translation>
</module>
Gruß
xmurrix
Gesperrt