/** File field (upload) */
public const FILE = 6;
+ public const MOVE_UP = 'up';
+ public const MOVE_DOWN = 'down';
+
public const PERM_USER_WRITE = 0;
public const PERM_ADMIN = 1;
public const PERM_STAFF = 2;
protected $values;
protected $form;
- protected $errors;
+ protected $errors = [];
protected $zdb;
if (is_int($args)) {
$this->load($args);
- } elseif ($args !== null && is_object($args)) {
+ } elseif (is_object($args)) {
$this->loadFromRs($args);
}
}
*
* @return DynamicField|false
*/
- public static function loadFieldType(Db $zdb, $id)
+ public static function loadFieldType(Db $zdb, int $id)
{
try {
$select = $zdb->select(self::TABLE);
/**
* Get correct field type instance
*
- * @param Db $zdb Database instance
- * @param int $t Field type
- * @param int $id Optional dynamic field id (to load data)
+ * @param Db $zdb Database instance
+ * @param int $t Field type
+ * @param int|null $id Optional dynamic field id (to load data)
*
* @return DynamicField
*/
- public static function getFieldType(Db $zdb, $t, $id = null)
+ public static function getFieldType(Db $zdb, int $t, int $id = null)
{
$df = null;
switch ($t) {
break;
default:
throw new \Exception('Unknown field type ' . $t . '!');
- break;
}
return $df;
}
*
* @return void
*/
- public function load($id)
+ public function load(int $id): void
{
try {
$select = $this->zdb->select(self::TABLE);
*
* @return void
*/
- public function loadFromRs($rs, $values = true)
+ public function loadFromRs($rs, bool $values = true): void
{
$this->id = (int)$rs->field_id;
$this->name = $rs->field_name;
$this->index = (int)$rs->field_index;
$this->perm = (int)$rs->field_perm;
- $this->required = ($rs->field_required == 1 ? true : false);
+ $this->required = $rs->field_required == 1;
$this->width = $rs->field_width;
$this->height = $rs->field_height;
- $this->repeat = $rs->field_repeat;
+ $this->repeat = (int)$rs->field_repeat;
$this->size = $rs->field_size;
$this->form = $rs->field_form;
if ($values && $this->hasFixedValues()) {
*
* @return string
*/
- public static function getFixedValuesTableName($id, $prefixed = false)
+ public static function getFixedValuesTableName(int $id, bool $prefixed = false): string
{
$name = 'field_contents_' . $id;
if ($prefixed === true) {
*
* @return integer
*/
- abstract public function getType();
+ abstract public function getType(): int;
/**
* Get field type name
*
* @return String
*/
- public function getTypeName()
+ public function getTypeName(): string
{
$types = $this->getFieldsTypesNames();
if (isset($types[$this->getType()])) {
return $types[$this->getType()];
} else {
throw new \RuntimeException(
- 'Unknow type ' . $this->getType()
+ 'Unknown type ' . $this->getType()
);
}
}
*
* @return boolean
*/
- public function hasData()
+ public function hasData(): bool
{
- return $this->has_data;
+ return (bool)$this->has_data;
}
/**
*
* @return boolean
*/
- public function hasWidth()
+ public function hasWidth(): bool
{
- return $this->has_width;
+ return (bool)$this->has_width;
}
/**
*
* @return boolean
*/
- public function hasHeight()
+ public function hasHeight(): bool
{
- return $this->has_height;
+ return (bool)$this->has_height;
}
/**
*
* @return boolean
*/
- public function hasSize()
+ public function hasSize(): bool
{
- return $this->has_size;
+ return (bool)$this->has_size;
}
/**
- * Is the field multi valued?
+ * Is the field multivalued?
*
* @return boolean
*/
- public function isMultiValued()
+ public function isMultiValued(): bool
{
- return $this->multi_valued;
+ return (bool)$this->multi_valued;
}
/**
*
* @return boolean
*/
- public function hasFixedValues()
+ public function hasFixedValues(): bool
{
- return $this->fixed_values;
+ return (bool)$this->fixed_values;
}
/**
*
* @return boolean
*/
- public function hasPermissions()
+ public function hasPermissions(): bool
{
- return $this->has_permissions;
+ return (bool)$this->has_permissions;
}
/**
* Get field id
*
- * @return integer
+ * @return integer|null
*/
- public function getId()
+ public function getId(): ?int
{
return $this->id;
}
/**
* Get field Permissions
*
- * @return integer
+ * @return integer|null
*/
- public function getPerm()
+ public function getPerm(): ?int
{
return $this->perm;
}
*
* @return boolean
*/
- public function isRequired()
+ public function isRequired(): bool
{
- return $this->required;
+ return (bool)$this->required;
}
/**
* Get field width
*
- * @return integer
+ * @return integer|null
*/
- public function getWidth()
+ public function getWidth(): ?int
{
return $this->width;
}
/**
* Get field height
*
- * @return integer
+ * @return integer|null
*/
- public function getHeight()
+ public function getHeight(): ?int
{
return $this->height;
}
*
* @return boolean
*/
- public function isRepeatable()
+ public function isRepeatable(): bool
{
return $this->repeat != null && trim($this->repeat) != '' && (int)$this->repeat >= 0;
}
/**
* Get fields repetitions
*
- * @return integer|boolean
+ * @return integer|null
*/
- public function getRepeat()
+ public function getRepeat(): ?int
{
return $this->repeat;
}
/**
* Get field size
*
- * @return integer
+ * @return integer|null
*/
- public function getSize()
+ public function getSize(): ?int
{
return $this->size;
}
/**
* Get field index
*
- * @return integer
+ * @return integer|null
*/
- public function getIndex()
+ public function getIndex(): ?int
{
return $this->index;
}
*
* @return array
*/
- public static function getPermsNames()
+ public static function getPermsNames(): array
{
return [
self::PERM_USER_WRITE => _T("User, read/write"),
*
* @return array
*/
- public static function getFormsNames()
+ public static function getFormsNames(): array
{
return [
'adh' => _T("Members"),
*
* @return string
*/
- public static function getFormTitle($form_name)
+ public static function getFormTitle(string $form_name): string
{
$names = self::getFormsNames();
return $names[$form_name];
*
* @return string
*/
- public function getPermName()
+ public function getPermName(): string
{
$perms = self::getPermsNames();
return $perms[$this->getPerm()];
*
* @return string
*/
- public function getForm()
+ public function getForm(): string
{
return $this->form;
}
*
* @param boolean $imploded Whether to implode values
*
- * @return array
+ * @return array|string|false
*/
- public function getValues($imploded = false)
+ public function getValues(bool $imploded = false)
{
if (!is_array($this->values)) {
return false;
*
* @return true|array
*/
- public function check($values)
+ public function check(array $values)
{
$this->errors = [];
$this->warnings = [];
*
* @return boolean
*/
- public function store($values)
+ public function store(array $values): bool
{
if (!$this->check($values)) {
return false;
*
* @return integer
*/
- protected function getNewIndex()
+ protected function getNewIndex(): int
{
$select = $this->zdb->select(self::TABLE);
$select->columns(
$results = $this->zdb->execute($select);
$result = $results->current();
$idx = $result->idx;
- return $idx;
+ return (int)$idx;
}
/**
*
* @return boolean
*/
- public function isDuplicate()
+ public function isDuplicate(): bool
{
//let's consider field is duplicated, in case of future errors
$duplicated = true;
}
return $duplicated;
}
+
/**
* Move a dynamic field
*
- * @param string $action What to do (either 'up' or 'down')
+ * @param string $action What to do (one of self::MOVE_*)
*
* @return boolean
*/
- public function move($action)
+ public function move(string $action): bool
{
+ if ($action !== self::MOVE_UP && $action !== self::MOVE_DOWN) {
+ throw new \RuntimeException(('Unknown action ' . $action));
+ }
+
try {
$this->zdb->connection->beginTransaction();
$old_rank = $this->index;
- $direction = $action == 'up' ? -1 : 1;
+ $direction = $action == self::MOVE_UP ? -1 : 1;
$new_rank = $old_rank + $direction;
$update = $this->zdb->update(self::TABLE);
$update->set([
*
* @return boolean
*/
- public function remove()
+ public function remove(): bool
{
try {
if ($this->hasFixedValues()) {
*
* @return array
*/
- public static function getFieldsTypesNames()
+ public static function getFieldsTypesNames(): array
{
$names = [
self::SEPARATOR => _T("separator"),
*
* @return array
*/
- public function getErrors()
+ public function getErrors(): array
{
return $this->errors;
}
*
* @return array
*/
- public function getWarnings()
+ public function getWarnings(): array
{
return $this->warnings;
}