Pods::next_id PHP Method

next_id() public method

Return the next item ID, loops at the first id to return the last
Since: 2.0
public next_id ( integer $id = null, $params_override = null ) : integer
$id integer
return integer
    public function next_id($id = null, $params_override = null)
    {
        if (null === $id) {
            $id = $this->id();
        }
        $id = (int) $id;
        $params = array('select' => "`t`.`{$this->data->field_id}`", 'where' => "{$id} < `t`.`{$this->data->field_id}`", 'orderby' => "`t`.`{$this->data->field_id}` ASC", 'limit' => 1);
        if (!empty($params_override) || !empty($this->params)) {
            if (!empty($params_override)) {
                $params = $params_override;
            } elseif (!empty($this->params)) {
                $params = $this->params;
            }
            if (is_object($params)) {
                $params = get_object_vars($params);
            }
            if (0 < $id) {
                if (isset($params['where']) && !empty($params['where'])) {
                    $params['where'] = (array) $params['where'];
                    $params['where'][] = "{$id} < `t`.`{$this->data->field_id}`";
                } else {
                    $params['where'] = "{$id} < `t`.`{$this->data->field_id}`";
                }
            } elseif (!isset($params['offset'])) {
                if (!empty($this->params) && -1 < $this->row_number) {
                    $params['offset'] = $this->row_number + 1;
                } else {
                    $params['offset'] = 1;
                }
            } else {
                $params['offset'] += 1;
            }
            $params['select'] = "`t`.`{$this->data->field_id}`";
            $params['limit'] = 1;
        }
        $pod = pods($this->pod, $params);
        $new_id = 0;
        if ($pod->fetch()) {
            $new_id = $pod->id();
        }
        $new_id = $this->do_hook('next_id', $new_id, $id, $pod, $params_override);
        return $new_id;
    }