/**
  * Save the data for one section.
  * @return Redirect to the index.
  */
 function saveSection()
 {
     $repository = $this->param('repository');
     $new = (bool) $this->param('new');
     $ok = true;
     // Get services data
     $services = lizmap::getServices();
     // Repository (first take the default one)
     $lrep = lizmap::getRepository($repository);
     // what to do if it's a new one!
     // Get the form
     $form = jForms::get('admin~config_section');
     // token
     $token = $this->param('__JFORMS_TOKEN__');
     if (!$token) {
         $ok = false;
         jMessage::add('missing form token');
     }
     // If the form is not defined, redirection
     if (!$form) {
         $ok = false;
     }
     // Redirection in case of errors
     if (!$ok) {
         $rep = $this->getResponse("redirect");
         $rep->action = "admin~config:index";
         return $rep;
     }
     // Rebuild form fields
     /*foreach(lizmap::getRepositoryProperties() as $k){
         if ( $propertiesOptions[$k]['fieldType'] == 'checkbox' ) {
           $ctrl = new jFormsControlCheckbox($k);
         }
         else {
           $ctrl = new jFormsControlInput($k);
         }
         $ctrl->required = $propertiesOptions[$k]['required'];
         $ctrl->label = jLocale::get("admin~admin.form.admin_section.repository.".$k.".label");
         $datatype = new jDatatypeString();
         $ctrl->datatype=$datatype;
         $form->addControl($ctrl);
       }*/
     lizmap::constructRepositoryForm($lrep, $form);
     if ($lrep) {
         $form = $this->populateRepositoryRightsFormControl($form, $lrep->getKey(), false);
     }
     // Set form data from request data
     $form->initFromRequest();
     // Check the form
     $ok = true;
     if (!$form->check()) {
         $ok = false;
     }
     if (!$new && !$lrep) {
         $form->setErrorOn('repository', jLocale::get("admin~admin.form.admin_section.message.repository.wrong"));
         $ok = false;
     }
     // Check paths
     if (in_array('path', lizmap::getRepositoryProperties())) {
         $npath = $form->getData('path');
         if ($npath[0] != '/' and $npath[1] != ':') {
             $npath = jApp::varPath() . $npath;
         }
         if (!file_exists($npath) or !is_dir($npath)) {
             $form->setErrorOn('path', jLocale::get("admin~admin.form.admin_section.message.path.wrong"));
             $ok = false;
         }
         $rootRepositories = $services->getRootRepositories();
         if ($rootRepositories != '') {
             if ($lrep && substr($lrep->getPath(), 0, strlen($rootRepositories)) !== $rootRepositories) {
                 //Can't update path
                 $form->setData('path', $lrep->getData('path'));
             } else {
                 if ($lrep && substr($lrep->getPath(), 0, strlen($rootRepositories)) === $rootRepositories && substr(realpath($npath), 0, strlen($rootRepositories)) !== $rootRepositories) {
                     $form->setErrorOn('path', jLocale::get("admin~admin.form.admin_section.message.path.not_authorized"));
                     jLog::log('rootRepositories == ' . $rootRepositories . ', repository ' . $lrep->getKey() . ' path == ' . realpath($npath));
                     $ok = false;
                 } else {
                     if ($lrep == null && substr(realpath($npath), 0, strlen($rootRepositories)) !== $rootRepositories) {
                         $form->setErrorOn('path', jLocale::get("admin~admin.form.admin_section.message.path.not_authorized"));
                         jLog::log('rootRepositories == ' . $rootRepositories . ', new repository path == ' . realpath($npath));
                         $ok = false;
                     }
                 }
             }
         }
     }
     if (!$ok) {
         // Errors : redirection to the display action
         $rep = $this->getResponse('redirect');
         $rep->action = 'admin~config:editSection';
         $rep->params['repository'] = $repository;
         $rep->params['errors'] = "1";
         foreach (jApp::coord()->request->params as $k => $v) {
             if (preg_match('#^' . $this->lizmapClientPrefix . '#', $k)) {
                 $rep->params[$k] = $v;
             }
         }
         if ($new) {
             $form->setReadOnly('repository', false);
         }
         return $rep;
     }
     // Repository data
     $data = array();
     foreach (lizmap::getRepositoryProperties() as $prop) {
         $data[$prop] = $form->getData($prop);
         // Check paths
         if ($prop == 'path') {
             # add a trailing / if needed
             if (!preg_match('#/$#', $data[$prop])) {
                 $data[$prop] .= '/';
             }
         }
     }
     // Save the data
     if ($new && !$lrep) {
         $lrep = lizmap::createRepository($repository, $data);
     } else {
         if ($lrep) {
             $modifySection = $lrep->update($data);
         }
     }
     jMessage::add(jLocale::get("admin~admin.form.admin_section.message.data.saved"));
     // group rights data
     $this->saveRepositoryRightsFromRequest($form, $repository);
     // Redirect to the validation page
     $rep = $this->getResponse("redirect");
     $rep->params['repository'] = $repository;
     if ($new) {
         $rep->params['new'] = 1;
     }
     $rep->action = "admin~config:validateSection";
     return $rep;
 }