diff --git a/scripts/createAdminUser.php b/scripts/createAdminUser.php new file mode 100644 index 0000000..c891ebf --- /dev/null +++ b/scripts/createAdminUser.php @@ -0,0 +1,38 @@ + + */ +include '../configuration.inc'; + +$person = new Person(); +$user = new User(); + +// Fill these out as needed +$person->setFirstname('Firstname'); +$person->setLastname('Lastname'); +$person->setEmail('email@somewhere'); + + +// You most likely want Administrator +$user->setUsername('username'); +$user->setAuthenticationMethod('local'); +$user->setPassword('whatever you like'); +$user->setRoles(array('Administrator')); + +$person->save(); +$user->setPerson($person); +$user->save(); diff --git a/scripts/generateBlocks.php b/scripts/generateBlocks.php deleted file mode 100644 index 94e3c66..0000000 --- a/scripts/generateBlocks.php +++ /dev/null @@ -1,330 +0,0 @@ - - */ -include '../configuration.inc'; -$zend_db = Database::getConnection(); - -foreach ($zend_db->listTables() as $tableName) { - $fields = array(); - $primary_keys = array(); - foreach ($zend_db->describeTable($tableName) as $row) { - $type = preg_replace("/[^a-z]/","",strtolower($row['DATA_TYPE'])); - - // Translate database datatypes into PHP datatypes - if (preg_match('/int/',$type)) { - $type = 'int'; - } - if (preg_match('/enum/',$type) || preg_match('/varchar/',$type)) { - $type = 'string'; - } - - $fields[] = array('field'=>$row['COLUMN_NAME'],'type'=>$type); - - if ($row['PRIMARY']) { - $primary_keys[] = $row['COLUMN_NAME']; - } - } - - // Only generate code for tables that have a single-column primary key - // Code for other tables will need to be created by hand - if (count($primary_keys) != 1) { - continue; - } - $key = $primary_keys[0]; - - $tableName = strtolower($tableName); - $className = Inflector::classify($tableName); - $variableName = Inflector::singularize($tableName); - $acl_resource = ucfirst($tableName); - - /** - * Generate the list block - */ - $getId = "get".ucwords($key); - $HTML = "
-

- - Add - - \"; - } - ?> - {$className}s -

- -
"; - -$contents = " -$HTML"; - - $dir = APPLICATION_HOME."/scripts/stubs/blocks/$tableName"; - if (!is_dir($dir)) { - mkdir($dir,0770,true); - } - file_put_contents("$dir/{$variableName}List.inc",$contents); - - -/** - * Generate the addForm - */ -$HTML = "

Add $className

-
\"> -
$className Info - -"; - foreach ($fields as $field) { - if ($field['field'] != $key) { - $fieldFunctionName = ucwords($field['field']); - switch ($field['type']) { - case 'date': - $HTML.=" - - - "; - break; - - case 'datetime': - case 'timestamp': - $HTML.=" - - - "; - break; - - case 'text': - $HTML.= " - - - - "; - break; - - default: - $HTML.= " - - - - "; - } - } - } - $HTML.= " -
- - \" /> -
- - \" /> - - -
-
-
- - - -
-
"; - -$contents = " -$HTML"; -file_put_contents("$dir/add{$className}Form.inc",$contents); - -/** - * Generate the Update Form - */ -$HTML = "

Update $className

-
\"> -
$className Info - {$variableName}->{$getId}(); ?>\" /> - -"; - foreach ($fields as $field) { - if ($field['field'] != $key) { - $fieldFunctionName = ucwords($field['field']); - switch ($field['type']) { - case 'date': - $HTML.=" - - - "; - break; - - case 'datetime': - case 'timestamp': - $HTML.=" - - - "; - break; - - case 'text': - $HTML.= " - - - - "; - break; - - default: - $HTML.= " - - - - "; - } - } - } - $HTML.= " -
- - \" /> -
- - \" /> - - -
-
{$variableName}->get$fieldFunctionName(); ?>\" /> -
- - - -
-
"; -$contents = " -$HTML"; -file_put_contents("$dir/update{$className}Form.inc",$contents); - -echo "$className\n"; -} diff --git a/scripts/generateClasses.php b/scripts/generateClasses.php deleted file mode 100644 index 177bc6c..0000000 --- a/scripts/generateClasses.php +++ /dev/null @@ -1,382 +0,0 @@ - - */ -include '../configuration.inc'; -$zend_db = Database::getConnection(); - -foreach ($zend_db->listTables() as $tableName) { - $fields = array(); - $primary_keys = array(); - foreach ($zend_db->describeTable($tableName) as $row) { - $type = preg_replace("/[^a-z]/","",strtolower($row['DATA_TYPE'])); - - // Translate database datatypes into PHP datatypes - if (preg_match('/int/',$type)) { - $type = 'int'; - } - if (preg_match('/enum/',$type) || preg_match('/varchar/',$type)) { - $type = 'string'; - } - - $fields[] = array('field'=>$row['COLUMN_NAME'],'type'=>$type); - - if ($row['PRIMARY']) { - $primary_keys[] = $row['COLUMN_NAME']; - } - } - - // Only generate code for tables that have a single-column primary key - // Code for other tables will need to be created by hand - if (count($primary_keys) != 1) { - continue; - } - $key = $primary_keys[0]; - - $tableName = strtolower($tableName); - $className = Inflector::classify($tableName); - //-------------------------------------------------------------------------- - // Constructor - //-------------------------------------------------------------------------- - $constructor = " - /** - * Populates the object with data - * - * Passing in an associative array of data will populate this object without - * hitting the database. - * - * Passing in a scalar will load the data from the database. - * This will load all fields in the table as properties of this class. - * You may want to replace this with, or add your own extra, custom loading - * - * @param int|array \$$key - */ - public function __construct(\$$key=null) - { - if (\$$key) { - if (is_array(\$$key)) { - \$result = \$$key; - } - else { - \$zend_db = Database::getConnection(); - \$sql = 'select * from $tableName where $key=?'; - \$result = \$zend_db->fetchRow(\$sql,array(\$$key)); - } - - if (\$result) { - foreach (\$result as \$field=>\$value) { - if (\$value) { - \$this->\$field = \$value; - } - } - } - else { - throw new Exception('$tableName/unknown$className'); - } - } - else { - // This is where the code goes to generate a new, empty instance. - // Set any default values for properties that need it here - } - } - "; - - //-------------------------------------------------------------------------- - // Properties - //-------------------------------------------------------------------------- - $properties = ''; - $linkedProperties = array(); - foreach ($fields as $field) { - $properties.= "\tprivate \$$field[field];\n"; - - if (substr($field['field'],-3) == '_id') { - $linkedProperties[] = $field['field']; - } - } - - if (count($linkedProperties)) { - $properties.="\n\n"; - foreach ($linkedProperties as $property) { - $field = substr($property,0,-3); - $properties.= "\tprivate \$$field;\n"; - } - } - - //-------------------------------------------------------------------------- - // Getters - //-------------------------------------------------------------------------- - $getters = ''; - foreach ($fields as $field) { - $fieldFunctionName = ucwords($field['field']); - - switch ($field['type']) - { - case 'date': - case 'datetime': - case 'timestamp': - $getters.= " - /** - * Returns the date/time in the desired format - * - * Format is specified using PHP's date() syntax - * http://www.php.net/manual/en/function.date.php - * If no format is given, the Date object is returned - * - * @param string \$format - * @return string|DateTime - */ - public function get$fieldFunctionName(\$format=null) - { - if (\$format && \$this->$field[field]) { - return \$this->$field[field]->format(\$format); - } - else { - return \$this->$field[field]; - } - } -"; - break; - - default: $getters.= " - /** - * @return $field[type] - */ - public function get$fieldFunctionName() - { - return \$this->$field[field]; - } -"; - } - } - - foreach ($linkedProperties as $property) { - $field = substr($property,0,-3); - $fieldFunctionName = ucwords($field); - $getters.= " - /** - * @return $fieldFunctionName - */ - public function get$fieldFunctionName() - { - if (\$this->$property) { - if (!\$this->$field) { - \$this->$field = new $fieldFunctionName(\$this->$property); - } - return \$this->$field; - } - return null; - } -"; - } - - - //-------------------------------------------------------------------------- - // Setters - //-------------------------------------------------------------------------- - $setters = ''; - foreach ($fields as $field) { - if ($field['field'] != $key) { - $fieldFunctionName = ucwords($field['field']); - switch ($field['type']) { - case 'int': - if (in_array($field['field'],$linkedProperties)) { - $property = substr($field['field'],0,-3); - $object = ucfirst($property); - $setters.= " - /** - * @param $field[type] \$$field[type] - */ - public function set$fieldFunctionName(\$$field[type]) - { - \$this->$property = new $object(\$int); - \$this->$field[field] = \$$field[type]; - } -"; - } - else { - $setters.= " - /** - * @param $field[type] \$$field[type] - */ - public function set$fieldFunctionName(\$$field[type]) - { - \$this->$field[field] = preg_replace(\"/[^0-9]/\",\"\",\$$field[type]); - } -"; - } - break; - - case 'string': - $setters.= " - /** - * @param $field[type] \$$field[type] - */ - public function set$fieldFunctionName(\$$field[type]) - { - \$this->$field[field] = trim(\$$field[type]); - } -"; - break; - - case 'date': - case 'datetime': - case 'timestamp': - $setters.= " - /** - * Sets the date - * - * Date arrays should match arrays produced by getdate() - * - * Date string formats should be in something strtotime() understands - * http://www.php.net/manual/en/function.strtotime.php - * - * @param int|string|array \$$field[type] - */ - public function set$fieldFunctionName(\$$field[type]) - { - if (\$$field[type]) { - \$this->$field[field] = new Date(\$$field[type]); - } - else { - \$this->$field[field] = null; - } - } -"; - break; - - case 'float': - $setters.= " - /** - * @param $field[type] \$$field[type] - */ - public function set$fieldFunctionName(\$$field[type]) - { - \$this->$field[field] = preg_replace(\"/[^0-9.\-]/\",\"\",\$$field[type]); - } -"; - break; - - case 'bool': - $setters.= " - /** - * @param boolean \$$field[type] - */ - public function set$fieldFunctionName(\$$field[type]) - { - \$this->$field[field] = \$$field[type] ? true : false; - } -"; - break; - - default: - $setters.= " - /** - * @param $field[type] \$$field[type] - */ - public function set$fieldFunctionName(\$$field[type]) - { - \$this->$field[field] = \$$field[type]; - } -"; - } - } - } - - foreach ($linkedProperties as $field) { - $property = substr($field,0,-3); - $object = ucfirst($property); - $setters.= " - /** - * @param $object \$$property - */ - public function set$object(\$$property) - { - \$this->$field = \${$property}->getId(); - \$this->$property = \$$property; - } -"; - } - - //-------------------------------------------------------------------------- - // Output the class - //-------------------------------------------------------------------------- -$contents = "validate(); - - \$data = array(); -"; - foreach ($fields as $field) { - if ($field['field'] != $key) { - $contents.="\t\t\$data['$field[field]'] = \$this->$field[field] ? \$this->$field[field] : null;\n"; - } - } -$contents.= " - if (\$this->$key) { - \$this->update(\$data); - } - else { - \$this->insert(\$data); - } - } - - private function update(\$data) - { - \$zend_db = Database::getConnection(); - \$zend_db->update('$tableName',\$data,\"$key='{\$this->$key}'\"); - } - - private function insert(\$data) - { - \$zend_db = Database::getConnection(); - \$zend_db->insert('$tableName',\$data); - \$this->$key = \$zend_db->lastInsertId('$tableName','$key'); - } - - //---------------------------------------------------------------- - // Generic Getters - //---------------------------------------------------------------- -$getters - //---------------------------------------------------------------- - // Generic Setters - //---------------------------------------------------------------- -$setters - - //---------------------------------------------------------------- - // Custom Functions - // We recommend adding all your custom code down here at the bottom - //---------------------------------------------------------------- -} -"; - $dir = APPLICATION_HOME.'/scripts/stubs/classes'; - if (!is_dir($dir)) { - mkdir($dir,0770,true); - } - file_put_contents("$dir/$className.php",$contents); - echo "$className\n"; -} diff --git a/scripts/generateControllers.php b/scripts/generateControllers.php deleted file mode 100644 index cb6e98b..0000000 --- a/scripts/generateControllers.php +++ /dev/null @@ -1,135 +0,0 @@ - - */ -include '../configuration.inc'; -$zend_db = Database::getConnection(); - -foreach ($zend_db->listTables() as $tableName) { - $fields = array(); - $primary_keys = array(); - foreach ($zend_db->describeTable($tableName) as $row) { - $type = preg_replace("/[^a-z]/","",strtolower($row['DATA_TYPE'])); - - // Translate database datatypes into PHP datatypes - if (preg_match('/int/',$type)) { - $type = 'int'; - } - if (preg_match('/enum/',$type) || preg_match('/varchar/',$type)) { - $type = 'string'; - } - - $fields[] = array('field'=>$row['COLUMN_NAME'],'type'=>$type); - - if ($row['PRIMARY']) { - $primary_keys[] = $row['COLUMN_NAME']; - } - } - - // Only generate code for tables that have a single-column primary key - // Code for other tables will need to be created by hand - if (count($primary_keys) != 1) { - continue; - } - $key = $primary_keys[0]; - - $tableName = strtolower($tableName); - $className = Inflector::classify($tableName); - $variableName = Inflector::singularize($tableName); - $acl_resource = ucfirst($tableName); - -/** - * Generate home.php - */ -$PHP = " -\${$variableName}List = new {$className}List(); -\${$variableName}List->find(); - -\$template = new Template(); -\$template->blocks[] = new Block('{$variableName}s/{$variableName}List.inc',array('{$variableName}List'=>\${$variableName}List)); -echo \$template->render();"; - -$contents = "\$value) { - \$set = 'set'.ucfirst(\$field); - \${$variableName}->\$set(\$value); - } - - try { - \${$variableName}->save(); - header('Location: '.BASE_URL.'/$tableName'); - exit(); - } - catch(Exception \$e) { - \$_SESSION['errorMessages'][] = \$e; - } -} - -\$template = new Template(); -\$template->blocks[] = new Block('{$variableName}s/add{$className}Form.inc'); -echo \$template->render();"; -$contents = "\$value) { - \$set = 'set'.ucfirst(\$field); - \${$variableName}->\$set(\$value); - } - - try { - \${$variableName}->save(); - header('Location: '.BASE_URL.'/$tableName'); - exit(); - } - catch (Exception \$e) { - \$_SESSION['errorMessages'][] = \$e; - } -} - -\$template = new Template(); -\$template->blocks[] = new Block('{$variableName}s/update{$className}Form.inc',array('{$variableName}'=>\${$variableName})); -echo \$template->render();"; -$contents = " - */ -include '../configuration.inc'; -$zend_db = Database::getConnection(); - -foreach ($zend_db->listTables() as $tableName) { - $fields = array(); - $primary_keys = array(); - foreach ($zend_db->describeTable($tableName) as $row) { - $type = preg_replace("/[^a-z]/","",strtolower($row['DATA_TYPE'])); - - // Translate database datatypes into PHP datatypes - if (preg_match('/int/',$type)) { - $type = 'int'; - } - if (preg_match('/enum/',$type) || preg_match('/varchar/',$type)) { - $type = 'string'; - } - - $fields[] = array('field'=>$row['COLUMN_NAME'],'type'=>$type); - - if ($row['PRIMARY']) { - $primary_keys[] = $row['COLUMN_NAME']; - } - } - - // Only generate code for tables that have a single-column primary key - // Code for other tables will need to be created by hand - if (count($primary_keys) != 1) { - continue; - } - $key = $primary_keys[0]; - - $tableName = strtolower($tableName); - $className = Inflector::classify($tableName); - - //-------------------------------------------------------------------------- - // Output the class - //-------------------------------------------------------------------------- -$contents = "find(\$fields); - } - } - - /** - * Populates the collection - * - * @param array \$fields - * @param string|array \$order Multi-column sort should be given as an array - * @param int \$limit - * @param string|array \$groupBy Multi-column group by should be given as an array - */ - public function find(\$fields=null,\$order='$key',\$limit=null,\$groupBy=null) - { - \$this->select->from('$tableName'); - - // Finding on fields from the $tableName table is handled here - if (count(\$fields)) { - foreach (\$fields as \$key=>\$value) { - \$this->select->where(\"\$key=?\",\$value); - } - } - - // Finding on fields from other tables requires joining those tables. - // You can handle fields from other tables by adding the joins here - // If you add more joins you probably want to make sure that the - // above foreach only handles fields from the $tableName table. - - \$this->select->order(\$order); - if (\$limit) { - \$this->select->limit(\$limit); - } - if (\$groupBy) { - \$this->select->group(\$groupBy); - } - \$this->populateList(); - } - - /** - * Hydrates all the $className objects from a database result set - * - * This is a callback function, called from ZendDbResultIterator. It is - * called once per row of the result. - * - * @param int \$key The index of the result row to load - * @return $className - */ - protected function loadResult(\$key) - { - return new $className(\$this->result[\$key]); - } -} -"; - $dir = APPLICATION_HOME.'/scripts/stubs/classes'; - if (!is_dir($dir)) { - mkdir($dir,0770,true); - } - file_put_contents("$dir/{$className}List.php",$contents); - echo "$className\n"; -} diff --git a/scripts/generateTests.php b/scripts/generateTests.php deleted file mode 100644 index faf0723..0000000 --- a/scripts/generateTests.php +++ /dev/null @@ -1,250 +0,0 @@ - - */ -include '../configuration.inc'; -$dir = APPLICATION_HOME.'/scripts/stubs/tests'; -if (!is_dir($dir)) { - mkdir($dir,0770,true); -} - -$dir = APPLICATION_HOME.'/scripts/stubs/tests/DatabaseTests'; -if (!is_dir($dir)) { - mkdir($dir,0770,true); -} - -$dir = APPLICATION_HOME.'/scripts/stubs/tests/UnitTests'; -if (!is_dir($dir)) { - mkdir($dir,0770,true); -} - -$dir = APPLICATION_HOME.'/scripts/stubs/tests'; - - -$zend_db = Database::getConnection(); -$classes = array(); -foreach ($zend_db->listTables() as $tableName) { - $fields = array(); - $primary_keys = array(); - foreach ($zend_db->describeTable($tableName) as $row) { - $type = preg_replace("/[^a-z]/","",strtolower($row['DATA_TYPE'])); - - // Translate database datatypes into PHP datatypes - if (preg_match('/int/',$type)) { - $type = 'int'; - } - if (preg_match('/enum/',$type) || preg_match('/varchar/',$type)) { - $type = 'string'; - } - - $fields[] = array('field'=>$row['COLUMN_NAME'],'type'=>$type); - - if ($row['PRIMARY']) { - $primary_keys[] = $row['COLUMN_NAME']; - } - } - - // Only generate code for tables that have a single-column primary key - // Code for other tables will need to be created by hand - if (count($primary_keys) != 1) { - continue; - } - $key = $primary_keys[0]; - - $tableName = strtolower($tableName); - $className = Inflector::classify($tableName); - $classes[] = $className; - - $variable = strtolower($className); - -//------------------------------------------------------------------------------ -// Generate the Unit Tests -//------------------------------------------------------------------------------ -$contents = "validate(); - \$this->fail('Missing name failed to throw exception'); - } - catch (Exception \$e) { - - } - - \${$variable}->setName('Test {$className}'); - \${$variable}->validate(); - } -} -"; -file_put_contents("$dir/UnitTests/{$className}UnitTest.php",$contents); - -//------------------------------------------------------------------------------ -// Generate the Database Tests -//------------------------------------------------------------------------------ -$contents = "setName('Test {$className}'); - try { - \${$variable}->save(); - \$id = \${$variable}->getId(); - \$this->assertGreaterThan(0,\$id); - } - catch (Exception \$e) { - \$this->fail(\$e->getMessage()); - } - - \${$variable} = new {$className}(\$id); - \$this->assertEquals(\${$variable}->getName(),'Test {$className}'); - - \${$variable}->setName('Test'); - \${$variable}->save(); - - \${$variable} = new {$className}(\$id); - \$this->assertEquals(\${$variable}->getName(),'Test'); - } -} -"; -file_put_contents("$dir/DatabaseTests/{$className}DbTest.php",$contents); - -//------------------------------------------------------------------------------ -// Generate the Database List Tests -//------------------------------------------------------------------------------ -$contents = "query('select id from $tableName order by id'); - \$result = \$query->fetchAll(); - - \$list = new {$className}List(); - \$list->find(); - \$this->assertEquals(\$list->getSort(),'id'); - - foreach (\$list as \$i=>\${$variable}) { - \$this->assertEquals(\${$variable}->getId(),\$result[\$i]['id']); - } - } -} -"; -file_put_contents("$dir/DatabaseTests/{$className}ListDbTest.php",$contents); - -echo "$className\n"; -} - -//------------------------------------------------------------------------------ -// Generate the All Tests Suite -//------------------------------------------------------------------------------ -$contents = "addTest(UnitTests::suite()); - \$suite->addTest(DatabaseTests::suite()); - return \$suite; - } -} -"; -file_put_contents("$dir/AllTests.php",$contents); - -//------------------------------------------------------------------------------ -// Generate the All Tests Suite -//------------------------------------------------------------------------------ -$contents = "addTestSuite('{$className}DbTest');\n"; - $contents.= "\t\t\$suite->addTestSuite('{$className}ListDbTest');\n"; -} -$contents.= " - return \$suite; - } -} -"; -file_put_contents("$dir/DatabaseTests.php",$contents); - -//------------------------------------------------------------------------------ -// Generate the Unit Tests Suite -//------------------------------------------------------------------------------ -$contents = "addTestSuite('{$className}UnitTest');\n"; -} -$contents.= " - return \$suite; - } -} -"; -file_put_contents("$dir/UnitTests.php",$contents); diff --git a/scripts/scaffold b/scripts/scaffold deleted file mode 100755 index 99aa99d..0000000 --- a/scripts/scaffold +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -PHP=/usr/local/bin/php -$PHP generateBlocks.php -$PHP generateControllers.php -$PHP generateClasses.php -$PHP generateLists.php -$PHP generateTests.php