# Extension¶

The purpose of an extension is to become a standard interface which you can use to share common function that utilize either a CI Library or your own library across your Gas models/instances.

We use an Interface instead of a Class in the extension implementation because :

• Interfaces allow us to define/create a common structure for our classes – to set a standard for objects.
• Interfaces solves the problem of single inheritance – they allow us to inject ‘qualities’ from multiple sources.
• Interfaces provide a flexible base/root structure that we don’t get with classes.

Here you can found some default extension which are bundled with Gas ORM.

## Dummy Extension¶

I included this dummy extension within the repo as well. Lets look how it works :

```use \Gas\Extension;

class Dummy implements Extension {

/**
* @var mixed Gas instance(s)
*/
public \$gas;

/**
* Extension initialization method
*
* @param  object
* @return void
*/
function __init(\$gas)
{
// Here, Gas will transport your instance
\$this->gas = \$gas;

return \$this;
}

/**
* Simple example of Extension usage
*
* @param  string Argument
* @return string Explanation
*/
public function explain(\$arg = NULL)
{
\$CI =& get_instance();

// Build the Extension information
\$argument = var_export(\$arg, TRUE);
\$fullname = __CLASS__;
\$fragment = explode('\\', \$fullname);
\$nickname = strtolower(end(\$fragment));
\$path     = __FILE__;

// Determine the caller condition
if (is_object(\$this->gas))
{
// This for single Gas instance
\$model         = ucfirst(\$this->gas->model());
\$structure     = \$this->gas->meta->get('collumns');
\$relationships = array_keys(\$this->gas->meta->get('entities'));

// Get all the records and start build the record table
\$records = \$this->gas->record->get();

foreach (\$records as \$record)
{
}

\$table = \$CI->table->generate();
\$CI->table->clear();
}
elseif (is_array(\$this->gas) && ! empty(\$this->gas))
{
// This for a collection of Gas instance(s)
\$sample        = \$this->gas;
\$gas           = array_shift(\$sample);
\$model         = ucfirst(\$gas->model());
\$structure     = \$gas->meta->get('collumns');
\$relationships = array_keys(\$gas->meta->get('entities'));

// Get all the records and start build the record table
\$records = \$this->gas;

foreach (\$records as \$record)
{
}

\$table = \$CI->table->generate();
\$CI->table->clear();
}
else
{
// This for nothing
\$model         = 'NULL';
\$structure     = array('undefined');
\$relationships = array('undefined');
\$records       = array();
\$table         = '<strong>Empty Record</strong>'."\n";
}

// Now build the explanation
\$explanation  = 'Hello, i am an extension. ';
\$explanation .= 'My nickname is <strong>'.\$nickname.'</strong> and my fullname is <strong>'.\$fullname.'</strong>.';
\$explanation .= "\n";
\$explanation .= 'You can found me at '.\$path.'.'."\n";
\$explanation .= 'You call me through '.\$model.' instance, and passing bellow arguments : '."\n";
\$explanation .= \$argument ."\n";
\$explanation .= 'to processed, and to explain what '.\$model.' model looks like.'."\n";
\$explanation .= \$model.' have table structure : ';
\$explanation .= implode(', ', \$structure)."\n";
\$explanation .= \$model.' have defined relationships : ';
\$explanation .= implode(', ', \$relationships)."\n";
\$explanation .= \$model.' instance now is holding : '.count(\$records).' record(s)'."\n";
\$explanation .= 'With little help from CodeIgniter\'s Table and Typography library,'."\n";
\$explanation .= 'I can create this paragraph, also output the record into this table : '."\n";
\$explanation .= \$table;
\$explanation .= 'So basicly, my purpose is to become a standard interface which you can use,'."\n";
\$explanation .= 'to share common function which utilize either CI Library or your own library, '."\n";
\$explanation .= 'across your Gas models/instances.'."\n";
\$explanation .= 'This is all I can say.'."\n";

// Format the explanation, then output it
\$formatted_explanation = \$CI->typography->auto_typography(\$explanation);

return '<pre>'.\$formatted_explanation.'</pre>';
}
}
```

You can directly use it.

```echo Model\User::dummy()->all()->explain();
```

This should be a simple way to describe how an extension works in Gas ORM.

## Result Extension¶

This extension provides a convinient way to work with Gas ORM instances and records :

```\$result = Model\User::result()->all();

// Easy way to debug over your result
echo \$result;

// Convert the instances into an array of instance
\$users = \$result->as_array();

foreach (\$users as \$user)
{
// \$user will be a typical Gas Instance
}

// Convert all instances record into various format
\$records_array = \$result->to_array(); // assoc array
\$records_json = \$result->to_json();   // JSON
\$records_xml = \$result->to_xml();     // XML

// Convert all instance's record into Gas\Data object
\$records_data = \$result->to_data();

\$first_user = \$records_data->get('data.0'); // Return the first index
\$first_user_name = \$records_data->get('data.0.name'); // Return the first index name
\$some_user_name = \$records_data->get('data.100.name', 'Default Name');```

If you need more functionality, simply extend it on your application/libraries/gas/extension/result.php.

## HTML Extension¶

You can see the demo for table on my sandbox  . Generate HTML table from Gas model records

```// execute some Gas finder
\$users = Model\User::html()->all();

// simple usage
echo \$users->table();

// hide some collumn

echo \$users->hide(\$hidden)->table();

// set table template
\$template = array( 'table_open' => '<table border="1" cellpadding="4" cellspacing="0">');

echo \$users->template(\$template)->table();

// hide some collumn, set table heading, set template
\$hidden = array('email');

\$template = array( 'table_open' => '<table border="1" cellpadding="4" cellspacing="0">');

```

Generate HTML form from Gas model records

```// execute some Gas finder
\$user = Model\User::html()->find(1);

// simple usage
echo \$user->form('controller/function');

// define entity type
\$entities = array();

\$entities['email'] = array('hidden' => array('id' => 'email'));

echo \$user->definition(\$entities)->form('controller/function');
```

There are options for setting submit, separator, entity and hide as well. You can see the demo on my sandbox 

## jQuery Extension¶

This extension will be a good place for sharing common handlers for any similar jQuery data processor plugins (eg : flot  for outputing graph or chart).

For now, it provides a method to handle and generate responses for datatables. 

Assume you have downloaded the file, put it into your application directory and configured it properly, point it to some controller as ajax source, then within your controller (which receives the ajax request), you only need to put

```if (\$_POST)
{
echo Model\User::jquery()->datatable(\$_POST);
}
else
{
echo Model\User::jquery()->datatable(\$_GET);
}
```

That will serve a datatable for browsing the user table. You can see the demo on my sandbox  .

## Write your own Gas ORM extension¶

From the above extension examples, if you are ready to create your own you should remember :

• Your extension, should have namespace Gas\Extension.
• Your extension, should implements Gas\Extension interface.
• Your extension, should have __init(\$gas) method (notice the double underscore to distinguish it from your model’s init method).
• Your extension, should be located under the libraries/gas/extension folder within your application
{"ft_lang_label":"__label__en","ft_lang_prob":0.6167457,"math_prob":0.7785119,"size":8146,"snap":"2020-10-2020-16","text_gpt3_token_len":1990,"char_repetition_ratio":0.14050601,"word_repetition_ratio":0.07670455,"special_character_ratio":0.29094034,"punctuation_ratio":0.17253774,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9615311,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-26T04:19:37Z\",\"WARC-Record-ID\":\"<urn:uuid:3f948198-ef27-415a-8dc0-807426ba7cb5>\",\"Content-Length\":\"39539\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ad0d4496-f0af-46c9-a34d-7996edbda2a3>\",\"WARC-Concurrent-To\":\"<urn:uuid:1395f7a3-f053-4a1e-b510-359b477d04e8>\",\"WARC-IP-Address\":\"103.23.20.94\",\"WARC-Target-URI\":\"http://gasorm-doc.taufanaditya.com/extension.html\",\"WARC-Payload-Digest\":\"sha1:LVXS3O4UTTZ4TBTRCQV5XOWPCEKYMZKN\",\"WARC-Block-Digest\":\"sha1:MRYM5QHP3O2Z5YVYPRVZYWV5OTZLBRR7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875146186.62_warc_CC-MAIN-20200226023658-20200226053658-00356.warc.gz\"}"}