**/ abstract class twIf_Object_Access { // These are keept for compability const TYPE_BOOL = twIf_Widget_Control::TYPE_BOOL; const TYPE_ENUM = twIf_Widget_Control::TYPE_ENUM; const TYPE_TEXT = twIf_Widget_Control::TYPE_TEXT; const TYPE_FLOAT = twIf_Widget_Control::TYPE_FLOAT; const TYPE_NUMBER = twIf_Widget_Control::TYPE_NUMBER; const TYPE_UPLOAD = twIf_Widget_Control::TYPE_UPLOAD; const TYPE_STATIC = twIf_Widget_Control::TYPE_STATIC; const TYPE_PASSWORD = twIf_Widget_Control::TYPE_PASSWORD; const TYPE_TEXTAREA = twIf_Widget_Control::TYPE_TEXTAREA; const TYPE_CUSTOM = twIf_Widget_Control::TYPE_CUSTOM; const TYPE_DATE = twIf_Widget_Control::TYPE_DATE; const OUTPUT_BOOL = twIf_Widget_Control::TYPE_BOOL; const OUTPUT_CURRENCY_CENT = twIf_Widget_Control::TYPE_CURRENCY_CENT; const OUTPUT_CURRENCY_EURO = twIf_Widget_Control::TYPE_CURRENCY_EURO; const OUTPUT_DATE = twIf_Widget_Control::TYPE_DATE; const OUTPUT_TIME = 'time'; const OUTPUT_DATETIME = twIf_Widget_Control::TYPE_DATETIME; const OUTPUT_PERCENT = 'percent'; const OUTPUT_BYTES = 'BYTES'; // {{{ byteToHuman /** * Convert bytes to a human readable value * * @param int $Value * * @access private * @return string **/ private function byteToHuman ($n) { $E = array ('Byte', 'KiB', 'MiB', 'GiB', 'TiB'); $c = 0; while ($n > 1) { $n = $n / 1024; $c++; } $n *= 1024; $c--; if ($c < 0) $c = 0; $Locale = localeconv (); return number_format ($n, 2, $Locale ['decimal_point'], $Locale ['thousands_sep']) . ' ' . $E [$c]; } // }}} // {{{ currencyToHuman /** * Convert a currency value to a humane readable value * * @param float $Value * * @access private * @return string **/ private function currencyToHuman ($Value) { $i = round ($Value * 100) / 100; $s = number_format ($i, 2, '.', ''); if ((($p = strpos ($s, '.')) !== false)) { $b = substr ($s, 0, $p); $e = substr ($s, $p + 1); while (strlen ($e) < 2) $e .= '0'; return $b . ',' . $e; } return $s . ',00'; } // }}} function getClassVars ($Class) { // Generate first data $myFields = get_class_vars ($pClass = $Class); $Fields = array (); $Inherit = defined ($pClass . '::INHERIT_PARAMS'); // Strip fields from parent class while ($pClass = get_parent_class ($pClass)) { if (!$Inherit) { $pFields = get_class_vars ($pClass); foreach ($pFields as $pKey=>$Nirvana) unset ($myFields [$pKey]); } $Inherit = defined ($pClass . '::INHERIT_PARAMS'); } if (is_array ($myFields)) foreach ($myFields as $Key=>$Default) $Fields [strtolower ($Key)] = $Key; return $Fields; } // {{{ parseFieldDef /** * Parse a Field-Description and handle defaults etc. * * @access protected * @return array */ protected function parseFieldDef ($Key, $Definition, $Object, $Level = 0, $insertOverride = false) { // Create empty structure $Result = array ( 'Name' => $Key, 'Read' => false, 'ReadParams' => array (), 'ReadStrip' => '', 'Write' => false, 'WriteParams' => array (), 'Value' => '', 'Default' => (isset ($Definition ['Default']) ? $Definition ['Default'] : ''), 'Type' => (isset ($Definition ['Type']) ? $Definition ['Type'] : twIf_Widget_Control::TYPE_TEXT), 'Enum' => (isset ($Definition ['Enum']) ? $Definition ['Enum'] : array ()), 'ByVal' => (isset ($Definition ['ByVal']) ? $Definition ['ByVal'] : false), 'Caption' => (isset ($Definition ['Caption']) ? $Definition ['Caption'] : $Key), 'Class' => (isset ($Definition ['Class']) ? $Definition ['Class'] : ''), 'Filter' => (isset ($Definition ['Filter']) ? $Definition ['Filter'] : null), 'PrintF' => (isset ($Definition ['PrintF']) ? $Definition ['PrintF'] : null), 'Override' => null, // twIf_Widget_Table 'OrderKey' => (isset ($Definition ['OrderKey']) ? $Definition ['OrderKey'] : null), 'Multiplier' => (isset ($Definition ['Multiplier']) ? min (1, intval ($Definition ['Multiplier'])) : null), 'Total' => (isset ($Definition ['Total']) ? ($Definition ['Total'] == true) : false), 'Wrap' => (isset ($Definition ['Wrap']) ? ($Definition ['Wrap'] == true) : false), // twIf_Widget_Object 'Level' => (isset ($Definition ['Level']) ? intval ($Definition ['Level']) : $Level), 'Column' => (isset ($Definition ['Column']) ? intval ($Definition ['Column']) : 1), 'Rows' => (isset ($Definition ['Rows']) ? intval ($Definition ['Rows']) : 1), 'Hidden' => (isset ($Definition ['Hidden']) ? $Definition ['Hidden'] : false), 'Focus' => (isset ($Definition ['Focus']) ? $Definition ['Focus'] : false), 'Extended' => (array_key_exists ('Extended', $Definition) ? $Definition ['Extended'] : false), 'Prefix' => (isset ($Definition ['Prefix']) ? $Definition ['Prefix'] : ''), 'Suffix' => (isset ($Definition ['Suffix']) ? $Definition ['Suffix'] : ''), ); // Read-/Write-Access on object $ReadFunc = null; $WriteFunc = null; $ReadParams = array ('%s'); $WriteParams = array ('%s'); // Try to get old-style property-pointer from definition if (isset ($Definition ['Field'])) { $ReadFunc = $WriteFunc = $Definition ['Field']; if (isset ($Definition ['Params']) && is_array ($Definition ['Params'])) $ReadParams = $WriteParams = $Definition ['Params']; } // Get new-stlye read/write-pointer from definition if (isset ($Definition ['Read'])) $ReadFunc = $Definition ['Read']; if (isset ($Definition ['ReadParams']) && is_array ($Definition ['ReadParams'])) $ReadParams = $Definition ['ReadParams']; if (isset ($Definition ['Write'])) $WriteFunc = $Definition ['Write']; if (isset ($Definition ['WriteParams']) && is_array ($Definition ['WriteParams'])) $WriteParams = $Definition ['WriteParams']; if (!is_object ($Object) && !class_exists ($Object)) { # trigger_error (twIf_i18n::getText ('Invalid Object-Type %s', $Object)); } else { // Determine which class to handle if (is_object ($Object)) $Class = get_class ($Object); else $Class = $Object; // Load properties of class $Vars = self::getClassVars ($Class); // Validate the new pointers $ReadFunc = self::getPointer ($ReadFunc, $Object, $Vars, $Key); $WriteFunc = self::getPointer ($WriteFunc, $Object, $Vars, $Key); if ($Result ['Filter'] !== null) $Result ['Filter'] = self::getPointer ($Result ['Filter'], $Object, $Vars, $Key); if ($Result ['OrderKey'] !== null) $Result ['OrderKey'] = self::getPointer ($Result ['OrderKey'], $Object, $Vars, $Key, false); } if (($ReadFunc === null) && ($WriteFunc === null)) { if ($Object !== null) trigger_error (twIf_i18n::getText ('No read nor write function for %s found', $Key), E_USER_WARNING); return false; } $Result ['Read'] = $ReadFunc; $Result ['ReadParams'] = $ReadParams; $Result ['Write'] = $WriteFunc; $Result ['WriteParams'] = $WriteParams; if ((!is_object ($Object) && class_exists ($Object)) || ($insertOverride && is_array ($Result ['Read']) && ($Result ['Read'][0] == $Object))) $Result ['Override'] = true; elseif ($insertOverride) $Result ['Override'] = false; // Handle some other crap ;-) if (isset ($Definition ['Description']) && ($Definition ['Description'] != '')) $Result ['Description'] = $Definition ['Description']; if (isset ($Definition ['Strip'])) $Result ['ReadStrip'] = $Definition ['Strip']; if (isset ($Definition ['Multiplier']) && ($Definition ['Multiplier'] !== null)) { if (is_array ($Vars) && (($p = self::getPointer ($Definition ['Multiplier'], $Object, $Vars, false)) !== null)) $Result ['Multiplier'] = $p; else $Result ['Multiplier'] = (($v = intval ($Definition ['Multiplier'])) > 0 ? $v : null); } // Pre-generate control $Result ['__Control'] = twIf_Widget_Control::generateControl ($Result ['Type'], md5 (self::getFieldName ($Result))); return $Result; } // }}} // {{{ getControl /** * Retrive objective-representation of a control * * @param array $FieldData * * @access protected * @return object **/ protected function getControl (&$FieldData) { # TODO: Maybe create the control here on-demand return $FieldData ['__Control']; } // }}} // {{{ isReadFunction /** * Check wheter a specified definition contains a read-function * * @param array $Definition * * @access protected * @return bool **/ protected function isReadFunction ($Definition) { return is_array ($Definition ['Read']); } // }}} // {{{ getStatic /** * Small hack to store static values inside definitions * * @param mixed $Value * * @accress public * @return mixed **/ public function getStatic ($Value) { return $Value; } // }}} // {{{ getPointer /** * Retrive pointer to an object-method or -property * * @param string $Pointer * @param object $Object * @param array $Vars (optional) Cached variables on object * @param string $Default (optional) * @param bool $AllowCallback (optional) Allow the use of callbacks/function-calls (default) * * @access protected * @return mixed */ protected function getPointer ($Pointer, $Object, $Vars = null, $Default = false, $AllowCallback = true) { // Check for a null value and directly forward default if ($Pointer === null) { if ($Default !== null) return self::getPointer ($Default, $Object, $Vars, null, $AllowCallback); return $Pointer; } // Determine which class to handle if (is_object ($Object)) $Class = get_class ($Object); else $Class = $Object; // Check if this is a callback (and let them pass directly) if (is_array ($Pointer) && (count ($Pointer) == 2) && is_callable ($Pointer)) return $Pointer; // Check wheter this might be a function call if (($p = strpos ($Pointer, '(')) > 0) { if (!$AllowCallback) return null; $Function = substr ($Pointer, 0, $p); $Pointer = array ($Object, $Function); # TODO: Check if the function is really callable // We are accessing a property } else { // Load properties of class if (!is_array ($Vars)) $Vars = self::getClassVars ($Class); // Check if property really exists if (!isset ($Vars [strtolower ($Pointer)])) { // Check for magic function if (defined ($Class . '::TWIF_LAZY_VARIABLES') && (constant ($Class . '::TWIF_LAZY_VARIABLES') == 1)) return $Pointer; // Try to fall back to default elseif ($Default !== null) $Pointer = self::getPointer ($Default, $Object, $Vars, null, $AllowCallback); else $Pointer = null; } } return $Pointer; } // }}} // {{{ getPostValue /** * Get contents of a field as posted by the client * * @param array $FieldData * * @access protected * @return mixed */ protected function getPostValue ($FieldData, $forWrite = false) { // Generate POST-Name $Name = md5 (self::getFieldName ($FieldData)); // Handle static fields if ($FieldData ['Type'] == twIf_Widget_Control::TYPE_STATIC) return $FieldData ['Default']; $Control = self::getControl ($FieldData); if (isset ($FieldData ['Enum'])) $Control->setItems ($FieldData ['Enum']); if (($Value = $Control->getPostValue ()) === null) return null; if (($FieldData ['Type'] == twIf_Widget_Control::TYPE_ENUM) && ($Value === null) && $FieldData ['ByVal'] && isset ($_POST [$Name])) $Value = stripslashes ($_POST [$Name]); // Process any filter if ($FieldData ['Filter'] !== null) return call_user_func ($FieldData ['Filter'], $Value, $forWrite, null); return $Value; } // }}} // {{{ processValue /** * Process a given value to match the given field * * @see twIf_Object_Access::getPostValue() * @access protected * @return mixed **/ protected function processValue ($FieldData, $Value) { // Retrive handle of control $Control = self::getControl ($FieldData); // Setup the control (just to be sure if (isset ($FieldData ['Enum'])) $Control->setItems ($FieldData ['Enum']); // Forward the call $orgValue = $Value; $Value = $Control->processValue ($Value); if (($FieldData ['Type'] == twIf_Widget_Control::TYPE_ENUM) && ($Value === null) && $FieldData ['ByVal']) return $orgValue; return $Value; } // }}} // {{{ getFieldValue /** * Retrive the value of an object-property * * @param object $Object * @param array $Definition * * @access protected * @return mixed */ protected function getFieldValue ($Object, $Definition, $overrideCallbacks = false) { // Don't try to access anything if we haven't an object if (!is_object ($Object)) $Value = $Definition ['Default']; else { $Field = $Definition ['Read']; // Load the value from object if (is_array ($Field) && is_callable ($Field)) { if ($Definition ['Override'] !== null) $overrideCallbacks = $Definition ['Override']; if ($overrideCallbacks && (is_object ($Field [0]) || ($Object instanceof $Field [0]))) $Field [0] = $Object; $Params = $Definition ['ReadParams']; foreach ($Params as $i=>$Param) if ($Param == '%s') $Params [$i] = $Object; $Value = call_user_func_array ($Field, $Params); } elseif ($Field) $Value = $Object->$Field; else $Value = $Definition ['Default']; } // Optinally strip first bytes from value if ((($l = strlen ($Definition ['ReadStrip'])) > 0) && (substr ($Value, 0, $l) == $Definition ['ReadStrip'])) $Value = substr ($Value, $l); if ($Definition ['Filter'] !== null) $Value = call_user_func ($Definition ['Filter'], $Value, false, $Object); if ($Definition ['PrintF'] !== null) $Value = sprintf ($Definition ['PrintF'], $Value); return $Value; } // }}} protected function getFieldValueSimple ($Object, $FieldName) { $Definition = array ( 'Read' => self::getPointer ($FieldName, $Object, null, false), 'ReadParams' => array (), 'Default' => null, 'ReadStrip' => false, ); return self::getFieldValue ($Object, $Definition); } // {{{ getFieldName /** * Get the name of a field * * @param array $FieldDef * * @access protected * @return string */ protected function getFieldName ($FieldDef) { $Arr = null; if (is_array ($FieldDef ['Write'])) $Arr = $FieldDef ['Write']; elseif ($FieldDef ['Write'] !== null) return $FieldDef ['Name'] . '/' . $FieldDef ['Write']; elseif (is_array ($FieldDef ['Read'])) $Arr = $FieldDef ['Read']; elseif ($FieldDef ['Read'] !== null) return $FieldDef ['Name'] . '/' . $FieldDef ['Read']; if (!is_array ($Arr)) return false; if (is_object ($Arr [0])) return $FieldDef ['Name'] . '/' . strtolower (get_class ($Arr [0]) . '::' . $Arr [1]); else return $FieldDef ['Name'] . '/' . strtolower ($Arr [0] . '::' . $Arr [1]); } // }}} // {{{ formatOutput /** * Try to put an input-value into the right output-format * * @param mixed $Value Input-Value to format * @param string $Format Output-Format * @param mixed $Default (optional) Unused * @param bool $Table (optional) Output as table-cell * @param bool $Wrap (optional) Allow Word-Wrapping * @param string $Style (optional) Add these Style-Flags * * @access private * @return string */ protected function formatOutput ($Value, $Format, $Default = '', $Prefix = '', $Suffix = '') { // Check wheter to output an array of items if (is_array ($Value)) { # TODO: Remove Style-Tag $buf = str_replace (' __CLASS__', '', $Prefix) . '
' . $v . '