diff --git a/lib/zin/zentao/field.class.php b/lib/zin/zentao/field.class.php index 01db166d31..3a4f86e88e 100644 --- a/lib/zin/zentao/field.class.php +++ b/lib/zin/zentao/field.class.php @@ -28,11 +28,16 @@ class field extends setting public mixed $default; - public function __construct(?string $name = null, ?fieldList $fieldList = null, ?field $parent = null) + public function __construct(string|object|array|null $nameOrProps = null, ?fieldList $fieldList = null, ?field $parent = null) { $this->fieldList = $fieldList; $this->parent = $parent; - parent::__construct(array('name' => $name)); + + if(is_string($nameOrProps)) $nameOrProps = array('name' => $nameOrProps); + elseif($nameOrProps instanceof field) $nameOrProps = $nameOrProps->toArray(); + elseif(is_object($nameOrProps)) $nameOrProps = get_object_vars($nameOrProps); + + parent::__construct($nameOrProps); } public function getName(): string @@ -236,8 +241,9 @@ class field extends setting return $this->parent->control($this->toArray()); } - function items(array|object|null $items): field + function items(array|object|false|null $items): field { + if($items === false) return $this->remove('items'); return $this->addToList('items', $items); } @@ -309,6 +315,16 @@ class field extends setting return $this; } + function detach(): field + { + if(is_null($this->parent)) + { + trigger_error('[ZIN] The field named ' . $this->getName() . ' has no parent, maybe you should add self to a fieldList firstly.', E_USER_ERROR); + } + $this->parent->remove($this->getName()); + return $this; + } + function toArray(): array { $array = parent::toArray(); diff --git a/lib/zin/zentao/field.func.php b/lib/zin/zentao/field.func.php index 1f5aa394d0..59d2ca69fc 100644 --- a/lib/zin/zentao/field.func.php +++ b/lib/zin/zentao/field.func.php @@ -23,22 +23,21 @@ function defineFieldList(string $name = null, string|array|field|fieldList|null return fieldList::define($name, ...$args); } +function defineField(string $name, ?string $listName = null): field +{ + if(str_contains($name, '/') && is_null($listName)) + { + list($listName, $name) = explode('/', $name); + } + return defineFieldList($listName)->field($name); +} + function fieldList(string $name) { return fieldList::ensure($name); } -function field(string $name, ?string $listName = null): field -{ - if(str_contains($name, '/') && is_null($listName)) - { - list($listName, $name) = explode('/', $name); - return fieldList::ensure($listName)->field($name); - } - return defineFieldList($listName)->field($name); -} - -function createField(string $name): field +function field(string $name): field { return new field($name); } diff --git a/lib/zin/zentao/fieldlist.class.php b/lib/zin/zentao/fieldlist.class.php index 9f5f25cbf4..9cf894251f 100644 --- a/lib/zin/zentao/fieldlist.class.php +++ b/lib/zin/zentao/fieldlist.class.php @@ -64,7 +64,7 @@ class fieldList return $this->field($name); } - public function field(string $name): field + public function field(string $name, array|object|null $fieldProps = null): field { $field = $this->get($name); if(is_null($field)) @@ -72,6 +72,7 @@ class fieldList $field = new field($name, $this); $this->add($field); } + if(!is_null($fieldProps)) $field->set($fieldProps); return $field; } @@ -80,8 +81,11 @@ class fieldList return isset($this->fields[$name]) ? $this->fields[$name] : null; } - public function add(field $field): fieldList + public function add(field|array|stdClass $field): fieldList { + if(!($field instanceof field)) $field = get_object_vars($field); + if(is_array($field)) $field = new field($field); + $this->fields[$field->getName()] = $field; return $this; } @@ -92,12 +96,25 @@ class fieldList if(is_string($info)) { - $list = array(); - foreach(explode(',', $info) as $name) + if(str_starts_with($info, '!')) { - $list[] = static::getByName($name); + return $this->remove(substr($info, 1)); + } + + if(str_contains($info, '/')) + { + $info = static::getListFields($info); + } + else + { + $listNames = explode(',', $info); + $info = array(); + foreach($listNames as $listName) + { + $fieldList = static::getList($listName); + if($fieldList) $info[] = $fieldList; + } } - $info = $list; } if(is_array($info)) @@ -186,13 +203,23 @@ class fieldList public function toList(string|array|null $names = null): array { - return $this->fields; + if(is_null($names)) return $this->fields; + + if(is_string($names)) $names = explode(',', $names); + + $list = array(); + foreach($names as $name) + { + $field = $this->get($name); + if($field) $list[$name] = $field; + } + return $list; } - public function toArray(): array + public function toArray(string|array|null $names = null): array { $list = array(); - foreach($this->toList() as $field) + foreach($this->toList($names) as $field) { $list[$field->getName()] = $field->toArray(); } @@ -212,25 +239,18 @@ class fieldList return $fieldList; } - public static function getList(string $name): ?fieldList + public static function getList(string $listName): ?fieldList { - return isset(static::$map[$name]) ? static::$map[$name] : null; + return isset(static::$map[$listName]) ? static::$map[$listName] : null; } - public static function getListField(string $listName, string $name = null): ?field + public static function getListFields(string $listName, string $fieldNames = null): array { - if(is_null($name)) list($listName, $name) = explode('/', $listName); - if(is_null($name)) return null; + if(is_null($fieldNames)) list($listName, $fieldNames) = explode('/', $listName); + if(is_null($fieldNames)) return null; $fieldList = static::getList($listName); - return is_null($fieldList) ? null : $fieldList->get($name); - } - - public static function getByName(string $name): field|fieldList|null - { - if(str_ends_with($name, '/')) return static::getList(substr($name, 0, -1)); - if(str_contains($name, '/')) return static::getListField($name); - return static::getList($name); + return is_null($fieldList) ? array() : $fieldList->toList($fieldNames); } public static function ensure(string $name): fieldList