Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
meysampg committed Oct 8, 2016
0 parents commit 42b160b
Show file tree
Hide file tree
Showing 13 changed files with 5,435 additions and 0 deletions.
178 changes: 178 additions & 0 deletions FormBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

namespace meysampg\formbuilder;

use yii\base\Widget;
use yii\helpers\Inflector;
use yii\base\InvalidConfigException;

class FormBuilder extends Widget
{
/**
* @property string HTML tag for form builder constructor
*/
public $elementType = 'div';

/**
* @property array list of elements for rendering as default elements of form builder
*/
public $data = [];

/**
* @property string indicates that input and output data must be XML or JSON
*/
public $dataType = 'xml';

/**
* @property array list of label strings on a desired language.
*/
public $messages = [];

/**
* @property array list of plugin options, see [FormBuilder Documentations](http://formbuilder.readthedocs.io/en/latest/)
*/
public $options = [];

/**
* @property string JavaScript variable name for accessing to formbuilder contents on development
*/
public $accessVariableName = 'formBuilderJsVariable';

/**
* @property boolean indicates that control buttons be showed or not
*/
public $showActionButtons = false;

private $view;

private $arrayToTypeFunction;

public function init()
{
parent::init();

$this->view = $this->getView();

if ($this->dataType == 'json') {
$this->arrayToTypeFunction = 'arrayJsonEncode';
} else if ($this->dataType == 'xml') {
$this->arrayToTypeFunction = 'arrayXmlEncode';
} else {
throw new InvalidConfigException('Property $dataType must be "xml" or "json".');
}
}

public function run()
{
FormBuilderAsset::register($this->view);
$this->view->registerJs($this->getFBJs());

return '<' . $this->elementType . ' id="' . $this->fBId . '"></' . $this->elementType . '>';
}

/**
* @return string unique id of form builder
*/
public function getFBId()
{
return $this->id . '-fb-element';
}

/**
* @return string initialize form builder javascript code
*/
private function getFBJs()
{

$str = "var {$this->getFBJsVariableElement()} = $('#{$this->getFBId()}');\n"
. "$({$this->getFBJsVariableElement()}).formBuilder({$this->getFBOptions()});\n"
. "var {$this->accessVariableName} = $('#{$this->getFBId()}');\n";

return $str;
}

private function getFBJsVariableElement()
{
return lcfirst(Inflector::id2camel($this->getFBId() . '-' . 'variable'));
}

private function getFBOptions()
{
$this->options = array_merge(
[
'dataType' => $this->dataType,
'formData' => $this->{$this->arrayToTypeFunction}($this->data),
'messages' => $this->messages,
'showActionButtons' => $this->showActionButtons,
],
$this->options
);

return json_encode($this->options);
}

private function arrayJsonEncode($options)
{
return json_encode($options);
}

private function arrayXmlEncode($options, $tag = 'field', $generateHeaderAndFooter = true)
{
$xmlString = '';

if ($generateHeaderAndFooter) {
$xmlString .= "<form-template>"
. "<fields>";
}

if (is_array($options)) {
if (!empty($options)) {
foreach ($options as $key => $option) {
$label = isset($option['label']) ? $option['label'] : '';
$attributes = $this->arrayToAttribute($option);

if (strlen($attributes)) {
$xmlString .= "<$tag"
. $attributes
. ">";
}

if (is_array($option)) {
$xmlString .= $this->arrayXmlEncode($option, Inflector::singularize($key), false);
}

if ('option' == $tag) {
$xmlString .= $label;
}

if (strlen($attributes)) {
$xmlString .= "</$tag>";
}
}
}
} else {
throw new InvalidConfigException('Property $data must be an array.');
}

if ($generateHeaderAndFooter) {
$xmlString .= "</fields>"
. "</form-template>";
}

return $xmlString;
}

private function arrayToAttribute(&$options)
{
$str = '';

foreach ($options as $key => $value) {
if (is_scalar($value)) {
$str .= " $key=" . (is_bool($value) ? "'" . var_export($value, 1) . "'" : var_export($value, 1));
unset($options[$key]);
}
}

return $str;
}
}
25 changes: 25 additions & 0 deletions FormBuilderAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace meysampg\formbuilder;

use yii\web\AssetBundle;
use yii\base\InvalidConfigException;

class FormBuilderAsset extends AssetBundle
{
public $sourcePath = '@vendor/meysampg/yii2-formbuilder/dist';

public function init()
{
// Add css file based on app environment
$this->css[] = YII_DEBUG ? 'form-builder.css' : 'form-builder.min.css';

// Add js file based on app environment
$this->js[] = YII_DEBUG ? 'form-builder.js' : 'form-builder.min.js';
$this->js[] = YII_DEBUG ? 'form-render.js' : 'form-render.min.js';
}

public $depends = [
'yii\jui\JuiAsset',
];
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Meysam GanJi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit 42b160b

Please sign in to comment.