/** * Get the name of any model * * @param \Model $model * @return string The name of the model */ private function getModelName($model) { if ($model instanceof \NamedModel) { return $model->getName(); } if ($model instanceof \AliasModel) { return $model->getAlias(); } return $model->getId(); }
fURL::redirect(fURL::get()); } catch (fExpectedException $e) { fMessaging::create('error', fURL::get(), $e->getMessage()); } // Get manufacturers also for drop-down box $manufacturers = fRecordSet::build('Manufacturer', NULL, array('name' => 'asc')); include 'views/models/addedit.php'; } /** * Delete a model */ if ($action == 'delete') { // Get ID $id = fRequest::get('id', 'integer'); try { $m = new Model($id); if (fRequest::isPost()) { $m->delete(); fMessaging::create('success', fURL::get(), 'The model ' . $m->getName() . ' was successfully deleted.'); fURL::redirect(fURL::get()); } } catch (fNotFoundException $e) { fMessaging::create('error', fURL::get(), 'The model requested, ID ' . $id . ', could not be found.'); fURL::redirect($manage_url); } catch (fExpectedException $e) { fMessaging::create('error', fURL::get(), $e->getMessage()); } catch (fSQLException $e) { fMessaging::create('error', fURL::get(), 'Database error: ' . $e->getMessage()); } include 'views/models/delete.php'; }
public static function compare(Model $m1, Model $m2) { return (int) $m1->getName() - (int) $m2->getName(); }
/** * A very ugly function to parse the property type based on a massive arbitrary set of rules. * * @return string */ private function parseType() { //Spelling errors in the docs if (preg_match('/^((a\\s)?bool|true\\b|booelan)/i', $this->description)) { $type = Object::PROPERTY_TYPE_BOOLEAN; } //Spelling errors in the docs if (preg_match('/UTC$/', $this->getName())) { $type = Object::PROPERTY_TYPE_TIMESTAMP; } if (preg_match('/^Has[A-Z]\\w+/', $this->getName())) { $type = Object::PROPERTY_TYPE_BOOLEAN; } if (preg_match('/(^sum\\b|decimal|the\\stotal|total\\s(of|tax)|rate\\b|amount\\b)/i', $this->description)) { //If not the name of the field itself and not an 'amount type' if (stripos($this->name, 'name') === false && stripos($this->name, 'description') === false && stripos($this->description, 'amount type') === false) { $type = Object::PROPERTY_TYPE_FLOAT; } } if (preg_match('/(alpha numeric)/i', $this->description)) { $type = Object::PROPERTY_TYPE_STRING; } if (preg_match('/(^int(eger)?\\b)/i', $this->description)) { $type = Object::PROPERTY_TYPE_INT; } if (!isset($type) && preg_match('/(\\bdate\\b)/i', $this->description)) { $type = Object::PROPERTY_TYPE_DATE; } if (preg_match('/Xero (generated )?(unique )?identifier/i', $this->description)) { $type = Object::PROPERTY_TYPE_GUID; } if ($this->getModel()->getClassName() . 'ID' == $this->getName()) { $type = Object::PROPERTY_TYPE_GUID; $this->getModel()->setGUIDProperty($this); } if (preg_match('/(Code|ID)$/', $this->getName())) { $type = Object::PROPERTY_TYPE_STRING; } $result = null; if (!isset($type)) { //The ns hint for searching, look for subclasses of this first. $ns_hint = sprintf('%s\\%s', $this->getModel()->getNamespace(), $this->getModel()->getClassName()); if (preg_match('/see\\s(?<model>[^.]+)/i', $this->getDescription(), $matches)) { //Try NS'ing it with existing models... MNA htis is getting ugly. foreach ($this->getModel()->getAPI()->getModels() as $model) { $class_name = $model->getClassName(); $model_name = $matches['model']; if (strpos($model_name, $class_name) === 0) { //this means it starts with the model name $search_text = sprintf('%s\\%s', substr($model_name, 0, strlen($class_name)), substr($model_name, strlen($class_name))); $result = $this->getModel()->getAPI()->searchByKey(str_replace(' ', '', ucwords($search_text)), $this->getModel()->getNamespace()); } } } if ($result === null && substr_count($ns_hint, '\\') > 1) { $parent_ns_hint = substr($ns_hint, 0, strrpos($ns_hint, '\\')); $result = $this->getModel()->getAPI()->searchByKey($this->getName(), $parent_ns_hint); } if ($result === null) { $result = $this->getModel()->getAPI()->searchByKey($this->getName(), $ns_hint); } if ($result === null) { foreach ($this->links as $link) { $search_text = str_replace(' ', '', ucwords($link['text'])); $result = $this->getModel()->getAPI()->searchByKey($search_text, $ns_hint); //then try anchor if ($result === null) { if (preg_match('/#(?<anchor>.+)/i', $link['href'], $matches)) { $result = $this->getModel()->getAPI()->searchByKey($matches['anchor'], $ns_hint); } } } } //Otherwise, just have a stab again, this needs to be after other references if ($result === null) { if (preg_match('/see\\s(?<model>[^.]+)/i', $this->getDescription(), $matches)) { $result = $this->getModel()->getAPI()->searchByKey(str_replace(' ', '', ucwords($matches['model'])), $ns_hint); } } //Look for pointy bracketed references if ($result === null) { if (preg_match('/<(?<model>[^>]+)>/i', $this->getDescription(), $matches)) { $result = $this->getModel()->getAPI()->searchByKey(str_replace(' ', '', ucwords($matches['model'])), $ns_hint); } } //I have tried very hard to avoid special cases! if ($result === null) { if (preg_match('/^(?<model>Purchase|Sale)s?Details/i', $this->getName(), $matches)) { $result = $this->getModel()->getAPI()->searchByKey($matches['model'], $ns_hint); } } } if ($result instanceof Enum) { $type = Object::PROPERTY_TYPE_ENUM; } elseif ($result instanceof Model) { $type = Object::PROPERTY_TYPE_OBJECT; $this->related_object = $result; //If docs have case-typos in them, take the class name as authoritative. if (strcmp($this->getName(), $this->related_object->getName()) !== 0 && strcasecmp($this->getName(), $this->related_object->getName()) === 0) { $this->name = $this->related_object->getName(); } } if (!isset($type)) { $type = Object::PROPERTY_TYPE_STRING; } return $type; }
/** * Generate a slug from a base string that is unique in the database * @param String $base * @param Model $model Model object of this record * @param String $slugField Name of the slug column in the database * @param String $lang Optional language. Used with internationalized models * @return String $slug The generated unique slug */ public function generateUniqueSlug($base, $model, $slugField, $lang = null) { $slug = $this->generateSlug($base); $select = $model->getAdapter()->select()->from($model->getName(), 'COUNT(*)'); $this->_setWhereClause($select, $slugField, $slug, $model, $lang); $n = 1; while ($this->_rowsExist($select)) { $this->_incrementSlug($slug, ++$n, $base); $this->_setWhereClause($select, $slugField, $slug, $model, $lang); } return $slug; }
function damageRoll(Model $defender, $pow=0, $dice=2, AttackResult $result) { $rollTxt = "{$dice}D6"; $roll = Sim::rollDice($dice); $roll = $roll['roll']; $dmg = $this->getStr() + $pow + $roll; $arm = $defender->getArm(); if ($this->hasBuff('incite')) { $dmg += 2; } if ($defender->hasBuff('warp-arm')) { $arm += 2; } if ($defender->hasBuff('spiny-growth')) { $arm += 2; } $res = $dmg - $arm; $this->getSim()->debug("[{$defender->getName()}] (P {$pow} + S {$this->getStr()} + roll {$roll} ({$rollTxt}) = {$dmg} - {$arm})"); $result->setDamage($res > 0 ? $res : 0); return $result; }
function addSchematic(Model $model) { return $this->_models[$model->getName()] = $model; }