Skip to content

Commit

Permalink
Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
Ronef committed Oct 31, 2019
0 parents commit e8295fd
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# (MODX)EvolutionCMS.snippets.ddStash changelog


## Version 1.0 (2019-10-31)
* \+ The first release.


<style>ul{list-style:none;}</style>
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# (MODX)EvolutionCMS.snippets.ddStash

Сохранение данных для последующего использования.


## # Requires
* PHP >= 5.4
* [(MODX)EvolutionCMS](https://github.com/evolution-cms/evolution) >= 1.1
* [(MODX)EvolutionCMS.libraries.ddTools](http://code.divandesign.biz/modx/ddtools) >= 0.28


## # Documentation


### ## Installation

Elements → Snippets: Create a new snippet with the following data:

1. Snippet name: `ddStash`.
2. Description: `<b>1.0</b> Сохранение данных для последующего использования.`.
3. Category: `Core`.
4. Parse DocBlock: `no`.
5. Snippet code (php): Insert content of the `ddStash_snippet.php` file from the archive.


### ## Parameters description

* `save`
* Desctription: Data to save in stash. Arrays are supported too: `some[a]=one&some[b]=two` => `[+some.a+]`, `[+some.b+]`; `some[]=one&some[]=two` => `[+some.0+]`, `[some.1]`.
* Valid values:
* `stirng_json` — as [JSON](https://en.wikipedia.org/wiki/JSON)
* `string_queryFormated` — as [Query string](https://en.wikipedia.org/wiki/Query_string)
* Default value: —

* `get`
* Desctription: Data key for getting from stash.
* Valid values: `string`
* Default value: —

* `get_tpl`
* Desctription: Output template.
Available placeholders:
* `[+snippetResult+]` — Data from stash.
* Valid values:
* `string_chunkName`
* `string` — use inline templates starting with `@CODE:`
* Default value: `'@CODE:[+snippetResult+]'`

* `storage`
* Desctription: Data storage.
* Valid values:
* `'post'` — `$_POST`
* `'session'` — `$_SESSION`
* Default value: `'post'`


### ## Examples


#### ### Save some data

```
[[ddStash?
&save=`{
"userData": {
"name": "John",
"photo": "[[ddGetDocumentField?
&docId=`1`
&docField=`photo`
]]"
},
"someData": "someValue"
}`
]]
```


#### ### Get saved data

```
[[ddStash? &get=`userData.name`]]
```

Returns `John`.
15 changes: 15 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "dd/evolutioncms-snippets-ddstash",
"type": "modxevo-snippet",
"version": "1.0",
"description": "Сохранение данных для последующего использования.",
"keywords": [
"modx",
"modx evo",
"modx evolution",
"evo",
"evo cms",
"evolution cms",
"ddstash"
]
}
73 changes: 73 additions & 0 deletions ddStash_snippet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* ddStash
* @version 1.0 (2019-10-31)
*
* @see README.md
*
* @copyright 2019 DivanDesign {@link http://www.DivanDesign.biz }
*/

//Include (MODX)EvolutionCMS.libraries.ddTools
require_once($modx->getConfig('base_path') . 'assets/libs/ddTools/modx.ddtools.class.php');

//The snippet must return an empty string even if result is absent
$snippetResult = '';


//Prepare storage
if (!isset($storage)){
$storage = 'post';
}

switch ($storage){
case 'session':
$storage = &$_SESSION;
break;

case 'post':
default:
$storage = &$_POST;
break;
}


//Save to stash
if (isset($save)){
$save = \ddTools::unfoldArray(\ddTools::encodedStringToArray($save));

foreach (
$save as
$dataName =>
$dataValue
){
$storage['ddStash.' . $dataName] = $dataValue;
}
}


//Get from stash
if (
isset($get) &&
isset($storage['ddStash.' . $get])
){
$snippetResult = $storage['ddStash.' . $get];

if (
//If template is used
isset($get_tpl) &&
//And result is not empty
!empty($snippetResult)
){
$snippetResult = \ddTools::parseText([
'text' => $modx->getTpl($get_tpl),
'data' => [
'snippetResult' => $snippetResult
]
]);
}
}


return $snippetResult;
?>

0 comments on commit e8295fd

Please sign in to comment.