/**
  * Add checkboxes controls to a repository form for each lizmap subject.
  * Used to manage rights for each subject and for each group of each repositories.
  * @param object $form Jform object concerned.
  * @param object $repository Repository key.
  * @param boolean $load If true, load data from jacl2 database and set form control data.
  * @return object Modified form.
  */
 protected function populateRepositoryRightsFormControl($form, $repository, $load = 'db')
 {
     // Daos to use
     $daosubject = jDao::get('jacl2db~jacl2subject', 'jacl2_profile');
     $daogroup = jDao::get('jacl2db~jacl2group', 'jacl2_profile');
     $daoright = jDao::get('jacl2db~jacl2rights', 'jacl2_profile');
     // Loop through the jacl2 subjects
     foreach ($daosubject->findAllSubject() as $subject) {
         // Filter only lizmap subjects
         if (preg_match('#^' . $this->lizmapClientPrefix . '#', $subject->id_aclsbj)) {
             // Create a new form control
             $ctrl = new jFormsControlCheckboxes($subject->id_aclsbj);
             $ctrl->label = $this->getLabel($subject->id_aclsbj, 'admin~jacl2.' . $subject->id_aclsbj);
             $dataSource = new jFormsStaticDatasource();
             $mydata = array();
             // Initialize future values to set
             $dataValues = array();
             // Loop through each group
             foreach ($daogroup->findAll() as $group) {
                 // Retrieve only normal groups wich are not blacklisted
                 if (!in_array($group->id_aclgrp, $this->groupBlacklist) and $group->grouptype == 0) {
                     $mydata[$group->id_aclgrp] = $group->name;
                     // Get rights with resources for the current group
                     if ($load == 'db') {
                         $conditions = jDao::createConditions();
                         $conditions->addCondition('id_aclsbj', '=', $subject->id_aclsbj);
                         $conditions->addCondition('id_aclgrp', '=', $group->id_aclgrp);
                         $conditions->addCondition('id_aclres', '=', $repository);
                         $res = $daoright->findBy($conditions);
                         foreach ($res as $rec) {
                             $dataValues[] = $rec->id_aclgrp;
                         }
                     }
                 }
             }
             $dataSource->data = $mydata;
             $ctrl->datasource = $dataSource;
             $form->addControl($ctrl);
             // Get data from form on error if needed
             if ($load == 'request') {
                 // Edit control ref to get request params
                 $param = str_replace('.', '_', $subject->id_aclsbj);
                 $dataValues = array_values(jApp::coord()->request->params[$param]);
             }
             // Set the preselected data if needed
             if ($load) {
                 $form->setData($subject->id_aclsbj, $dataValues);
             }
         }
     }
     return $form;
 }
 /**
  * Dynamically update form by modifying the filter by login control
  *
  * @param object $form Jelix form to modify control.
  * @param string $save does the form will be used for update or insert.
  * @return modified form.
  */
 private function updateFormByLogin($form, $save)
 {
     if (!is_array($this->loginFilteredLayers)) {
         //&& $this->loginFilteredOveride )
         $this->filterDataByLogin($this->layerName);
     }
     if (is_array($this->loginFilteredLayers)) {
         $type = $this->loginFilteredLayers['type'];
         $attribute = $this->loginFilteredLayers['attribute'];
         // Check if a user is authenticated
         if (!jAuth::isConnected()) {
             return True;
         }
         $user = jAuth::getUserSession();
         if (!$this->loginFilteredOveride) {
             if ($type == 'login') {
                 $user = jAuth::getUserSession();
                 $form->setData($attribute, $user->login);
                 $form->setReadOnly($attribute, True);
             } else {
                 $oldCtrl = $form->getControl($attribute);
                 $userGroups = jAcl2DbUserGroup::getGroups();
                 $userGroups[] = 'all';
                 $uGroups = array();
                 foreach ($userGroups as $uGroup) {
                     if ($uGroup != 'users' and substr($uGroup, 0, 7) != "__priv_") {
                         $uGroups[$uGroup] = $uGroup;
                     }
                 }
                 $dataSource = new jFormsStaticDatasource();
                 $dataSource->data = $uGroups;
                 $ctrl = new jFormsControlMenulist($attribute);
                 $ctrl->required = true;
                 if ($oldCtrl != null) {
                     $ctrl->label = $oldCtrl->label;
                 } else {
                     $ctrl->label = $attribute;
                 }
                 $ctrl->datasource = $dataSource;
                 $value = null;
                 if ($oldCtrl != null) {
                     $value = $form->getData($attribute);
                     $form->removeControl($attribute);
                 }
                 $form->addControl($ctrl);
                 if ($value != null) {
                     $form->setData($attribute, $value);
                 }
             }
         } else {
             $oldCtrl = $form->getControl($attribute);
             $value = null;
             if ($oldCtrl != null) {
                 $value = $form->getData($attribute);
             }
             $data = array();
             if ($type == 'login') {
                 $plugin = jApp::coord()->getPlugin('auth');
                 if ($plugin->config['driver'] == 'Db') {
                     $authConfig = $plugin->config['Db'];
                     $dao = jDao::get($authConfig['dao'], $authConfig['profile']);
                     $cond = jDao::createConditions();
                     $cond->addItemOrder('login', 'asc');
                     $us = $dao->findBy($cond);
                     foreach ($us as $u) {
                         $data[$u->login] = $u->login;
                     }
                 }
             } else {
                 $gp = jAcl2DbUserGroup::getGroupList();
                 foreach ($gp as $g) {
                     if ($g->id_aclgrp != 'users') {
                         $data[$g->id_aclgrp] = $g->id_aclgrp;
                     }
                 }
                 $data['all'] = 'all';
             }
             $dataSource = new jFormsStaticDatasource();
             $dataSource->data = $data;
             $ctrl = new jFormsControlMenulist($attribute);
             $ctrl->required = true;
             if ($oldCtrl != null) {
                 $ctrl->label = $oldCtrl->label;
             } else {
                 $ctrl->label = $attribute;
             }
             $ctrl->datasource = $dataSource;
             $form->removeControl($attribute);
             $form->addControl($ctrl);
             if ($value != null) {
                 $form->setData($attribute, $value);
             } else {
                 if ($type == 'login') {
                     $form->setData($attribute, $user->login);
                 }
             }
         }
     }
     return True;
 }