Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
seticzech committed Aug 6, 2016
0 parents commit 631fc3d
Show file tree
Hide file tree
Showing 16 changed files with 1,324 additions and 0 deletions.
51 changes: 51 additions & 0 deletions Debug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* --------------------------------
* PEL - Phalcon Extensions Library
* --------------------------------
*
* This code is distributed under New BSD license.
* License is bundled with this package in file LICENSE.txt.
*
* @author Jiri Pazdernik <[email protected]>
*/

namespace Pel;

/**
* Class for debug appliaction and symbols
*
* @author Jiri Pazdernik <[email protected]>
*/
class Debug
{

/**
* Dump specific variable
*
* @param mixed $var variable to dump
* @param string $label (OPTIONAL) text of user defined label
* @param boolean $echo (OPTIONAL) FALSE for suppress print of the dump
* @param boolean $wrapPre (OPTIONAL) FALSE for not to wrap content with <pre> tag
* @return string information about dumped variable
*/
public static function dump($var, $label = null, $echo = true, $wrapPre = true)
{
ob_start();
var_dump($var);
$r = ob_get_clean();

$result = $label . " " . $r;
if ($wrapPre) {
$result = "<pre>" . $result . "</pre>";
}

if ($echo) {
echo $result;
}

return $result;
}

}
8 changes: 8 additions & 0 deletions Forms/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Pel\Forms;

class Exception extends \Phalcon\Exception
{

}
241 changes: 241 additions & 0 deletions Forms/Form.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
<?php
/**
* --------------------------------
* PEL - Phalcon Extensions Library
* --------------------------------
*
* This code is distributed under New BSD license.
* License is bundled with this package in file LICENSE.txt.
*
* @author Jiri Pazdernik <[email protected]>
*/

namespace Pel\Forms;

/**
* Class for working with forms
*
* @author Jiri Pazdernik <[email protected]>
*/
class Form extends \Phalcon\Forms\Form
{

/**
* Groups of elements
*
* @var array
*/
protected $_groups = array();

/**
* JavaScript lines
*
* @var array
*/
protected $_javaScript = array();

/**
* Constructor
*
* @param object $entity
* @param array $userOptions
*/
public function __construct($entity = null, $userOptions = null)
{
parent::__construct($entity, $userOptions);

$this->initialize();

$validation = new \Phalcon\Validation();
$this->setValidation($validation);
}

/**
* Add group
*
* @param string $name group name
* @param string $caption (OPTIONAL) group caption
* @param array $elements (OPTIONAL) elements to add
* @return \Pel\Forms\Group
*/
public function createGroup($name, $caption = null, array $elements = array())
{
if (isset($this->_groups[$name])) {
throw new Exception("Error when creating form group. Group '{$name}' already exists in form", $code, $previous);
}

$group = new Group($name, $this, $caption, $elements);
$this->_groups[$name] = $group;

return $group;
}

public function addJS($line)
{
$this->_javaScript[] = $line;

return $this;
}

/**
* Replace bind() for use with multidim arrays
*
* @see \Phalcon\Forms\Form::bind()
*/
public function bind(array $data, $entity, array $whitelist = null)
{
parent::bind($data, $entity, $whitelist);

foreach ($data as $key => $value) {
if (! empty($whitelist) && ! isset($whitelist[$key])) {
continue;
}

$this->_setDefault($key, $value);
}
}

/**
* Get group
*
* @param string $name group name
* @return \Pel\Forms\Group|null
*/
public function getGroup($name)
{
return isset($this->_groups[$name]) ? $this->_groups[$name] : null;
}

/**
* Get all groups in the form
*
* @return array
*/
public function getGroups()
{
return $this->_groups;
}

/**
* Get all groups names
*
* @return array
*/
public function getGroupsNames()
{
return array_keys($this->_groups);
}

/**
* Get all values
*
* @param array $groups (OPTIONAL) get values only for specified groups
* @return array
*/
public function getValues($groups = array())
{
$result = array();

if (! empty($groups)) {
foreach ($groups as $name) {
$result = array_merge($result, $this->getGroup($name)->getValues());
}
} else {
foreach ($this as $element) {
$result[$element->getName()] = $element->getValue();
}
}

return $result;
}

/**
* Check if form contains specified group
*
* @param string $name group name
* @return boolean
*/
public function hasGroup($name)
{
return isset($this->_groups[$name]);
}

/**
* Initialization
*/
public function initialize()
{

}

/**
* Remove group
*
* @param string $name group name
* @throws \Phalcon\Forms\Exception
* @return \Pel\Forms\Form
*/
public function removeGroup($name)
{
if (! array_key_exists($name, $this->_groups)) {
throw new \Phalcon\Forms\Exception("Form does not contain group '{$name}'", 1);
}

unset($this->_groups[$name]);

return $this;
}

/**
* Remove element from the form
*
* @param string $name element name
* @return boolean
*/
public function remove($name)
{
if (! empty($this->_groups)) {
foreach ($this->_groups as $group) {
$group->remove($name);
}
}

return parent::remove($name);
}

public function renderJS($lineBreaks = true)
{
$xhtml = "";
$break = ($lineBreaks) ? "\n" : "";

if (! empty($this->_javaScript)) {
$xhtml .= "<script type=\"text/javascript\">" . $break;
$xhtml .= implode($break, $this->_javaScript) . $break;
$xhtml .= "</script>" . $break;
}

return $xhtml;

}

protected function _setDefault($key, $value)
{
if (is_array($value)) {
foreach ($value as $subKey => $subVal) {
$this->_setDefault($key . "[$subKey]", $subVal);
}
} else {
if ($this->has($key)) {
$this->get($key)->setDefault($value);
}
}
}

public function setDefaults($values)
{
foreach ($values as $key => $value) {
$this->_setDefault($key, $value);
}
}

}
Loading

0 comments on commit 631fc3d

Please sign in to comment.