sugarcrm_dev /include/SugarFields/Parsers/MetaParser.php

Language PHP Lines 809
MD5 Hash 4ff1d9bb7b178e4c2e525a9aef53faaf
Repository https://github.com/mikmagic/sugarcrm_dev.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*********************************************************************************
 * SugarCRM Community Edition is a customer relationship management program developed by
 * SugarCRM, Inc. Copyright (C) 2004-2011 SugarCRM Inc.
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Affero General Public License version 3 as published by the
 * Free Software Foundation with the addition of the following permission added
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
 * 
 * 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.
 * 
 * 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 or write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 * 
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
 * 
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU Affero General Public License version 3.
 * 
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "Powered by
 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
 * technical reasons, the Appropriate Legal Notices must display the words
 * "Powered by SugarCRM".
 ********************************************************************************/


/**
 * MetaParser.php
 * 
 * This is a utility base file to parse HTML
 * @author Collin Lee
 */
class MetaParser {

var $mPHPFile;
var $mView;
var $mModule;
var $mCustomPanels;

function MetaParser() {
   	
}

function parse() {
   return "NOT AVAILABLE";	
}	

/**
 * getFormContents
 * Parses for contents enclosed within <form>...</form> tags
 */
function getFormContents($contents, $all = true) {
   if($all) { 
      preg_match_all("'(<form[^>]*?>)(.*?)(</form[^>]*?>)'si", $contents, $matches);	
      return $matches;
   } 
   	
   preg_match("'(<form[^>]*?>)(.*?)(</form[^>]*?>)'si", $contents, $matches);
   return $this->convertToTagElement($matches);
   //return $matches;
}


/**
 * getFormElements
 * Parses for input, select, textarea types from string content
 * @param $contents The String contents to parse
 * @return $matches Array of matches of PREG_SET_ORDER 
 */
function getFormElements($contents) {
   preg_match_all("'(<[ ]*?)(textarea|input|select)([^>]*?)(>)'si", $contents, $matches, PREG_PATTERN_ORDER);
   $elems = array();
   foreach($matches[3] as $match) {
   	  $elems[] = $match;
   }
   return $elems;
}


/**
 * getFormElementsNames
 * Parses for the name values of input, select, textarea types from string content
 * @param $contents The String contents to parse
 * @return $matches Array of name/value pairs
 */
function getFormElementsNames($contents) {
   preg_match_all("'(<[ ]*?)(textarea|input|select)[^>]*?name=[\'\"]([^\'\"]*?)(\[\])?(_basic)?[\'\"]([^>]*?>)'si", $contents, $matches, PREG_PATTERN_ORDER);
   return !empty($matches[3]) ? $matches[3] : null;
}


/**
 * getTagAttribute
 * Returns the name/value of a tag attribute where name is set to $name
 * @param $name The name of the attribute
 * @param $contents The contents to parse
 * @param $filter Option regular expression to filter value
 * @return Array of name/value for matching attribute
 */
function getTagAttribute($name, $contents, $filter = '') {
   //$exp = "'".$name."[ ]*?=[ ]*?[\'\"]([a-zA-Z0-9\_\[\]]*)[\'\"]'si";
   
   $exp = "'".$name."[\s]*?=[\s]*?[\'\"]([^\'^\"]*?)[\'\"]'si";
   preg_match_all($exp, $contents, $matches, PREG_SET_ORDER);
   if(empty($filter)) {
   	  return !empty($matches[0][1]) ? $matches[0][1] : '';
   }
   
   $filtered = array();
   foreach($matches as $tag) {
   	  if(preg_match($filter, $tag[1])) {	
   	  	 $filtered[] = $tag;
   	  }
   }
   return $filtered;
}

/**
 * getTables
 * Returns an Array of the tables found in the file.  If $tableClass parameter
 * is supplied, it'll return only those tables that have a matching class attribute 
 * equal to $tableClass
 * @param $tableClass Optional table class parameter value
 * @return Array of table elements found
 */
function getTables($tableClass = null, $contents) {
   preg_match_all("'(<table[^>]*?>)(.*?)(</table[^>]*?>)'si", $contents, $matches, PREG_SET_ORDER);
   if($tableClass == null) {
   	  return $matches;
   }
   
   $tables = array();
   foreach($matches as $key => $table) {
   	  if(strpos($table[1], $tableClass) > 0) {
   	  	 $tables[] = $table;
   	  }
   }
   return $this->convertToTagElement($tables);
}

/**
 * getElementsByType
 * 
 * Returns an Array of all elements matching type.  It will match
 * for the outermost tags.  For example given contents:
 * "<tr><td>Text <table><tr><td>a</td></tr></table></td></tr>"
 * and method call getElementsByType("<td>", $contents) returns
 * "<td>Text <table><tr><td>a</td></tr></table></td>"
 * 
 * @param $type The type of element to parse out and return 
 * @return a tag element format Array
 */
function getElementsByType($type, $contents) {
   $x = strlen($contents);
   $mark = 0;
   $count = 0;
   $stag1 = "<" . trim($type, " <>") . '>';
   $stag2 = "<" . trim($type, " <>") . ' ';
   $etag = "</".$type.">";
   $sincrement = strlen($stag1);
   $eincrement = strlen($etag);
   $sarr = array();
   $values = array();

   while($count < $x) {
   	     $stok = substr($contents, $count, $sincrement);
   	     $etok = substr($contents, $count, $eincrement);
   	     if($stok == $stag1 || $stok == $stag2) {
   	     	//Reset mark;   	         
   	        if(count($sarr) == 0) {
   	           $mark = $count;
   	        }
            $sarr[] = $count;
   	        
   	     } else if($etok == $etag) {
   	        array_shift($sarr);
   	        if(count($sarr) == 0) {
   	           $val = substr($contents, $mark, ($count - $mark) + $eincrement);
   	           $values[] = $val;
   	           $mark = $count;
   	        }	
   	     }
   	     $count++;
   }
   
   $count = 0;
   return $values;
} 



/**
 * getElementValue
 * 
 */
function getElementValue($type, $contents, $filter = "(.*?)") {
   $exp = "'<".$type."[^>]*?>".$filter."</".$type."[^>]*?>'si";
   preg_match($exp, $contents, $matches);
   return isset($matches[1]) ? $matches[1] : '';	
}


function stripComments($contents) {
   return preg_replace("'(<!--.*?-->)'si", "", $contents);
}

/**
 * stripFlavorTags
 * This method accepts the file contents and uses the $GLOBALS['sugar_flavor'] value
 * to remove the flavor tags in the file contents if present.  If $GLOBALS['sugar_flavor']
 * is not set, it defaults to PRO flavor
 * @param $contents The file contents as a String value
 * @param $result The file contents with non-matching flavor tags and their nested comments removed
 */
function stripFlavorTags($contents) {
   $flavor = isset($GLOBALS['sugar_flavor']) ? $GLOBALS['sugar_flavor'] : 'PRO';
   $isPro = ($flavor == 'ENT' || $flavor == 'PRO') ? true : false;
   if($isPro) {
   	 $contents = preg_replace('/<!-- BEGIN: open_source -->.*?<!-- END: open_source -->/', '', $contents);
   } else {
   	 $contents = preg_replace('/<!-- BEGIN: pro -->.*?<!-- END: pro -->/', '', $contents);
   }
   return $contents;   	
}

/**
 * getMaxColumns
 * Returns the highest number of <td>...</td> blocks within a <tr>...</tr> block.
 * @param $contents The table contents to parse
 * @param $filter Optional filter to parse for an attribute within the td block.
 * @return The maximum column count
 */ 
function getMaxColumns($contents, $filter) {
   preg_match_all("'(<tr[^>]*?>)(.*?)(</tr[^>]*?>)'si", $contents, $matches, PREG_SET_ORDER);
   $max = 0;
   foreach($matches as $tableRows) {
           $count = substr_count($tableRows[2], $filter);
           if($count > $max) {
           	  $max = $count;
           }
   }
   
   return $max;
}

function convertToTagElement($matches) {
   
   $elements = array();
  
   foreach($matches as $data) {
   	   // We need 4 because the 1,2,3 indexes make up start,body,end 
	   if(count($data) == 4) {
	   	  $element = array();
	   	  $element['start'] = $data[1];
	   	  $element['body'] = $data[2];
	   	  $element['end'] = $data[3];
	   	  $elements[] = $element;
	   }
   }

   return empty($elements) ? $matches : $elements;	
}	

/*
 * trimHTML
 * This function removes the \r (return), \n (newline) and \t (tab) markup from string
 */
function trimHTML($contents) {
   $contents = str_replace(array("\r"), array(""), $contents);
   $contents = str_replace(array("\n"), array(""), $contents);
   $contents = str_replace(array("\t"), array(""), $contents);
   return $contents;
}


/**
 * getJavascript
 * 
 * This method parses the given $contents String and grabs all <script...>...</script> blocks.
 * The method also converts values enclosed within "{...}" blocks that may need to be converted
 * to Smarty syntax.
 * 
 * @param $contents The HTML String contents to parse
 * 
 * @return $javascript The formatted script blocks or null if none found
 */
function getJavascript($contents, $addLiterals = true) {

$javascript = null;

//Check if there are Javascript blocks of code to process
preg_match_all("'(<script[^>]*?>)(.*?)(</script[^>]*?>)'si", $contents, $matches, PREG_PATTERN_ORDER);
if(empty($matches)) {
   return $javascript;	
}

foreach($matches[0] as $scriptBlock) {
	    $javascript .= "\n" . $scriptBlock;
} //foreach

$javascript = substr($javascript, 1);

//Remove stuff first
//1) Calendar.setup {..} blocks
$javascript = preg_replace('/Calendar.setup[\s]*[\(][^\)]*?[\)][\s]*;/si', '', $javascript);

//Find all blocks that may need to be replaced with Smarty syntax
preg_match_all("'([\{])([a-zA-Z0-9_]*?)([\}])'si", $javascript, $matches, PREG_PATTERN_ORDER);
if(!empty($matches)) {
	$replace = array();
	
	foreach($matches[0] as $xTemplateCode) {
		    if(!isset($replace[$xTemplateCode])) {
		       $replace[$xTemplateCode] = str_replace("{", "{\$", $xTemplateCode);
		    } //if
	} //foreach
	
	$javascript = str_replace(array_keys($replace), array_values($replace), $javascript);	
} //if

if(!$addLiterals) {
   return $javascript;
}

return $this->parseDelimiters($javascript);
	
}

function parseDelimiters($javascript) {
	$newJavascript = '';
	$scriptLength = strlen($javascript);
	$count = 0;
	$inSmartyVariable = false;
	
	while($count < $scriptLength) {
	
	      if($inSmartyVariable) {
	      	 $start = $count;
	      	 $numOfChars = 1;
	      	 while(isset($javascript[$count]) && $javascript[$count] != '}') {
	      	 	   $count++;
	      	 	   $numOfChars++;
	      	 }
	      	 
	      	 $newJavascript .= substr($javascript, $start, $numOfChars);
	      	 $inSmartyVariable = false;
	      	
	      } else {
	      
			  $char = $javascript[$count];
			  $nextChar = ($count + 1 >= $scriptLength) ? '' : $javascript[$count + 1];
			 
			  if($char == "{" && $nextChar == "$") {
			  	 $inSmartyVariable = true;
			  	 $newJavascript .= $javascript[$count];
			  } else if($char == "{") {
			  	 $newJavascript .=  " {ldelim} ";
			  } else if($char == "}") {
			  	 $newJavascript .= " {rdelim} ";
			  } else {
			     $newJavascript .= $javascript[$count];
			  }
	      }
		  $count++;
	} //while
	
	return $newJavascript;	
}

/**
 * findAssignedVariableName
 * This method provides additional support in attempting to parse the  module's corresponding
 * PHP file for either the EditView or DetailView.  In the event that the subclasses cannot
 * find a matching vardefs.php entry in the HTML file, this method can be called to parse the
 * PHP file to see if the assignment was made using the bean's variable.  If so, we return
 * this variable name.
 * 
 * @param $name The tag name found in the HTML file for which we want to search
 * @param $filePath The full file path for the HTML file
 * @return The variable name found in PHP file, original $name variable if not found
 */
function findAssignedVariableName($name, $filePath) {
	
	if($this->mPHPFile == "INVALID") {
	   return $name;	
	}
	
	if(!isset($this->mPHPFile)) {
	   if(preg_match('/(.*?)(DetailView).html$/', $filePath, $matches)) {
	   	 $dir = $matches[1];
	   } else if(preg_match('/(.*?)(EditView).html$/', $filePath, $matches)) {
	   	 $dir = $matches[1];
	   }

	   if(!isset($dir) || !is_dir($dir)) {
	      $this->mPHPFile = "INVALID";	
	      return $name;
	   }

       $filesInDir = $this->dirList($dir);
       $phpFile = $matches[2].'.*?[\.]php';
       foreach($filesInDir as $file) {
       	  if(preg_match("/$phpFile/", $file)) {
       	  	 $this->mPHPFile = $matches[1] . $file;
       	  	 break;
       	  }
       }
        
       if(!isset($this->mPHPFile) || !file_exists($this->mPHPFile)) {
       	  $this->mPHPFile = "INVALID";
       	  return $name;
       }
	}

	$phpContents = file_get_contents($this->mPHPFile);
	$uname = strtoupper($name);
	if(preg_match("/xtpl->assign[\(][\"\']".$uname."[\"\'][\s]*?,[\s]*?[\$]focus->(.*?)[\)]/si", $phpContents, $matches)) {
	   return $matches[1];
	}
	return $name;
}


/**
 * dirList
 * Utility method to list all the files in a given directory.
 * 
 * @param $directory The directory to scan
 * @return $results The files in the directory that were found
 */
function dirList ($directory) {

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // keep going until all files in directory have been read
    while ($file = readdir($handler)) {
        // if $file isn't this directory or its parent, 
        // add it to the results array
        if ($file != '.' && $file != '..')
            $results[] = $file;
    }

    // tidy up: close the handler
    closedir($handler);
    return $results;
}


/**
 * isCustomField
 * This method checks the mixed variable $elementNames to see if it is a custom field.  A custom
 * field is simply defined as a field that ends with "_c".  If $elementNames is an Array
 * any matching custom field value will result in a true evaluation
 * @param $elementNames Array or String value of form element name(s).
 * @return String name of custom field; null if none found
 */
function getCustomField($elementNames) {
   
   if(!isset($elementNames) || (!is_string($elementNames) && !is_array($elementNames))) {
   	  return null;
   }
   
   if(is_string($elementNames)) {
   	  if(preg_match('/(.+_c)(_basic)?(\[\])?$/', $elementNames, $matches)) {
   	  	 return count($matches) == 1 ? $matches[0] : $matches[1];
   	  }
   	  return null;
   }
   
   foreach($elementNames as $name) {
   	  if(preg_match('/(.+_c)(_basic)?(\[\])?$/', $name, $matches)) {
   	  	 return count($matches) == 1 ? $matches[0] : $matches[1];
   	  }
   }
   
   return null;
}

function applyPreRules($moduleDir, $panels) {
   if(file_exists("include/SugarFields/Parsers/Rules/".$moduleDir."ParseRule.php")) {
	  require_once("include/SugarFields/Parsers/Rules/".$moduleDir."ParseRule.php");
	  $class = $moduleDir."ParseRule";
	  $parseRule = new $class();	
	  $panels = $parseRule->preParse($panels, $this->mView);
   } 
   return $panels;	
}

function applyRules($moduleDir, $panels) {
   return $this->applyPostRules($moduleDir, $panels);
}

function applyPostRules($moduleDir, $panels) {
   //Run module specific rules
   if(file_exists("include/SugarFields/Parsers/Rules/".$moduleDir."ParseRule.php")) {
	  require_once("include/SugarFields/Parsers/Rules/".$moduleDir."ParseRule.php");
	  $class = $moduleDir."ParseRule";
	  $parseRule = new $class();	
	  $panels = $parseRule->parsePanels($panels, $this->mView);
   } 

   //Now run defined rules
   require_once("include/SugarFields/Parsers/Rules/ParseRules.php");
   $rules = ParseRules::getRules();

   foreach($rules as $rule) {
   	  if(!file_exists($rule['file'])) {
   	  	 $GLOBALS['log']->error("Cannot run rule for " . $rule['file']);
   	  	 continue;
   	  } //if
   	  require_once($rule['file']);
   	  $runRule = new $rule['class'];
   	  $panels = $runRule->parsePanels($panels, $this->mView);

   } //foreach
   
   return $panels;	
}

function createFileContents($moduleDir, $panels, $templateMeta=array(), $htmlFilePath) {

$header = "<?php\n\n";

if(empty($templateMeta)) {
$header .= "\$viewdefs['$moduleDir']['$this->mView'] = array(
    'templateMeta' => array('maxColumns' => '2', 
                            'widths' => array(
                                            array('label' => '10', 'field' => '30'), 
                                            array('label' => '10', 'field' => '30')
                                            ),
    ),";
} else {
$header .= "\$viewdefs['$moduleDir']['$this->mView'] = array(
    'templateMeta' =>" . var_export($templateMeta, true) . ",";	
} 

//Replace all the @sq (single quote tags that may have been inserted)
$header = preg_replace('/\@sq/', "'", $header);

/*
$contents = file_get_contents($htmlFilePath);

$javascript = $this->getJavascript($contents, true);

if(!empty($javascript)) {
	$javascript = str_replace("'", "\\'", $javascript);
	$header .= "\n 'javascript' => '" . $javascript . "',\n";	
} //if
*/
$header .= "\n 'panels' =>";   

$footer = "
\n
);
?>";

   $metadata = ''; 
   $body = var_export($panels, true);
   $metadata = $header . $body . $footer;
   $metadata = preg_replace('/(\d+)[\s]=>[\s]?/',"",$metadata);
   return $metadata;
	
}


/**
 * mergePanels
 * This function merges the $panels Array against the $masterCopy's meta data definition
 * @param $panels meta data Array to merge
 * @param $moduleDir Directory name of the module
 * @param $masterCopy file path to the meta data master copy
 * @return Array of merged $panel definition
 */
function mergePanels($panels, $vardefs, $moduleDir, $masterCopy) {	
   require($masterCopy);
   $masterpanels = $viewdefs[$moduleDir][$this->mView]['panels'];
   $hasMultiplePanels = $this->hasMultiplePanels($masterpanels);

   if(!$hasMultiplePanels) {
   	    $keys = array_keys($viewdefs[$moduleDir][$this->mView]['panels']);
        if(!empty($keys) && count($keys) == 1) {
        	if(strtolower($keys[0]) == 'default') {
        	   $masterpanels = array('default'=>$viewdefs[$moduleDir][$this->mView]['panels'][$keys[0]]);
        	} else {
        	   $firstPanel = array_values($viewdefs[$moduleDir][$this->mView]['panels']);
	           $masterpanels = array('default'=> $firstPanel[0]);
        	}
        } else {
        	$masterpanels = array('default'=>$viewdefs[$moduleDir][$this->mView]['panels']);
        }
   }
   foreach($masterpanels as $name=>$masterpanel) {
   	       if(isset($panels[$name])) {
	   	       	  // Get all the names in the panel
	   	       	  $existingElements = array();
	   	       	  $existingLocation = array();
	   	       	  
	   	       	  foreach($panels[$name] as $rowKey=>$row) {
	   	       	  	 foreach($row as $colKey=>$column) {
	   	       	  	 	if(is_array($column) && !empty($column['name'])) {
	   	       	  	 	   $existingElements[$column['name']] = $column['name'];
	   	       	  	 	   $existingLocation[$column['name']] = array("panel"=>$name, "row"=>$rowKey, "col"=>$colKey);	
	   	       	  	 	} else if(!is_array($column) && !empty($column)) {
	   	       	  	 	   $existingElements[$column] = $column;
	   	       	  	 	   $existingLocation[$column] = array("panel"=>$name, "row"=>$rowKey, "col"=>$colKey);	
	   	       	  	 	}
	   	       	  	 } //foreach
	   	       	  } //foreach
	   	       	  
	   	       	  // Now check against the $masterCopy
	   	       	  foreach($masterpanel as $rowKey=>$row) {
	   	       	  	
	   	       	  	 $addRow = array();
	
	   	       	  	 foreach($row as $colKey=>$column) {
	   	       	  	 	if(is_array($column) && isset($column['name'])) {
	   	       	  	 	   $id = $column['name'];	
	   	       	  	 	} else if(!is_array($column) && !empty($column)) {
	   	       	  	 	   $id = $column;
	   	       	  	 	} else {
	   	       	  	 	   continue;	
	   	       	  	 	}
	   	       	  	 	if(empty($existingElements[$id])) {
	   	       	  	 	   //Only add if 
	   	       	  	 	   // 1) if it is a required field (as defined in metadata)
	   	       	  	 	   // 2) or if it has a customLabel and customCode (a very deep customization)
	   	       	  	 	   if((is_array($column) && !empty($column['displayParams']['required'])) ||
	   	       	  	 	      (is_array($column) && !empty($column['customCode']) && !empty($column['customLabel']))) {
	   	       	  	 	   	  $addRow[] = $column;
	   	       	  	 	   }
	   	       	  	 	} else {
	   	       	  	 	   //Use definition from master copy instead
	   	       	  	 	   $panels[$existingLocation[$id]['panel']][$existingLocation[$id]['row']][$existingLocation[$id]['col']] = $column;
	   	       	  	 	} 	  	 	
	   	       	  	 } //foreach
	   	       	  	 
	   	       	  	 // Add it to the $panels 
	   	       	  	 if(!empty($addRow)) {
	   	       	  	 	$panels[$name][] = $addRow;
	   	       	  	 }
	   	       	  } //foreach
	   	       	  
   	       } else {
	   	       	  $panels[$name] = $masterpanel;
   	       }
   } //foreach
   
   // We're not done yet... go through the $panels Array now and try to remove duplicate
   // or empty panels
   foreach($panels as $name=>$panel) {
   	   if(count($panel) == 0 || !isset($masterpanels[$name])) {
   	   	  unset($panels[$name]);
   	   }
   } //foreach

   return $panels;
}

/**
 * mergeTemplateMeta
 * This function merges the $templateMeta Array against the $masterCopy's meta data definition
 * @param $templateMeta meta data Array to merge
 * @param $moduleDir Directory name of the module
 * @param $masterCopy file path to the meta data master copy
 * @return Array of merged $templateMeta definition
 */
function mergeTemplateMeta($templateMeta, $moduleDir, $masterCopy) {
   require($masterCopy);
   $masterTemplateMeta = $viewdefs[$moduleDir][$this->mView]['templateMeta'];

   if(isset($masterTemplateMeta['javascript'])) {
   	  //Insert the getJSPath code back into src value
   	  $masterTemplateMeta['javascript'] = preg_replace('/src\s*=\s*[\'\"].*?(modules\/|include\/)([^\.]*?\.js)([^\'\"]*?)[\'\"]/i', 'src="@sq . getJSPath(@sq${1}${2}@sq) . @sq"', $masterTemplateMeta['javascript']);
   }
   
   return $masterTemplateMeta;	
}

function hasRequiredSpanLabel($html) {
   if(empty($html)) {
   	  return false;
   }
   
   return preg_match('/\<(div|span) class=(\")?required(\")?\s?>\*<\/(div|span)>/si', $html);
}

function hasMultiplePanels($panels) {

   if(!isset($panels) || empty($panels) || !is_array($panels)) {
   	  return false;
   }
   
   if(is_array($panels) && (count($panels) == 0 || count($panels) == 1)) {
   	  return false;
   }	
   
   foreach($panels as $panel) {
   	  if(!empty($panel) && !is_array($panel)) {
   	  	 return false;
   	  } else {
   	  	 foreach($panel as $row) {
   	  	    if(!empty($row) && !is_array($row)) {
   	  	       return false;		
   	  	    } //if
   	  	 } //foreach
   	  } //if-else
   } //foreach
   
   return true;
}

function getRelateFieldName($mixed='') {
   if(!is_array($mixed)) {
   	  return '';
   } else if(count($mixed) == 2){
      $id = '';
   	  $name = '';
   	  foreach($mixed as $el) {
   	  	 if(preg_match('/_id$/', $el)) {
   	  	    $id = $el;
   	  	 } else if(preg_match('/_name$/', $el)) {
   	  	    $name = $el;
   	  	 }
   	  }
   	  return (!empty($id) && !empty($name)) ? $name : '';
   }
   return '';
}

function getCustomPanels() {
   return $this->mCustomPanels;	
}

/**
 * fixTablesWithMissingTr
 * This is a very crude function to fix instances where files declared a table as
 * <table...><td> instead of <table...><tr><td>.  Without this helper function, the
 * parsing could messed up.
 * 
 */
function fixTablesWithMissingTr($tableContents) {
   if(preg_match('/(<table[^>]*?[\/]?>\s*?<td)/i', $tableContents, $matches)) {
   	  return preg_replace('/(<table[^>]*?[\/]?>\s*?<td)/i', '<table><tr><td', $tableContents);
   }
   return $tableContents;
}

/**
 * fixRowsWithMissingTr
 * This is a very crude function to fix instances where files have an </tr> tag immediately followed by a <td> tag
 */
function fixRowsWithMissingTr($tableContents) {
   if(preg_match('/(<\/tr[^>]*?[\/]?>\s*?<td)/i', $tableContents, $matches)) {
   	  return preg_replace('/(<\/tr[^>]*?[\/]?>\s*?<td)/i', '</tr><tr><td', $tableContents);
   }
   return $tableContents;
}

/**
 * fixDuplicateTrTags
 * This is a very crude function to fix instances where files have two consecutive <tr> tags
 */
function fixDuplicateTrTags($tableContents) {
   if(preg_match('/(<tr[^>]*?[\/]?>\s*?<tr)/i', $tableContents, $matches)) {
   	  return preg_replace('/(<tr[^>]*?[\/]?>\s*?<tr)/i', '<tr', $tableContents);
   }
   return $tableContents;	
}

/**
 * findSingleVardefElement
 * Scans array of form elements to see if just one is a vardef element and, if so,
 * return that vardef name
 */
function findSingleVardefElement($formElements=array(), $vardefs=array()) {
   if(empty($formElements) || !is_array($formElements)) {
   	  return '';
   }
   
   $found = array();
   foreach($formElements as $el) {
   	   if(isset($vardefs[$el])) {
   	   	  $found[] = $el;
   	   }
   }
   
   return count($found) == 1 ? $found[0] : '';
}

	
}
?>
Back to Top