Files
luos/gulliver/system/class.dynaformhandler.php

649 lines
20 KiB
PHP
Raw Normal View History

2010-12-02 23:34:41 +00:00
<?php
/**
2011-01-24 20:33:07 +00:00
* class.dynaformhandler.php
* @package gulliver.system
2011-01-24 20:33:07 +00:00
2010-12-02 23:34:41 +00:00
* ProcessMaker Open Source Edition
2011-01-24 20:33:07 +00:00
* Copyright (C) 2004 - 2008 Colosa Inc.
*
2010-12-02 23:34:41 +00:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
2010-12-02 23:34:41 +00:00
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* For more information, contact Colosa Inc, 2566 Le Jeune Rd.,
2010-12-02 23:34:41 +00:00
* Coral Gables, FL, 33134, USA, or email info@colosa.com.
*
2010-12-02 23:34:41 +00:00
*/
/**
2011-01-25 22:38:27 +00:00
* @author Erik Amaru Ortiz <erik@colosa.com>
* @date Aug 26th, 2009
* @description This class is a Dynaform handler for modify directly into file
2011-01-14 11:51:34 +00:00
* @package gulliver.system
2010-12-02 23:34:41 +00:00
*/
class dynaFormHandler
{
private $xmlfile;
private $dom;
private $root;
/**
* Function constructor
* @access public
* @param string $file
* @return void
*/
function __construct($file=null)
{
if( !isset($file) )
2010-12-02 23:34:41 +00:00
throw new Exception('[Class dynaFormHandler] ERROR: xml file was not set!!');
$this->xmlfile = $file;
$this->load();
}
function load(){
$this->dom = new DOMDocument();
$this->dom->preserveWhiteSpace = false;
$this->dom->formatOutput = true;
if( is_file($this->xmlfile) ) {
if ( @$this->dom->load($this->xmlfile) === true ) {
$this->root = $this->dom->firstChild;
} else
throw new Exception('Error: '.$this->xmlfile.' is a invalid xml file!');
} else {
throw new Exception('[Class dynaFormHandler] ERROR: the ('.$this->xmlfile.') file doesn\'t exits!!');
}
}
function reload(){
$this->dom = NULL;
$this->load();
}
/**
* Function __cloneEmpty
* @access public
* @return void
*/
function __cloneEmpty()
{
2010-12-02 23:34:41 +00:00
$xPath = new DOMXPath($this->dom);
$nodeList = $xPath->query('/dynaForm/*');
foreach ($nodeList as $domElement){
$elements[] = $domElement->nodeName;
}
$this->remove($elements);
//return $cloneObj;
}
/**
* Function toString
* @access public
* @param string $op
* @return void
*/
function toString($op='')
{
switch($op){
case 'html': return htmlentities(file_get_contents($this->xmlfile));
default: return file_get_contents($this->xmlfile);
}
}
2010-12-02 23:34:41 +00:00
/**
* Function getNode
* @access public
* @param string $nodename
* @return void
*/
function getNode($nodename)
{
return $this->root->getElementsByTagName($nodename)->item(0);
}
/**
* Function getNode
* @access public
* @param object $node
* @return object
*/
function setNode($node){
$newnode = $this->root->appendChild($node);
$this->save();
return $newnode;
}
/**
* Add Function
* @param string $name
* @param array $attributes
* @param array $childs
* @param array $childs_childs
* @return void
*/
//attributes (String node-name, Array attributes(atribute-name =>attribute-value, ..., ...), Array childs(child-name=>child-content), Array Child-childs())
function add($name, $attributes, $childs, $childs_childs=null)
{
$newnode = $this->root->appendChild($this->dom->createElement($name));
if( isset($attributes['#cdata']) ) {
$newnode->appendChild($this->dom->createTextNode("\n"));
$newnode->appendChild($this->dom->createCDATASection($attributes['#cdata']));
$newnode->appendChild($this->dom->createTextNode("\n"));
unset($attributes['#cdata']);
}
2010-12-02 23:34:41 +00:00
foreach($attributes as $att_name => $att_value) {
$newnode->setAttribute($att_name, $att_value);
}
if(is_array($childs)){
foreach($childs as $child_name => $child_text) {
$newnode_child = $newnode->appendChild($this->dom->createElement($child_name));
$newnode_child->appendChild($this->dom->createTextNode($child_text));
if($childs_childs != null and is_array($childs_childs)){
foreach($childs_childs as $cc) {
$ccmode = $newnode_child->appendChild($this->dom->createElement($cc['name']));
$ccmode->appendChild($this->dom->createTextNode($cc['value']));
foreach($cc['attributes'] as $cc_att_name => $cc_att_value) {
$ccmode->setAttribute($cc_att_name, $cc_att_value);
}
}
}
}
} else {
$text_node = $childs;
$newnode->appendChild($this->dom->createCDATASection($text_node));
}
$this->save();
}
private function hasChild($p)
{
if ($p->hasChildNodes()) {
foreach ($p->childNodes as $c) {
if ($c->nodeType == XML_ELEMENT_NODE) {
return true;
}
}
}
return false;
}
private function getChildNode($x)
{
$chidNode = array();
foreach ($x->childNodes as $p) {
if ($this->hasChild($p)) {
getChildNode($p);
} else {
if ($p->nodeType == XML_ELEMENT_NODE) {
$chidNode[] = array('node' => $x->nodeName, 'nodeName' => $p->nodeName,
'name' => $p->getAttribute('name'), 'nodeValue' => $p->nodeValue);
}
}
}
return array($x->nodeName => $chidNode);
}
2010-12-02 23:34:41 +00:00
/**
* Function replace
* @access public
* @param string $replaced
* @param string $name
* @param array $attributes
* @param array $childs
* @param array $childs_childs
* @return void
*/
2011-01-25 22:38:27 +00:00
function replace($replaced, $name, $attributes, $childs=null, $childs_childs=null)
2010-12-02 23:34:41 +00:00
{
$chidNode= array();
2010-12-02 23:34:41 +00:00
$element = $this->root->getElementsByTagName($replaced)->item(0);
$this->root->replaceChild($this->dom->createElement($name), $element);
// $newnode = $element = $this->root->getElementsByTagName($name)->item(0);
$newnode = $this->root->getElementsByTagName($name)->item(0);
2011-01-25 22:38:27 +00:00
if( isset($attributes['#text']) ) {
$newnode->appendChild($this->dom->createTextNode($attributes['#text']));
unset($attributes['#text']);
}
if( isset($attributes['#cdata']) ) {
$newnode->appendChild($this->dom->createTextNode("\n"));
2011-01-25 22:38:27 +00:00
$newnode->appendChild($this->dom->createCDATASection($attributes['#cdata']));
$newnode->appendChild($this->dom->createTextNode("\n"));
2011-01-25 22:38:27 +00:00
unset($attributes['#cdata']);
}
2010-12-02 23:34:41 +00:00
foreach($attributes as $att_name => $att_value) {
if (!is_array($att_value)) {
$newnode->setAttribute($att_name, $att_value);
}
2010-12-02 23:34:41 +00:00
}
if(is_array($childs)){
foreach ($element->childNodes as $pNode) {
if( $pNode->nodeName != SYS_LANG && $pNode->nodeName != '#cdata-section' && $pNode->nodeName != '#text') {
$chidNode[] = $this->getChildNode($pNode);
$childs[$pNode->nodeName] = $pNode->firstChild->nodeValue;
}
}
2010-12-02 23:34:41 +00:00
foreach($childs as $child_name => $child_text) {
$newnode->appendChild($this->dom->createTextNode(" "));
2010-12-02 23:34:41 +00:00
$newnode_child = $newnode->appendChild($this->dom->createElement($child_name));
2011-01-25 22:38:27 +00:00
if( is_string($child_text) )
$newnode_child->appendChild($this->dom->createTextNode($child_text));
else if( is_array($child_text) && isset($child_text['cdata']) )
$newnode_child->appendChild($this->dom->createCDATASection($child_text));
if ($child_name == SYS_LANG ) {
if($childs_childs != null and is_array($childs_childs)){
foreach($childs_childs as $cc) {
$ccmode = $newnode_child->appendChild($this->dom->createElement($cc['name']));
$ccmode->appendChild($this->dom->createTextNode($cc['value']));
foreach($cc['attributes'] as $cc_att_name => $cc_att_value) {
$ccmode->setAttribute($cc_att_name, $cc_att_value);
}
}
}
} else {
foreach ($chidNode as $valueNode) {
if ( array_key_exists($child_name, $valueNode) ) {
foreach($valueNode[$child_name] as $valOption) {
$ccmode = $newnode_child->appendChild($this->dom->createElement($valOption['nodeName']));
$ccmode->appendChild($this->dom->createTextNode($valOption['nodeValue']));
$ccmode->setAttribute('name', $valOption['name']);
}
}
2010-12-02 23:34:41 +00:00
}
}
$newnode->appendChild($this->dom->createTextNode("\n"));
2010-12-02 23:34:41 +00:00
}
2011-01-25 22:38:27 +00:00
} else if( isset($childs) ){
2010-12-02 23:34:41 +00:00
$text_node = $childs;
$newnode->appendChild($this->dom->createTextNode($text_node));
}
$this->save();
}
/**
* Function save
* @param string $fname
* @return void
*/
function save($fname=null)
{
if( ! is_writable($this->xmlfile) ) {
throw new Exception("The file {$this->xmlfile} is not writeable!");
}
if( !isset($fname) ){
$this->dom->save($this->xmlfile);
} else {
$this->xmlfile = $fname;
$this->dom->save($this->xmlfile);
}
}
/**
* Function fixXmlFile
* @return void
*/
function fixXmlFile()
{
$newxml = '';
$content = file($this->xmlfile);
foreach($content as $line){
if( trim($line) != ''){
$newxml .= $line;
}
}
file_put_contents($this->xmlfile, $newxml);
}
/**
* Function setHeaderAttribute
* @param string $att_name
* @param string $att_value
* @return void
*/
function setHeaderAttribute($att_name, $att_value)
{
$this->root->setAttribute($att_name, $att_value);
$this->save();
}
function getHeaderAttribute($att_name)
{
return $this->root->getAttribute($att_name);
}
2010-12-02 23:34:41 +00:00
/**
* Function modifyHeaderAttribute
* @param string $att_name
* @param string $att_new_value
* @return void
*/
function modifyHeaderAttribute($att_name, $att_new_value)
{
$this->root->removeAttribute($att_name);
$this->root->setAttribute($att_name, $att_new_value);
$this->save();
}
/**
* Function updateAttribute
* @param string $node_name
* @param string $att_name
* @param string $att_new_value
* @return void
*/
function updateAttribute($node_name, $att_name, $att_new_value)
{
$xpath = new DOMXPath($this->dom);
$nodeList = $xpath->query("/dynaForm/$node_name");
$node = $nodeList->item(0);
$node->removeAttribute($att_name);
$node->setAttribute($att_name, $att_new_value);
$this->save();
}
/**
* Function remove
* @param string $v
* @return void
*/
function remove($v)
{
if(!is_array($v)){
$av[0] = $v;
} else{
$av = $v;
}
foreach($av as $e){
$xnode = $this->root->getElementsByTagName($e)->item(0);
if ( $xnode->nodeType == XML_ELEMENT_NODE ) {
$dropednode = $this->root->removeChild($xnode);
/*evaluation field aditional routines*/
$xpath = new DOMXPath($this->dom);
$nodeList = $xpath->query("/dynaForm/JS_$e");
if($nodeList->length != 0){
$tmp_node = $nodeList->item(0);
$this->root->removeChild($tmp_node);
}
} else {
print("[Class dynaFormHandler] ERROR: The \"$e\" element doesn't exist!<br>");
}
}
$this->save();
}
2010-12-02 23:34:41 +00:00
/**
* Function nodeExists
* @param string $node_name
* @return boolean
*/
function nodeExists($node_name)
{
$xpath = new DOMXPath($this->dom);
$nodeList = $xpath->query("/dynaForm/$node_name");
$node = $nodeList->item(0);
if($nodeList->length != 0){
return true;
} else {
return false;
}
}
//new features
2010-12-02 23:34:41 +00:00
/**
* Function moveUp
* @param string $selected_node
* @return void
*/
function moveUp($selected_node)
{
/*DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
This function inserts a new node right before the reference node. If you plan
to do further modifications on the appended child you must use the returned node. */
$xpath = new DOMXPath($this->dom);
$nodeList = $xpath->query("/dynaForm/*");
$flag = false;
for($i = 0; $i < $nodeList->length; $i++) {
$xnode = $nodeList->item($i);
if($selected_node == $xnode->nodeName){
//if is a first node move it to final with a circular logic
if( $flag === false ){
$removed_node = $this->root->removeChild($xnode);
$this->root->appendChild($removed_node);
break;
} else {
$removed_node = $this->root->removeChild($xnode);
$predecessor_node = $nodeList->item($i-1);
$this->root->insertBefore($removed_node, $predecessor_node);
break;
}
}
$flag = true;
}
$this->save();
}
/**
* Function moveDown
* @param string $selected_node
* @return void
*/
function moveDown($selected_node)
{
/*DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
This function inserts a new node right before the reference node. If you plan
to do further modifications on the appended child you must use the returned node. */
$xpath = new DOMXPath($this->dom);
$nodeList = $xpath->query("/dynaForm/*");
$real_length = $nodeList->length;
for($i = 0; $i < $nodeList->length; $i++) {
$xnode = $nodeList->item($i);
if($selected_node == $xnode->nodeName){
//if is a last node move it to final with a circular logic
if( ($i+1) == $real_length){
if($real_length != 1){
$first_node = $nodeList->item(0);
$removed_node = $this->root->removeChild($xnode);
$this->root->insertBefore($removed_node, $first_node);
}
break;
} else {
if( ($i+3) <= $real_length ){
$removed_node = $this->root->removeChild($xnode);
$predecessor_node = $nodeList->item($i+2);
$this->root->insertBefore($removed_node, $predecessor_node);
break;
} else {
$removed_node = $this->root->removeChild($xnode);
$this->root->appendChild($removed_node);
break;
}
}
}
}
$this->save();
}
2010-12-02 23:34:41 +00:00
/**
* Function getFields
* @param array $aFilter
* @return array
*/
function getFields( $aFilter = Array() )
{
$xpath = new DOMXPath($this->dom);
$nodeList = $xpath->query("/dynaForm/*");
$aList = Array();
for($i = 0; $i < $nodeList->length; $i++) {
$xnode = $nodeList->item($i);
if( is_array($aFilter) && sizeof($aFilter) > 0 ){
if( isset($aFilter['IN']) ){
if( isset($aFilter['NOT_IN']) ){
if( in_array($xnode->nodeName, $aFilter['IN']) && !in_array($xnode->nodeName, $aFilter['NOT_IN']) ){
array_push($aList, $xnode);
}
} else {
if( in_array($xnode->nodeName, $aFilter['IN']) ){
array_push($aList, $xnode);
}
}
} else if( isset($aFilter['NOT_IN']) ){
if( !in_array($xnode->nodeName, $aFilter['NOT_IN']) ){
array_push($aList, $xnode);
}
} else {
array_push($aList, $xnode);
}
} else {
array_push($aList, $xnode);
}
}
return $aList;
}
2010-12-02 23:34:41 +00:00
/**
* Function getFieldNames
* @param array $aFilter
* @return array
*/
function getFieldNames( $aFilter = Array() )
{
$aList = $this->getFields($aFilter);
$aFieldNames = Array();
foreach( $aList as $item ){
array_push($aFieldNames, $item->nodeName);
}
return $aFieldNames;
}
//
2010-12-02 23:34:41 +00:00
function addChilds($name, $childs, $childs_childs=null)
{
//
$xpath = new DOMXPath($this->dom);
$nodeList = @$xpath->query("/dynaForm/$name");
if( ! $nodeList ){
throw new Exception("Error trying get the field dynaform $name, maybe it doesn't exist in {$this->xmlfile}");
}
2010-12-02 23:34:41 +00:00
if( $nodeList->length == 0 ) {
$element = $this->root->appendChild($this->dom->createElement($name));
} else
$element = $this->root->getElementsByTagName($name)->item(0);
2010-12-02 23:34:41 +00:00
if( is_array($childs) ) {
foreach( $childs as $child_name => $child_text ) {
2010-12-02 23:34:41 +00:00
$nodeList = $xpath->query("/dynaForm/$name/$child_name");
2010-12-02 23:34:41 +00:00
if( $nodeList->length == 0 ){ //the node doesn't exist
//$newnode_child
2010-12-02 23:34:41 +00:00
$childNode = $element->appendChild($this->dom->createElement($child_name));
$childNode->appendChild($this->dom->createCDATASection($child_text));
} else { // the node already exists
//update its value
$childNode = $element->getElementsByTagName($child_name)->item(0);
2010-12-02 23:34:41 +00:00
//
if($child_text !== NULL){
$xnode = $this->dom->createElement($childNode->nodeName);
$xnode->appendChild($this->dom->createCDATASection($child_text));
$element->replaceChild($xnode, $childNode);
2010-12-02 23:34:41 +00:00
$childNode = $element->getElementsByTagName($child_name)->item(0);
}
}
2010-12-02 23:34:41 +00:00
if($childs_childs != null and is_array($childs_childs)){
foreach($childs_childs as $cc) {
$ccnode = $childNode->appendChild($this->dom->createElement($cc['name']));
$ccnode->appendChild($this->dom->createCDATASection($cc['value']));
foreach($cc['attributes'] as $cc_att_name => $cc_att_value) {
$ccnode->setAttribute($cc_att_name, $cc_att_value);
}
}
}
}
} else {
$text_node = $childs;
$newnode->appendChild($this->dom->createTextNode($text_node));
}
$this->save();
}
function addOrUpdateChild($xnode, $childName, $childValue, $childAttributes){
2010-12-02 23:34:41 +00:00
$newNode = $this->dom->createElement($childName);
$newNode->appendChild($this->dom->createCDATASection($childValue));
2010-12-02 23:34:41 +00:00
foreach($childAttributes as $attName => $attValue) {
$newNode->setAttribute($attName, $attValue);
}
2010-12-02 23:34:41 +00:00
if( $xnode->hasChildNodes() ) {
foreach($xnode->childNodes as $cnode) {
if( $cnode->nodeName == $childName ) {
$xnode->replaceChild($newNode, $cnode);
break;
}
}
} else
2010-12-02 23:34:41 +00:00
$xnode->appendChild($newNode);
}
function getArray($node, $attributes = null)
{
$array = false;
$array['__nodeName__'] = $node->nodeName;
$text = simplexml_import_dom($node);
$array['__nodeText__'] = trim((string) $text);
if ($node->hasAttributes()) {
if( isset($attributes) ) {
foreach ($attributes as $attr) {
if( $node->hasAttribute($attr) )
$array[$attr] = $node->getAttribute($attr);
}
} else {
foreach ($node->attributes as $attr) {
$array[$attr->nodeName] = $attr->nodeValue;
}
}
}
if ($node->hasChildNodes()) {
if ($node->childNodes->length == 0)
$return;
else {
foreach ($node->childNodes as $childNode) {
$childNode->normalize();
//if ($childNode->nodeType == XML_TEXT_NODE || $childNode->nodeType == XML_CDATA_SECTION_NODE) {
if ($childNode->nodeType == XML_ELEMENT_NODE) {
$array[$childNode->nodeName][] = $this->getArray($childNode);
} else if ($childNode->nodeType == XML_TEXT_NODE || $childNode->nodeType == XML_CDATA_SECTION_NODE) {
//$array[$childNode->nodeName] = $childNode->textContent;
$text = simplexml_import_dom($childNode->parentNode);
$array['__nodeText__'] = trim((string) $text);
}
}
}
}
return $array;
}
2010-12-02 23:34:41 +00:00
}