Wrote a script to create admin users

git-svn-id: https://rosehill.googlecode.com/svn/trunk@75 100bd78a-fc82-11de-b5bc-ffd2847a4b57
This commit is contained in:
inghamn 2013-08-01 20:20:03 +00:00
parent 703f49b711
commit ad7a8c688d
7 changed files with 38 additions and 1242 deletions

View File

@ -0,0 +1,38 @@
<?php
/**
* Use this script to create an initial Administrator account.
*
* This script should only be used by people with root access
* on the web server.
* If you are planning on using local authentication, you must
* provide a password here. The password will be encrypted when
* the new person's account is saved
*
* If you are doing Employee or CAS authentication you do
* not need to save a password into the database.
*
* @copyright 2011-2013 City of Bloomington, Indiana
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
* @author Cliff Ingham <inghamn@bloomington.in.gov>
*/
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();

View File

@ -1,330 +0,0 @@
<?php
/**
* @copyright 2006-2009 City of Bloomington, Indiana
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
* @author Cliff Ingham <inghamn@bloomington.in.gov>
*/
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 = "<div class=\"interfaceBox\">
<h1>
<?php
if (userIsAllowed('$acl_resource')) {
echo \"
<button type=\\\"button\\\" class=\\\"add\\\" onclick=\\\"document.location.href='\".BASE_URL.\"/$tableName/add$className.php';\\\">
Add
</button>
\";
}
?>
{$className}s
</h1>
<ul><?php
foreach (\$this->{$variableName}List as \${$variableName}) {
\$editButton = '';
if (userIsAllowed('$acl_resource')) {
\$url = new URL(BASE_URL.'/$tableName/update$className.php');
\$url->$key = \${$variableName}->{$getId}();
\$editButton = \"
<button type=\\\"button\\\" class=\\\"edit\\\" onclick=\\\"document.location.href='\$url';\\\">
Edit
</button>
\";
}
echo \"<li>\$editButton \$$variableName</li>\";
}
?>
</ul>
</div>";
$contents = "<?php\n";
$contents.= COPYRIGHT;
$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 = "<h1>Add $className</h1>
<form method=\"post\" action=\"<?php echo \$_SERVER['SCRIPT_NAME']; ?>\">
<fieldset><legend>$className Info</legend>
<table>
";
foreach ($fields as $field) {
if ($field['field'] != $key) {
$fieldFunctionName = ucwords($field['field']);
switch ($field['type']) {
case 'date':
$HTML.="
<tr><td><label for=\"{$variableName}-$field[field]-mon\">$field[field]</label></td>
<td><select name=\"{$variableName}[$field[field]][mon]\" id=\"{$variableName}-$field[field]-mon\">
<option></option>
<?php
\$now = getdate();
for (\$i=1; \$i<=12; \$i++) {
\$selected = (\$i==\$now['mon']) ? 'selected=\"selected\"' : '';
echo \"<option \$selected>\$i</option>\";
}
?>
</select>
<select name=\"{$variableName}[$field[field]][mday]\">
<option></option>
<?php
for (\$i=1; \$i<=31; \$i++) {
\$selected = (\$i==\$now['mday']) ? 'selected=\"selected\"' : '';
echo \"<option \$selected>\$i</option>\";
}
?>
</select>
<input name=\"{$variableName}[$field[field]][year]\" id=\"{$variableName}-$field[field]-year\" size=\"4\" maxlength=\"4\" value=\"<?php echo \$now['year']; ?>\" />
</td>
</tr>";
break;
case 'datetime':
case 'timestamp':
$HTML.="
<tr><td><label for=\"{$variableName}-$field[field]-mon\">$field[field]</label></td>
<td><select name=\"{$variableName}[$field[field]][mon]\" id=\"{$variableName}-$field[field]-mon\">
<option></option>
<?php
\$now = getdate();
for (\$i=1; \$i<=12; \$i++) {
\$selected = (\$i==\$now['mon']) ? 'selected=\"selected\"' : '';
echo \"<option \$selected>\$i</option>\";
}
?>
</select>
<select name=\"{$variableName}[$field[field]][mday]\">
<option></option>
<?php
for (\$i=1; \$i<=31; \$i++) {
\$selected = (\$i==\$now['mday']) ? 'selected=\"selected\"' : '';
echo \"<option \$selected>\$i</option>\";
}
?>
</select>
<input name=\"{$variableName}[$field[field]][year]\" id=\"{$variableName}-$field[field]-year\" size=\"4\" maxlength=\"4\" value=\"<?php echo \$now['year']; ?>\" />
<select name=\"{$variableName}[$field[field]][hours]\" id=\"{$variableName}-$field[field]-hours\">
<?php
for (\$i=0; \$i<=23; \$i++) {
\$selected = (\$i==\$now['hours']) ? 'selected=\"selected\"' : '';
echo \"<option \$selected>\$i</option>\";
}
?>
</select>
<select name=\"{$variableName}[$field[field]][minutes]\" id=\"{$variableName}-$field[field]-minutes\">
<?php
for (\$i=0; \$i<=59; \$i+=15) {
\$selected = (\$i==\$now['minutes']) ? 'selected=\"selected\"' : '';
echo \"<option \$selected>\$i</option>\";
}
?>
</select>
</td>
</tr>";
break;
case 'text':
$HTML.= "
<tr><td><label for=\"{$variableName}-$field[field]\">$field[field]</label></td>
<td><textarea name=\"{$variableName}[$field[field]]\" id=\"{$variableName}-$field[field]\" rows=\"3\" cols=\"60\"></textarea>
</td>
</tr>
";
break;
default:
$HTML.= "
<tr><td><label for=\"{$variableName}-$field[field]\">$field[field]</label></td>
<td><input name=\"{$variableName}[$field[field]]\" id=\"{$variableName}-$field[field]\" />
</td>
</tr>
";
}
}
}
$HTML.= "
</table>
<button type=\"submit\" class=\"submit\">Submit</button>
<button type=\"button\" class=\"cancel\" onclick=\"document.location.href='<?php echo BASE_URL; ?>/{$variableName}s';\">
Cancel
</button>
</fieldset>
</form>";
$contents = "<?php\n";
$contents.= COPYRIGHT;
$contents.="
?>
$HTML";
file_put_contents("$dir/add{$className}Form.inc",$contents);
/**
* Generate the Update Form
*/
$HTML = "<h1>Update $className</h1>
<form method=\"post\" action=\"<?php echo \$_SERVER['SCRIPT_NAME']; ?>\">
<fieldset><legend>$className Info</legend>
<input name=\"$key\" type=\"hidden\" value=\"<?php echo \$this->{$variableName}->{$getId}(); ?>\" />
<table>
";
foreach ($fields as $field) {
if ($field['field'] != $key) {
$fieldFunctionName = ucwords($field['field']);
switch ($field['type']) {
case 'date':
$HTML.="
<tr><td><label for=\"{$variableName}-$field[field]-mon\">$field[field]</label></td>
<td><select name=\"{$variableName}[$field[field]][mon]\" id=\"{$variableName}-$field[field]-mon\">
<option></option>
<?php
\$$field[field] = \$this->{$variableName}->dateStringToArray(\$this->{$variableName}->get$fieldFunctionName());
for (\$i=1; \$i<=12; \$i++) {
\$selected = (\$i==\$$field[field]['mon']) ? 'selected=\"selected\"' : '';
echo \"<option \$selected>\$i</option>\";
}
?>
</select>
<select name=\"{$variableName}[$field[field]][mday]\">
<option></option>
<?php
for (\$i=1; \$i<=31; \$i++) {
\$selected = (\$i==\$$field[field]['mday']) ? 'selected=\"selected\"' : '';
echo \"<option \$selected>\$i</option>\";
}
?>
</select>
<input name=\"{$variableName}[$field[field]][year]\" id=\"{$variableName}-$field[field]-year\" size=\"4\" maxlength=\"4\" value=\"<?php echo \$$field[field]['year']; ?>\" />
</td>
</tr>";
break;
case 'datetime':
case 'timestamp':
$HTML.="
<tr><td><label for=\"{$variableName}-$field[field]-mon\">$field[field]</label></td>
<td><select name=\"{$variableName}[$field[field]][mon]\" id=\"{$variableName}-$field[field]-mon\">
<option></option>
<?php
\$$field[field] = \$this->{$variableName}->dateStringToArray(\$this->{$variableName}->get$fieldFunctionName());
for (\$i=1; \$i<=12; \$i++) {
\$selected = (\$i==\$$field[field]['mon']) ? 'selected=\"selected\"' : '';
echo \"<option \$selected>\$i</option>\";
}
?>
</select>
<select name=\"{$variableName}[$field[field]][mday]\">
<option></option>
<?php
for (\$i=1; \$i<=31; \$i++) {
\$selected = (\$i==\$$field[field]['mday']) ? 'selected=\"selected\"' : '';
echo \"<option \$selected>\$i</option>\";
}
?>
</select>
<input name=\"{$variableName}[$field[field]][year]\" id=\"{$variableName}-$field[field]-year\" size=\"4\" maxlength=\"4\" value=\"<?php echo \$$field[field]['year']; ?>\" />
<select name=\"{$variableName}[$field[field]][hours]\" id=\"{$variableName}-$field[field]-hours\">
<?php
for (\$i=0; \$i<=23; \$i++) {
\$selected = (\$i==\$$field[field]['hours']) ? 'selected=\"selected\"' : '';
echo \"<option \$selected>\$i</option>\";
}
?>
</select>
<select name=\"{$variableName}[$field[field]][minutes]\" id=\"{$variableName}-$field[field]-minutes\">
<?php
for (\$i=0; \$i<=59; \$i+=15) {
\$selected = (\$i==\$$field[field]['minutes']) ? 'selected=\"selected\"' : '';
echo \"<option \$selected>\$i</option>\";
}
?>
</select>
</td>
</tr>";
break;
case 'text':
$HTML.= "
<tr><td><label for=\"{$variableName}-$field[field]\">$field[field]</label></td>
<td><textarea name=\"{$variableName}[$field[field]]\" id=\"{$variableName}-$field[field]\" rows=\"3\" cols=\"60\"><?php echo \$this->{$variableName}->get$fieldFunctionName(); ?></textarea>
</td>
</tr>
";
break;
default:
$HTML.= "
<tr><td><label for=\"{$variableName}-$field[field]\">$field[field]</label></td>
<td><input name=\"{$variableName}[$field[field]]\" id=\"{$variableName}-$field[field]\" value=\"<?php echo \$this->{$variableName}->get$fieldFunctionName(); ?>\" />
</td>
</tr>
";
}
}
}
$HTML.= "
</table>
<button type=\"submit\" class=\"submit\">Submit</button>
<button type=\"button\" class=\"cancel\" onclick=\"document.location.href='<?php echo BASE_URL; ?>/{$variableName}s';\">
Cancel
</button>
</fieldset>
</form>";
$contents = "<?php\n";
$contents.= COPYRIGHT;
$contents.="
?>
$HTML";
file_put_contents("$dir/update{$className}Form.inc",$contents);
echo "$className\n";
}

View File

@ -1,382 +0,0 @@
<?php
/**
* Generates ActiveRecord class files for each of the database tables
*
* @copyright 2006-2009 City of Bloomington, Indiana
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
* @author Cliff Ingham <inghamn@bloomington.in.gov>
*/
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 = "<?php\n";
$contents.= COPYRIGHT;
$contents.= "
class $className
{
$properties
$constructor
/**
* Throws an exception if anything's wrong
* @throws Exception \$e
*/
public function validate()
{
// Check for required fields here. Throw an exception if anything is missing.
}
/**
* Saves this record back to the database
*/
public function save()
{
\$this->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";
}

View File

@ -1,135 +0,0 @@
<?php
/**
* @copyright 2006-2009 City of Bloomington, Indiana
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
* @author Cliff Ingham <inghamn@bloomington.in.gov>
*/
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 = "<?php\n";
$contents.= COPYRIGHT."\n";
$contents.= $PHP;
$dir = APPLICATION_HOME."/scripts/stubs/html/$tableName";
if (!is_dir($dir)) {
mkdir($dir,0770,true);
}
file_put_contents("$dir/home.php",$contents);
/**
* Generate the Add controller
*/
$PHP = "
if (!userIsAllowed('$acl_resource')) {
\$_SESSION['errorMessages'][] = new Exception('noAccessAllowed');
header('Location: '.BASE_URL.'/$tableName');
exit();
}
if (isset(\$_POST['{$variableName}'])) {
\${$variableName} = new {$className}();
foreach (\$_POST['{$variableName}'] as \$field=>\$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 = "<?php\n";
$contents.= COPYRIGHT."\n";
$contents.= $PHP;
file_put_contents("$dir/add{$className}.php",$contents);
/**
* Generate the Update controller
*/
$PHP = "
if (!userIsAllowed('$acl_resource')) {
\$_SESSION['errorMessages'][] = new Exception('noAccessAllowed');
header('Location: '.BASE_URL.'/$tableName');
exit();
}
\${$variableName} = new {$className}(\$_REQUEST['$key']);
if (isset(\$_POST['$variableName'])) {
foreach (\$_POST['$variableName'] as \$field=>\$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 = "<?php\n";
$contents.= COPYRIGHT."\n";
$contents.= $PHP;
file_put_contents("$dir/update{$className}.php",$contents);
echo "$className\n";
}

View File

@ -1,138 +0,0 @@
<?php
/**
* Generates a Collection class for each the ActiveRecord objects
*
* @copyright 2006-2009 City of Bloomington, Indiana
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
* @author Cliff Ingham <inghamn@bloomington.in.gov>
*/
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 = "<?php
/**
* A collection class for $className objects
*
* This class creates a zend_db select statement.
* ZendDbResultIterator handles iterating and paginating those results.
* As the results are iterated over, ZendDbResultIterator will pass each desired
* row back to this class's loadResult() which will be responsible for hydrating
* each $className object
*
* Beyond the basic \$fields handled, you will need to write your own handling
* of whatever extra \$fields you need
*/
";
$contents.= COPYRIGHT;
$contents.="
class {$className}List extends ZendDbResultIterator
{
/**
* Creates a basic select statement for the collection.
*
* Populates the collection if you pass in \$fields
* Setting itemsPerPage turns on pagination mode
* In pagination mode, this will only load the results for one page
*
* @param array \$fields
* @param int \$itemsPerPage Turns on Pagination
* @param int \$currentPage
*/
public function __construct(\$fields=null,\$itemsPerPage=null,\$currentPage=null)
{
parent::__construct(\$itemsPerPage,\$currentPage);
if (is_array(\$fields)) {
\$this->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";
}

View File

@ -1,250 +0,0 @@
<?php
/**
* @copyright 2008-2009 City of Bloomington, Indiana
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
* @author Cliff Ingham <inghamn@bloomington.in.gov>
*/
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 = "<?php
require_once 'PHPUnit/Framework.php';
class {$className}UnitTest extends PHPUnit_Framework_TestCase
{
public function testValidate()
{
\${$variable} = new {$className}();
try {
\${$variable}->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 = "<?php
require_once 'PHPUnit/Framework.php';
class {$className}DbTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
\$dir = dirname(__FILE__);
exec('/usr/local/mysql/bin/mysql -u '.DB_USER.' -p'.DB_PASS.' '.DB_NAME.\" < \$dir/../testData.sql\");
}
public function testSaveLoad()
{
\${$variable} = new {$className}();
\${$variable}->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 = "<?php
require_once 'PHPUnit/Framework.php';
class {$className}ListDbTest extends PHPUnit_Framework_TestCase
{
protected function setUp()
{
\$dir = dirname(__FILE__);
exec('/usr/local/mysql/bin/mysql -u '.DB_USER.' -p'.DB_PASS.' '.DB_NAME.\" < \$dir/../testData.sql\");
}
/**
* Makes sure find returns all $tableName ordered correctly by default
*/
public function testFindOrderedByName()
{
\$PDO = Database::getConnection();
\$query = \$PDO->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 = "<?php
require_once 'PHPUnit/Framework.php';
require_once 'UnitTests.php';
require_once 'DatabaseTests.php';
class AllTests extends PHPUnit_Framework_TestSuite
{
public static function suite()
{
\$suite = new AllTests('".APPLICATION_NAME."');
\$suite->addTest(UnitTests::suite());
\$suite->addTest(DatabaseTests::suite());
return \$suite;
}
}
";
file_put_contents("$dir/AllTests.php",$contents);
//------------------------------------------------------------------------------
// Generate the All Tests Suite
//------------------------------------------------------------------------------
$contents = "<?php\nrequire_once 'PHPUnit/Framework.php';\n\n";
foreach ($classes as $className) {
$contents.= "require_once 'DatabaseTests/{$className}DbTest.php';\n";
$contents.= "require_once 'DatabaseTests/{$className}ListDbTest.php';\n";
}
$contents.= "
class DatabaseTests extends PHPUnit_Framework_TestSuite
{
protected function setUp()
{
\$dir = dirname(__FILE__);
exec('/usr/local/mysql/bin/mysql -u '.DB_USER.' -p'.DB_PASS.' '.DB_NAME.\" < \$dir/testData.sql\");
}
protected function tearDown()
{
\$dir = dirname(__FILE__);
exec('/usr/local/mysql/bin/mysql -u '.DB_USER.' -p'.DB_PASS.' '.DB_NAME.\" < \$dir/testData.sql\");
}
public static function suite()
{
\$suite = new DatabaseTests('".APPLICATION_NAME." Classes');
";
foreach ($classes as $className) {
$contents.= "\t\t\$suite->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 = "<?php\nrequire_once 'PHPUnit/Framework.php';\n\n";
foreach ($classes as $className) {
$contents.= "require_once 'UnitTests/{$className}UnitTest.php';\n";
}
$contents.= "
class UnitTests extends PHPUnit_Framework_TestSuite
{
public static function suite()
{
\$suite = new UnitTests('".APPLICATION_NAME." Classes');
";
foreach ($classes as $className) {
$contents.= "\t\t\$suite->addTestSuite('{$className}UnitTest');\n";
}
$contents.= "
return \$suite;
}
}
";
file_put_contents("$dir/UnitTests.php",$contents);

View File

@ -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