Helix Installation   Getting Started   Reference Documentation   Global Functions   Quick Start
ModulesClassesFunctions
 

Class Reference

class generator

Hide Inherited MembersHelix Core
Description
No description.
 
Constants
No constants.
 
Properties
protected_fkdata[ generator ]
protected_input[ generator ]
protected_outdir[ generator ]
protected_output[ generator ]
publictables[ generator ]
 
Methods
public__construct ( )[ generator ]
Parameters
No parameters for this method
Code
010  public function __construct()
011  {
012      $this->outdir = SITES . "helix/generator/";
013      $this->input = null;
014      $this->output = null;
015      $this->_fkdata = array ();
016      $this->tables = array ();
017  }
publiccreate_output_dirs ( table )[ generator ]
Parameters
requiredtable
Code
593  public function create_output_dirs($table) {
594      if (is_dir ( MODULES . $table->prefix )) {
595          $this->outdir = SITES . "helix/generator/modules/";
596      } else {
597          $this->outdir = SITES . "helix/generator/sites/";
598      }
599 
600      if (! is_dir ( $this->outdir . $table->prefix . "/lib/database" )) {
601          mkdir ( $this->outdir . $table->prefix . "/lib/database", 0777, true );
602      }
603      if (! is_dir ( $this->outdir . $table->prefix . "/lib/custom" )) {
604          mkdir ( $this->outdir . $table->prefix . "/lib/custom", 0777, true );
605      }
606      if (! is_dir ( $this->outdir . $table->prefix . "/lib/extended" )) {
607          mkdir ( $this->outdir . $table->prefix . "/lib/extended", 0777, true );
608      }
609      if (! is_dir ( $this->outdir . $table->prefix . "/lib/final" )) {
610          mkdir ( $this->outdir . $table->prefix . "/lib/final", 0777, true );
611      }
612  }
publicdummy_inserts ( table )[ generator ]
Parameters
requiredtable
Code
1547  public function dummy_inserts($__table) {
1548      $inserts = "";
1549      $inserted_parent=false;
1550      if($__table->parent)
1551      {
1552          $inserts = $this->dummy_inserts($__table->parent);
1553          //use the immediate parent if it has the final id
1554          if(array_key_exists("id",$__table->parent->columns))
1555          {
1556              $inserts .= "SELECT @parent_id:= LAST_INSERT_ID();\n";
1557          }
1558          else //use the primary key of the parent table
1559          {
1560              $inserts .= "SELECT @last_inserted_id:= LAST_INSERT_ID();\n";
1561              $inserts .= "SELECT @parent_id:= `{$__table->parent->primary_key}` from `{$__table->parent->name}` where `_id`=@last_inserted_id ;\n";
1562          }
1563          $inserted_parent=true;
1564      }
1565 
1566      if (! $__table->is_type) {
1567          $inserts .= "INSERT into {$__table->name} (";
1568          foreach ( $__table->columns as $col ) {
1569              //unique keys autogen
1570              if ($col->field!="_id" && $col->field!="id") {
1571                  $inserts .= "`{$col->field}`,";
1572              }
1573          }
1574          $inserts = substr ( $inserts, 0, strlen ( $inserts ) - 1 );
1575          $inserts .= ") values (";
1576          foreach ( $__table->columns as $col ) {
1577              if ($col->field=="_id" || $col->field=="id") {
1578                  $inserts .= "";
1579              }
1580              else if ($col->is_primary) {
1581                  if($inserted_parent)
1582                  {
1583                      $inserts .= "@parent_id,";
1584                  }
1585              }
1586              //always make deleted=0 and finish off the SQL insert string
1587              else if ($col->field == 'deleted') {
1588                  $inserts .= "0);\n";
1589              }else if ($col->field == 'primary') {
1590                  $inserts .= "0,";
1591              } else if ($col->field == 'first_name') {
1592                  $inserts .= "'" . data::rand_first_name () . "',";
1593              } else if ($col->field == 'abbreviation') {
1594                  $inserts .= "'" . unique() . "',";
1595              } else if ($col->field == 'code') {
1596                  $inserts .= "'" . unique() . "',";
1597              } else if ($col->field == 'middle_name') {
1598                  $inserts .= "'" . data::rand_first_name () . "',";
1599              } else if (stristr ( $col->field, "type" ) && stristr ( $col->type, "text" )) {
1600                  $inserts .= "'" . data::rand_type () . "',";
1601              } else if ($col->field == 'last_name') {
1602                  $inserts .= "'" . data::rand_last_name () . "',";
1603              } else if ($col->field == 'username') {
1604                  $inserts .= "'" . unique() . "',";
1605              } else if ($col->field == 'postal_code') {
1606                  $inserts .= "'" . data::rand(10000,89000) . "',";
1607              } else if ($col->field == 'name') {
1608                  $inserts .= "'" . unique() . "',";
1609              } else if ($col->field == 'city') {
1610                  $inserts .= "'" . data::rand_city() . "',";
1611              } else if ($col->field == 'time') {
1612                  $inserts .= "'".data::rand_time()."',";
1613              } else if ($col->field == 'duration') {
1614                  $inserts .= "'".data::rand_time()."',";
1615              } else if ($col->field == 'addressline1') {
1616                  $inserts .= "'" . data::rand(1,99999) . " " . data::rand_words(2,4) . "',";
1617              } else {
1618                  switch ($col->type) {
1619                      case (stristr ( $col->type, "int" )) :
1620                          $inserts .= data::rand () . ",";
1621                          break;
1622                      case (stristr ( $col->type, "decimal" )) :
1623                          $inserts .= data::rand (1,100) . ".".rand(1,99).",";
1624                          break;
1625                      case (stristr ( $col->type, "datetime" )) :
1626                          $inserts .= "'" . data::rand_datetime () . "',";
1627                          break;
1628                      case (stristr ( $col->type, "text" )) :
1629                          if ($col->field == "description") {
1630                              $inserts .= "'" . data::rand_words ( 5, 50 ) . "',";
1631                          } elseif ($col->field == "data") {
1632                              $inserts .= "'" . data::rand_words ( 50, 1000 ) . "',";
1633                          } else {
1634                              $inserts .= "'" . data::rand_words ( 3, 15 ) . "',";
1635                          }
1636                          break;
1637                      default :
1638                          $inserts .= "null,";
1639                          break;
1640                  }
1641              }
1642          }
1643          $inserts .= "\n";
1644      }
1645      return $inserts;
1646  }
publicdummy_relationships ( table , limit [15] )[ generator ]
Parameters
requiredtable
optionallimit [15]
Code
1648  public function dummy_relationships($__table,$__limit=15)
1649  {
1650      $i=0;
1651      foreach ( $__table->columns as $col ) {
1652          if($i==1)
1653          {
1654              $col1 = $col;
1655          }
1656          if($i==2)
1657          {
1658              $col2 = $col;
1659          }
1660          if(stristr($col->field,"type"))
1661          {
1662              $col3 = $col;
1663          }
1664          $i++;
1665      }
1666      $inserts = "INSERT INTO $__table->name (";
1667      $inserts.= "`".$col1->field."`,";
1668      $inserts.= "`".$col2->field."`,";
1669      $inserts.= "`".$col3->field."`)";
1670      $inserts.= " (";
1671      $inserts .= " SELECT DISTINCT ";
1672      $inserts .= "table1.".$col1->referenced_column_name.",";
1673      $inserts .= "table2.".$col2->referenced_column_name.",";
1674      $inserts .= $col3->referenced_table_name.".".$col3->referenced_column_name;
1675      $inserts .= " FROM ";
1676      $inserts .= $col1->referenced_table_name." table1,";
1677      $inserts .= $col2->referenced_table_name." table2,";
1678      $inserts .= $col3->referenced_table_name;
1679      $inserts .= " ORDER BY rand() limit {$__limit}";
1680 
1681      $inserts .= ");\n";
1682      return $inserts;
1683  }
publicgenerate ( prefix_list [Array] )[ generator ]
Parameters
optionalprefix_list [Array]
Code
019  public function generate($prefix_list = array())
020  {
021      $start = microtime(true);
022      echo "Started generator at " . timestamp(); br();
023      $this->parse_tables();
024      $table_count = 0;
025      $aliases = array();
026      $classes = array();
027      foreach ($this->tables as $table)
028      {
029          if (preg_match ( '/^(' . implode ( "|", $prefix_list ) . ')/i', $table->name ))
030          {
031              if (preg_match ( '/_(ext|cus)$/i', $table->name ))
032              {
033                  debug ("[Table #" . lpad((++ $table_count),4,0) . "] Ignoring custom table : $table->name");
034              }
035              else
036              {
037                  $msg = "[Table #" . lpad((++ $table_count),4,0) . "] Generating classes for: $table->name";
038                  if (count ( $table->linked_tables ) > 0) {
039                      $msg .= " [Linked to " . implode ( ",", $table->linked_tables ) . "]";
040                  }
041                  debug ( $msg, __FILE__, __LINE__ );
042                  $this->create_output_dirs ( $table );
043                  $this->write_classes ( $table );
044              }
045          }
046      }
047 
048      debug("[Aliases    ] Generating Alias Files");
049      $table_count = 0;
050      $classes = array ();
051      foreach ($this->tables as $tablename=>$table) {
052          if (preg_match('/_(ext|cus)$/i',$tablename))
053          {
054              continue;
055          }
056          $prefix = substr ( $tablename, 0, strpos ( $tablename, "_" ) );
057          $classname = preg_replace("/^{$prefix}_/i","",$tablename);
058          $classes [$prefix] [] = "\$classes[\"$classname\"] = \"$prefix\";";
059      }
060 
061      foreach ( $classes as $prefix => $aliases ) {
062          if (is_dir ( MODULES . $prefix )) {
063              $aliaspath = SITES . "helix/generator/modules/$prefix/etc/$prefix.aliases.php";
064              if (! is_dir ( SITES . "helix/generator/modules/$prefix/etc" )) {
065                  mkdir ( SITES . "helix/generator/modules/$prefix/etc", 0777, true );
066              }
067          } else {
068              $aliaspath = SITES . "helix/generator/sites/$prefix/etc/$prefix.aliases.php";
069              if (! is_dir ( SITES . "helix/generator/sites/$prefix/etc" )) {
070                  mkdir ( SITES . "helix/generator/sites/$prefix/etc", 0777, true );
071              }
072          }
073          file_put_contents ( $aliaspath, "<?php\n" . implode ( "\n", $aliases ) . "\n" );
074      }
075 
076      debug ( "[Done       ] Generated class structures for " . count ( $this->tables ) . " tables [" . ceil(microtime(true)-$start) . " Seconds]" );
077      return true;
078  }
publicgenerate_class_properties ( table )[ generator ]
Parameters
requiredtable
Code
840  public function generate_class_properties($table) {
841      $prop_declaration = "";
842      foreach ( $table->columns as $column ) {
843          $prop_declaration .= "\tprotected \$_" . $column->nice_name . ";\n";
844      }
845      foreach ( $table->columns as $column ) {
846          $prop_declaration .= "\n\tprotected \$_" . $column->nice_name . "_dirty;";
847      }
848      return $prop_declaration;
849  }
publicgenerate_class_property_defaults ( table )[ generator ]
Parameters
requiredtable
Code
851  public function generate_class_property_defaults($table) {
852      $prop_default = "";
853      if ($table->is_child && $table->parent->name=="cor_resource")
854      {
855          $prop_default .= "\t\tparent::__construct(\$__resource_id, \$__hash, \$__file_array, \$__path);\n\n";
856      }
857 
858      foreach ( $table->collapsed_columns as $column ) {
859          $val = "";
860          if ($column->field == "hash") {
861              $val = "unique()";
862          } else if (in_array ( $column->field, $table->unique_keys ) && $column->field!="_id" && $column->field!="id") {
863              $val = "\$__$column->field";
864          } else if ($table->is_col_ext && $column->field == "value") {
865              $val = "\$__value";
866          } else if (preg_match ( "/tinyint/i", $column->type )) {
867              $val = "false";
868          } else if (preg_match ( "/int/i", $column->type )) {
869              $val = "0";
870          } else if (preg_match ( "/decimal/i", $column->type )) {
871              $val = "0";
872          } else if (preg_match ( "/date|time/i", $column->type )) {
873              $val = "null";
874          } else if (preg_match ( "/text|char/i", $column->type )) {
875              $val = "\"\"";
876          } else {
877              $val = "null";
878          }
879          $prop_default .= "\t\t\$this->_" . $column->nice_name . " = $val;\n";
880      }
881 
882      $prop_default .= "\n";
883      foreach ( $table->collapsed_columns as $column ) {
884          $prop_default .= "\t\t\$this->_" . $column->nice_name . "_dirty" . " = false;\n";
885      }
886      $prop_default .= "\n";
887      return $prop_default;
888  }
publicgenerate_database_property_values ( table )[ generator ]
Parameters
requiredtable
Code
890  public function generate_database_property_values($table) {
891      $s = "";
892      $t = $table;
893      $tables [] = $t;
894      while ( ! is_null ( $t->parent ) ) {
895          $tables [] = $t->parent;
896          $t = $t->parent;
897      }
898      foreach ( array_reverse ( $tables ) as $t ) {
899          foreach ( $t->columns as $column ) {
900              if ($t->name == $table->name || ($t->name != $table->name && ! in_array ( $column->nice_name, array ("_id", "mdate", "cdate", "deleted" ) ))) {
901                  $s .= "\t\t\t\$this->_" . $column->nice_name . " = \$db->f(\"{$column->field}\");\n";
902              }
903          }
904      }
905      return $s;
906  }
publicgenerate_delete_filter_clause ( table )[ generator ]
Parameters
requiredtable
Code
1418  public function generate_delete_filter_clause($table) {
1419      $s = "";
1420      return $s;
1421  }
publicgenerate_delete_filter_params ( table )[ generator ]
Parameters
requiredtable
Code
1413  public function generate_delete_filter_params($table) {
1414      $s = "";
1415      return $s;
1416  }
publicgenerate_dummydata ( prefix_list [Array] )[ generator ]
Parameters
optionalprefix_list [Array]
Code
1509  public function generate_dummydata($prefix_list = array())
1510  {
1511      $this->build_tables();
1512      $table_count = 0;
1513      $inserts = "SET NAMES utf8;\nSET SQL_MODE='';\nSET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;\nSET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';\n";
1514      $relationship_tables = array();
1515 
1516      $ignore_tables = array ("cor_config","cor_resource","cor_perms","cor_page","cor_perm","cor_role","cor_page_perm","cor_perm_role","mvp_dsv");
1517 
1518      foreach ( $this->tables as $table )
1519      {
1520          if (preg_match ( '/^(' . implode ( "|", $prefix_list ) . ')/i', $table->name ) && ! preg_match ( '/_(ext|cus)$/i', $table->name )) {
1521              $msg = "[table #" . (++ $table_count) . "] generating dummy data for $table->name";
1522              debug ( $msg );
1523              if($table->type==datatable::RELATIONSHIP)
1524              {
1525                  $relationship_tables[] = $table;
1526              }
1527 
1528              else if(!in_array(strtolower($table->name),$ignore_tables))
1529              {
1530                  for($i = 1; $i <= 5; $i ++) {
1531                      $inserts .= $this->dummy_inserts ($table);
1532                  }
1533              }
1534          }
1535      }
1536      foreach($relationship_tables as $rt)
1537      {
1538          $inserts .= $this->dummy_relationships($rt);
1539      }
1540      $inserts .= "SET SQL_MODE=@OLD_SQL_MODE;\nSET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;";
1541      $fp = fopen ( $this->outdir . "dummy.mysql.sql", "w" );
1542      fwrite ( $fp, $inserts );
1543      fclose ( $fp );
1544      debug ( "there are " . count ( $tables ) . " tables in the $this->db_name database" );
1545  }
publicgenerate_get_all_param ( table )[ generator ]
Parameters
requiredtable
Code
1035  public function generate_get_all_param($table) {
1036      $s = "";
1037      if ($table->primary_key != "id" && ! $table->is_child) {
1038          $keys = array ();
1039          foreach ( explode ( ",", $table->primary_key ) as $key ) {
1040              $keys [] = "\$__$key=null";
1041          }
1042          $s .= implode ( ", ", $keys );
1043      }
1044      return $s;
1045  }
publicgenerate_get_all_param_to_pass ( table )[ generator ]
Parameters
requiredtable
Code
1047  public function generate_get_all_param_to_pass($table) {
1048      $s = "";
1049      if ($table->primary_key != "id" && ! $table->is_child) {
1050          $keys = array ();
1051          foreach ( explode ( ",", $table->primary_key ) as $key ) {
1052              $keys [] = "\$__$key";
1053          }
1054          $s .= implode ( ", ", $keys );
1055      }
1056      return $s;
1057  }
publicgenerate_get_where_clause ( table )[ generator ]
Parameters
requiredtable
Code
1401  public function generate_get_where_clause($table) {
1402      $s = "";
1403      $s .= "\$q .= \" WHERE $table->name.deleted = 0 \";";
1404      $s .= "\n\t\t\$q .= is_null(\$__where_clause) ? null : \" AND (\$__where_clause) \";";
1405      $lookups = array ();
1406      foreach ( $table->lookups as $lookup ) {
1407          $lookups [] = "\n\t\t\$q .= array_key_exists(\"$lookup\",\$lookups) ? \" AND {$table->name}.$lookup='\" . \$lookups[\"$lookup\"] . \"' \" : \"\";";
1408      }
1409      $s .= implode ( "", $lookups );
1410      return $s;
1411  }
publicgenerate_insert_column_names ( table )[ generator ]
Parameters
requiredtable
Code
947  public function generate_insert_column_names($table) {
948      $ins_col_names = "";
949      foreach ( $table->columns as $column ) {
950          if ($column->field != "cdate" && $column->field != "mdate" && $column->field != "deleted" && $column->extra != "auto_increment") {
951              $ins_col_names .= "\t\t\$q .= \$db->param_string(\"$column->field\") . \", \";\n";
952          }
953      }
954      return $ins_col_names;
955  }
publicgenerate_insert_column_values ( table )[ generator ]
Parameters
requiredtable
Code
957  public function generate_insert_column_values($table) {
958      $ins_col_vals = "";
959      foreach ( $table->columns as $column ) {
960          if ($column->field != "cdate" && $column->field != "mdate" && $column->field != "deleted" && $column->extra != "auto_increment") {
961              $value = $column->is_boolean ? "(\$this->_" . $column->nice_name . " ? \"'1'\" : \"'0'\")" : "\$db->value_string(\$this->_" . $column->nice_name . ")";
962              $ins_col_vals .= "\t\t\$q .= $value . \", \";\n";
963          }
964      }
965      return $ins_col_vals;
966  }
publicgenerate_join_parent_objects ( table )[ generator ]
Parameters
requiredtable
Code
1423  public function generate_join_parent_objects($table) {
1424      $s = "";
1425      $child = $table;
1426      $parent = $child->parent;
1427      while ( ! is_null ( $parent ) ) {
1428          $s .= "\n\t\t\$q .= \" INNER JOIN $parent->name ON {$child->name}.{$child->primary_key}={$parent->name}.{$parent->primary_key} \";";
1429          $lookups = array ();
1430          foreach ( $parent->lookups as $lookup ) {
1431              $lookups [] = "\n\t\t\$q .= array_key_exists(\"$lookup\",\$lookups) ? \" AND {$parent->name}.$lookup='\" . \$lookups[\"$lookup\"] . \"' \" : \"\";";
1432          }
1433          $s .= implode ( "", $lookups );
1434          $child = $parent;
1435          $parent = $child->parent;
1436      }
1437      return $s;
1438  }
publicgenerate_map ( table )[ generator ]
Parameters
requiredtable
Code
1000  public function generate_map($table) {
1001      $inits = "";
1002 
1003      $fields = array_keys ( $table->columns );
1004 
1005      $parent = $table->parent;
1006      while ( ! is_null ( $parent ) ) {
1007          foreach ( $parent->columns as $column ) {
1008              if ($column->field != $parent->primary_key && $column->field != "_id" && ! in_array ( $column->field, $fields )) {
1009                  $inits .= "\t\t\$this->_" . $column->nice_name . " = array_key_exists(\"" . $table->class . ":" . $column->nice_name . "\",\$_REQUEST) ? reqval(\"" . $table->class . ":" . $column->nice_name . "\") : \$this->_" . $column->nice_name . ";\n";
1010              }
1011          }
1012          $parent = $parent->parent;
1013      }
1014      foreach ( $table->columns as $column ) {
1015          if ($column->field != "id" && ! in_array ( $column->field, $table->primary_keys ) && $column->field != "_id") {
1016              $inits .= "\t\t\$this->_" . $column->nice_name . " = array_key_exists(\"" . $table->class . ":" . $column->nice_name . "\",\$_REQUEST) ? reqval(\"" . $table->class . ":" . $column->nice_name . "\") : \$this->_" . $column->nice_name . ";\n";
1017          }
1018      }
1019      return $inits;
1020  }
publicgenerate_mssql ( )[ generator ]
Parameters
No parameters for this method
Code
614  public function generate_mssql() {
615      if (! is_dir ( $this->outdir )) {
616          mkdir ( $this->outdir );
617      }
618      $dbo = new db ( );
619      $q = " SELECT TABLE_NAME FROM information_schema.TABLES ";
620      $q .= " WHERE TABLE_SCHEMA='$this->db_name' AND TABLE_NAME NOT REGEXP '_ext$'";
621      $dbo->query ( $q );
622 
623      $tablenames = array ();
624      while ( $dbo->next_record () ) {
625          $tablenames [] = $dbo->f ( "TABLE_NAME" );
626      }
627 
628      $table_count = 0;
629      $mssql = "";
630      $drop_tables_mssql = "";
631      $fkeys_mssql = "";
632      foreach ( $tablenames as $tablename ) {
633          debug ( "[table #" . (++ $table_count) . "] generating MSSQL for $tablename", __FILE__, __LINE__ );
634          //$drop_tables .="DROP table $tablename;";
635 
636 
637          $drop_tables_mssql .= "if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[$tablename]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[$tablename];\n";
638 
639          $table = new datatable ( "$tablename" );
640          $prefix = substr ( $tablename, 0, strpos ( $tablename, "_" ) );
641          $mssql .= "\n\nCREATE TABLE $tablename (";
642          foreach ( $table->columns as $column ) {
643              $mssql .= $column->mssql_create_string . ",";
644          }
645          $mssql .= " PRIMARY KEY (" . implode ( ",", $table->primary_keys ) . ")";
646          $mssql .= ");\n\n";
647 
648          //Add foreign keys
649          if (count ( $table->referenced_tables ) > 0) {
650              $fkeys_mssql .= "\nALTER TABLE [dbo].[$tablename] WITH NOCHECK ADD ";
651              for($i = 0; $i < count ( $table->referenced_tables ); $i ++) {
652                  $fkeys_mssql .= " CONSTRAINT [FK_{$tablename}_{$table->referenced_tables[$i]}_" . ($i + 1) . "] FOREIGN KEY ([{$table->fk_columns[$i]}]) REFERENCES [dbo].[{$table->referenced_tables[$i]}] ([{$table->referenced_columns[$i]}]) NOT FOR REPLICATION";
653                  $fkeys_mssql .= $i < count ( $table->referenced_tables ) - 1 ? "," : "";
654              }
655              $fkeys_mssql .= ";";
656          }
657      }
658 
659      $fp = fopen ( $this->outdir . "helix.mssql.sql", "w" );
660      fwrite ( $fp, $mssql );
661      fclose ( $fp );
662 
663      $fp = fopen ( $this->outdir . "droptables.mssql.sql", "w" );
664      fwrite ( $fp, $drop_tables_mssql );
665      fclose ( $fp );
666 
667      $fp = fopen ( $this->outdir . "foreignkeys.mssql.sql", "w" );
668      fwrite ( $fp, $fkeys_mssql );
669      fclose ( $fp );
670 
671  }
publicgenerate_null_param_check ( table )[ generator ]
Parameters
requiredtable
Code
1440  public function generate_null_param_check($table) {
1441      $s = "";
1442      $conditions = array ();
1443      foreach ( array_merge ( $table->primary_keys, $table->collapsed_unique_keys ) as $key ) {
1444          if ($key != "_id") {
1445              $conditions [] = "is_null(\$__$key)";
1446          }
1447      }
1448      if (count ( $conditions ) > 0) {
1449          $s .= "\t\tif (" . implode ( " && ", $conditions ) . ")\n";
1450          $s .= "\t\t{\n";
1451          $s .= "\t\t\treturn;\n";
1452          $s .= "\t\t}\n\n";
1453      }
1454      return $s;
1455  }
publicgenerate_order_by ( table )[ generator ]
Parameters
requiredtable
Code
995  public function generate_order_by($table) {
996      $order_by = "\$q .= \" ORDER BY \" . \$db->param_string(\"order\");";
997      return $table->is_ordered ? $order_by : "// THERE IS NO `order` COLUMN ON THIS OBJECT";
998  }
publicgenerate_parent_join_constructor ( table )[ generator ]
Parameters
requiredtable
Code
1457  public function generate_parent_join_constructor($table) {
1458      $s = "";
1459      $child = $table;
1460      $parent = $child->parent;
1461      while ( ! is_null ( $parent ) ) {
1462          $s .= "\n\t\t\$q .= \" INNER JOIN $parent->name ON {$child->name}.{$child->primary_key}={$parent->name}.{$parent->primary_key} \";";
1463          $filters = array ();
1464          if ($child->name == $table->name) {
1465              foreach ( $parent->primary_keys as $key ) {
1466                  $filters [] = "\n\t\t\$q .= is_null(\$__{$child->primary_key}) ? \"\" : \" AND {$parent->name}.$key='\$__{$child->primary_key}' \";";
1467              }
1468              foreach ( $child->unique_keys as $key ) {
1469                  if ($key != "_id") {
1470                      $filters [] = "\n\t\t\$q .= is_null(\$__$key) ? \"\" : \" AND {$child->name}.$key='\$__$key' \";";
1471                  }
1472              }
1473          }
1474          foreach ( $parent->unique_keys as $key ) {
1475              if ($key != "_id") {
1476                  $filters [] = "\n\t\t\$q .= is_null(\$__$key) ? \"\" : \" AND {$parent->name}.$key='\$__$key' \";";
1477              }
1478          }
1479          $s .= implode ( "", $filters );
1480          $child = $parent;
1481          $parent = $child->parent;
1482      }
1483      return $s;
1484  }
publicgenerate_primary_key_constructor ( table )[ generator ]
Parameters
requiredtable
Code
1109  public function generate_primary_key_constructor($table) {
1110      $s = "";
1111      if ($table->is_child) {
1112          return $s;
1113      }
1114      if ($table->primary_key != "id" && ! $table->is_child) {
1115          $s .= "\n\t\tif (!is_null(\$__id))";
1116          $s .= "\n\t\t{";
1117          $s .= "\n\t\t\t\$q .= \" WHERE id = '\$__id' \";";
1118          if ($table->type == "RESOURCE") {
1119              //                $s .= "\n\t\t\t\$run_query = true;";
1120          }
1121          $s .= "\n\t\t}";
1122      }
1123 
1124      $conditions = array ();
1125      foreach ( $table->primary_keys as $primary_key ) {
1126          $conditions [] = "!is_null(\$__$primary_key)";
1127      }
1128      $condition = join ( " && ", $conditions );
1129 
1130      if ($table->primary_key != "id" && ! $table->is_child) {
1131          $s .= "\n\t\telse if ($condition)";
1132      } else {
1133          $s .= "\n\t\tif ($condition)";
1134      }
1135      $s .= "\n\t\t{";
1136      $conditions = array ();
1137      foreach ( $table->primary_keys as $primary_key ) {
1138          $conditions [] = "$primary_key = '\$__$primary_key'";
1139          if ($table->type == "RELATIONSHIP") {
1140  //                $col = new db_column ( $table->name, $primary_key, $table->database );
1141              $col = $table->columns[$primary_key];
1142              $s .= "\n\t\t\t\$this->$col->nice_name = \$__$primary_key;";
1143          }
1144      }
1145      $condition = join ( " AND ", $conditions );
1146      $s .= "\n\t\t\t\$q .= \" WHERE $condition \";";
1147      if ($table->type == "RESOURCE") {
1148          //            $s .= "\n\t\t\t\$run_query = true;";
1149      }
1150      $s .= "\n\t\t}";
1151 
1152      return $s;
1153  }
publicgenerate_primary_key_filter ( table )[ generator ]
Parameters
requiredtable
Code
1498  public function generate_primary_key_filter($table) {
1499      $s = "";
1500      foreach ( $table->primary_keys as $key ) {
1501          $s .= "\n\t\tif (!is_null(\$__$key))";
1502          $s .= "\n\t\t{";
1503          $s .= "\n\t\t\t\$filters[] = \" $key = '\$__$key' \";";
1504          $s .= "\n\t\t}";
1505      }
1506      return $s;
1507  }
publicgenerate_primary_key_param ( table )[ generator ]
Parameters
requiredtable
Code
1022  public function generate_primary_key_param($table) {
1023      $s = $table->is_child ? "\$__$table->primary_key=null" : "\$__id=null";
1024      if ($table->primary_key != "id" && ! $table->is_child) {
1025          foreach ( explode ( ",", $table->primary_key ) as $key ) {
1026              $s .= ", \$__$key=" . (preg_match ( '/_type_id$/i', $key ) ? "0" : "null");
1027          }
1028      }
1029      if ($table->is_col_ext) {
1030          $s .= ", \$__value=null";
1031      }
1032      return $s;
1033  }
publicgenerate_query_column_list ( table )[ generator ]
Parameters
requiredtable
Code
908  public function generate_query_column_list($table) {
909      $s = "";
910      $t = $table;
911      $tables [] = $t;
912      while ( ! is_null ( $t->parent ) ) {
913          $tables [] = $t->parent;
914          $t = $t->parent;
915      }
916      $columns = array ();
917      foreach ( array_reverse ( $tables ) as $t ) {
918          foreach ( $t->columns as $column ) {
919              if ($t->name == $table->name || ($t->name != $table->name && ! in_array ( $column->nice_name, array ("_id", "mdate", "cdate", "deleted" ) ))) {
920                  $columns [] = "{$t->name}.{$column->field}";
921              }
922          }
923      }
924      $s .= implode ( ",", $columns );
925      return $s;
926  }
publicgenerate_reconstruct_parent ( table )[ generator ]
Parameters
requiredtable
Code
936  public function generate_reconstruct_parent($table) {
937      $s = "";
938      if ($table->is_child) {
939          //                $s .= "\n\t\t\t\tif (\$reconstruct_parent)";
940      //                $s .= "\n\t\t\t\t{";
941      //                $s .= "\n\t\t\t\t\tparent::__construct(\$this->_$table->primary_key);";
942      //                $s .= "\n\t\t\t\t}\n";
943      }
944      return $s;
945  }
publicgenerate_reconstruct_parent_initialize ( table )[ generator ]
Parameters
requiredtable
Code
928  public function generate_reconstruct_parent_initialize($table) {
929      $s = "";
930      if ($table->is_child) {
931          //            $s .= "\$reconstruct_parent = false;";
932      }
933      return $s;
934  }
publicgenerate_related_object_properties ( table , layer ["database"] )[ generator ]
publicgenerate_resource_constructors ( table )[ generator ]
Parameters
requiredtable
Code
1102  public function generate_resource_constructors($table) {
1103      $s = "";
1104      if ($table->type == "RESOURCE") {
1105      }
1106      return $s;
1107  }
publicgenerate_resource_params ( table )[ generator ]
Parameters
requiredtable
Code
1094  public function generate_resource_params($table) {
1095      $s = "";
1096      if ($table->type == "RESOURCE") {
1097          //            $s .= ", \$__file_array=null, \$__path=null";
1098      }
1099      return $s;
1100  }
publicgenerate_search_clause ( table )[ generator ]
Parameters
requiredtable
Code
826  public function generate_search_clause($table) {
827      $search_clause_parts = array ();
828      $child = $table;
829      while ( ! is_null ( $child ) ) {
830          foreach ( $child->columns as $column ) {
831              if (preg_match ( "/text/i", $column->type )) {
832                  $search_clause_parts [] = " \" . \$db->param_string(\"$column->field\") . \" LIKE '%\$word%' ";
833              }
834          }
835          $child = is_null ( $child->parent ) ? null : $child->parent;
836      }
837      return implode ( " OR ", $search_clause_parts );
838  }
publicgenerate_search_join_parent_objects ( table )[ generator ]
Parameters
requiredtable
Code
1486  public function generate_search_join_parent_objects($table) {
1487      $s = "";
1488      $child = $table;
1489      $parent = $child->parent;
1490      while ( ! is_null ( $parent ) ) {
1491          $s .= "\n\t\t\$q .= \" INNER JOIN $parent->name ON {$child->name}.{$child->primary_key}={$parent->name}.{$parent->primary_key} \";";
1492          $child = $parent;
1493          $parent = $child->parent;
1494      }
1495      return $s;
1496  }
publicgenerate_to_string ( table )[ generator ]
Parameters
requiredtable
Code
979  public function generate_to_string($table) {
980      $prop_declaration = "";
981      if ($table->is_col_ext) {
982          return "value";
983      } else {
984          foreach ( $table->columns as $column ) {
985              if ($column->field == "name") {
986                  return "name";
987              } else if ($column->field == "description") {
988                  return "description";
989              }
990          }
991          return "id";
992      }
993  }
publicgenerate_unique_key_constructors ( table )[ generator ]
Parameters
requiredtable
Code
1074  public function generate_unique_key_constructors($table) {
1075      $s = "";
1076      if ($table->is_child) {
1077          return $s;
1078      }
1079      foreach ( $table->collapsed_unique_keys as $unique_key ) {
1080          if ($unique_key!="_id" && $unique_key!="id") {
1081              $s .= "\n\t\telse if (!is_null(\$__$unique_key))";
1082              $s .= "\n\t\t{";
1083              $s .= "\n\t\t\t\$q .= \" WHERE $unique_key = '\$__$unique_key' \";";
1084              //                $s .= "\n\t\t\t\$reconstruct_parent = true;";
1085              if ($table->type == "RESOURCE") {
1086                  //                    $s .= "\n\t\t\t\$run_query = true;";
1087              }
1088              $s .= "\n\t\t}";
1089          }
1090      }
1091      return $s;
1092  }
publicgenerate_unique_key_params ( table )[ generator ]
Parameters
requiredtable
Code
1059  public function generate_unique_key_params($table) {
1060      $s = "";
1061      foreach ( $table->collapsed_unique_keys as $unique_key ) {
1062          if ($unique_key=="_id" || $unique_key=="id") {
1063              continue;
1064          }
1065          $s .= ", \$__$unique_key=null";
1066      }
1067      if ($table->is_child && $table->parent->is_resource)
1068      {
1069          $s .= ", \$__file_array=null, \$__path=null";
1070      }
1071      return $s;
1072  }
publicgenerate_update_columns ( table )[ generator ]
Parameters
requiredtable
Code
968  public function generate_update_columns($table) {
969      $upd_col_names = "";
970      foreach ( $table->columns as $column ) {
971          if ($column->field != "cdate" && $column->field != "mdate" && $column->field != "deleted" && $column->extra != "auto_increment") {
972              $value = $column->is_boolean ? "(\$this->_" . $column->nice_name . " ? \"'1'\" : \"'0'\")" : "\$db->value_string(\$this->_" . $column->nice_name . ")";
973              $upd_col_names .= "\t\t\$q .= \$db->param_string(\"$column->field\") . \" = \" . $value . \", \";\n";
974          }
975      }
976      return $upd_col_names;
977  }
publicparse_tables ( )[ generator ]
Parameters
No parameters for this method
Code
080  public function parse_tables()
081  {
082      ///////////////////////////////////////////////////////////////
083      // This will hold all of the table data once it is parsed.  The
084      // keys of this array will be the tablenames.
085      ///////////////////////////////////////////////////////////////
086      $tables = array();
087 
088      ///////////////////////////////////////////////////////////////
089      // Get the MySQL schema definition files for all modules and
090      // sites.
091      ///////////////////////////////////////////////////////////////
092      $text = "";
093      foreach (glob(HELIX . "*/*/etc/*.mysql.sql") as $sql)
094      {
095          $text .= file_get_contents($sql);
096      }
097 
098      ///////////////////////////////////////////////////////////////
099      // This is an array to hold each line of the schema files as
100      // an element of the array.  This will strip out only the
101      // schema file lines that create tables, columns, or keys.
102      // One final line needs to be added so the parser can run once
103      // after the final statement.
104      ///////////////////////////////////////////////////////////////
105      $lines = array_merge(preg_grep('/^\s*(create\s+table|`|unique\s+key|primary\s+key|constraint)/i',explode("\n",$text)));
106      $lines[] = "final line";
107 
108      ///////////////////////////////////////////////////////////////
109      // This is the column set that defines a column extension table
110      ///////////////////////////////////////////////////////////////
111      $col_ext_cols = array("id","value","mdate","cdate","deleted");
112 
113      ///////////////////////////////////////////////////////////////
114      // Theses are the possible table types for tables in a Helix
115      // database.
116      ///////////////////////////////////////////////////////////////
117      $types = array("STANDARD","RELATIONSHIP","CUSTOM","LOOKUP","STANDARD_TYPE","RELATIONSHIP_TYPE","COLUMN_EXTENSION","RESOURCE","MULTIPLE_RELATIONSHIP");
118 
119      ///////////////////////////////////////////////////////////////
120      // Initialize the $tablename before looping through the schema
121      // file lines.
122      ///////////////////////////////////////////////////////////////
123      $tablename = null;
124      foreach ($lines as $num=>$line)
125      {
126          ///////////////////////////////////////////////////////////////
127          // If this is a CREATE TABLE line, or the final line, do some
128          // special parsing to create/finalize the table data
129          ///////////////////////////////////////////////////////////////
130          if (preg_match('/final line|create\s+table[^a-z_0-9]+([a-z_0-9]+)/i',$line,$matches))
131          {
132              ///////////////////////////////////////////////////////////////
133              // This will only run after $tablename is set, and thus only
134              // after all columns have been processed for a table
135              ///////////////////////////////////////////////////////////////
136              if (!is_null($tablename))
137              {
138                  ///////////////////////////////////////////////////////////////
139                  // This will set an initial value for collapsed_column_list and
140                  // collapsed_unique_keys.  The "collapsed" arrays will hold
141                  // values for all inherited tables in one array.
142                  ///////////////////////////////////////////////////////////////
143                  foreach ($tables[$tablename]["column_list"] as $colname=>$data)
144                  {
145                      $tables[$tablename]["collapsed_column_list"][$colname] = $data;
146                  }
147                  foreach ($tables[$tablename]["unique_keys"] as $colname)
148                  {
149                      $tables[$tablename]["collapsed_unique_keys"][$colname] = $colname;
150                  }
151 
152                  ///////////////////////////////////////////////////////////////
153                  // The num_parts is the number of segments in the tablename
154                  // separated by an underscore.  This helps determine the table
155                  // type.
156                  ///////////////////////////////////////////////////////////////
157                  if ($tables[$tablename]["num_parts"]===2)
158                  {
159                      ///////////////////////////////////////////////////////////////
160                      // The is_child key will be true if the table inherits another
161                      // table.  This can be determined by the existence of the _id
162                      // column instead of the id column.
163                      ///////////////////////////////////////////////////////////////
164                      $tables[$tablename]["is_child"] = in_array("_id",array_keys($tables[$tablename]["column_list"]));
165 
166 
167                      ///////////////////////////////////////////////////////////////
168                      // Determine the table type for table with 2 parts
169                      ///////////////////////////////////////////////////////////////
170                      if (count(array_diff(array_keys($tables[$tablename]["column_list"]),$col_ext_cols))==0)
171                      {
172                          $tables[$tablename]["type"] = "COLUMN_EXTENSION";
173                          $tables[$tablename]["is_col_ext"] = true;
174                      }
175                      else if ($tables[$tablename]["is_resource"])
176                      {
177                          $tables[$tablename]["type"] = "STANDARD";
178                      }
179                      else
180                      {
181                          $tables[$tablename]["type"] = "STANDARD";
182                      }
183                  }
184                  else if ($tables[$tablename]["num_parts"]===3)
185                  {
186                      ///////////////////////////////////////////////////////////////
187                      // Determine the table type for table with 3 parts
188                      ///////////////////////////////////////////////////////////////
189                      if (preg_match('/_type$/i',$tablename))
190                      {
191                          $tables[$tablename]["type"] = "STANDARD_TYPE";
192                          $tables[$tablename]["is_type"] = true;
193                      }
194                      else if (preg_match('/_(ext|cus)$/i',$tablename))
195                      {
196                          $tables[$tablename]["type"] = "CUSTOM";
197                      }
198                      else
199                      {
200                          $tables[$tablename]["type"] = "RELATIONSHIP";
201 
202                          ///////////////////////////////////////////////////////////////
203                          // If this is a recursive relationship table that references
204                          // the same table twice -- once as child and once as parent --
205                          // then set is_self_referential to true.
206                          ///////////////////////////////////////////////////////////////
207                          if ($tables[$tablename]["parts"][1] == $tables[$tablename]["parts"][2])
208                          {
209                              $tables[$tablename]["is_self_referential"] = true;
210                              $tables[$tablename]["object1"] = "child";
211                              $tables[$tablename]["object2"] = "parent";
212                          }
213                          else
214                          {
215                              $tables[$tablename]["object1"] = $tables[$tablename]["parts"][1];
216                              $tables[$tablename]["object2"] = $tables[$tablename]["parts"][2];
217                          }
218                      }
219                  }
220                  else if ($tables[$tablename]["num_parts"]===4)
221                  {
222                      ///////////////////////////////////////////////////////////////
223                      // Determine the table type for table with 4 parts.  Every
224                      // relationship table should have a corresponding type table.
225                      ///////////////////////////////////////////////////////////////
226                      if (preg_match('/_type$/i',$tablename))
227                      {
228                          $tables[$tablename]["type"] = "RELATIONSHIP_TYPE";
229                          $tables[$tablename]["is_type"] = true;
230                      }
231                      else if (preg_match('/_(ext|cus)$/i',$tablename))
232                      {
233                          $tables[$tablename]["type"] = "CUSTOM";
234                      }
235                  }
236              }
237 
238              ///////////////////////////////////////////////////////////////
239              // Initialize the data array for this table
240              ///////////////////////////////////////////////////////////////
241              if (!preg_match('/final line/i',$line))
242              {
243                  $tablename = $matches[1];
244                  $tables[$tablename] = array(
245                      "name"=>$tablename,
246                      "class"=>substr($tablename,strpos($tablename,"_")+1),
247                      "is_col_ext"=>false,
248                      "has_type"=>false,
249                      "is_type"=>preg_match('/_type$/i',$tablename)>0,
250                      "type"=>null,
251                      "is_ordered"=>false,
252                      "parts"=>explode("_",$tablename),
253                      "num_parts"=>count(explode("_",$tablename)),
254                      "prefix"=>keyval(explode("_",$tablename),0),
255                      "object"=>substr($tablename,strpos($tablename,"_")+1),
256                      "object1"=>null,
257                      "object2"=>null,
258                      "is_self_referential"=>false,
259                      "is_ordered"=>false,
260                      "parent"=>null,
261                      "parent_tablename"=>null,
262                      "primary_key"=>null,
263                      "is_child"=>false,
264                      "is_parent"=>false,
265                      "is_resource"=>preg_match('/_resource$/i',$tablename)>0,
266                      "primary_keys"=>array(),
267                      "unique_keys"=>array(),
268                      "lookups"=>array(),
269                      "linked_tables"=>array(),
270                      "linked_columns"=>array(),
271                      "linked_relationship_tables"=>array(),
272                      "referenced_tables"=>array(),
273                      "referenced_columns"=>array(),
274                      "fk_columns"=>array(),
275                      "collapsed_unique_keys"=>array(),
276                      "collapsed_column_list"=>array(),
277                      "constraints"=>array(),
278                      "column_list"=>array()
279                  );
280 
281                  ///////////////////////////////////////////////////////////////
282                  // The position variable is the ordinal position of columns in
283                  // current table.  Reset it after each table is initialized.
284                  ///////////////////////////////////////////////////////////////
285                  $position = 0;
286 
287                  ///////////////////////////////////////////////////////////////
288                  // Use a shorter variable name for the current table for
289                  // readability.
290                  ///////////////////////////////////////////////////////////////
291                  $t = $tables[$tablename];
292              }
293          }
294          else
295          {
296              ///////////////////////////////////////////////////////////////
297              // This will match lines that contain column definitions.  The
298              // schema files should only have one column definition on each
299              // line.
300              ///////////////////////////////////////////////////////////////
301              if (preg_match('/^\s*`([a-z_0-9]+)`[^a-z]+([a-z]+(\s*\([0-9,\s]+\))?)/i',$line,$matches))
302              {
303                  $colname = $matches[1];
304 
305                  ///////////////////////////////////////////////////////////////
306                  // Initialize the data array for the current column.
307                  ///////////////////////////////////////////////////////////////
308                  $t["column_list"][$colname]["position"] = $position++;
309                  $t["column_list"][$colname]["field"] = $matches[1];
310                  $t["column_list"][$colname]["type"] = $matches[2];
311                  $t["column_list"][$colname]["is_boolean"] = preg_match('/tinyint\(1\)/i',$matches[2])>0;
312                  $t["column_list"][$colname]["default"] = preg_match('/default\s+\'?(null|[^\',]+|[0-9]+)\'?/i',$line,$matches) ? $matches[1] : null;
313                  $t["column_list"][$colname]["key"] = null;
314                  $t["column_list"][$colname]["null"] = preg_match('/not\s+null/i',$line) ? "NO" : "YES";
315                  $t["column_list"][$colname]["extra"] = preg_match('/auto_increment/i',$line) ? "auto_increment" : null;
316                  $t["column_list"][$colname]["in_relationship"] = (count(explode("_",$tablename))==3 && !preg_match('/_(ext|cus|type)$/i',$tablename));
317                  $t["column_list"][$colname]["is_primary"] = false;
318                  $t["column_list"][$colname]["is_unique"] = false;
319                  $t["column_list"][$colname]["referenced_table_name"] = null;
320                  $t["column_list"][$colname]["referenced_column_name"] = null;
321                  $t["column_list"][$colname]["nice_name"] = preg_match('/^(parent|child)_/i',$colname,$matches) ? "{$matches[1]}_id" : $colname;
322                  $t["column_list"][$colname]["mssql_create_string"] = null;
323                  $t["is_ordered"] = in_array("order",array_keys($t["column_list"]));
324              }
325              ///////////////////////////////////////////////////////////////
326              // This will match lines that contain primary or unique key
327              // definitions.  These lines should come after all column
328              // definitions in the schema file.
329              ///////////////////////////////////////////////////////////////
330              else if (preg_match('/^\s*(primary|unique)\s+key([^a-z0-9_]+([a-z0-9_]+).+)$/i',$line,$matches))
331              {
332                  if (strtolower($matches[1])=="primary")
333                  {
334                      ///////////////////////////////////////////////////////////////
335                      // Find all the primary key information for the current column.
336                      ///////////////////////////////////////////////////////////////
337                      preg_match_all('/([a-z0-9_]+)[^a-z0-9_]/i',$matches[2],$matchset,PREG_SET_ORDER);
338                      foreach ($matchset as $matches)
339                      {
340                          $colname = $matches[1];
341                          $t["column_list"][$colname]["key"] = "PRI";
342                          $t["column_list"][$colname]["is_primary"] = true;
343                          $t["primary_keys"][$t["column_list"][$colname]["position"]] = $colname;
344                      }
345 
346                      ///////////////////////////////////////////////////////////////
347                      // Sort the primary keys by their ordinal position in the
348                      // schema file.  The primary keys for each referenced table --
349                      // excluding the primary key for the type table -- should be
350                      // sorted in alphabetical order in the schema file.
351                      ///////////////////////////////////////////////////////////////
352                      $tmp = $t["primary_keys"];
353                      ksort($tmp,SORT_NUMERIC);
354 
355                      ///////////////////////////////////////////////////////////////
356                      // Reset the numeric keys to begin at 0.
357                      ///////////////////////////////////////////////////////////////
358                      $pks = array();
359                      foreach ($tmp as $pk)
360                      {
361                          $pks[] = $pk;
362                      }
363 
364                      ///////////////////////////////////////////////////////////////
365                      // Set the primary_keys array and the primary_key property
366                      ///////////////////////////////////////////////////////////////
367                      $t["primary_keys"] = $pks;
368                      $t["primary_key"] = implode(",",$t["primary_keys"]);
369 
370                      ///////////////////////////////////////////////////////////////
371                      // The has_type variable will only be true if {class}_type_id
372                      // column is set as a foreign key to the type table.
373                      ///////////////////////////////////////////////////////////////
374                      $t["has_type"] = stripos($t["primary_key"],"{$t["class"]}_type_id")!==false;
375                  }
376                  else if (strtolower($matches[1])=="unique")
377                  {
378                      ///////////////////////////////////////////////////////////////
379                      // Find all the unique key information for the current column.
380                      ///////////////////////////////////////////////////////////////
381                      $colname = $matches[3];
382                      $t["column_list"][$colname]["key"] = "UNI";
383                      $t["column_list"][$colname]["is_unique"] = true;
384                      $t["unique_keys"][] = $colname;
385                  }
386              }
387              ///////////////////////////////////////////////////////////////
388              // This will match all lines that contain foreign key
389              // definitions for the current table.
390              ///////////////////////////////////////////////////////////////
391              else if (preg_match('/^\s*constraint[^a-z0-9_]+([a-z0-9_]+)[^a-z0-9_]+foreign\s+key\s+\([^a-z0-9_]*([a-z0-9_]+)[^a-z0-9_]*\)\s+references[^a-z0-9_]+([a-z0-9_]+)[^a-z0-9_]+\s+\([^a-z0-9_]*([a-z0-9_]+)[^a-z0-9_]*\)/i',$line,$matches))
392              {
393                  $constraint = $matches[1];
394                  $colname = $matches[2];
395                  $t["constraints"][$constraint]["COLUMN_NAME"] = $colname;
396                  $t["constraints"][$constraint]["REFERENCED_TABLE_NAME"] = $matches[3];
397                  $t["constraints"][$constraint]["REFERENCED_COLUMN_NAME"] = $matches[4];
398                  $t["column_list"][$colname]["referenced_table_name"] = $matches[3];
399                  $t["column_list"][$colname]["referenced_column_name"] = $matches[4];
400                  $t["referenced_tables"][] = $matches[3];
401                  $t["referenced_columns"][] = $matches[4];
402                  $t["fk_columns"][] = $colname;
403 
404                  if ($t["column_list"][$colname]["is_primary"] && $t["column_list"][$colname]["in_relationship"] && !preg_match('/^(parent|child)_/i',$colname))
405                  {
406                      ///////////////////////////////////////////////////////////////
407                      // The nice_name property will be used as the class property
408                      // and will not necessarily be the same as the column name.  It
409                      // will be different for relationship tables referencing child
410                      // tables.  For example, the relationship table cor_role_user
411                      // has columns role_id and user_person_entity_id -- the column
412                      // nice_name's would be role_id and user_id in the class.
413                      ///////////////////////////////////////////////////////////////
414                      $t["column_list"][$colname]["nice_name"] = substr($matches[3],strpos($matches[3],"_")+1) . "_id";
415                  }
416 
417                  if (preg_match('/.+_id$/i',$colname) && !$t["column_list"][$colname]["is_primary"] && !$t["column_list"][$colname]["is_unique"])
418                  {
419                      ///////////////////////////////////////////////////////////////
420                      // The lookups array contains references to columns that have
421                      // foreign keys to lookup tables, but they are not unique or
422                      // primary columns.
423                      ///////////////////////////////////////////////////////////////
424                      $t["lookups"][] = $colname;
425                      $t["has_type"] = $t["has_type"] || ($colname=="{$t["class"]}_type_id");
426                  }
427              }
428              $tables[$tablename] = $t;
429          }
430      }
431 
432      ///////////////////////////////////////////////////////////////
433      // Now that all tables have been parsed one time, we need to
434      // make a few more passes to determine relationship and linked
435      // tables.
436      ///////////////////////////////////////////////////////////////
437      foreach ($tables as $tablename=>$table)
438      {
439          if ($table["type"]=="RELATIONSHIP")
440          {
441              ///////////////////////////////////////////////////////////////
442              // The linked_tables array holds tables that are linked to the
443              // given table.  For example, since there is a relationship
444              // table named cor_role_user, the table cor_user is linked to
445              // cor_role, and vice-versa.  The linked_columns array holds
446              // the columns that are set as foreign keys from the
447              // relationship table.
448              ///////////////////////////////////////////////////////////////
449              $linked_tables = array();
450              $linked_columns = array();
451 
452              ///////////////////////////////////////////////////////////////
453              // The referenced_tables array holds tables that are standard
454              // tables foreign key'd by this relationship table.
455              ///////////////////////////////////////////////////////////////
456              foreach ($table["referenced_tables"] as $index=>$referenced_table)
457              {
458                  if (($tables[$referenced_table]["type"]=="STANDARD" || $tables[$referenced_table]["type"]=="COLUMN_EXTENSION" || $tables[$referenced_table]["type"]=="RELATIONSHIP") && $table["column_list"][$table["fk_columns"][$index]]["is_primary"])
459                  {
460                      ///////////////////////////////////////////////////////////////
461                      // The linked_relationship_tables array holds tables that are
462                      // relationship tables foreign key'd to a given standard table.
463                      // It is the reverse of referenced_tables.
464                      ///////////////////////////////////////////////////////////////
465                      $tables[$referenced_table]["linked_relationship_tables"][] = $tablename;
466                      $linked_tables[] = $referenced_table;
467 
468                      ///////////////////////////////////////////////////////////////
469                      // There referenced_columns array holds columns that correspond
470                      // to the referenced_tables array.  The arrays are in the same
471                      // order so the element correspond.
472                      ///////////////////////////////////////////////////////////////
473                      $linked_columns[] = $table["referenced_columns"][$index];
474                  }
475              }
476 
477              ///////////////////////////////////////////////////////////////
478              // Set the linked_tables and linked_columns if they were found
479              // above, unless the relationship is self_referential.
480              ///////////////////////////////////////////////////////////////
481              if (count($linked_tables)==2 && $linked_tables[0]!=$linked_tables[1])
482              {
483                  $tables[$linked_tables[0]]["linked_tables"][$tablename] = $linked_tables[1];
484                  $tables[$linked_tables[1]]["linked_tables"][$tablename] = $linked_tables[0];
485                  $tables[$linked_tables[0]]["linked_columns"][$linked_tables[1]] = $linked_columns[1];
486                  $tables[$linked_tables[1]]["linked_columns"][$linked_tables[0]] = $linked_columns[0];
487              }
488          }
489          else if ($table["type"]=="STANDARD")
490          {
491              ///////////////////////////////////////////////////////////////
492              // Set the parent table for any child table.
493              ///////////////////////////////////////////////////////////////
494              foreach ($table["referenced_tables"] as $index=>$referenced_table)
495              {
496                  if ($tables[$referenced_table]["type"]=="STANDARD" && $table["column_list"][$table["fk_columns"][$index]]["is_primary"])
497                  {
498                      $tables[$tablename]["parent_tablename"] = $referenced_table;
499                      $tables[$referenced_table]["is_parent"] = true;
500                  }
501              }
502          }
503      }
504 
505      ///////////////////////////////////////////////////////////////
506      // Now that all tables have been parsed 2 times and all linked
507      // tables and referenced tables have been set, set the parent
508      // property to hold the data array of the parent table, and
509      // not just the name of the parent table.
510      ///////////////////////////////////////////////////////////////
511      foreach ($tables as $tablename=>$data)
512      {
513          if (!is_null($data["parent_tablename"]))
514          {
515              $parents = array();
516              $parent = $tables[$data["parent_tablename"]];
517              while (!is_null($parent["name"]))
518              {
519                  $parents[] = $parent["name"];
520                  if (!is_null($parent["parent_tablename"]))
521                  {
522                      $parent = $tables[$parent["parent_tablename"]];
523                  }
524                  else
525                  {
526                      break;
527                  }
528              }
529              $parents = array_reverse($parents);
530              array_shift($parents);
531              if (count($parents)>0)
532              {
533                  foreach ($parents as $parent_tablename)
534                  {
535                      $tables[$parent_tablename]["parent"] = new datatable($tables[$tables[$parent_tablename]["parent_tablename"]]);
536                  }
537              }
538              $tables[$tablename]["parent"] = new datatable($tables[$data["parent_tablename"]]);
539          }
540      }
541 
542      ///////////////////////////////////////////////////////////////
543      // Now that all parent tables have been set to hold the data
544      // array of the parent, set the collapsed columns and unique
545      // keys of each child to contain all of its parents (recursive)
546      // properties.
547      ///////////////////////////////////////////////////////////////
548      foreach ($tables as $tablename=>$data)
549      {
550          if ($data["is_child"])
551          {
552              $childname = $data["name"];
553              while (array_key_exists($childname,$tables))
554              {
555                  $child = $tables[$childname];
556                  foreach ($child["column_list"] as $colname=>$coldata)
557                  {
558                      $tables[$tablename]["collapsed_column_list"][$colname] = $coldata;
559                  }
560                  foreach ($child["unique_keys"] as $colname)
561                  {
562                      $tables[$tablename]["collapsed_unique_keys"][$colname] = $colname;
563                  }
564                  $childname = $child["parent_tablename"];
565              }
566          }
567      }
568 
569      ///////////////////////////////////////////////////////////////
570      // Write the contents of the table array to a text file for
571      // debugging.
572      ///////////////////////////////////////////////////////////////
573      if (!file_exists(SITES . "helix/generator"))
574      {
575          mkdir(SITES . "helix/generator");
576      }
577      file_put_contents(SITES . "helix/generator/tables-array.txt",print_r($tables,true));
578      echo "Done parsing tables at " . timestamp(); br();
579 
580      ///////////////////////////////////////////////////////////////
581      // Convert each table in the tables array to a datatable object
582      // so that it can be used with the current generator code.
583      // This is really for backwards-compatability with the old
584      // generator code, and hopefully won't be necessary in the
585      // future.
586      ///////////////////////////////////////////////////////////////
587      foreach ($tables as $tablename=>$data)
588      {
589          $this->tables[$tablename] = new datatable($data);
590      }
591  }
publicreplace ( old , new )[ generator ]
Parameters
requiredold
requirednew
Code
822  public function replace($old, $new) {
823      $this->output = str_replace ( $old, $new, $this->output );
824  }
publicwrite_classes ( table )[ generator ]
Parameters
requiredtable
Code
673  public function write_classes($table) {
674      $this->write_db_class ( $table );
675      $this->write_cus_class ( $table );
676      $this->write_ext_class ( $table );
677      $this->write_final_class ( $table );
678  }
publicwrite_cus_class ( table )[ generator ]
Parameters
requiredtable
Code
756  public function write_cus_class($table) {
757      if (is_dir ( MODULES . $table->prefix )) {
758          $this->outdir = SITES . "helix/generator/modules/";
759      } else {
760          $this->outdir = SITES . "helix/generator/sites/";
761      }
762      $infile = "skeleton_cus.php";
763      $inpath = SITES . "helix/resources/docs/$infile";
764      $outpath = $this->outdir . "$table->prefix/lib/custom/$table->class" . "_cus.php";
765      $input = file_get_contents ( $inpath );
766      $output = $input;
767 
768      $output = str_replace ( "%CLASSNAME%", $table->class, $output );
769      $output = str_replace ( "%RELATED_OBJECT_PROPERTIES%", $this->generate_related_object_properties ( $table, "custom" ), $output );
770 
771      file_put_contents ( $outpath, $output );
772  }
publicwrite_db_class ( table )[ generator ]
Parameters
requiredtable
Code
680  public function write_db_class($table) {
681      if (is_dir ( MODULES . $table->prefix )) {
682          $this->outdir = SITES . "helix/generator/modules/";
683      } else {
684          $this->outdir = SITES . "helix/generator/sites/";
685      }
686      switch ($table->type) {
687          case "STANDARD" :
688          case "LOOKUP" :
689          case "CUSTOM" :
690          case "STANDARD_TYPE" :
691          case "RELATIONSHIP_TYPE" :
692          case "COLUMN_EXTENSION" :
693              $infile = "skeleton_db.php";
694              break;
695          case "RELATIONSHIP" :
696          case "MULTIPLE_RELATIONSHIP" :
697              $infile = "skeleton_relationship_db.php";
698              break;
699          case "RESOURCE" :
700              $infile = "skeleton_db.php";
701              break;
702          default :
703              debug("[$table->type] is not a valid type for table: $table->name");
704      }
705      $inpath = SITES . "helix/resources/docs/$infile";
706      $outpath = $this->outdir . "$table->prefix/lib/database/$table->class" . "_db.php";
707      $this->input = file_get_contents ( $inpath );
708      $this->output = $this->input;
709 
710      $this->replace ( "%CLASSNAME%", $table->class );
711      $this->replace ( "%TABLENAME%", $table->name );
712      $this->replace ( "%EXTENDS_PARENT%", "extends " . (is_null ( $table->parent ) ? "obj" : $table->parent->class) );
713      $this->replace ( "%PRIMARY_KEY%", $table->primary_key );
714      $this->replace ( "%PRIMARY_KEY_PARAM%", $this->generate_primary_key_param ( $table ) );
715      $this->replace ( "%GET_ALL_PARAM%", $this->generate_get_all_param ( $table ) );
716      $this->replace ( "%GET_ALL_PARAM_TO_PASS%", $this->generate_get_all_param_to_pass ( $table ) );
717      $this->replace ( "%PRIMARY_KEY_FILTER%", $this->generate_primary_key_filter ( $table ) );
718      $this->replace ( "%PARENT_CONSTRUCT%", $table->is_child ? "" /*\t\tparent::__construct(\$__$table->primary_key);\n"*/ : "" );
719      $this->replace ( "%PARENT_INSERT%", $table->is_child ? "\t\tparent::insert();\n" : "" );
720      $this->replace ( "%PARENT_UPDATE%", $table->is_child ? "\t\tparent::update();\n" : "" );
721      $this->replace ( "%PARENT_DELETE%", $table->is_child ? "\t\tparent::delete();\n" : "" );
722      $this->replace ( "%PARENT_UNDELETE%", $table->is_child ? "\t\tparent::undelete();\n" : "" );
723      $this->replace ( "%PARENT_PURGE%", $table->is_child ? "\t\tparent::purge();\n" : "" );
724      $this->replace ( "%SEARCH_CLAUSE%", $this->generate_search_clause ( $table ) );
725      $this->replace ( "%CLASS_PROPERTIES%", $this->generate_class_properties ( $table ) );
726      $this->replace ( "%CLASS_PROPERTY_DEFAULTS%", $this->generate_class_property_defaults ( $table ) );
727      $this->replace ( "%DATABASE_PROPERTY_VALUES%", $this->generate_database_property_values ( $table ) );
728      $this->replace ( "%RECONSTRUCT_PARENT_INITIALIZE%", $this->generate_reconstruct_parent_initialize ( $table ) );
729      $this->replace ( "%RECONSTRUCT_PARENT%", $this->generate_reconstruct_parent ( $table ) );
730      $this->replace ( "%SET_INSERT_ID%", $table->is_child ? "" : "\n\t\t\$this->_id = \$db->insert_id;" );
731      $this->replace ( "%SET_INSERT_PRIMARY_KEY%", ($table->is_child ? "\t\t\$this->_" . $table->primary_key . " = \$this->id;\n" : "") );
732      $this->replace ( "%INSERT_COLUMN_NAMES%", $this->generate_insert_column_names ( $table ) );
733      $this->replace ( "%INSERT_COLUMN_VALUES%", $this->generate_insert_column_values ( $table ) );
734      $this->replace ( "%UPDATE_COLUMNS%", $this->generate_update_columns ( $table ) );
735      $this->replace ( "%TO_STRING%", $this->generate_to_string ( $table ) );
736      $this->replace ( "%ORDER_BY%", $this->generate_order_by ( $table ) );
737      $this->replace ( "%PROPERTY_MAP%", $this->generate_map ( $table ) );
738      $this->replace ( "%PRIMARY_KEY_CONSTRUCTOR%", $this->generate_primary_key_constructor ( $table ) );
739      $this->replace ( "%NULL_PARAM_CHECK%", $this->generate_null_param_check ( $table ) );
740      $this->replace ( "%PARENT_JOIN_CONSTRUCTOR%", $this->generate_parent_join_constructor ( $table ) );
741      $this->replace ( "%QUERY_COLUMN_LIST%", $this->generate_query_column_list ( $table ) );
742      $this->replace ( "%UNIQUE_KEY_PARAMS%", $this->generate_unique_key_params ( $table ) );
743      $this->replace ( "%RESOURCE_PARAMS%", $this->generate_resource_params ( $table ) );
744      $this->replace ( "%RESOURCE_CONSTRUCTORS%", $this->generate_resource_constructors ( $table ) );
745      $this->replace ( "%UNIQUE_KEY_CONSTRUCTORS%", $this->generate_unique_key_constructors ( $table ) );
746      $this->replace ( "%RELATED_OBJECT_PROPERTIES%", $this->generate_related_object_properties ( $table, "database" ) );
747      $this->replace ( "%JOIN_PARENT_OBJECTS%", $this->generate_join_parent_objects ( $table ) );
748      $this->replace ( "%SEARCH_JOIN_PARENT_OBJECTS%", $this->generate_search_join_parent_objects ( $table ) );
749      $this->replace ( "%GET_WHERE_CLAUSE%", $this->generate_get_where_clause ( $table ) );
750      $this->replace ( "%DELETE_FILTER_PARAMS%", $this->generate_delete_filter_params ( $table ) );
751      $this->replace ( "%DELETE_FILTER_CLAUSE%", $this->generate_delete_filter_clause ( $table ) );
752 
753      file_put_contents ( $outpath, $this->output );
754  }
publicwrite_ext_class ( table )[ generator ]
Parameters
requiredtable
Code
774  public function write_ext_class($table) {
775      if (is_dir ( MODULES . $table->prefix )) {
776          $this->outdir = SITES . "helix/generator/modules/";
777      } else {
778          $this->outdir =