partkeepr

fork of partkeepr
git clone https://git.e1e0.net/partkeepr.git
Log | Files | Refs | Submodules | README | LICENSE

commit 5887f24dc16ca4c285553db9e9a00c366dd91c38
parent bc1be5bfb5b206c8579f3a0ac3c195a92b2512ef
Author: Felicitus <felicitus@felicitus.org>
Date:   Sun, 25 Mar 2012 22:36:56 +0200

Refactored frontend to use twig

Diffstat:
Msrc/backend/de/RaumZeitLabor/PartKeepr/PartKeepr.php | 25+++++++++++++++++++++++--
Msrc/frontend/index.php | 100++++++++++++++++++++++---------------------------------------------------------
Msrc/frontend/js/PartKeepr.js | 4++--
Asrc/frontend/templates/index.tpl | 40++++++++++++++++++++++++++++++++++++++++
Msrc/setup/index.html | 1+
Msrc/setup/js/Cards/PrerequisitesTestCard.js | 1+
Asrc/setup/js/SetupTests/TwigTest.js | 10++++++++++
Asrc/setup/tests/check-twig.php | 35+++++++++++++++++++++++++++++++++++
8 files changed, 139 insertions(+), 77 deletions(-)

diff --git a/src/backend/de/RaumZeitLabor/PartKeepr/PartKeepr.php b/src/backend/de/RaumZeitLabor/PartKeepr/PartKeepr.php @@ -238,7 +238,7 @@ class PartKeepr { /** * Returns the EntityManager. Shortcut for getEntityManager(). - * @return Doctrine\ORM\EntityManager The EntityManager + * @return \Doctrine\ORM\EntityManager The EntityManager */ public static function getEM () { return self::getEntityManager(); @@ -399,4 +399,24 @@ class PartKeepr { if (is_string($var)) return "string"; return "unknown type"; } -} + + /** + * Returns the effective size from a human-readable byte format. + * + * Example: + * getBytesFromHumanReadable("1M") will return 1048576. + * + * @param string $size_str The byte + * @return int The bytes + */ + public static function getBytesFromHumanReadable ($size_str) + { + switch (substr ($size_str, -1)) + { + case 'M': case 'm': return (int)$size_str * 1048576; + case 'K': case 'k': return (int)$size_str * 1024; + case 'G': case 'g': return (int)$size_str * 1073741824; + default: return $size_str; + } + } +}+ \ No newline at end of file diff --git a/src/frontend/index.php b/src/frontend/index.php @@ -8,15 +8,18 @@ use de\RaumZeitLabor\PartKeepr\User\User, de\RaumZeitLabor\PartKeepr\Util\Configuration; include("../src/backend/de/RaumZeitLabor/PartKeepr/PartKeepr.php"); +include_once 'Twig/Autoloader.php'; PartKeepr::initialize(""); +/* Fill parameters with most common options */ $aParameters = array(); $aParameters["doctrine_orm_version"] = \Doctrine\ORM\Version::VERSION; $aParameters["doctrine_dbal_version"] = \Doctrine\DBAL\Version::VERSION; $aParameters["doctrine_common_version"] = \Doctrine\Common\Version::VERSION; $aParameters["php_version"] = phpversion(); +/* HTTP auth */ if (Configuration::getOption("partkeepr.auth.http", false) === true) { if (!isset($_SERVER["PHP_AUTH_USER"])) { // @todo Redirect to permission denied page @@ -36,84 +39,35 @@ if (Configuration::getOption("partkeepr.auth.http", false) === true) { $session = SessionManager::getInstance()->startSession($user); + $aParameters["autoLoginUsername"] = $user->getUsername(); $aParameters["auto_start_session"] = $session->getSessionID(); } -?> -<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" - "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> -<html> - <head> - <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> - <title>PartKeepr</title> - - <link href='http://fonts.googleapis.com/css?family=Ubuntu:400,700italic' rel='stylesheet' type='text/css'> - - <!-- Include the ExtJS CSS Theme --> - <link rel="stylesheet" type="text/css" href="css/partkeepr-theme.css"/> - - <link rel="stylesheet" type="text/css" href="js/Ext.ux/statusbar/css/statusbar.css"/> - - <link rel="stylesheet" type="text/css" href="css/PartKeepr.css"/> - - <link rel="icon" href="favicon.ico"/> - - <!-- Include the ExtJS JavaScript Library --> - <script type="text/javascript" src="extjs/bootstrap.js"></script> - - <?php - // @todo This is ugly, but how to fix? - if (Configuration::getOption("partkeepr.frontend.debug", false) === true) { - ?> - <script type="text/javascript" src="extjs/ext-all-debug.js"></script> - <script type="text/javascript" src="js/partkeepr-debug.js"></script> - <?php - } else { - ?> - <script type="text/javascript" src="extjs/ext-all.js"></script> - <script type="text/javascript" src="js/partkeepr.js"></script> - <?php - } - ?> - <script type="text/javascript" src="js/org.phpjs.lib/php.default.min.js"></script> - <script type="text/javascript" src="js/webcam.js"></script> - - </head> -<body> -<div id="loading"><span class="logo"></span></div> -<?php -// @todo put that somewhere else. This is only a stupid hack. -function return_bytes ($size_str) -{ - switch (substr ($size_str, -1)) - { - case 'M': case 'm': return (int)$size_str * 1048576; - case 'K': case 'k': return (int)$size_str * 1024; - case 'G': case 'g': return (int)$size_str * 1073741824; - default: return $size_str; - } -} +/* Information about maximum upload sizes */ +$maxPostSize = PartKeepr::getBytesFromHumanReadable(ini_get("post_max_size")); +$maxFileSize = PartKeepr::getBytesFromHumanReadable(ini_get("upload_max_filesize")); -$maxPostSize = return_bytes(ini_get("post_max_size")); -$maxFilesize = return_bytes(ini_get("upload_max_filesize")); - -$maxUploadSize = ($maxPostSize < $maxFilesize) ? $maxPostSize : $maxFilesize; +$aParameters["maxUploadSize"] = min($maxPostSize, $maxFileSize); +/* ImageMagick formats */ $imagick = new \Imagick(); -?> -<script type="text/javascript"> -window.maxUploadSize = <?php echo $maxUploadSize; ?>; -window.availableImageFormats = <?php echo json_encode($imagick->queryFormats()); ?>; +$aParameters["availableImageFormats"] = $imagick->queryFormats(); -<?php +/* Automatic Login */ if (Configuration::getOption("partkeepr.frontend.autologin.enabled", false) === true) { -?> -window.autoLoginUsername = "<?php echo Configuration::getOption("partkeepr.frontend.autologin.username"); ?>"; -window.autoLoginPassword = "<?php echo Configuration::getOption("partkeepr.frontend.autologin.password"); ?>"; -<?php + $aParameters["autoLoginUsername"] = Configuration::getOption("partkeepr.frontend.autologin.username"); + $aParameters["autoLoginPassword"] = Configuration::getOption("partkeepr.frontend.autologin.password"); } -?> -window.parameters = <?php echo json_encode($aParameters); ?>; -</script> -</body> -</html>- \ No newline at end of file + +/* Load and render the template */ +\Twig_Autoloader::register(); + +$loader = new \Twig_Loader_Filesystem(dirname(__FILE__) . '/templates/'); +$twig = new \Twig_Environment($loader); + +$template = $twig->loadTemplate("index.tpl"); + +echo $template->render(array( + "debug" => Configuration::getOption("partkeepr.frontend.debug", false), + "parameters" => $aParameters + ));+ \ No newline at end of file diff --git a/src/frontend/js/PartKeepr.js b/src/frontend/js/PartKeepr.js @@ -17,8 +17,8 @@ Ext.application({ PartKeepr.application = this; // Set static data of the server - PartKeepr.setMaxUploadSize(window.maxUploadSize); - PartKeepr.setAvailableImageFormats(window.availableImageFormats); + PartKeepr.setMaxUploadSize(window.parameters.maxUploadSize); + PartKeepr.setAvailableImageFormats(window.parameters.availableImageFormats); this.sessionManager = new PartKeepr.SessionManager(); diff --git a/src/frontend/templates/index.tpl b/src/frontend/templates/index.tpl @@ -0,0 +1,39 @@ +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" + "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> +<html> + <head> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> + <title>PartKeepr</title> + + <link href='http://fonts.googleapis.com/css?family=Ubuntu:400,700italic' rel='stylesheet' type='text/css'> + + <!-- Include the ExtJS CSS Theme --> + <link rel="stylesheet" type="text/css" href="css/partkeepr-theme.css"/> + + <link rel="stylesheet" type="text/css" href="js/Ext.ux/statusbar/css/statusbar.css"/> + + <link rel="stylesheet" type="text/css" href="css/PartKeepr.css"/> + + <link rel="icon" href="favicon.ico"/> + + <!-- Include the ExtJS JavaScript Library --> + <script type="text/javascript" src="extjs/bootstrap.js"></script> + {% if debug %} + <script type="text/javascript" src="extjs/ext-all-debug.js"></script> + <script type="text/javascript" src="js/partkeepr-debug.js"></script> + {% else %} + <script type="text/javascript" src="extjs/ext-all.js"></script> + <script type="text/javascript" src="js/partkeepr.js"></script> + {% endif %} + + <script type="text/javascript" src="js/org.phpjs.lib/php.default.min.js"></script> + <script type="text/javascript" src="js/webcam.js"></script> + + </head> +<body> +<div id="loading"><span class="logo"></span></div> +<script type="text/javascript"> +window.parameters = {{ parameters|json_encode|raw }}; +</script> +</body> +</html>+ \ No newline at end of file diff --git a/src/setup/index.html b/src/setup/index.html @@ -46,6 +46,7 @@ <script type="text/javascript" src="js/SetupTests/PHPTest.js"></script> <script type="text/javascript" src="js/SetupTests/PHPPrerequisitesTest.js"></script> <script type="text/javascript" src="js/SetupTests/DoctrineTest.js"></script> + <script type="text/javascript" src="js/SetupTests/TwigTest.js"></script> <script type="text/javascript" src="js/SetupTests/DatabaseConnectivityTest.js"></script> <script type="text/javascript" src="js/SetupTests/PHPSettingsTest.js"></script> <script type="text/javascript" src="js/SetupTests/FilesystemPermissionTest.js"></script> diff --git a/src/setup/js/Cards/PrerequisitesTestCard.js b/src/setup/js/Cards/PrerequisitesTestCard.js @@ -14,6 +14,7 @@ Ext.define('PartKeeprSetup.PrerequisitesTestCard', { this.tests.push(new PartKeeprSetup.PHPPrerequisitesTest()); this.tests.push(new PartKeeprSetup.PHPSettingsTest()); this.tests.push(new PartKeeprSetup.DoctrineTest()); + this.tests.push(new PartKeeprSetup.TwigTest()); this.tests.push(new PartKeeprSetup.FilesystemPermissionTest()); } }); diff --git a/src/setup/js/SetupTests/TwigTest.js b/src/setup/js/SetupTests/TwigTest.js @@ -0,0 +1,9 @@ +/** + * Tests is doctrine is installed correctly on the server + */ +Ext.define('PartKeeprSetup.TwigTest', { + extend: 'PartKeeprSetup.AbstractTest', + url: 'tests/check-twig.php', + name: "PHP", + message: "Testing for the twig template engine" +});+ \ No newline at end of file diff --git a/src/setup/tests/check-twig.php b/src/setup/tests/check-twig.php @@ -0,0 +1,35 @@ +<?php +@include_once 'Twig/Autoloader.php'; + +$aWarnings = array(); + +/** + * Check if the Doctrine Common classloader can be loaded + */ +$installTwigError = "Twig needs to be installed and in the PHP include_path. "; +$installTwigError .= "You can install twig on most unix systems using: <br/>"; +$installTwigError .= "<br/><code>pear channel-discover pear.twig-project.org<br/>"; +$installTwigError .= "pear install twig/Twig</code><br/>"; + +if (!class_exists("\\Twig_Autoloader")) { + $installTwigError .= "<small>Twig_Autoloader was not found</small>"; + echo json_encode(array("error" => true, "message" => $installTwigError)); + exit; +} + +\Twig_Autoloader::register(); + + +/* + * Check for supported twig versions + */ +if (version_compare("1.6.0", \Twig_Environment::VERSION) > 0) { + $versionInvalidMessage = "Twig is installed, but needs to be at Version 1.6.0 or higher. "; + $versionInvalidMessage .= "Please run pear upgrade-all to bring your packages up-to-date."; + + echo json_encode(array("error" => true, "message" => $versionInvalidMessage)); + exit; + +} +echo json_encode(array("error" => false, "warnings" => $aWarnings)); +exit;