/** * This method composes the form and returns the output * * @return string Output of the form */ public function execute() { // Fetch the model needed for this form. We will need it because authorization can depend on the model itself if ($this->id) { $factory = One_Repository::getFactory($this->scheme->getName()); $model = $factory->selectOne($this->id); } else { $model = One::make($this->scheme->getName()); } if (is_null($model)) { throw new One_Exception('Could not generate a form for scheme "' . $this->scheme->getName() . '" with id ' . $this->id); } $this->authorize($this->scheme->getName(), $model->id); $session = One_Repository::getSession(); $formFile = $this->getVariable('formFile', 'form'); $form = One_Form_Factory::createForm($this->scheme, $formFile, $this->getVariable('lang'), 'oneForm', ''); // Create a DOM and render the form in it $dom = One_Repository::createDom(); $form->render($model, $dom); // print_r($dom); $this->view->setModel($model); $this->view->set('scheme', $this->scheme); $this->view->set('form', $form); $this->view->set('dom', $dom); $this->view->set('errors', $session->get('errors', 'OneFormErrors')); $vForm = $this->view->show(); $session->remove('errors', 'OneFormErrors'); $session->remove('posted', 'OneFormErrors'); return $vForm; }
public function testMultipleSingletons() { $instance_one = One::getInstance(); $instance_two = Two::getInstance(); $this->assertInstanceOf("oscarpalmer\\Mordin\\Singleton", $instance_one); $this->assertInstanceOf("oscarpalmer\\Mordin\\Singleton", $instance_two); $this->assertNotSame($instance_one, $instance_two); }
/** * Convert an array to an instance of the specified scheme * * @param One_Scheme $scheme * @param array $row * @return One_Model */ private function &arrayToInstance(&$scheme, &$row) { // check the scheme cache $idAttribute = $scheme->getIdentityAttribute(); $id = $row[$idAttribute->getName()]; $cached = One_Model_IdentityMap::find($scheme->getName(), $id); if ($cached) { return $cached; } // not found : create a new instance // @TODO: use a specific class specified in the scheme $model = One::make($scheme->getName()); $model->fromArray($row); // fire afterLoad event for model $model->afterLoad(); One_Model_IdentityMap::add($model); return $model; }
/** * This method renders the search form and if needed fetches and show the search results * @see plugins/system/one/lib/action/One_Controller_Action#execute() */ public function execute() { $this->authorize($this->scheme, $model->id); $results = NULL; $model = One::make($this->scheme->getName()); $cx = new One_Context(); $session = One_Repository::getSession(); if (!is_null($cx->get('doSearch'))) { $session->remove('usedSearchOptions', 'one_search'); $results = $this->performSearch(); $session->set('usedSearchOptions', $this->options, 'one_search'); } else { if (!is_null($cx->get('doWeightSearch'))) { $session->remove('usedSearchOptions', 'one_search'); $results = $this->performWeightSearch(); $session->set('usedSearchOptions', $this->options, 'one_search'); } else { if (isset($this->options['savedSearch'])) { if ($session->varExists('usedSearchOptions', 'one_search')) { $this->options = $session->get('usedSearchOptions', 'one_search'); if (isset($this->options['doSearch'])) { $results = $this->performSearch(); } else { if (isset($this->options['doWeightSearch'])) { $results = $this->performWeightSearch(); } } } } else { $session->remove('usedSearchOptions', 'one_search'); $session->remove('results', 'one_search'); } } } $dom = One_Repository::createDom(); $this->view->setModel($model); $this->view->set('scheme', $this->scheme); $this->view->set('formfile', $this->searchform); $this->view->set('results', $results); $vForm = $this->view->show(); return $vForm; }
/** * Bind the model to the widget * * @param One_Model $model */ public function bindModel($model) { $value = $this->requestValue(); // bad name preg_match_all('/([^:]+):([^,]+),?/', $value, $matches); $parts = explode(':', $this->getCfg('role'), 2); $targetAttr = $this->getCfg('targetAttribute'); $tScheme = One_Repository::getScheme($model->getScheme()->getLink($parts[1])->getTarget()); $tIdAttr = $tScheme->getIdentityAttribute()->getName(); $value = array(); if (0 < count($matches[0])) { foreach ($matches[0] as $key => $tmp) { $usedKey = $matches[1][$key]; $usedValue = $matches[2][$key]; if (null === One_Repository::selectOne($tScheme->getName(), $usedKey)) { $tModel = One::make($tScheme->getName()); $tModel->{$targetAttr} = $usedValue; $tModel->insert(); $usedKey = $tModel->{$tIdAttr}; } $value[$usedKey] = $usedKey; } } if (is_null($value)) { $value = array(); } // if == NULL, set to 0, because NULL won't be picked up by $model->__modified $attributeName = $this->_name; // When the attributeName starts with 'r__', we're not saving an attribute but a relation if (preg_match('/^r__(.*)_(((?!._).)*)$/iU', $attributeName, $tmp)) { $relName = $tmp[1]; $relRole = $tmp[2]; $relValue = $value; $model->setRelated($relRole, $relValue); } else { $model->{$attributeName} = $value; } }
?> Answer: <input type=text name=answer> <p> <input type=submit name=submit value=Proceed> <input type=submit name=reset value=Reset> </form> <?php } function reset() { global $_REQUEST; return $_REQUEST["reset"]; } } class MyState extends State { var $message, $n1, $n2, $m1, $m2; function MyState($message, $n1, $n2, $m1, $m2) { $this->message = $message; $this->n1 = $n1; $this->n2 = $n2; $this->m1 = $m1; $this->m2 = $m2; } function isEmpty() { return !$this->message; } } $one = new One(); $one->execute();
/** * Convert an array to an instance of the specified scheme * * @param One_Scheme $scheme * @param array $row * @return One_Model */ protected function arrayToInstance(One_Scheme $scheme, $row) { // check the scheme cache $idAttribute = $scheme->getIdentityAttribute(); $id = $row[$idAttribute->getName()]; $cached = One_Model_IdentityMap::find($scheme->getName(), $id); if ($cached) { return $cached; } // not found : create a new instance //TODO: use a specific class specified in the scheme $model = One::make($scheme->getName()); // PD17OCT08: for optimal performance, raw-store the data row entirely $model->fromArray($row); // fire afterLoad event for model $model->afterLoad(); One_Model_IdentityMap::add($model); return $model; }
/** * @param One_Scheme $scheme * * POST schemename * Create a new instance */ public static function restPost($scheme) { try { // retrieve input data from body (a JSON encoded structure) $request = $this->slim->request(); $body = $request->getBody(); $input = json_decode($body, true); // instantiate and fill the model $model = One::make($scheme->getName()); $model->fromArray($input); $model->insert(); echo self::toPrettyJson($model->asRestResponse()); } catch (Exception $e) { $this->slim->response()->status(400); $this->slim->response()->header('X-Status-Reason', $e->getMessage()); } }
/** * Clear the query cache */ public static function clear_cache($table_name = null, $connection_name = self::DEFAULT_CONNECTION) { self::$_query_cache = []; if (isset(self::$_config[$connection_name]['clear_cache']) && is_callable(self::$_config[$connection_name]['clear_cache'])) { return call_user_func_array(self::$_config[$connection_name]['clear_cache'], array($table_name, $connection_name)); } }
/** * Return an empty instance of this model * * @return One_Model */ public function getInstance() { return One::make($this->scheme->getName()); }
$this->d = $letters_4; } public function printEverthing() { echo $this->a . "," . $this->b . "," . $this->c . "," . $this->d; } } class Three extends Two { public $e; public $f; function __construct($letters_1, $letters_2, $letters_3, $letters_4, $letters_5, $letters_6) { $this->a = $letters_1; $this->b = $letters_2; $this->c = $letters_3; $this->d = $letters_4; $this->e = $letters_5; $this->f = $letters_6; } public function printEverthing() { echo $this->a . "," . $this->b . "," . $this->c . "," . $this->d . "," . $this->e . "," . $this->f; } } $object1 = new One("dog", "cat" . PHP_EOL); $object2 = new Two("horse", "pig", "squirrel", "monkey" . PHP_EOL); $object3 = new Three("woodchuck", "mole", "giraffe", "shark", "deer", "robin"); $object1->printEverthing(); $object2->printEverthing(); $object3->printEverthing();
/** * Validate whether the form has been submitted correctly * * @param boolean $root * @param One_Form_Container_Abstract $container * @return boolean */ public function validate($root = true, $container = NULL) { $checksOut = true; $oc = new One_Context(); $scheme = $oc->get('scheme'); $schemes = One::meta('schemes'); if (in_array($scheme, $schemes)) { $scheme = One_Repository::getScheme($scheme); $use = $root ? $this : $container; foreach ($use->getContent() as $widget) { if ($widget instanceof One_Form_Widget_Abstract) { $attr = $scheme->getAttribute($widget->getName()); if ($attr instanceof One_Scheme_Attribute) { $type = strtolower(str_replace('One_Scheme_Attribute_Type', '', get_class($attr->getType()))); $widget->setCfg('type', $type); } if (!$widget->validate()) { $checksOut = false; $this->_errors[$widget->getName()]['error'] = $widget->getErrors(); $this->_errors[$widget->getName()]['label'] = $widget->getLabel(); } } else { if ($widget instanceof One_Form_Container_Abstract) { if (!self::validate(false, $widget)) { $checksOut = false; } } } } return $checksOut; } else { return false; } }
<?php trait Simple { public function className() { return __CLASS__ . "<br>"; } } class One { use Simple; } class Two { use Simple; } $one = new One(); echo $one->className(); $two = new Two(); echo $two->className();
/** * Gets an instance of a scheme according to a path * * @param string $path * @return One_Model */ public function get($path) { $parts = explode('.', trim(strtolower($path))); return One::make($parts[0]); }
public static function fetchAllRoutings() { if (!self::$_schemesFetched) { $schemeNames = One::meta('schemes'); foreach ($schemeNames as $schemeName) { One_Repository::getScheme($schemeName); } self::$_schemesFetched = true; } }
public function getScheme($schemeName) { return One::meta("schemes/{$schemeName}"); }
/** * This method validates a submitted form and returns to the proper page according to whether the submission * contained errors or whether the form was saved or applied */ public function execute() { $session = One_Repository::getSession(); $isNew = false; if ($this->id) { // update existing $factory = One_Repository::getFactory($this->scheme->getName()); $model = $factory->selectOne($this->id); if (is_null($model) && !$factory->getScheme()->getIdentityAttribute()->isAutoInc()) { $model = One::make($this->scheme->getName()); $isNew = true; } } else { $model = One::make($this->scheme->getName()); } $idAttrName = $model->getScheme()->getIdentityAttribute()->getName(); $this->authorize($this->scheme->getName(), $model->{$idAttrName}); $formFile = $this->getVariable('formFile', 'form'); $form = One_Form_Factory::createForm($this->scheme, $formFile, $this->getVariable('lang'), 'oneForm', ''); $flow = One_Controller_Flow::getInstance($this->scheme)->getRedirects(); $noErrors = $form->validate(); if ($noErrors || is_array($noErrors) && count($noErrors) == 0) { $form->bindModel($model); if ($this->id && !$isNew) { $model->update(); $id = $this->id; } else { $model->insert(); $idAttr = $this->scheme->getIdentityAttribute()->getName(); $id = $model->{$idAttr}; } $this->model = $model; // handle redirects // @TODO this code can use some cleanup $redirects = array_merge($flow, $form->getRedirects()); $todo = is_null($this->getVariable('action')) ? $this->getVariable('task') : $this->getVariable('action'); if (isset($this->options['flow'])) { $todo = $this->options['flow']; } $redirect = $redirects['default']; if (isset($redirects[$todo])) { $redirect = $redirects[$todo]; } if (isset($redirect['id']) && strtoupper(trim($redirect['id'])) == '::ID::') { $redirect['id'] = $model->{$idAttrName}; } $redirect = $this->replaceOtherVariables($redirect); $this->controller->setRedirect($redirect); } else { $errors = base64_encode(serialize($form->getErrors())); $session->set('executedReturn', $model, 'executedForm'); $session->set('errors', $form->getErrors(), 'OneFormErrors'); $session->set('posted', $_REQUEST, 'OneFormErrors'); $id = $this->id; $toView = 'edit'; if (!is_null($this->getVariable('returnToOne'))) { parse_str(base64_decode($this->getVariable('returnToOne')), $returnVals); $this->controller->setRedirect($returnVals); } else { $redirects = array_merge($flow, $form->getRedirects()); $todo = 'default'; if (isset($this->options['flowerror'])) { $todo = $this->options['flowerror']; } elseif (isset($redirects['formerror'])) { $todo = 'formerror'; } $redirect = $redirects[$todo]; if (isset($redirect['id']) && strtoupper(trim($redirect['id'])) == '::ID::') { $redirect['id'] = $model->{$idAttrName}; } $redirect = $this->replaceOtherVariables($redirect); $this->controller->setRedirect($redirect); } return false; } }
{ echo "{$var} Students"; } } // Student = new Student(); // echo $student->total_students; echo Student::$total_students . "<br/>"; echo Student::welcome_students() . "<br/>"; echo Student::welcome_students("Greetings") . "<br/>"; Student::$total_students = 1; echo Student::$total_students . "<br/>"; // static variables are shared throughout the inheritance tree class One { static $foo; } class Two extends One { } class Three extends Two { } One::$foo = 1; Two::$foo = 2; Three::$foo = 3; echo One::$foo; // 3 echo Two::$foo; //3 echo Three::$foo; //3
/** * Hydrate this model instance with an associative array of data. * WARNING: The keys in the array MUST match with columns in the * corresponding database table. If any keys are supplied which * do not match up with columns, the database will throw an error. * * @param Array $data * @return void */ public function hydrate($data) { $this->One->hydrate($data)->force_all_dirty(); }