<?php
/**
 * Pi Engine (http://piengine.org)
 *
 * @link         http://code.piengine.org for the Pi Engine source repository
 * @copyright    Copyright (c) Pi Engine http://piengine.org
 * @license      http://piengine.org/license.txt BSD 3-Clause License
 */

namespace Module\Article\Form\Element;

use Pi;
use Laminas\Form\Element\Select;

/**
 * Account element class
 *
 * @author Zongshu Lin <lin40553024@163.com>
 */
class Account extends Select
{
    /**
     * Read account from database
     *
     * @return array
     */
    public function getValueOptions()
    {
        if (empty($this->valueOptions)) {
            // Getting children role of article module
            $rowRole = Pi::model('acl_role')->getChildren('article-manager');
            $roles   = ['article-manager'];
            foreach ($rowRole as $role) {
                $roles[] = $role;
            }

            // Getting account ID
            $rowset = Pi::model('user_role')->select(['role' => $roles]);
            $ids    = [0];
            foreach ($rowset as $row) {
                $ids[$row->user] = $row->user;
            }

            // Getting active account
            $rowset  = Pi::user()->get($ids, ['id', 'name']);
            $account = [];
            foreach ($rowset as $row) {
                $account[$row['id']] = $row['name'];
            }
            $account            = empty($account) ? [0 => __('Null')] : $account;
            $this->valueOptions = $account;
        }

        return $this->valueOptions;
    }
}
