/** * @param array $schema * @param bool $returnHtml If true, HTML is returned, otherwise an array of fields * * @return string */ protected static function _buildFormFields($schema, $returnHtml = true) { $_form = null; $_fields = array(); foreach ($schema as $_field => $_settings) { // No private fields are ever rendered if (false !== Option::get($_settings, 'private', false)) { continue; } $_value = Option::get($_settings, 'value', Option::get($_settings, 'default')); $_label = Option::get($_settings, 'label', Inflector::display($_field)); $_labelAttributes = Option::get($_settings, 'label_attributes', array('for' => $_field)); $_attributes = array_merge(array('name' => $_field, 'id' => $_field), Option::get($_settings, 'attributes', array())); if (false !== ($_required = Option::get($_settings, 'required', false))) { $_attributes['class'] = HtmlMarkup::addValue(Option::get($_attributes, 'class'), 'required'); } $_form .= HtmlMarkup::label($_labelAttributes, $_label); $_fields[$_field]['label'] = array('value' => $_label, 'attributes' => $_labelAttributes); switch ($_settings['type']) { case 'text': $_form .= HtmlMarkup::tag('textarea', $_attributes, $_value) . PHP_EOL; $_fields[$_field] = array_merge($_fields[$_field], array('type' => 'textarea', 'contents' => $_value), $_attributes); break; case 'select': $_attributes['size'] = Option::get($_settings, 'size', 1); $_attributes['value'] = $_value; $_fields[$_field] = array_merge($_fields[$_field], array('type' => 'select', 'data' => Option::get($_settings, 'options', array())), $_attributes); $_form .= HtmlMarkup::select($_fields[$_field]['data'], $_attributes) . PHP_EOL; break; default: $_attributes['maxlength'] = Option::get($_settings, 'length'); $_attributes['value'] = $_value; $_attributes['type'] = 'text'; $_fields[$_field] = array_merge($_fields[$_field], array('type' => 'input', 'value' => $_value), $_attributes); $_form .= HtmlMarkup::tag('input', $_attributes, null, true, true) . PHP_EOL; break; } } return $returnHtml ? $_form : $_fields; }
/** * @param array $options * @param bool $fromCreate * * @throws DreamFactory\Platform\Exceptions\BadRequestException */ public function actionUpdate($options = array(), $fromCreate = false) { $_id = $_schema = $_errors = null; // New provider if (false !== $fromCreate && null !== ($_resourceId = FilterInput::request('resource'))) { // New request $_model = ResourceStore::model($_resourceId); $_displayName = Inflector::display($_resourceId); $_schema = $this->_loadConfigSchema($_resourceId, $_model->hasAttribute('config_text') ? $_model->config_text : array()); } else { // Requests will come in like: /admin/update/<resource>/<id> list($_resourceId, $_id) = $this->_parseRequest(); $_displayName = Inflector::display($_resourceId); // Load it up. /** @var $_model BasePlatformSystemModel */ if (null !== ($_model = ResourceStore::model($_resourceId)->findByPk($_id))) { $_schema = $this->_loadConfigSchema($_resourceId, $_model->hasAttribute('config_text') ? $_model->config_text : array()); if (Pii::postRequest()) { // On a post, update if (null === ($_data = Option::get($_POST, $_displayName))) { throw new BadRequestException('No payload received.'); } /** @var Provider $_model */ if ($_model->hasAttribute('config_text')) { $_len = strlen(static::SCHEMA_PREFIX); $_config = Option::clean($_model->config_text); foreach ($_data as $_key => $_value) { if (static::SCHEMA_PREFIX == substr($_key, 0, $_len)) { $_newKey = str_replace(static::SCHEMA_PREFIX, null, $_key); if (isset($_config[$_newKey])) { $_config[$_newKey] = $_value; } unset($_data[$_key]); } } $_model->config_text = $_config; unset($_data['config_text']); } $_model->setAttributes($_data); $_model->save(); Pii::setState('status_message', 'Resource Updated Successfully'); $this->redirect('/admin/' . $_resourceId . '/update/' . $_id); } } if ($_model->hasAttribute('name')) { $_displayName = $_model->name; } else { if ($_model->hasAttribute('provider_name')) { $_displayName = $_model->provider_name; } else { if ($_model->hasAttribute('api_name')) { $_displayName = $_model->api_name; } } } } $this->render('update', array('model' => $_model, 'schema' => $_schema, 'errors' => $_errors, 'resourceName' => $_resourceId, 'displayName' => $_displayName, 'update' => null !== $_id)); }
<?php /** * @var WebController $this * @var array $schema * @var BasePlatformSystemModel $model * @var array $_formOptions Provided by includer * @var array $errors Errors if any * @var string $resourceName The name of this resource (i.e. App, AppGroup, etc.) Essentially the model name * @var string $displayName * @var array $_data_ */ use DreamFactory\Yii\Utility\BootstrapForm; use Kisma\Core\Utility\Inflector; $update = false; $_form = new BootstrapForm(); $_options = array('breadcrumbs' => array('Admin Dashboard' => '/admin', Inflector::display($resourceName) . 's' => '/admin', $displayName => false)); $_formOptions = $_form->pageHeader($_options); // Render the form $this->renderPartial('_' . $resourceName . '_form', $_data_);
$_apps .= HtmlMarkup::tag('option', $_attributes, $_model->name); unset($_model); } unset($_models); } $_models = ResourceStore::model('provider')->findAll(array('select' => 'id, provider_name, api_name', 'order' => 'provider_name')); if (!empty($_models)) { $_first = true; /** @var Provider[] $_models */ foreach ($_models as $_model) { $_attributes = array('name' => $_model->api_name, 'value' => $_model->api_name, 'data-provider-id' => $_model->id, 'data-provider-tag' => Inflector::neutralize($_model->provider_name) . ':' . $_model->api_name, 'data-profile-resource' => strtolower($_model->provider_name) == 'facebook' ? '/me' : '/user'); if ($_first) { $_attributes['selected'] = 'selected'; $_first = false; } $_providers .= HtmlMarkup::tag('option', $_attributes, $_model->api_name . ' (' . Inflector::display($_model->provider_name) . ')'); $_providerCache->{$_model->api_name} = $_model->getAttributes(); unset($_model, $_config, $_attributes, $_profileResource); } unset($_models); } // Default url $_defaultUrl = '/rest/system/user'; ?> <!DOCTYPE html> <html lang="en"> <head> <title>DreamFactory Portal Sandbox</title> <meta charset="utf-8" /> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" />
/** * Returns the constant name as a string * * @param string $constant * @param bool $flipped If false, the $constantValue should contain the constant name and the value will be returned * @param bool $pretty If true, returned value is prettified (acme.before_event becomes "Acme Before Event") * * @throws \InvalidArgumentException * @return string */ public static function nameOf($constant, $flipped = true, $pretty = true) { if (in_array($constant, array_keys($_constants = static::getDefinedConstants($flipped)))) { return $pretty ? Inflector::display(Inflector::neutralize($_constants[$constant])) : $_constants[$constant]; } throw new \InvalidArgumentException('A constant with the value of "' . $constant . '" does not exist.'); }