/** * Saves the CB plugin view after an edit view form submit * * @param array $options * @param array $actionPath * @param array $keyValues * @param array $parametersValues * @param SimpleXMLElement $viewModel * @param TableInterface $data * @param RegistryEditController $params * @param string $mode * @param string $dataModelType * @param PluginTable $plugin * @param SimpleXMLElement $dataModel * @param RegistryInterface $pluginParams * @param string $cbprevstate * @param int $ui * @return null|string NULL: ok, STRING: error */ protected function savePluginView($options, $actionPath, $keyValues, $parametersValues, $viewModel, $data, $params, &$mode, $dataModelType, $plugin, $dataModel, $pluginParams, $cbprevstate, $ui) { global $_CB_framework; new cbTabs(false, 2, -1, false); // prevents output of CB tabs js code until we are done with drawing (or redirecting) $resultingMsg = null; cbSpoofCheck('plugin'); $postArray = $this->input->getNamespaceRegistry('post')->asArray(); // List of variables to exclude from the $postArray: $exclude = array('option', 'cid', 'cbprevstate', cbSpoofField()); foreach ($actionPath as $k => $v) { $exclude[] = $k; } // Remove the exclude variables from the $postArray before being used in the below cases: foreach ($exclude as $v) { if (isset($postArray[$v])) { unset($postArray[$v]); } } // Fix multi-selects and multi-checkboxes arrays to |*|-delimited strings: $postArray = $this->recursiveMultiSelectFix($postArray); foreach ($postArray as $key => $value) { if (property_exists($data, $key)) { $postArray[$key] = is_array($value) ? json_encode($value) : $value; } } $errorMsg = null; switch ($dataModelType) { case 'sql:row': if ($ui == 2) { if (true !== ($error = RegistryEditView::validateAndBindPost($params, $postArray))) { $errorMsg = $error; break; } if (!$data->bind($postArray)) { $errorMsg = $data->getError(); break; } } else { RegistryEditView::setFieldsListArrayValues(true); $fields = $params->draw(null, null, null, null, null, null, false, 'param', 'fieldsListArray'); // New CB2.0 way for bind(): foreach ($fields as $key => $value) { if (property_exists($data, $key)) { $data->{$key} = is_array($value) ? json_encode($value) : $value; } } } if (!$data->check()) { $errorMsg = $data->getError(); break; } $dataModelKey = $data->getKeyName(); $dataModelValueOld = $data->{$dataModelKey}; if ($mode == 'savecopy') { if (!$data->canCopy($data)) { $errorMsg = $data->getError(); break; } if (!$data->copy($data)) { $errorMsg = $data->getError(); break; } } else { if (!$data->store()) { $errorMsg = $data->getError(); break; } } $dataModelValue = $data->{$dataModelKey}; // Id changed; be sure to update the url encase of redirect: if (count($keyValues) == 1) { $urlKeys = array_keys($keyValues); $urlDataKey = $urlKeys[0]; if ($mode == 'savenew') { unset($actionPath[$urlDataKey]); } elseif ($dataModelValue != $dataModelValueOld) { $actionPath[$urlDataKey] = $dataModelValue; } } if ($data->hasFeature('checkout')) { /** @var \CBLib\Database\Table\CheckedOrderedTable $data */ $data->checkin(); } $this->savePluginViewOrder($data, $viewModel); $resultingMsg = $data->cbResultOfStore(); break; case 'sql:field': // <data name="params" type="sql:field" table="#__cbsubs_config" class="cbpaidConfig" key="id" value="1" valuetype="sql:int" /> $dataModelName = $dataModel->attributes('name'); $dataModelKey = $dataModel->attributes('key'); $dataModelValue = $dataModel->attributes('value'); if ($ui == 2) { if (true !== ($error = RegistryEditView::validateAndBindPost($params, $postArray))) { $errorMsg = $error; break; } } $rawParams = array(); $rawParams[$dataModelName] = json_encode($postArray); $xmlsql = new XmlQuery($this->db, null, $pluginParams); $xmlsql->process_data($dataModel); if ($dataModelValue) { $result = $xmlsql->queryUpdate($rawParams); } else { $result = $xmlsql->queryInsert($rawParams, $dataModelKey); } if (!$result) { $errorMsg = $xmlsql->getErrorMsg(); } break; case 'parameters': if ($ui == 2) { if (true !== ($error = RegistryEditView::validateAndBindPost($params, $postArray))) { $errorMsg = $error; break; } } $rawParams = array(); $rawParams['params'] = json_encode($postArray); // $plugin = new PluginTable( $this->_db ); // $plugin->load( $pluginId ); if (!$plugin->bind($rawParams)) { $errorMsg = $plugin->getError(); break; } if (!$plugin->check()) { $errorMsg = $plugin->getError(); break; } if (!$plugin->store()) { $errorMsg = $plugin->getError(); break; } $plugin->checkin(); $plugin->updateOrder("type='" . $plugin->getDbo()->getEscaped($plugin->type) . "' AND ordering > -10000 AND ordering < 10000 "); $resultingMsg = $plugin->cbResultOfStore(); break; case 'class': if ($ui == 2) { if (true !== ($error = RegistryEditView::validateAndBindPost($params, $postArray))) { $errorMsg = $error; break; } } if (!$data->bind($postArray)) { $errorMsg = $data->getError(); break; } if (!$data->check()) { $errorMsg = $data->getError(); break; } if (!$data->store()) { $errorMsg = $data->getError(); break; } if ($data->hasFeature('checkout')) { /** @var \CBLib\Database\Table\CheckedOrderedTable $data */ $data->checkin(); } $this->savePluginViewOrder($data, $viewModel); $resultingMsg = $data->cbResultOfStore(); break; case 'sql:multiplerows': default: echo 'Save error: showview data type: ' . $dataModelType . ' not implemented !'; exit; break; } if ($ui == 2) { $url = 'index.php?option=' . $options['option'] . '&view=' . $options['view']; if ($options['view'] == 'editPlugin') { $url .= '&cid=' . $options['pluginid']; } $url = $_CB_framework->backendUrl($url); } else { $url = 'index.php'; if (count($options) > 0) { $fixOptions = array(); foreach ($options as $k => $v) { $fixOptions[$k] = $k . '=' . urlencode($v); } $url .= '?' . implode('&', $fixOptions); } } if (isset($data->title)) { $dataItem = CBTxt::T($data->title); } elseif (isset($data->name)) { $dataItem = CBTxt::T($data->name); } else { $dataItem = null; } if ($errorMsg) { if (in_array($mode, array('save', 'savenew', 'savecopy'))) { $mode = 'apply'; } $msg = CBTxt::T('FAILED_TO_SAVE_LABEL_ITEM_BECAUSE_ERROR', 'Failed to save [label] [item] because: [error]', array('[label]' => $viewModel->attributes('label'), '[item]' => $dataItem, '[error]' => $errorMsg)); $msgType = 'error'; } else { $msg = CBTxt::T('SUCCESSFULLY_SAVED_LABEL_ITEM', 'Successfully saved [label] [item]', array('[label]' => $viewModel->attributes('label'), '[item]' => $dataItem)); $msgType = 'message'; } switch ($mode) { case 'apply': case 'savenew': case 'savecopy': unset($actionPath['view']); foreach ($actionPath as $k => $v) { if ($v !== '') { $url .= '&' . $k . '=' . $v; } } foreach ($parametersValues as $k => $v) { $url .= '&' . $k . '=' . $v; } if ($cbprevstate) { $url .= '&cbprevstate=' . $cbprevstate; } break; case 'save': if ($cbprevstate) { $prevUrl = base64_decode($cbprevstate); // $parametersValues[] = "'" . base64_encode( implode( '&', $cbprevstate ) ) . "'"; if (!preg_match('$[:/]$', $prevUrl)) { $prevUrl = str_replace('&pluginid=', '&cid=', $prevUrl); if ($ui == 2) { $url = $_CB_framework->backendUrl('index.php?' . $prevUrl); } else { $url = 'index.php?' . $prevUrl; } } } break; } if ($resultingMsg) { if ($ui != 2) { return $resultingMsg; // in frontend, for now, don't redirect here: think this is right ! } else { // If not an apply then change it to an apply so we can redisplay the view with the resulting message above it: if (in_array($mode, array('save', 'savenew', 'savecopy'))) { $mode = 'apply'; } echo $resultingMsg; } } else { if ($ui != 2) { return null; // in frontend, for now, don't redirect here: think this is right ! // $url = cbUnHtmlspecialchars( cbSef( $url ) ); } if ($mode == 'apply' && $errorMsg) { $_CB_framework->enqueueMessage($msg, $msgType); } else { cbRedirect($ui == 2 ? $url : cbSef(htmlspecialchars($url), false), $msg, $msgType); } } return null; }