示例#1
0
})->via('GET', 'POST', 'PUT');
// GET and PUT table details
$app->map("/{$v}/tables/:table/?", function ($table) use($ZendDb, $acl, $params, $requestPayload, $app) {
    $TableGateway = new TableGateway($acl, 'directus_tables', $ZendDb, null, null, null, 'table_name');
    $ColumnsTableGateway = new TableGateway($acl, 'directus_columns', $ZendDb);
    /* PUT updates the table */
    if ($app->request()->isPut()) {
        $data = $requestPayload;
        $table_settings = array('table_name' => $data['table_name'], 'hidden' => (int) $data['hidden'], 'single' => (int) $data['single'], 'is_junction_table' => (int) $data['is_junction_table'], 'footer' => (int) $data['footer'], 'primary_column' => array_key_exists('primary_column', $data) ? $data['primary_column'] : '');
        //@TODO: Possibly pretty this up so not doing direct inserts/updates
        $set = $TableGateway->select(array('table_name' => $table))->toArray();
        //If item exists, update, else insert
        if (count($set) > 0) {
            $TableGateway->update($table_settings, array('table_name' => $table));
        } else {
            $TableGateway->insert($table_settings);
        }
        $column_settings = array();
        foreach ($data['columns'] as $col) {
            $columnData = array('table_name' => $table, 'column_name' => $col['column_name'], 'ui' => $col['ui'], 'hidden_input' => $col['hidden_input'], 'required' => $col['required'], 'master' => $col['master'], 'sort' => array_key_exists('sort', $col) ? $col['sort'] : 99999, 'comment' => array_key_exists('comment', $col) ? $col['comment'] : '');
            $existing = $ColumnsTableGateway->select(array('table_name' => $table, 'column_name' => $col['column_name']))->toArray();
            if (count($existing) > 0) {
                $columnData['id'] = $existing[0]['id'];
            }
            array_push($column_settings, $columnData);
        }
        $ColumnsTableGateway->updateCollection($column_settings);
    }
    $response = TableSchema::getTable($table);
    JsonView::render($response);
})->via('GET', 'PUT')->name('table_meta');