Created
December 2, 2015 19:38
-
-
Save chrisgo/942d2baa22d5871e243d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
defined('SYSPATH') or die('No direct script access.'); | |
/** | |
* Lookup | |
* | |
* @package Velocimedia | |
*/ | |
class Velocimedia_Lookup | |
{ | |
/** | |
* Outputs the modules, types and status into a file | |
*/ | |
public static function outputFile($filename) | |
{ | |
// Set the database | |
$database = 'lookup'; // Use the lookup database defined in config.php | |
// (1) Get all the data | |
$modules = Jelly::query('module') | |
->order_by('rank') | |
->select_all($database); | |
$types = Jelly::query('type') | |
->with('module') | |
->order_by('module') | |
->order_by('rank') | |
->select_all($database); | |
$statuses = Jelly::query('status') | |
->with('module') | |
->order_by('module') | |
->order_by('rank') | |
->select_all($database); | |
// (2) Start the output string | |
$str = '<?php ' . "\n\n" | |
. "// This file is generated when a user updates the lookup tables\n" | |
. "// in the database. Any changes made to this file will be overwritten the\n" | |
. "// time a lookup table is updated. Please make your changes to the\n" | |
. "// database!! \n\n" | |
. "// Generated on: " . Date::formatted_time('now') . "\n\n" | |
. "\n\n"; | |
if (isset($modules) and count($modules) > 0) { | |
$constants = []; | |
$names = []; | |
foreach ($modules as $module) { | |
$constants[$module->code] = $module->id; | |
$names[$module->id] = $module->name; | |
} | |
$str .= self::generateConstantsCode("Module", $constants, $names); | |
} | |
if (isset($types) and count($types) > 0) { | |
// Create a new array | |
$typesByModules = []; | |
foreach ($types as $type) { | |
if (! array_key_exists($type->module->name, $typesByModules)) { | |
$typesByModules[$type->module->name] = []; | |
} | |
array_push($typesByModules[$type->module->name], $type); | |
} | |
foreach ($typesByModules as $module => $currTypes) { | |
$constants = []; | |
$names = []; | |
$descriptions = []; | |
foreach ($currTypes as $type) { | |
$constants[$type->code] = $type->id; | |
$names[$type->id] = $type->name; | |
$descriptions[$type->id] = $type->description; | |
} | |
$str .= self::generateConstantsCode($module . '_Type', $constants, $names, $descriptions); | |
} | |
} | |
if (isset($statuses) and count($statuses) > 0) { | |
// Create a new array | |
$statusesByModules = []; | |
foreach ($statuses as $status) { | |
if (! array_key_exists($status->module->name, $statusesByModules)) { | |
$statusesByModules[$status->module->name] = []; | |
} | |
array_push($statusesByModules[$status->module->name], $status); | |
} | |
foreach ($statusesByModules as $module => $curr_statuses) { | |
$constants = []; | |
$names = []; | |
foreach ($curr_statuses as $status) { | |
$constants[$status->code] = $status->id; | |
$names[$status->id] = $status->name; | |
} | |
$str .= self::generateConstantsCode($module . '_Status', $constants, $names); | |
} | |
} | |
// Also grab the roles (this is based on the AUTH Stuff) | |
$roles = Jelly::query('Role')->select_all($database); | |
if (isset($roles) and count($roles) > 0) { | |
$constants = []; | |
$names = []; | |
foreach ($roles as $role) { | |
$code = strtoupper($role->name); | |
$code = str_replace('.', '_', $code); | |
$constants[$code] = $role->id; | |
$names[$role->id] = ucfirst(str_replace('_', ' ', $role->name)); | |
} | |
$str .= self::generateConstantsCode('Role', $constants, $names); | |
} | |
// ACLs - commented out for now, clashes with legacy model classes | |
/* | |
$acls = Jelly::query('Acl')->select_all(); | |
if (isset($acls) AND count($acls)>0) { | |
$constants = []; | |
$names = []; | |
foreach ($acls as $acl) { | |
$constants[$acl->code] = $acl->id; | |
$names[$acl->id] = ucfirst(str_replace('_', ' ', $acl->name)); | |
} | |
$str .= self::generateConstantsCode('Acl', $constants, $names); | |
} | |
*/ | |
// Preferences - commented out for now, clashes with legacy model classes | |
$preferences = Jelly::query('Preference')->select_all($database); | |
if (isset($preferences) and count($preferences) > 0) { | |
$constants = []; | |
$names = []; | |
foreach ($preferences as $preference) { | |
$code = str_replace('propertyrate.promise.', '', $preference->code); | |
$constants[$code] = $preference->id; | |
$names[$preference->id] = ucfirst(str_replace('_', ' ', $preference->name)); | |
} | |
$str .= self::generateConstantsCode('Preference', $constants, $names); | |
} | |
// Products | |
$products = Jelly::query('Product')->select_all($database); | |
if (isset($products) and count($products) > 0) { | |
$constants = []; | |
$names = []; | |
foreach ($products as $product) { | |
$constants[$product->code] = $product->id; | |
$names[$product->id] = ucfirst(str_replace('_', ' ', $product->name)); | |
} | |
$str .= "/**\n\n"; | |
$str .= self::generateConstantsCode('Product', $constants, $names); | |
$str .= "\n*/\n\n"; | |
} | |
// End class generation stuff | |
// (14) Clean up the output | |
$str .= "\n"; | |
// Dump out to file | |
file_put_contents($filename, $str); | |
} | |
/** | |
* Convenience method that builds very specific classes that | |
* define modules, types and statuses so that it can be loaded | |
* and used for auto-completion and constants on an IDE | |
*/ | |
private static function generateConstantsCode($className, $constants, $names, $descriptions = null) | |
{ | |
$str = ""; | |
if (strpos(strtoupper($className), 'UNKNOWN') > - 1) { | |
return $str; | |
} | |
$str .= "class " . str_replace(' ', '_', $className) . " \n{\n"; | |
if (! array_key_exists('UNKNOWN', $constants)) { | |
$str .= " const UNKNOWN = 0;\n"; | |
} | |
foreach ($constants as $code => $id) { | |
if (strpos($code, '.')) { | |
$code = preg_replace('/\B([A-Z])/', '_$1', $code); | |
} | |
$code = str_replace('.', '_', $code); | |
$code = str_replace(' ', '_', $code); | |
$code = str_replace('-', '_', $code); | |
$code = str_replace('NEW', 'CREATED', $code); | |
$code = str_replace('DEFAULT', 'STANDARD', $code); | |
$code = str_replace('BREAK', 'BREAK_TIME', $code); | |
$code = str_replace('#', ' NUMBER', $code); | |
if (is_numeric($code)) { | |
$code = 'TYPE_' . strtoupper($code); | |
} | |
$str .= ' const ' . strtoupper($code) . ' = ' . $id . ";\n"; | |
} | |
$str .= "\n"; | |
$str .= ' public static function getName($id)' . "\n"; | |
$str .= " {\n"; | |
$str .= ' switch($id)' . "\n"; | |
$str .= ' {' . "\n"; | |
foreach ($names as $id => $name) { | |
$name = str_replace("'", "\'", $name); | |
$name = str_replace("#", "NUMBER", $name); | |
$str .= " case " . $id . ": return '" . $name . "';\n"; | |
unset($name); | |
} | |
$str .= " default: return 'Unknown';\n"; | |
$str .= " }\n"; | |
$str .= " }\n"; | |
if (isset($descriptions) and count($descriptions) > 0) { | |
$str .= ' public static function getDescription($id)' . "\n"; | |
$str .= " {\n"; | |
$str .= ' switch($id)' . "\n"; | |
$str .= ' {' . "\n"; | |
foreach ($descriptions as $id => $description) { | |
$str .= " case " . $id . ": return '" . str_replace("'", "\'", $description) . "';\n"; | |
} | |
$str .= " default: return 'Unknown';\n"; | |
$str .= " }\n"; | |
$str .= " }\n"; | |
} | |
$str .= ' public static function getIdByName($name)' . "\n"; | |
$str .= " {\n"; | |
$str .= ' switch($name)' . "\n"; | |
$str .= ' {' . "\n"; | |
foreach ($names as $id => $name) { | |
$name = str_replace("'", "\'", $name); | |
$name = str_replace("#", "NUMBER", $name); | |
$str .= " case '" . $name . "': return " . $id . ";\n"; | |
unset($name); | |
} | |
$str .= " default: return 0;\n"; | |
$str .= " }\n"; | |
$str .= " }\n"; | |
$str .= ' public static function getCode($id)' . "\n"; | |
$str .= " {\n"; | |
$str .= ' switch($id)' . "\n"; | |
$str .= ' {' . "\n"; | |
foreach ($constants as $code => $id) { | |
$code = str_replace("#", "NUMBER", $code); | |
$str .= " case " . $id . ": return '" . $code . "';\n"; | |
} | |
$str .= " default: return 'Unknown';\n"; | |
$str .= " }\n"; | |
$str .= " }\n"; | |
$str .= ' public static function getIdByCode($code)' . "\n"; | |
$str .= " {\n"; | |
$str .= ' switch($code)' . "\n"; | |
$str .= ' {' . "\n"; | |
foreach ($constants as $code => $id) { | |
$code = str_replace("#", "NUMBER", $code); | |
$str .= " case '" . $code . "': return " . $id . ";\n"; | |
} | |
$str .= " default: return 0;\n"; | |
$str .= " }\n"; | |
$str .= " }\n"; | |
$str .= " public static function getNames()\n"; | |
$str .= " {\n"; | |
$str .= ' $array = array();' . "\n"; | |
foreach ($names as $id => $name) { | |
$name = str_replace("'", "\'", $name); | |
$name = str_replace("#", "NUMBER", $name); | |
$str .= ' $array[' . $id . "] = '" . $name . "';\n"; | |
} | |
$str .= ' return $array;' . "\n"; | |
$str .= " }\n"; | |
$str .= " public static function getCodes()\n"; | |
$str .= " {\n"; | |
$str .= ' $array = array();' . "\n"; | |
foreach ($constants as $code => $id) { | |
$code = str_replace("#", "NUMBER", $code); | |
$str .= ' $array[' . $id . "] = '" . $code . "';\n"; | |
} | |
$str .= ' return $array;' . "\n"; | |
$str .= " }\n"; | |
$str .= "}\n\n"; | |
return $str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment