One way to achieve hide/show some nodes may be as follows:
In views you should have your db table names.
Look for the table name you are interested in.
Inside the folder of the table name there should be files names something like: _form.php, _script.php and index.php
The index.php file should have some use among of which there should be:
...
use kartik\tree\TreeView;
use kartik\tree\Module;
...
After these use statements, you can add this code:
/** @var integer $uid */
// get current logged in user id.
// this is used to control showing tree content, and
// to control form fields.
if (isset(Yii::$app->user)) {
$uid = Yii::$app->user->getId();
}
So now the logged in user id is saved in variable $uid
In the same index.php file you should have code that renders the tree view. Something starts with: echo TreeView::widget
Inside this TreeView::widget([.. you can add a query that renders only current logged in user content, like this:
'query' => YourTableName::find()->where(['user_id' => $uid])->addOrderBy('root, lft'),
... Other settings ...
If you want the admin(s) to see or change the content, you can add a Controller for example named AdminController, then in this index.php TreeView::widget you can add more options like this:
echo TreeView::widget([
'query' => YourTableName::find()->where(['user_id' => $uid])->addOrderBy('root, lft'),
'headingOptions' => ['label' => 'YourLableName'],
//'rootOptions' => ['label' => '<span class="text-success">Root</span>'],
'fontAwesome' => false,
'isAdmin' => true,
'showInactive' => AdminController::isAdmin(),
'displayValue' => 0,
'showIDAttribute' => true,
'emptyNodeMsg' => ' type some msg here ... .',
'showCheckbox' => false,
'multiple' => false,
'options' => ['id' => 'treeID'],
'allowNewRoots' => false,
'toolbar' => [
'create' => ['alwaysDisabled' => true],
//'remove' => ['alwaysDisabled' => !(AdminController::isAdmin())],
// 'move-up' => ['alwaysDisabled' => !(AdminController::isAdmin())],
// 'move-down' => ['alwaysDisabled' => !(AdminController::isAdmin())],
// 'move-left' => ['alwaysDisabled' => !(AdminController::isAdmin())],
// 'move-right' => ['alwaysDisabled' => !(AdminController::isAdmin())],
//'remove' => false,
],
'cascadeSelectChildren' => false,
//'softDelete' => false,
'iconEditSettings'=> [
'show' => 'list',
'listData' => [
// 'folder' => 'Folder',
'file' => 'File',
'star' => 'Star',
'bell' => 'Bell',
// 'phone' => 'Phone',
]
],
'cacheSettings' => ['enableCache' => true],
'nodeAddlViews' => [
Module::VIEW_PART_1 => '@app/views/mappings/_form',
],
]);
This is just a small start, but you can take it further. For example suppose the user is not logged in or suppose you want to show some nodes any way. In these cases you can use switch case statement and check for example if $uid is not defined (it is not set because the user is not logged in) in this case, you can ask the user to log in or render or show a different tree view:
switch ($SomeVariable) {
case "case_to_check":
echo TreeView::widget([
...
'query' => TableName::find()->where(['user_id' => $uid])->addOrderBy('root, lft'),
...
break;
case "another_case":
echo TreeView::widget([
...
break;
default:
echo TreeView::widget([
...
== You can also add html select at the top of index.php, something like this:
<select name="Give_any_name_you_like" size=1 class="btn btn-primary" style="margin-bottom: 0.5em; margin-left: 0.5em; ">
<option value="0">Select Node</option>
<option value="1">mynodes</option>
<option value="2">othernodes</option>
<option value="3">allnodes</option>
</select>
then use java script to filter and capture the selected value, then you can use this vale in switch case to show certain nodes. You can place the java script code inside <?php block and before switch case or echo TreeView::widget([ . java script code may look something like this:
$this->registerJs("
$('select[name=" . "The_name_you_give_in_select" . "]').change(function(){
var value = $(this).val();
switch(value) {
case '1':
window.location.href = \"your-page-name?what=mynodes\" ;
break;
case '2':
window.location.href = \"your-page-name?what=othernodes\" ;
break;
default:
window.location.href = \"your-page-name?what=allnodes\" ;
}
});", View::POS_READY);
=== Then you check for the value of what in switch cases and use it to filter which tree node to show. Something like this code just below above java script code:
/** @var integer $The_name_you_give_in_select */
// get value from selection menu.
// this is used to filter and show desired tree.
if (isset($_GET['what'])) {
$The_name_you_give_in_select = $_GET['what'];
} else {
$The_name_you_give_in_select = "defaultcase";
}
// Then in switch case:
switch ($The_name_you_give_in_select) {
case "mynodes":
echo TreeView::widget([
...
break;
Case "othernodes":
echo TreeView::widget([
...
break;
default:
echo TreeView::widget([
...
=============
=== Also you may want to do some changes in views/your_table_name/_form.php
In _form.php you can also control what fields to show, what fields to make editable or read only, etc. something like this:
== _form.php:
...
/** @var integer $userid */
// save current node user id in var $userid
// to be used to control form fields
$userid = $node->user_id;
...
if(isset($userid)){
$username = Yii::$app->user->identity;
}
...
<div class="your-form">
...
<?= $form->field($node, 'annotation')->textarea(['rows' => 6, 'readonly' => !(Yii::$app->user->identity->id == $userid or AdminController::isAdmin())]) ?>
<?= $form->field($node, 'comments')->textarea(['rows' => 6, 'readonly' => !(Yii::$app->user->identity->id == $userid or AdminController::isAdmin())]) ?>
<!-- <?/*= $form->field($username, 'username')->textInput(['maxlength' => true, 'readonly'=>true])->label('Created by User') */?>-->
<?= $form->field($node, 'user_id')->textInput(['readonly'=>true]) ?>
<?= $form->field($node, 'date_added')->textInput(['placeholder' => 'Date Added', 'readonly'=>true]) ?>
</div>
=========
=== The AdminContoler.php may look something like this:
<?php
namespace app\controllers;
use Yii;
...
class AdminController extends Controller
{
/**
*
* manage admins.
* add admins here
* this will allow admins more control on all tables, but not accessing and managing users
* controlling, accessing and managing users is configured through:
* - config/web.php and views/layouts/main.php
* - (1) in config/web.php go to modules -> user -> admins and add username(s)that you want to be admin accessing and managing users
* - (2) THEN in view views/layouts/main.php, follow the same logic in line 62 and add username(s).
*
* @return bool
*/
public static function isAdmin()
{
if (
Yii::$app->user->identity->username == 'type user name here'
or
Yii::$app->user->identity->username == 'type user name here'
// add more here for example by uncommenting the following lines and typing username that you want to be admin
// or
// Yii::$app->user->identity->username == 'type user name here'
// or
// Yii::$app->user->identity->username == 'type user name here'
// or
// Yii::$app->user->identity->username == 'type user name here'
) {
return true;
} else {
return false;
}
}
}