<email ⁄>
<windows live messenger ⁄>
<myCurriculum type="pdf" ⁄>
<?php /** * Configuration Settings example file * * Contains static and constants * * @author pedrocorreia.net */ class ConfigurationSettings{ const LANG_ADD_RECORD = "Add Record"; const LANG_REMOVE_RECORD = "Remove Record"; const LANG_UPDATE_RECORD = "Update Record"; const TBL_WORKERS_DATA = "workers_data"; const TBL_WORKERS_DATA_PK = "pk_worker"; static $_SIGNUP_LIMIT = "2012-01-01"; static $_SIGNUP_MAX_SEATS = 500; } ?>
/** * Manage Events * * @see http://ejohn.org/projects/flexible-javascript-events/ */ var Events = function(){ var _add = function(obj, type, fn) { if ( obj.attachEvent ) { obj['e'+type+fn] = fn; obj[type+fn] = function(){obj['e'+type+fn]( window.event );} obj.attachEvent( 'on'+type, obj[type+fn] ); } else{ obj.addEventListener( type, fn, false ); } }, _remove = function(obj, type, fn){ if ( obj.detachEvent ) { obj.detachEvent( 'on'+type, obj[type+fn] ); obj[type+fn] = null; } else{ obj.removeEventListener( type, fn, false ); } }; return {Add: _add, Remove: _remove} }();
<?php /** * Export a given Class, Constants and/ or Static properties * * @author pedrocorreia.net */ class ExportSettings{ private static $_allowed_prefix = array(); /** * Export settings to an Array * * @param Object Class * @param Integer Type: { 1: Constants and Static (default); 2: Only Constants; 3: Only Static } * @param Optional Array Restrict allowed prefixes to export * @return Array */ public static function ToArray($class, $type = 1, $allowed_prefix = null){ $self = new ReflectionClass($class); $allowed_prefix = ($allowed_prefix && !is_array($allowed_prefix)) ? array($allowed_prefix) : $allowed_prefix; $type = (!$type || !is_numeric($type)) ? 1 : $type; self::$_allowed_prefix = $allowed_prefix; switch ($type){ case 1: //export constants and static properties $arrProperties = array_merge($self->getConstants(), $self->getStaticProperties()); break; case 2: //export only constants $arrProperties = $self->getConstants(); break; case 3: //export only static properties $arrProperties = $self->getStaticProperties(); break; } return array_intersect_key( $arrProperties, array_flip( array_filter( array_keys($arrProperties), array("self", "filter_array_by_key")) ) ); } /** * Export settings to json format * * @param Object Class * @param Optional Integer Type: { 1: Constants and Static (default); 2: Only Constants; 3: Only Static } * @param Optional Array Restrict allowed prefixes to export * @return Object Json */ public static function ToJson($export_class, $type = 1, $allowed_prefix = null){ return json_encode(self::ToArray($export_class, $type, $allowed_prefix)); } /** * Export settings to json format, allowing it to declare a Javascript variable * * @param Object Class * @param String Json variable name * @param Integer Type: { 1: Constants and Static (default); 2: Only Constants; 3: Only Static } * @param Array Restrict allowed prefixes to export * @return String Json javascript variable declaration */ public static function ToJsonVariable($export_class, $json_var, $type = 1, $allowed_prefix = null){ return sprintf("var %s = %s;", $json_var, self::ToJson($export_class, $type, $allowed_prefix)); } /** * Filter key prefixes * * @param String Filter * @uses self::$_allowed_prefix * @return Boolean */ private static function filter_array_by_key($key){ $found = false; if(is_array(self::$_allowed_prefix)){ for ($i=0, $count=sizeof(self::$_allowed_prefix); $i<$count; $i++){ if(substr_compare(self::$_allowed_prefix[$i], $key, 0, strlen($key))===0){ $found = true; break; } } } else { //no filter was specified, therefore, all keys are valid $found = true; } return $found; } } ?>
<?php /** * Generate javascript file using Json format * @author: pedrocorreia.net */ require_once("../../_Class_/class.ExportSettings.php"); require_once("../../_Class_/class.ConfigurationSettings.php"); echo ExportSettings::ToJsonVariable(ConfigurationSettings, "settings_all", 1), "\r\n\r\n", ExportSettings::ToJsonVariable(ConfigurationSettings, "settings_constants", 2), "\r\n\r\n", ExportSettings::ToJsonVariable(ConfigurationSettings, "settings_static", 3); ?>
/** * Read json object, in this example we'll only output it to a html div, * but you can use it, just like a regular json object * * @author: pedrocorreia.net */ Events.Add(window, "load", function(){ var _output = []; _output.push("settings_all.LANG_ADD_RECORD = " + settings_all.LANG_ADD_RECORD); _output.push("<br/>settings_all._SIGNUP_LIMIT = " + settings_all._SIGNUP_LIMIT); _output.push("<br/><br/>settings_constants.LANG_ADD_RECORD = " + settings_constants.LANG_UPDATE_RECORD); _output.push("<br/>settings_constants.TBL_WORKERS_DATA = " + settings_constants.TBL_WORKERS_DATA); _output.push("<br/><br/>settings_static._SIGNUP_MAX_SEATS = " + settings_static._SIGNUP_MAX_SEATS); _output.push("<br/>settings_static._SIGNUP_LIMIT = " + settings_static._SIGNUP_LIMIT); _output.push("<br/><br/> <b>All Values in \"settings_all\"</b><br/>"); for(key in settings_all){ _output.push("settings_all." + key + " = " + settings_all[key] + "<br/>"); } document.getElementById("javascript_console").innerHTML = _output.join(""); });
<?php require_once("_Class_/class.ExportSettings.php"); require_once("_Class_/class.ConfigurationSettings.php"); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="_Assets_/Javascript/export_settings.php"></script> <script type="text/javascript" src="_Assets_/Javascript/Events.js"></script> <script type="text/javascript" src="_Assets_/Javascript/Init.js"></script> </head> <body> <strong>Javascript Console</strong><br/> <div id="javascript_console"></div> <br/><br/> <strong>PHP output Console</strong><br/><br/> <div id="php_console"> <?php echo "<strong>Export <u>All</u> object properties, with prefix \"LANG\"</strong>", "<pre>", print_r(ExportSettings::ToJson(ConfigurationSettings, 1, "LANG"), 1), "</pre>", "<br/><strong>Export only <u>Static</u> properties with prefix \"_SIGNUP\" and/or \"TBL\"</strong>", " (in this case \"TBL\" properties are Constants, ie, won't be exported)", "<pre>", print_r(ExportSettings::ToJson(ConfigurationSettings, 3, array("_SIGNUP", "TBL")), 1), "</pre>", "<br/><strong>Export Array with <u>All</u> settings</strong>", "<pre>", print_r(ExportSettings::ToArray(ConfigurationSettings), 1), "</pre>"; ?> </div> </body> </html>