Runonce Code Beispiele: Unterschied zwischen den Versionen

Aus Contao Community Documentation

K
K
 
(13 dazwischenliegende Versionen von einem Benutzer werden nicht angezeigt)
Zeile 1: Zeile 1:
 +
{{AppliesTo|TLVersion=ab 2.7|Version=ab 2.9}}
 +
Code Beispiele für die [[Runonce|Runonce]] Funktionalität.
 +
Der Class Name muss bei den OOP Varianten eineindeutig im System sein!
 
[[Category:Dev HOWTOS]]
 
[[Category:Dev HOWTOS]]
 
[[Category:Dev Snippets]]
 
[[Category:Dev Snippets]]
{{AppliesTo|TLVersion=ab 2.7|Version=ab 2.9}}
+
===Datenbank Insert===
===Hinweis===
+
<source lang="php">
{{Achtung|Artikel wird grad überarbeitet, bitte nichts dran ändern solange dieser Hinweis noch besteht. Dringende Hinweise über IRC an mich. (BugBuster)}}
+
<?php
== Code Beispiele ==
+
class BannerRunonceJob extends Controller
 +
{
 +
  public function __construct()
 +
  {
 +
      parent::__construct();
 +
      $this->import('Database');
 +
  }
 +
  public function run()
 +
  {
 +
      $arrInsert=array(
 +
          'action'    => 'runonce',
 +
          'text'      => 'runonce'
 +
      );
 +
      $this->Database->prepare("INSERT INTO tl_log %s")->set($arrInsert)->execute();
 +
  }
 +
}
 +
$objBannerRunonceJob = new BannerRunonceJob();
 +
$objBannerRunonceJob->run();
 +
?>
 +
</source>
 +
 
 +
===Datenbank Migration unter Bedingungen===
 +
Gekürzt, soll nur das Prinzip zeigen.
 +
<source lang="php">
 +
<?php
 +
class BannerRunonceJob extends Controller
 +
{
 +
  public function __construct()
 +
  {
 +
      parent::__construct();
 +
      $this->import('Database');
 +
  }
 +
  public function run()
 +
  {
 +
      //nur ab Contao 2.9
 +
      if (version_compare(VERSION, '2.8', '>'))
 +
      {
 +
          if ($this->Database->tableExists('tl_banner_category'))  
 +
          {
 +
              if ($this->Database->fieldExists('banner_template', 'tl_banner_category')
 +
              && !$this->Database->fieldExists('banner_template', 'tl_module'))
 +
      {
 +
                  //Feld anlegen
 +
                  $this->Database->execute("ALTER TABLE `tl_module` ADD `banner_template` varchar(32) NOT NULL default ''");
 +
                  //nun sollte es angelegt sein
 +
                  if ( $this->Database->fieldExists('banner_template', 'tl_banner_category')
 +
                    && $this->Database->fieldExists('banner_template', 'tl_module') )
 +
                  {
 +
                      //füllen
 +
                      $this->Database->execute("UPDATE tl_module SET banner_template='mod_banner_list_all' WHERE type='banner' AND banner_template=''");
 +
                  }
 +
              }
 +
          } // if tableExists
 +
      } // if Version > 2.8
 +
  } // run
 +
} // class
 +
$objBannerRunonceJob = new BannerRunonceJob();
 +
$objBannerRunonceJob->run();
 +
?>
 +
</source>
 +
 
 
== Code Beispiel Spezialfall ==
 
== Code Beispiel Spezialfall ==
 +
Universal-Runonce von Andreas Schempp, /system/runonce.php, für Contao 2.9.
 +
Die eigendliche runonce.php liegt im config Verzeichnis des Moduls und wird in Contao 2.9 über die Universal-Runonce aufgerufen, ab Contao 2.10 von Contao direkt.
 +
<source lang="php">
 +
class UniversalRunonce extends Controller
 +
{
 +
  /**
 +
  * Initialize the object
 +
  */
 +
  public function __construct()
 +
  {
 +
    parent::__construct();
 +
   
 +
    // Fix potential Exception on line 0 because of __destruct method (see http://dev.contao.org/issues/2236)
 +
    $this->import((TL_MODE=='BE' ? 'BackendUser' : 'FrontendUser'), 'User');
 +
    $this->import('Database');
 +
  }
 +
 +
  /**
 +
  * Execute all runonce files in module config directories
 +
  */
 +
  public function run()
 +
  {
 +
    $this->import('Files');
 +
    $arrModules = scan(TL_ROOT . '/system/modules/');
 +
   
 +
    foreach ($arrModules as $strModule)
 +
    {
 +
      if ((@include(TL_ROOT . '/system/modules/' . $strModule . '/config/runonce.php')) !== false)
 +
      {
 +
        $this->Files->delete('system/modules/' . $strModule . '/config/runonce.php');
 +
      }
 +
    }
 +
  }
 +
}
 +
  
 +
/**
 +
* Instantiate controller
 +
*/
 +
if (version_compare(VERSION, '2.10', '<'))
 +
{
 +
  $objUniversalRunonce = new UniversalRunonce();
 +
  $objUniversalRunonce->run();
 +
}
 +
</source>
 +
Wenn nun jeder Modulentwickler dieses Universal-Runonce Prinzip für Contao 2.9 verwenden würde, wäre das Problem des Überschreibens dort beseitigt. Logisch, oder? Also, los!
  
 
----
 
----
 
--[[Benutzer:BugBuster|BugBuster]] 14:17, 13. Nov. 2011 (CET)
 
--[[Benutzer:BugBuster|BugBuster]] 14:17, 13. Nov. 2011 (CET)

Aktuelle Version vom 15. November 2011, 12:33 Uhr

betrifft
TYPOlight Version ab 2.7
Contao Version ab 2.9

Code Beispiele für die Runonce Funktionalität. Der Class Name muss bei den OOP Varianten eineindeutig im System sein!

Datenbank Insert

<?php
class BannerRunonceJob extends Controller
{
   public function __construct()
   {
       parent::__construct();
       $this->import('Database');
   }
   public function run()
   {
       $arrInsert=array(
           'action'    => 'runonce',
           'text'      => 'runonce'
       );
       $this->Database->prepare("INSERT INTO tl_log %s")->set($arrInsert)->execute();
   }
}
$objBannerRunonceJob = new BannerRunonceJob();
$objBannerRunonceJob->run();
?>

Datenbank Migration unter Bedingungen

Gekürzt, soll nur das Prinzip zeigen.

<?php
class BannerRunonceJob extends Controller
{
   public function __construct()
   {
       parent::__construct();
       $this->import('Database');
   }
   public function run()
   {
       //nur ab Contao 2.9
       if (version_compare(VERSION, '2.8', '>'))
       {
           if ($this->Database->tableExists('tl_banner_category')) 
           {
               if ($this->Database->fieldExists('banner_template', 'tl_banner_category') 
               && !$this->Database->fieldExists('banner_template', 'tl_module'))
	       {
                   //Feld anlegen
                   $this->Database->execute("ALTER TABLE `tl_module` ADD `banner_template` varchar(32) NOT NULL default ''");
                   //nun sollte es angelegt sein
                   if ( $this->Database->fieldExists('banner_template', 'tl_banner_category') 
                     && $this->Database->fieldExists('banner_template', 'tl_module') )
                   {
                       //füllen
                       $this->Database->execute("UPDATE tl_module SET banner_template='mod_banner_list_all' WHERE type='banner' AND banner_template=''");
                   }
               }
           } // if tableExists
       } // if Version > 2.8
   } // run
} // class
$objBannerRunonceJob = new BannerRunonceJob();
$objBannerRunonceJob->run();
?>

Code Beispiel Spezialfall

Universal-Runonce von Andreas Schempp, /system/runonce.php, für Contao 2.9. Die eigendliche runonce.php liegt im config Verzeichnis des Moduls und wird in Contao 2.9 über die Universal-Runonce aufgerufen, ab Contao 2.10 von Contao direkt.

class UniversalRunonce extends Controller
{
  /**
   * Initialize the object
   */
  public function __construct()
  {
    parent::__construct();
 
    // Fix potential Exception on line 0 because of __destruct method (see http://dev.contao.org/issues/2236)
    $this->import((TL_MODE=='BE' ? 'BackendUser' : 'FrontendUser'), 'User');
    $this->import('Database');
  }
 
  /**
   * Execute all runonce files in module config directories
   */
  public function run()
  {
    $this->import('Files');
    $arrModules = scan(TL_ROOT . '/system/modules/');
 
    foreach ($arrModules as $strModule)
    {
      if ((@include(TL_ROOT . '/system/modules/' . $strModule . '/config/runonce.php')) !== false)
      {
        $this->Files->delete('system/modules/' . $strModule . '/config/runonce.php');
      }
    }
  }
}
 
 
/**
 * Instantiate controller
 */
if (version_compare(VERSION, '2.10', '<'))
{
  $objUniversalRunonce = new UniversalRunonce();
  $objUniversalRunonce->run();
}

Wenn nun jeder Modulentwickler dieses Universal-Runonce Prinzip für Contao 2.9 verwenden würde, wäre das Problem des Überschreibens dort beseitigt. Logisch, oder? Also, los!


--BugBuster 14:17, 13. Nov. 2011 (CET)

Ansichten
Meine Werkzeuge

Contao Community Documentation

<TheTril> Stateless Template, Stateless Elements, Stateless Renderer :)
<TheTril> everything is stateles :D
<leo-unglaub> TheTril: genau wie Ed Snowden *g*

Navigation
Verstehen
Verwenden
Entwickeln
Verschiedenes
Werkzeuge