Mayflower entdeckt Yii
9. Mai 2012
public function getHours() { $from = explode(':', $this->from); $to = explode(':', $this->to); $fromTime = mktime($from[0], $from[1]); $toTime = mktime($to[0], $to[1]); $diff = abs($toTime - $fromTime) - $this->pause * 60; $hours = $diff / 3600; $hours = round($hours * 2); return $hours / 2; }
public function getDatabase() { preg_match("/dbname=([^;]*)/", Yii::app()->db->connectionString, $matches); return $matches[1]; }
//protected/components/MyConsoleCommand.php class MyConsoleCommand extends CConsoleCommand { public function beforeAction($action, $params) { $this->println('...start'); return parent::beforeAction($action, $params); } public function afterAction($action, $params) { parent::afterAction($action, $params); $this->println('...end'); } public function println($str) { echo $str . PHP_EOL; } }
//protected/commands/TestCommand.php class TestCommand extends MyConsoleCommand { public function actionRun() { $this->install(1); $this->install(2); $this->run(array('install', '--name=Hallo')); $this->run(array('install', '--name=Welt')); $this->commandRunner->createCommand('new')->run(array('index', '--name=Hallo Welt')); } protected function install($i = 0) { $this->println('HERE WE ARE IN INSTALL: ' . $i); } public function actionInstall($name = '') { $this->println('HERE WE ARE IN ACTION INSTALL: ' . $name); } }
//protected/commands/NewCommand.php class NewCommand extends MyConsoleCommand { public function actionIndex($name = '') { $this->println('WE ARE NOW IN NEW::INDEX - ' . $name); } }
>protected/yiic test run
//in einer view-Datei, zeige mir alle Bilder eines Ordners in einer Slideshow an <?php $this->widget('ext.slideshow.SlideshowWidget', array('dir' => 'images')); ?>
Dafür laden wir uns mal eine JQuery-Slideshow.
Abspeichern unter extensions/slideshow/assets/slideshow.js.
Der Quellcode dieser Seite zeigt uns die simple Anwendung, die wir nur noch als Widget implementieren müssen.
//extensions.slideshow.SlideshowWidget.php class SlideshowWidget extends CWidget { public $dir; public $width = 600; public $height = 450; public static $assetsUrl; public function getImageUrl($file) { return Yii::app()->baseUrl . '/' . $this->dir . '/' . basename($file); } public function run() { $path = str_replace('/', '.', $this->dir); $path = Yii::getPathOfAlias('webroot.' . $path); $files = CFileHelper::findFiles($path, array('fileTypes' => array('jpg', 'jpeg', 'gif', 'png'), 'level' => 0)); Yii::app()->clientScript->registerScriptFile(self::getAssetsUrl() . '/slideshow.js'); Yii::app()->clientScript->registerScript('slideshow', "$('.slideshow').cycle({fx: 'fade'});"); Yii::app()->clientScript->registerCss('slideshow', ' .slideshow { height: ' . $this->height . 'px; width: ' . $this->width . 'px; margin: auto }' ); echo '<div class="slideshow">'; foreach ($files as $file) { echo '<img src="' . $this->getImageUrl($file) . '" />'; } echo '</div>'; } public static function getAssetsUrl() { if (self::$assetsUrl === null) { self::$assetsUrl = Yii::app()->getAssetManager()->publish( Yii::getPathOfAlias('ext.slideshow.assets') ); } return self::$assetsUrl; } }
<?php $this->widget('ext.widgets.google.maps.Map', array( 'id' => 'map', 'center' => array('53.12345', '12.56789'), 'zoom' => 14, 'itemView' => '_location', //template für das InfoWindow? 'dataProvider' => new CActiveDataProvider('Location')) //liefert Daten für die Marker )); ?>
//migration script $this->createTable('{{product}}', array( 'id' => 'pk', 'name' => 'string', 'price' => 'money' //speichert decimal(19,4) )); echo Yii::app()->numberFormatter->formatCurrency($model->price, 'EUR'); //ergibt €1000.00
Letzte Kommentare