Skip to content
This repository has been archived by the owner on May 21, 2020. It is now read-only.

Commit

Permalink
Add lib Zend, Symfony and Ionix. Add the database test, zend-form-gen…
Browse files Browse the repository at this point in the history
…erator cli and Example form files generated.
  • Loading branch information
ionixjunior committed Apr 30, 2012
0 parents commit dd07e1e
Show file tree
Hide file tree
Showing 2,917 changed files with 714,666 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .project
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>zend-form-generator</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
<nature>com.aptana.projects.webnature</nature>
</natures>
</projectDescription>
29 changes: 29 additions & 0 deletions Forms/Business.php
@@ -0,0 +1,29 @@
<?php

/**
* Application_Form_Business form file.
*/
class Application_Form_Business extends Zend_Form
{

public function __construct($options = null)
{
parent::__construct($options);

$this->setName('frmBusiness');
$this->setMethod('post');

$name = new Zend_Form_Element_Text('name');
$name->setLabel('Company name');
$name->setAttrib('maxlength', 100);
$name->setRequired(true);
$name->addValidator(new Zend_Validate_NotEmpty());
$this->addElement($name);

$submit = new Zend_Form_Element_Submit('bt_submit');
$submit->setLabel('Save');
$this->addElement($submit);
}


}
42 changes: 42 additions & 0 deletions Forms/Employee.php
@@ -0,0 +1,42 @@
<?php

/**
* Application_Form_Employee form file.
*/
class Application_Form_Employee extends Zend_Form
{

public function __construct(Array $dataBusinessId, $options = null)
{
parent::__construct($options);

$this->setName('frmEmployee');
$this->setMethod('post');

$name = new Zend_Form_Element_Text('name');
$name->setLabel('Employee name');
$name->setAttrib('maxlength', 80);
$name->setRequired(true);
$name->addValidator(new Zend_Validate_NotEmpty());
$this->addElement($name);

$age = new Zend_Form_Element_Text('age');
$age->setLabel('Employee age');
$age->addValidator(new Zend_Validate_Int());
$this->addElement($age);

$businessId = new Zend_Form_Element_Select('business_id');
$businessId->setLabel('Business');
$businessId->setRequired(true);
$businessId->addValidator(new Zend_Validate_NotEmpty());
$businessId->addValidator(new Zend_Validate_Int());
$businessId->addMultiOptions($dataBusinessId);
$this->addElement($businessId);

$submit = new Zend_Form_Element_Submit('bt_submit');
$submit->setLabel('Save');
$this->addElement($submit);
}


}
50 changes: 50 additions & 0 deletions README
@@ -0,0 +1,50 @@
This library is to create Zend Forms automatically based on information from the database.

Requirements for the usage:
PHP 5 >= 5.3.0

The forms files are created based on tables existing on database, so each table exist in database will be created a form file.

For the form files be created correctly, in the each column of tables need to be commented, and this comment is used in the label of field element of form file.

The creation of each field of the form file, some validations are considered. If the field type is string, the attribute maxlength is inserted on Zend Form Element. If the field type is integer, the Zend_Validate_Int is inserted in the Zend Form Element. If the field deny null values on column, the validator Zend_Validate_NotEmpty and option required will be inserted in the form element.

On each columns of tables on database, if the column exist a foreign key, automatically the form element inserted will be Zend_Form_Element_Select.

You can create the forms files the following ways:
- In the pattern suggested by Zend ( verify Zend manual section recommended structure )
- In your personal library ( using namespaces )



1) The database:

To generate the forms, you can use database MySQL or PostgreSQL. Configure your database file zend-form-generator.php and enjoy the library.

There are two test databases that you can check the files and "database-mysql.sql" and "database-postgresql.sql".

Remembering, you need to insert comment in all the columns of the database to the form generator function properly.



2) Run the library:

To generate your forms, just have to execute the following commands:

Generating forms inside the Forms folder using structure recommended by Zend:
$ php zend-form-generator.php generate-forms Forms


Generating forms inside the Forms folder using namespaces:
$ php zend-form-generator.php generate-forms Forms Name\Your\Namespace


Generating forms inside the Forms folder including the primary keys of tables.
$ php zend-form-generator.php generate-forms --primary-keys Forms
or
$ php zend-form-generator.php generate-forms --primary-keys Forms Name\Your\Namespace


Soon it will be added functionality to customize the forms for decorators.

Enjoy!
11 changes: 11 additions & 0 deletions autoloader.php
@@ -0,0 +1,11 @@
<?php

set_include_path(implode(PATH_SEPARATOR, array(
realpath(dirname(__FILE__) . '/library'),
get_include_path(),
)));

require_once 'Zend/Application.php';

$application = new \Zend_Application('development');
$application->setAutoloaderNamespaces(array('Symfony', 'Ionix'));
62 changes: 62 additions & 0 deletions databae-mysql.sql
@@ -0,0 +1,62 @@
-- phpMyAdmin SQL Dump
-- version 3.4.9
-- http://www.phpmyadmin.net
--
-- Servidor: 127.0.0.1
-- Tempo de Geração: 30/04/2012 às 00h05min
-- Versão do Servidor: 5.5.20
-- Versão do PHP: 5.3.9

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Banco de Dados: `zend_form_generator`
--

-- --------------------------------------------------------

--
-- Estrutura da tabela `business`
--

CREATE TABLE IF NOT EXISTS `business` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Company code',
`name` varchar(100) NOT NULL COMMENT 'Company name',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Estrutura da tabela `employee`
--

CREATE TABLE IF NOT EXISTS `employee` (
`id` int(11) NOT NULL COMMENT 'Employee code',
`name` varchar(80) NOT NULL COMMENT 'Employee name',
`age` int(11) DEFAULT NULL COMMENT 'Employee age',
`business_id` int(11) NOT NULL COMMENT 'Business',
PRIMARY KEY (`id`),
KEY `business_id` (`business_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Restrições para as tabelas dumpadas
--

--
-- Restrições para a tabela `employee`
--
ALTER TABLE `employee`
ADD CONSTRAINT `employee_ibfk_1` FOREIGN KEY (`business_id`) REFERENCES `business` (`id`);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
43 changes: 43 additions & 0 deletions database-postgres.sql
@@ -0,0 +1,43 @@
-- Table: employee

-- DROP TABLE employee;

CREATE TABLE employee
(
id serial NOT NULL, -- Employee code
"name" character varying(80) NOT NULL, -- Employee name
age integer, -- Employee age
business_id integer NOT NULL, -- Business
CONSTRAINT pk_employee_id PRIMARY KEY (id),
CONSTRAINT fk_employee_bussiness FOREIGN KEY (business_id)
REFERENCES business (id) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT
)
WITH (
OIDS=FALSE
);
ALTER TABLE employee OWNER TO postgres;
COMMENT ON COLUMN employee.id IS 'Employee code';
COMMENT ON COLUMN employee."name" IS 'Employee name';
COMMENT ON COLUMN employee.age IS 'Employee age';
COMMENT ON COLUMN employee.business_id IS 'Business';


-- Table: business

-- DROP TABLE business;

CREATE TABLE business
(
id serial NOT NULL, -- Company code
"name" character varying(100) NOT NULL, -- Company name
CONSTRAINT pk_bussiness_id PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE business OWNER TO postgres;
COMMENT ON COLUMN business.id IS 'Company code';
COMMENT ON COLUMN business."name" IS 'Company name';


100 changes: 100 additions & 0 deletions library/Ionix/Console/Command/Zend/Form/GenerateFormsCommand.php
@@ -0,0 +1,100 @@
<?php

namespace Ionix\Console\Command\Zend\Form;

use Symfony\Component\Console\Input\InputArgument,
Symfony\Component\Console\Input\InputOption,
Symfony\Component\Console;

/**
* Command to generate form classes.
*
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @author Ione Souza Junior <junior@ionixjunior.com.br>
*/
class GenerateFormsCommand extends Console\Command\Command
{
/**
* @see Console\Command\Command
*/
protected function configure()
{
$this
->setName('generate-forms')
->setDescription('Generate form classes from your database information.')
->setDefinition(array(
new InputArgument(
'dest-path',
InputArgument::REQUIRED,
'The path to generate your forms classes.'
),
new InputArgument(
'namespace',
InputArgument::OPTIONAL,
'The namespace name where the forms will be generated. If you do not want to use namespace, the forms are created in standard Application_Form_NAMEFORM.',
0
),
new InputOption(
'primary-keys',
null,
InputOption::VALUE_NONE,
'Defines the primary keys to be inserted on the forms.'
)
))
->setHelp(<<<EOT
Generate form classes from your database information.
EOT
);
}

/**
* @see Console\Command\Command
*/
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output)
{
$destPath = realpath($input->getArgument('dest-path'));

if ( ! file_exists($destPath) )
{
throw new \InvalidArgumentException(
sprintf('Forms destination directory "%s" does not exist.', $destPath)
);
}

if ( ! is_writable($destPath) )
{
throw new \InvalidArgumentException(
sprintf('Forms destination directory "%s" does not have write permissions.', $destPath)
);
}

$output->write('Loading information from the database...' . PHP_EOL);

$generic = new \Ionix\Zend\Form\Generator\Db\Generic($this->getHelper('dbAdapter')->getDbAdapter());
$databaseInformation = $generic->getDatabaseInformation();

$file = new \Ionix\Zend\Form\Generator\File\Creator();
$file->setFileDestination($destPath);

$namespace = $input->getArgument('namespace');
if( !empty($namespace) )
{
$file->setNamespace( $namespace );
}
if( $input->getOption('primary-keys') === true )
{
$file->setGeneratePrimaryKeys(true);
}

foreach( $databaseInformation as $schema => $tables )
{
foreach( $tables as $tableName => $columnsInformation )
{
$result = $file->generateFile( $tableName, $schema, $columnsInformation );
$output->write($result);
}
}

$output->write('Generation process forms completed successfully!');
}
}

0 comments on commit dd07e1e

Please sign in to comment.