Future Specification - lokothodida/DM_Matrix GitHub Wiki

Previous Specification

DM_query(query) - perform a simple query, results returned in an array
DM_getSchema - get the main Schema 
DM_saveSchema - Save the schema out to an XML file schema.xml
createSchemaFolder - create the base folder for the record files
createRecord - Add a record/file to a table
getNextRecord - returns the next record number in the table
createSchemaTable - Create a new table
dropSchemaTable - Delete a table
addSchemaField - add a field to a table
deleteSchemaField - Delete a field from a table
getSchemaTable - return an array of a tables records. 
DM_getRecord - get a single record from a table,  supplying tabel and id of record
tableexists - check if a table exists 
updateRecord - update a record 
DM_createForm - create an input form from a table 
DM_editForm - create a form for editing providing a record number.

Proposed Specification

Tables

bool matrix_table_exists(string $name)

Check that a table exists

if (matrix_table_exists('blog')) {
  // ...
}

bool matrix_create_table(string $name, array $params)

Create a table with given parameters $params. Returns true on success.

$create = matrix_create_table('blog', array(
  'maxrecords' => null,
  'fields'     => array(
    'slug' => array(
      'type' => 'slug'
    ),
    'title' => array(
      'type' => 'textlong',
    ),
    'content' => array(
      'type' => 'wysiwyg',
    ),
  )
));

bool matrix_edit_table(string $name, array $params)

Update a table with $params. Any fields set to false will be deleted. Returns true on success.

$edit = matrix_edit_table('blog', array(
  'maxrecords' => null,
  'fields'     => array(
    // Removes the title field
    'title' => false,

    // Changes content field to codeeditor type
    content' => array(
      'type' => 'codeeditor',
    ),
  )
));

bool matrix_delete_table(string $name)

Deletes a table. Returns true on success.

$delete = matrix_delete_table('blog');

array matrix_get_table_schema(string $name)

Gets the schema of a given table.

fields     => array of fields of the table
maxrecords => maximum number of records for the table
$schema = matrix_get_table_schema('articles');

echo $schema['fields']['title']['type']; // textlong

Records

array matrix_query(array $query)

Runs a query with $query parameters. Returns array:

results => array of results
total   => full size of the query (before limiting)
$results = matrix_query(array(
  // SELECT userid, username, title, pubdate
  'select' => array('userid', 'username', 'title', 'pubdate'),

  // FROM users, articles
  'from'   => array('users', 'articles'),

  // WHERE users.userid = articles.submittedby
  'where'  => function($user, $article) {
    return $user['userid'] == $article['submittedby'];
  },

  // ORDER BY pubdate DESC
  'orderby' => '-pubdate',

  // LIMIT 5
  'limit'   => 5,

  // OFFSET 5
  'offset' => 5
));

foreach ($results as $result['results']) {
  echo 'Article ' . $result['title'] . ' was submitted by '
                  . $result['username'] . ' on ' . $result['pubdate'];
}

bool matrix_create_record(string $table, array $data)

Creates a record in $table with $data. Returns true on success.

$create = matrix_create_record('articles', array(
  'title'   => 'A very interesting piece',
  'content' => 'With some very interesting stuff to say...',
));

bool matrix_edit_record(string $table, int $id, array $data)

Edits record in $table with id $id, using $data. Returns true on success.

$edit = matrix_edit_record('articles', 4, array(
  'title' => 'New title'
));

bool matrix_delete_record(string $table, int $id)

Deletes a record in $table with id $id. Returns true on success.

$delete = matrix_delete_record('articles', 4);

array matrix_get_record(string $table, int $id)

Returns record data in $table with id $id. Returns false if the record doesn't exist.

$article = matrix_get_record('articles', 4);

if ($article) {
  echo $article['title'];
}

array matrix_get_next_record(string $table)

Returns data in $table of the latest record. Returns false if table is empty or doesn't exist.

$article = matrix_get_next_record('articles');

if ($article) {
  echo $article['title'];
}

Forms

matrix_get_create_form(string $table)

Displays a form for creating a record in $table.

echo '<form action="create_entry.php" method="post">';
  matrix_get_create_form('blog');
  echo '<input type="submit" value="Create Entry"/>';
echo '</form>';

matrix_get_edit_form(string $table, int $id)

Displays a form for editing a record in $table with id $id.

echo '<form action="edit_entry.php" method="post">';
  matrix_get_edit_form('blog', 5);
  echo '<input type="submit" value="Update Entry"/>';
echo '</form>';
⚠️ **GitHub.com Fallback** ⚠️