Пример #1
0
 /**
  * @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));
 }