Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/pointybeard/symphony-section-builder

A set of classes that assist with the creating, updating, importing, and exporting of sections and their fields.
https://github.com/pointybeard/symphony-section-builder

Last synced: 9 days ago
JSON representation

A set of classes that assist with the creating, updating, importing, and exporting of sections and their fields.

Awesome Lists containing this project

README

        

# Symphony CMS: Section Builder

- Version: 0.2.1
- Date: Aug 6 2019
- [Release notes](https://github.com/pointybeard/symphony-section-builder/blob/master/CHANGELOG.md)
- [GitHub repository](https://github.com/pointybeard/symphony-section-builder)

A set of classes that assist with the creating and updating of sections and their fields.

## Installation

This libary can be used standalone or as part of a [Symphony CMS](https://getsymphony.com) installation (including Extension).

### Standalone

Clone desired version from the GitHub repository with `git clone https://github.com/pointybeard/symphony-section-builder.git` then run `composer update` within that folder. Note, this will install dev library `symphonycms/symphony-2` by default. Use `--no-dev` when running `composer update` to skip this.

### Using Composer

To install via [Composer](http://getcomposer.org/), use `composer require pointybeard/symphony-section-builder` or add `"pointybeard/pointybeard/symphony-section-builder": "^0.2.0"` to your `composer.json` file.

And run composer to update your dependencies, for example:

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar update

Note that this method will NOT install any dev libraries, specifically `symphonycms/symphony-2`. Generally this is the desired behaviour, however, should the core Symphony CMS library not get included anywhere via composer (e.g. Section Builder is being used as part of a library that doesn't already include Symphony CMS), be use to use the `--dev` flag (e.g. `composer update --dev`) to ensure `symphonycms/symphony-2` is also installed, or, use the `--symphony=PATH` option to tell Section Builder where to load the Symphony CMS core from.

## Usage

Quick example of how to use this library:

```php
name("Categories")
->handle("categories")
->navigationGroup("Content")
->allowFiltering(true)
->hideFromBackendNavigation(false)
->addField(
(new Models\Fields\Input)
->label("Name")
->elementName("name")
->location(SectionBuilder\AbstractField::PLACEMENT_MAIN_CONTENT)
->required(true)
->showColumn(true)
->validator("")
)
->commit();
;
}

$articles = Models\Section::loadFromHandle('articles');
if(!($articles instanceof Models\Section)) {
$articles = (new Models\Section)
->name("Articles")
->handle("articles")
->navigationGroup("Content")
->allowFiltering(true)
->hideFromBackendNavigation(false)
->addField(
(new Models\Fields\Input)
->label("Title")
->elementName("title")
->location(SectionBuilder\AbstractField::PLACEMENT_MAIN_CONTENT)
->required(true)
->showColumn(true)
->validator("")
)
->addField(
(new Models\Fields\Textarea)
->label("Body")
->elementName("body")
->location(SectionBuilder\AbstractField::PLACEMENT_MAIN_CONTENT)
->required(true)
->showColumn(true)
->size(10)
->formatter(null)
)
->addField(
(new Models\Fields\Date)
->label("Created At")
->elementName("date_created_at")
->location(SectionBuilder\AbstractField::PLACEMENT_SIDEBAR)
->required(true)
->showColumn(true)
->prePopulate("now")
->calendar(false)
->time(true)
)
->addField(
(new Models\Fields\Select)
->label("Categories")
->elementName("categories")
->location(SectionBuilder\AbstractField::PLACEMENT_SIDEBAR)
->required(true)
->showColumn(true)
->allowMultipleSelection(true)
->sortOptions(true)
->staticOptions(null)
->dynamicOptions(
$categories->findFieldByElementName("name")
)
)
->commit()
;
}

print "Success!!" . PHP_EOL;

} catch (\Exception $ex) {
print "FAILED: " . $ex->getMessage() . PHP_EOL;
}
```

### Importing JSON

Run `bin/import` from the command line or use code like this:

```php
__toJson();
print_r($section->__toArray());

```

If a full export is necessary, use the `all()` method and build the array before encoding it to JSON. e.g.

```php
[]];
foreach(Models\Section::all() as $section) {
// We use json_decode() to ensure ids (id and sectionId) are removed
// from the output. To keep ids, use __toArray()
$output["sections"][] = json_decode((string)$section, true);
}

print json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

```

Note that IDs (specifically Section and Field `id` and Field `sectionId` properties) are automatically stripped out by `__toString()` and `__toJson()`. To keep them, either use `__toArray()` and encode to JSON yourself, or using `__toJson()` but set `$excludeIds` to false. e.g. `$section->__toJson(false)`. See this implementation in the Trait `hasToStringToJsonTrait`.

### Diff

You can compare a database with a JSON export via `bin/diff` from the command line or use code like this:

```php