/** * Return an array of available items, suitable for dropDownList() * @param number $root ID of root nodes to retrieve * @param string $level The level of tree to retrieve * @return array $res An associate array with key - value */ public static function options($root = 0, $level = NULL) { $res = []; if ($root instanceof self) { $res[$root->id] = str_repeat('-', $root->level) . ' ' . $root->title; if ($level) { foreach ($root->children()->all() as $childRoot) { $res += self::options($childRoot, $level - 1); } } elseif (is_null($level)) { foreach ($root->children()->all() as $childRoot) { $res += self::options($childRoot, NULL); } } } elseif (is_scalar($root)) { if ($root == 0) { foreach (self::find()->roots()->all() as $rootItem) { if ($level) { $res += self::options($rootItem, $level - 1); } elseif (is_null($level)) { $res += self::options($rootItem, NULL); } } } else { $root = self::find($root); if ($root) { $res += self::options($root, $level); } } } return $res; }