Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor dump() and add barDump()
  • Loading branch information
BernhardBaumrock committed May 25, 2020
1 parent 42d2679 commit 436257b
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 34 deletions.
41 changes: 32 additions & 9 deletions README.md
Expand Up @@ -71,7 +71,9 @@ $RockFinder3->find("template=foo");
$modules->get('RockFinder3')->find("template=foo");
```

## Adding columns
![img](hr.svg)

# Adding columns

You'll most likely don't need only ids, so there is the `addColumns()` method for adding additional columns:

Expand All @@ -85,22 +87,39 @@ $RockFinder3

This makes it possible to easily add any field data of the requested page.

## Dumping data
![img](hr.svg)

# Dumping data

For small finders Tracy's `dump()` feature is enough, but if you have more complex finders or you have thousands of pages this might get really inconvenient. That's why RockFinder3 ships with a custom `dump()` method that works in the tracy console and turns the result of the finder into a paginated table (using tabulator.info).

For small finders Tracy's `dump()` feature is enough, but if you have more complex finders or you have thousands of pages this might get really inconvenient. That's why RockFinder3 ships with a custom `dump()` method that works in the tracy console and turns the result of the finder into a paginated table (using tabulator.info):
For all the dumping methods you can provide two parameters:

1. The title of the dump*
1. The settings for the rendered tabulator table

*Note that if you set the title to TRUE the method will not only dump the tabulator but also the current RockFinder3 object (see the next example).

## dump() or d()

```php
$RockFinder3
->find("id>0")
->find("template=cat")
->addColumns(['title', 'created'])
->dump();
->dump(true);
```

![img](https://i.imgur.com/dfHdrG7.png)
![img](https://i.imgur.com/MbIA7fZ.png)

## barDump() or bd()

For situations where you are not working in the console but maybe in a template file or a module the `barDump()` method might be useful.

![img](https://i.imgur.com/LHhqJk5.png)

## Dumping the SQL of the finder

To understand what is going on it is important to know the SQL that is executed. You can easily dump the SQL query via the `dumpSQL()` method. This even supports chaining:
To understand what is going on it is important to know the SQL that is executed. You can easily dump the SQL query via the `dumpSQL()` or `barDumpSQL()` methods. This even supports chaining:

```php
$RockFinder3
Expand All @@ -114,7 +133,9 @@ $RockFinder3

![img](https://i.imgur.com/AfUy2OF.png)

## Renaming columns (column aliases)
![img](hr.svg)

# Renaming columns (column aliases)

Sometimes you have complicated fieldnames like `my_great_module_field_foo` and you just want to get the values of this field as column `foo` in your result:

Expand All @@ -127,7 +148,9 @@ $RockFinder3

![img](https://i.imgur.com/TIpk3pu.png)

## Custom column types
![img](hr.svg)

# Custom column types

You can add custom column types easily. Just place them in a folder and tell RockFinder to scan this directory for columnTypes:

Expand Down
112 changes: 87 additions & 25 deletions RockFinder3.module.php
Expand Up @@ -340,43 +340,96 @@ public function getRows() {

/** ########## TRACY DEBUGGER ########## */

public function barDump($title = null, $options = null) {
\TD::barEcho($this->dump($title, $options, true));
/**
* dump to console
* @param string|bool $title
* @param array $config
* @return RockFinder3
*/
public function dump($title = null, $config = null) {
echo $this->_dump([
'title' => $title === true ? null : $title,
'dump' => $title === true,
'config' => $config,
]);
return $this;
}

/**
* dump to console
* @param string|bool $title
* @param array $config
* @return RockFinder3
*/
public function d($title = null, $config = null) {
return $this->dump($title, $config);
}

/**
* Dump this finder to the tracy console
*
* Set title to TRUE to dump the finder object.
*
* @return void
* dump to tracy bar
* @param string|bool $title
* @param array $config
* @return RockFinder3
*/
public function dump($title = null, $options = null, $bardump = false) {
$settings = $this->wire(new WireData()); /** @var WireData $settings */
$settings->setArray([
public function barDump($title = null, $config = null) {
\TD::barEcho($this->_dump([
'title' => $title === true ? null : $title,
'barDump' => $title === true,
'config' => $config,
]));
return $this;
}

/**
* dump to tracy bar
* @param string|bool $title
* @param array $config
* @return RockFinder3
*/
public function bd($title = null, $config = null) {
return $this->barDump($title, $config);
}

/**
* Get the markup for the dump() or barDump()
* @param array $options
* @return string
*/
private function _dump($options = []) {
// set the options object for this method
$opt = $this->wire(new WireData()); /** @var WireData $opt */
$opt->setArray([
'title' => null,
'dump' => false,
'barDump' => false,
'config' => null,
]);
$opt->setArray($options);

// dump object?
if($opt->dump) db($this);
if($opt->barDump) bdb($this);

// setup tabulator config object
$config = $this->wire(new WireData()); /** @var WireData $config */
$config->setArray([
'layout' => 'fitColumns',
'autoColumns' => true,
'pagination' => "local",
'paginationSize' => 10,
'paginationSizeSelector' => true,
]);
$settings->setArray($options ?: []);
$settings = $settings->getArray();
$settings['data'] = $this->getRowArray();
$json = json_encode($settings);
$config->setArray($options ?: []);
$config = $config->getArray();
$config['data'] = $this->getRowArray();
$json = json_encode($config);

// prepare output
$id = uniqid();
$out = '';

if($title === true) {
if($bardump) {
bdb($this);
}
else {
db($this);
}
}
elseif($title) $out.= "<h2>$title</h2>";
// build output string
if($opt->title) $out.= "<h2>$opt->title</h2>";
$out .= "<div id='tab_$id'>loading...</div>
<script>
if(typeof Tabulator == 'undefined') {
Expand Down Expand Up @@ -411,14 +464,23 @@ public function dump($title = null, $options = null, $bardump = false) {
}

/**
* Dump SQL of current finder to console
* @return void
* Dump SQL of current finder to console (supports chaining)
* @return RockFinder3
*/
public function dumpSQL() {
db($this->getSQL());
return $this;
}

/**
* Dump SQL of current finder to tracy bar (supports chaining)
* @return RockFinder3
*/
public function barDumpSQL() {
bdb($this->getSQL());
return $this;
}

/** ########## END TRACY DEBUGGER ########## */

/**
Expand Down

0 comments on commit 436257b

Please sign in to comment.