diff --git a/lib/zin/zentao/field.class.php b/lib/zin/zentao/field.class.php index 56d3002f3c..c7f71d0cc9 100644 --- a/lib/zin/zentao/field.class.php +++ b/lib/zin/zentao/field.class.php @@ -194,6 +194,11 @@ class field extends setting return $this->setVal($side == 'before' ? 'wrapBefore' : 'wrapAfter', $wrap); } + function text(bool|string|null $text): field + { + return $this->setVal('text', $text); + } + function tip(bool|string|null $tip): field { return $this->setVal('tip', $tip); @@ -242,21 +247,54 @@ class field extends setting return $this->parent->control($this->toArray()); } - function items(array|object|false|null $items): field + /** + * Set items. + * + * @access public + * @param array|false $items - Items. + * @param bool $reset - Whether to reset items. + * @return field + */ + function items(array|false $items, bool $reset = false): field { if($items === false) return $this->remove('items'); - return $this->addToList('items', $items); + if($reset) return $this->setVal('items', $items); + return $this->addToMap('items', $items); } - function item(string|array|object|null $itemOrName, array|object|null $item = null): field + /** + * Add item. + */ + function item(array|object ...$items): field { - if(is_null($itemOrName)) return $this; - if(is_string($itemOrName)) + $list = array(); + foreach($items as $item) { - if(is_null($item)) return $this->removeItem($itemOrName); - return $this->addToList('items', array($itemOrName => $item)); + $name = null; + if($item instanceof field) + { + $name = $item->getName(); + } + if($item instanceof dataset) + { + $name = $item->get('name'); + } + elseif(is_array($item)) + { + if(isset($item['name'])) $name = $item['name']; + } + if(is_null($name)) + { + trigger_error('[ZIN] The item for adding to field "' . $this->getName() . '" has no name.', E_USER_ERROR); + } + $list[$name] = $item; } - return $this->addToList('items', array($itemOrName)); + return $this->addToMap('items', $list); + } + + function removeItem(string ...$names): field + { + return $this->removeFromMap('items', ...$names); } function itemBegin(?string $itemName = null): field @@ -274,11 +312,6 @@ class field extends setting return $this->parent; } - function removeItem(string $itemName): field - { - return $this->removeFromList('items', $itemName); - } - function children(mixed ...$children): field { return $this->addToList('children', ...$children); diff --git a/lib/zin/zentao/field.func.php b/lib/zin/zentao/field.func.php index 59d2ca69fc..494f176412 100644 --- a/lib/zin/zentao/field.func.php +++ b/lib/zin/zentao/field.func.php @@ -16,11 +16,17 @@ namespace zin; require_once __DIR__ . DS . 'field.class.php'; require_once __DIR__ . DS . 'fieldlist.class.php'; -function defineFieldList(string $name = null, string|array|field|fieldList|null ...$args): fieldList +/** + * Define a field list. + * + * @param string $name The name of field list. + * @param string|array|field|fieldList|null ...$extends The extend fields and field list. + * + * @return fieldList + */ +function defineFieldList(string $name, string|array|field|fieldList|null ...$extends): fieldList { - global $app; - if(is_null($name)) $name = $app->rawModule; - return fieldList::define($name, ...$args); + return fieldList::define($name, ...$extends); } function defineField(string $name, ?string $listName = null): field @@ -29,7 +35,10 @@ function defineField(string $name, ?string $listName = null): field { list($listName, $name) = explode('/', $name); } - return defineFieldList($listName)->field($name); + + if(is_null($listName)) $listName = fieldList::$currentName; + $fieldList = fieldList::ensure($listName); + return $fieldList->field($name); } function fieldList(string $name) @@ -37,9 +46,9 @@ function fieldList(string $name) return fieldList::ensure($name); } -function field(string $name): field +function field(string|object|array|null $nameOrProps = null): field { - return new field($name); + return new field($nameOrProps); } function useFields(string|array|field|fieldList|null ...$args): fieldList diff --git a/lib/zin/zentao/fieldlist.class.php b/lib/zin/zentao/fieldlist.class.php index 9cf894251f..b2f8f8de6b 100644 --- a/lib/zin/zentao/fieldlist.class.php +++ b/lib/zin/zentao/fieldlist.class.php @@ -150,10 +150,16 @@ class fieldList return $this; } - public function remove(string|array $names): fieldList + public function remove(string ...$names): fieldList { - if(is_string($names)) $names = explode(',', $names); - foreach($names as $name) unset($this->fields[$name]); + foreach($names as $name) + { + $nameList = explode(',', $name); + foreach($nameList as $nameItem) + { + unset($this->fields[$nameItem]); + } + } return $this; } @@ -182,6 +188,11 @@ class fieldList return $this; } + public function names(): array + { + return array_keys($this->fields); + } + public function batchSet(string|array $names, string|array $prop, mixed $value = null): fieldList { return $this;