/** * Edit the translations keys * * @param array $filters The filters on the language keys */ public function editKeys($filters = array()) { if (empty($filters)) { $filters = $this->getFilters(); } $form = new Form(array('id' => 'edit-keys-form', 'action' => App::router()->getUri('save-language-keys'), 'onsuccess' => 'app.lists["language-key-list"].refresh();')); if (!$form->submitted()) { // Display the form return $form->wrap($this->listKeys()); } else { // Register the translations try { $keys = array(); $translations = App::request()->getBody('translation'); if (!empty($translations[$filters['tag']])) { foreach ($translations[$filters['tag']] as $langKey => $translation) { if (!empty($translation)) { list($plugin, $key) = explode('.', $langKey); $key = str_replace(array('{', '}'), array('[', ']'), $key); if (empty($keys[$plugin])) { $keys[$plugin] = array(); } $keys[$plugin][$key] = $translation; } } Language::getByTag($filters['tag'])->saveTranslations($keys); } App::logger()->info('The translations has been updated'); return $form->response(Form::STATUS_SUCCESS, Lang::get('language.update-keys-success')); } catch (DBException $e) { App::logger()->error('An error occured while updating translations : ' . $e->getMessage()); return $form->response(Form::STATUS_ERROR, DEBUG_MODE ? $e->getMessage() : Lang::get('language.update-keys-error')); } } }
/** * Display the main page of the permission settings */ public function index() { $permissionGroups = Permission::getAllGroupByPlugin(); $example = isset($this->roleId) ? array('roleId' => $this->roleId) : array(); $data = RolePermission::getListByExample(new DBExample($example)); $values = array(); foreach ($data as $value) { $values[$value->permissionId][$value->roleId] = $value->value; } $roles = isset($this->roleId) ? array(Role::getById($this->roleId)) : Role::getAll(null, array(), array(), true); $param = array('id' => 'permissions-form', 'fieldsets' => array('form' => array(), '_submits' => array(new SubmitInput(array('name' => 'valid', 'value' => Lang::get('main.valid-button')))))); foreach ($roles as $role) { foreach ($permissionGroups as $group => $permissions) { if (Plugin::get($group)) { foreach ($permissions as $permission) { if ($role->id == Role::ADMIN_ROLE_ID) { $default = 1; } elseif (isset($values[$permission->id][$role->id])) { $default = $values[$permission->id][$role->id]; } else { $default = 0; } $param['fieldsets']['form'][] = new CheckboxInput(array('name' => "permission-{$permission->id}-{$role->id}", 'disabled' => $role->id == Role::ADMIN_ROLE_ID || $role->id == Role::GUEST_ROLE_ID && !$permission->availableForGuests, 'default' => $default, 'class' => $permission->id == Permission::ALL_PRIVILEGES_ID ? 'select-all' : '', 'nl' => false)); } } } } $form = new Form($param); if (!$form->submitted()) { $page = View::make(Plugin::current()->getView("permissions.tpl"), array('permissions' => $permissionGroups, 'fields' => $form->inputs, 'roles' => $roles)); return NoSidebarTab::make(array('icon' => 'unlock-alt', 'title' => Lang::get('permissions.page-title'), 'page' => $form->wrap($page))); } else { try { foreach ($form->inputs as $name => $field) { if (preg_match('/^permission\\-(\\d+)\\-(\\d+)$/', $name, $match)) { $permissionId = $match[1]; $roleId = $match[2]; $value = App::request()->getBody($name) ? 1 : 0; if ($roleId != Role::ADMIN_ROLE_ID && !($roleId == Role::GUEST_ROLE_ID && !$permission->availableForGuests)) { $permission = new RolePermission(); $permission->set(array('roleId' => $roleId, 'permissionId' => $permissionId, 'value' => $value)); $permission->save(); } } } App::logger()->info('Permissions were succesfully updated'); return $form->response(Form::STATUS_SUCCESS, Lang::get("roles.permissions-update-success")); } catch (Exception $e) { App::logger()->error('An error occured while updating permissions'); return $form->response(Form::STATUS_ERROR, DEBUG_MODE ? $e->getMessage() : Lang::get("roles.permissions-update-error")); } } }
/** * Display the list of the profile questions */ public function listQuestions() { // Get all ProfileQuestions $questions = ProfileQuestion::getAll(); // Get all Roles $roles = Role::getAll(); // Create parameters for form $param = array('id' => 'display-questions-form', 'action' => App::router()->getUri('profile-questions'), 'fieldsets' => array('form' => array(), '_submits' => array(new SubmitInput(array('name' => 'valid', 'value' => Lang::get('main.valid-button'))), new ButtonInput(array('name' => 'new-question', 'value' => Lang::get($this->_plugin . '.new-question-btn'), 'class' => 'btn-success', 'href' => App::router()->getUri('edit-profile-question', array('name' => '_new')), 'target' => 'dialog', 'icon' => 'plus'))))); // For each ProfileQuestion add roles, displayInRegister and displayInProfile foreach ($questions as $question) { // Add the input to display in register form $param['fieldsets']['form'][] = new CheckboxInput(array('name' => "register-display-{$question->name}", 'default' => $question->displayInRegister, 'nl' => false)); // Add the input to display in the user profile $param['fieldsets']['form'][] = new CheckboxInput(array('name' => "profile-display-{$question->name}", 'default' => $question->displayInProfile, 'nl' => false)); // Get roles associate to this ProfileQuestion in json parameters $attributesRoles = ProfileQuestion::getByName($question->name)->getRoles(); // For each roles create a Checkbox foreach ($roles as $role) { // Add the input to display in the user profile $param['fieldsets']['form'][] = new CheckboxInput(array('name' => "role-{$role->name}-question-{$question->name}", 'default' => in_array($role->id, $attributesRoles) ? 1 : 0, 'nl' => false)); } } // Create form $form = new Form($param); // Create parameters for the list to display $paramList = array('id' => 'profile-questions-list', 'model' => 'ProfileQuestion', 'action' => App::router()->getUri('profile-questions'), 'lines' => 'all', 'navigation' => false, 'sort' => array('order' => DB::SORT_ASC), 'fields' => array('name' => array('hidden' => true), 'editable' => array('hidden' => true), 'actions' => array('independant' => true, 'display' => function ($value, $field, $line) { if ($line->editable) { return Icon::make(array('icon' => 'pencil', 'class' => 'text-info', 'href' => App::router()->getUri('edit-profile-question', array('name' => $line->name)), 'target' => 'dialog', 'title' => Lang::get($this->_plugin . '.edit-profile-question'))) . Icon::make(array('icon' => 'times', 'class' => 'text-danger delete-question', 'data-question' => $line->name, 'title' => Lang::get($this->_plugin . '.delete-profile-question'))); } else { return ''; } }, 'sort' => false, 'search' => false), 'label' => array('independant' => true, 'display' => function ($value, $field, $line) { return Lang::get($this->_plugin . ".profile-question-{$line->name}-label") . " ( {$line->name} )"; }, 'sort' => false, 'search' => false), 'displayInRegister' => array('label' => Lang::get($this->_plugin . ".list-questions-register-visible-label"), 'sort' => false, 'search' => false, 'display' => function ($value, $field, $line) use($form) { return $form->inputs["register-display-{$line->name}"]; }), 'displayInProfile' => array('label' => Lang::get($this->_plugin . '.list-questions-profile-visible-label'), 'sort' => false, 'search' => false, 'display' => function ($value, $field, $line) use($form) { return $form->inputs["profile-display-{$line->name}"]; }))); // For each roles create a checkbox by line profileQuestion! foreach ($roles as $role) { // Add the input to display in register form $paramList['fields'][$role->name] = array('independant' => true, 'label' => Lang::get("roles.role-{$role->id}-label"), 'search' => false, 'sort' => false, 'display' => function ($value, $field, $line) use($form) { return $form->inputs["role-{$field->name}-question-{$line->name}"]; }); } // Create List $list = new ItemList($paramList); if (!$form->submitted()) { if ($list->isRefreshing()) { return $list->display(); } $this->addKeysToJavaScript($this->_plugin . ".confirm-delete-question"); $content = View::make(Plugin::current()->getView("questions-list.tpl"), array('list' => $list, 'form' => $form)); return $form->wrap($content); } // Extract from form, all infos abour roles associate to ProfileQuestion $listRoles = array(); $roles = Role::getAll('name'); $save = array(); foreach ($form->inputs as $name => $field) { // Manage displayInRegister and displayInProfile if (preg_match("/^(register|profile)\\-display\\-(\\w+)\$/", $name, $match)) { $qname = $match[2]; $func = $match[1] == "register" ? 'displayInRegister' : 'displayInProfile'; if (!isset($save[$qname])) { $save[$qname] = new ProfileQuestion(); $save[$qname]->set('name', $qname); } $save[$qname]->set($func, (int) App::request()->getBody($name)); } else { if (preg_match("/^role\\-(\\w+)\\-question\\-(\\w+)\$/", $name, $match)) { $qname = $match[2]; $roleName = $match[1]; // If tab doesn't exit create it to avoid exception if (!isset($listRoles[$qname])) { $listRoles[$qname] = array(); } $role = $roles[$roleName]; // If checkbox is tag, add roleId if ($field->dbvalue()) { array_push($listRoles[$qname], intval($role->id)); } } } } foreach ($save as $question) { $question->update(); } // Save each ProfileQuestions foreach ($questions as $question) { $params = json_decode($question->parameters, true); $params['roles'] = $listRoles[$question->name]; $question->set('parameters', json_encode($params)); $question->update(); } return $form->response(Form::STATUS_SUCCESS); }
/** * Creates a textarea form input. * * @param string $name textarea name * @param string $body textarea body * @param array $attributes html attributes * @param boolean $double_encode encode existing HTML characters * * @param string $label * @param string|array $error * @param string|array $tip * @param boolean $bbcode * @return string */ public static function textarea_wrap($name, $body = '', array $attributes = null, $double_encode = true, $label = null, $error = null, $tip = null, $bbcode = null) { if (is_array($body)) { $body = Arr::get($body, $name); } else { if (is_object($body)) { $body = $body->{$name}; } } $attributes = (array) $attributes + array('id' => self::input_id($name)); $label = $label ? array($attributes['id'] => $label) : ''; $input = Form::textarea($name, $body, $attributes, $double_encode); if ($bbcode) { $input .= HTML::script_source(' head.ready("bbcode", function initMarkItUp() { $("#' . $attributes['id'] . '").markItUp(bbCodeSettings); }); '); } return Form::wrap($input, $name, $label, $error, $tip); }
/** * Creates a file upload form input. * * @param string $name input name * @param array $attributes html attributes * * @param string $label * @param string|array $error * @param string|array $tip * @return string */ public static function file_wrap($name, array $attributes = null, $label = null, $error = null, $tip = null) { $attributes = (array) $attributes + array('id' => self::input_id($name)); $label = $label ? array($attributes['id'] => $label) : ''; $input = Form::file($name, $attributes); return Form::wrap($input, $name, $label, $error, $tip); }