项目作者: Themroc

项目描述 :
Hassle remover for module developers.
高级语言: PHP
项目地址: git://github.com/Themroc/humhub_mod-helper.git
创建时间: 2019-09-21T16:20:39Z
项目社区:https://github.com/Themroc/humhub_mod-helper

开源协议:

下载


Description

Takes care of config-forms. No more tedious creation of view-files
needed. Just give it your model and a few hints on how your attributes
should be presented. The simplest model-file could look like

  1. <?php
  2. namespace ME\humhub\modules\MYMODULE\models;
  3. class AdminModel extends \themroc\humhub\modules\modhelper\models\AdminForm
  4. {
  5. const MH_API= 1;
  6. public $some_text;
  7. }

The Controller:

  1. <?php
  2. namespace ME\humhub\modules\MYMODULE\controllers;
  3. use Yii;
  4. use humhub\modules\admin\components\Controller;
  5. use themroc\humhub\modules\modhelper\behaviors\MhAdminController;
  6. use ME\humhub\modules\MYMODULE\models\AdminForm;
  7. class AdminController extends Controller
  8. {
  9. public $adminOnly= true;
  10. public $isTabbed= true;
  11. public $modelClass= AdminForm::class;
  12. public function init ()
  13. {
  14. if (null != Yii::$app->getModule('mod-helper'))
  15. $this->attachBehavior('MhAdmin', new MhAdminController());
  16. return parent::init();
  17. }
  18. public function actionIndex ()
  19. {
  20. if (null == Yii::$app->getModule('mod-helper'))
  21. return $this->render('error', [
  22. 'msg'=> 'Please install and activate the <a href="https://github.com/Themroc/humhub_mod-helper" target="_blank">Mod-Helper plugin</a>.',
  23. ]);
  24. return $this->MHactionIndex();
  25. }
  26. }

In that case, a configure-form with one string field labeled “Some Text”
and a “save”-button will be rendered. When clicked, the content of
$some_text will be saved in table “setting”.

Usually though, a bit more configuration will be needed. Like,

  1. <?php
  2. namespace ME\humhub\modules\MYMODULE\models;
  3. use humhub\modules\ui\form\widgets\IconPicker;
  4. class AdminModel extends \themroc\humhub\modules\modhelper\models\AdminForm
  5. {
  6. const MH_API= 1;
  7. public $icon;
  8. public $text_enable;
  9. public $some_text;
  10. protected $vars= [
  11. 'icon'=> [
  12. 'label'=> 'Select icon',
  13. 'trans'=> 'UiModule.form',
  14. 'form'=> ['type'=> 'widget', 'class'=> IconPicker::class],
  15. ],
  16. 'text_enable'=> [
  17. 'prefix'=> '<div class="some-box">',
  18. 'label'=> 'Show useless text field',
  19. 'rules'=> ['in', 'range'=> [0, 1]],
  20. 'form'=> ['type'=> 'checkbox'],
  21. ],
  22. 'some_text'=> [
  23. 'label'=> 'Some random text',
  24. 'hints'=> 'Type in whatever you like, it will be ignored anyway.',
  25. 'form'=> ['visible'=> ['text_enable'=> 1]],
  26. 'suffix'=> '</div>',
  27. ],
  28. ];
  29. }

This will give an icon picker and box containing a string field that can
be hidden. Plus the usual “save”-button, of course. More examples can be
found in https://github.com/Themroc/humhub_iframe/blob/master/models/AdminForm.php.

Possible config options are:

  1. $model->mod[
  2. '_'=> object = Yii::$app->getModule()
  3. 'settings'=> object = Yii::$app->getModule()->settings
  4. 'id'=> string = Yii::$app->controller->module->id
  5. 'name'=> string If unset, = ucfirst(mod['id'])
  6. 'trans'=> string Translation category. If unset, derived from mod['id'].
  7. See https://docs.humhub.org/docs/develop/i18n/
  8. 'options'=> [
  9. 'tab_attr'=> string Attribute name whose content selects a tab
  10. 'tab_sort'=> mixed If string: Attribute name whose content determines tab order
  11. If callable: `function ($tab_list)` returns sorted tab list
  12. ],
  13. 'form'=> [
  14. 'btn_pre'=> string Will be emitted verbatim before the "save" button code
  15. 'btn_post'=> string Will be emitted verbatim after the "save" button code
  16. ],
  17. ];
  18. $model->vars[
  19. 'attribute_name'=> [
  20. 'rules'=> mixed A single string like 'number' will give a ['number']-rule.
  21. A single array will be passed as is.
  22. A nested array gives multiple rules. Ex. [ ['required'], ['in', 'range'=> [0, 1]] ]
  23. See https://www.yiiframework.com/doc/api/2.0/yii-base-model#rules()-detail
  24. 'label'=> string If unset, will be derived from attribute name.
  25. See https://www.yiiframework.com/doc/api/2.0/yii-base-model#attributeLabels()-detail
  26. 'hints'=> string Explanatory text.
  27. See https://www.yiiframework.com/doc/api/2.0/yii-base-model#attributeHints()-detail
  28. 'trans'=> string Translation category. If unset, taken from mod['trans'].
  29. 'default'=> string This will show up if attribute content is empty
  30. 'options'=> [
  31. 'nosave'=> number If !=0, attribute content will not be stored in db
  32. 'notrim'=> number If !=0, attribute content will not be trimmed prior to saving
  33. ],
  34. 'form'=> [
  35. 'type'=> string One of 'checkbox', 'dropdown', 'radio', 'textarea', 'widget', 'hidden' or 'text' (the default)
  36. 'class'=> class yii widget class if type=='widget'
  37. See https://www.yiiframework.com/doc/api/2.0/yii-widgets-activefield#widget()-detail
  38. 'items'=> array Item list for dropdown or radio. Can be either a string like ".Label 1.Another label"
  39. an array like [ 0=> 'Label 1', 1=> 'Another label' ] or some reference to a function
  40. that returns such an array/string. Items will be translated using the category in 'trans'.
  41. 'options'=> array Will be passed to yii widget's $options variable
  42. See https://www.yiiframework.com/doc/api/2.0/yii-widgets-activefield
  43. 'visible'=> array List of conditions all of which must be fulfilled for field to be visible.
  44. Ex. ['block_enable'=> 3, 'text_enable'=> 1]
  45. 'depends'=> array Old, deprecated name for 'visible'
  46. 'prefix'=> string Will be emitted verbatim before the widget code
  47. 'suffix'=> string Will be emitted verbatim after the widget code
  48. ],
  49. 'function'=> [ // function follows form :)
  50. 'depends'=> array List of attributes whose content will determine the content of this attribute. Ex.
  51. ['protocol', 'domain']
  52. 'code'=> string Javascript code to create field content. Will be used as body of a function that
  53. will be called whenever one of the fields in 'depends' changes. Must return the result.
  54. Substrings in the form of '@attribute_name@' will be replaced by the actual attribute
  55. name in javascript. Ex. 'return $("@protocol@").val() + "://" + $("@domain@").val() + "/"'
  56. ],
  57. // deprecated:
  58. 'prefix'=> string See 'form'
  59. 'suffix'=> string See 'form'
  60. ];
  61. ];

All strings and arrays in vars can also be a callable. If a string is given
where an array is expected, it will be split using the first character as
separator.

mod and vars can also be supplied by protected functions that
should return an array like above. If those arrays are already present,
elements of them will get overridden by the corresponding element from
the function.

Installation

Unzip this into */protected/modules/mod-helper and activate the
module in Administration / Modules.

Module website: https://github.com/Themroc/humhub_mod-helper

Author: Themroc 7hemroc@gmail.com

Changelog

https://github.com/Themroc/humhub_mod-helper/commits/master

Bugtracker

https://github.com/Themroc/humhub_mod-helper/issues

ToDos

  • More formulas than just var=value should be possible in ‘form’=> [‘depends’=> […]]
  • Decent documentation

License

GNU AFFERO GENERAL PUBLIC LICENSE
Version 3, 19 November 2007
https://www.humhub.org/de/licences

Contains icon grapic made by https://www.flaticon.com/packs/material-design,
License: https://creativecommons.org/licenses/by/3.0/