Backend\Modules\FormBuilder\Engine\Api::entriesGet PHP Method

entriesGet() public static method

Get the entries for a form
public static entriesGet ( integer $id, integer $limit = 30, integer $offset ) : array
$id integer The id of the form.
$limit integer The maximum number of items to retrieve.
$offset integer The offset.
return array
    public static function entriesGet($id, $limit = 30, $offset = 0)
    {
        if (BaseAPI::isAuthorized() && BaseAPI::isValidRequestMethod('GET')) {
            // redefine
            $id = (int) $id;
            $limit = (int) $limit;
            $offset = (int) $offset;
            // validate
            if ($limit > 10000) {
                return BaseAPI::output(BaseAPI::ERROR, array('message' => 'Limit can\'t be larger than 10000.'));
            }
            $dataIDs = (array) BackendModel::getContainer()->get('database')->getColumn('SELECT a.id
                 FROM forms_data AS a
                 WHERE a.form_id = ?
                 ORDER BY a.sent_on DESC
                 LIMIT ?,?', array($id, $offset, $limit));
            if (empty($dataIDs)) {
                return array();
            }
            $fields = (array) BackendModel::getContainer()->get('database')->getRecords('SELECT i.type, i.settings
                 FROM forms_fields AS i
                 WHERE i.form_id = ?', array($id));
            $fieldTypes = array();
            foreach ($fields as $row) {
                $row['settings'] = unserialize($row['settings']);
                if (isset($row['settings']['label'])) {
                    $fieldTypes[$row['settings']['label']] = $row['type'];
                }
            }
            $entries = (array) BackendModel::getContainer()->get('database')->getRecords('SELECT i.*, f.data_id, f.label, f.value, UNIX_TIMESTAMP(i.sent_on) AS sent_on
                 FROM forms_data AS i
                 INNER JOIN forms_data_fields AS f ON i.id = f.data_id
                 WHERE i.id IN(' . implode(',', $dataIDs) . ')
                 ORDER BY i.sent_on DESC');
            $return = array('entries' => null);
            // any entries?
            if (empty($entries)) {
                return $return;
            }
            $data = array();
            foreach ($entries as $row) {
                if (!isset($data[$row['data_id']])) {
                    $data[$row['data_id']] = $row;
                }
                $data[$row['data_id']]['fields'][$row['label']] = unserialize($row['value']);
            }
            foreach ($data as $row) {
                $item['entry'] = array();
                // set attributes
                $item['entry']['@attributes']['form_id'] = $row['form_id'];
                $item['entry']['@attributes']['id'] = $row['id'];
                $item['entry']['@attributes']['sent_on'] = date('c', $row['sent_on']);
                // set content
                foreach ($row['fields'] as $key => $value) {
                    $item['entry']['fields']['fields'][] = array('field' => array('name' => $key, 'value' => $value, 'guessed_type' => isset($fieldTypes[$key]) ? $fieldTypes[$key] : 'textbox'));
                }
                $return['entries'][$row['id']] = $item;
            }
            $return['entries'] = array_values($return['entries']);
            return $return;
        }
    }