/** * compile the given class id. */ public function compile($selector) { jDaoCompiler::$daoId = $selector->toString(); jDaoCompiler::$daoPath = $selector->getPath(); jDaoCompiler::$dbType = $selector->driver; // chargement du fichier XML $doc = new DOMDocument(); if (!$doc->load(jDaoCompiler::$daoPath)) { throw new jException('jelix~daoxml.file.unknow', jDaoCompiler::$daoPath); } if ($doc->documentElement->namespaceURI != JELIX_NAMESPACE_BASE . 'dao/1.0') { throw new jException('jelix~daoxml.namespace.wrong', array(jDaoCompiler::$daoPath, $doc->namespaceURI)); } $parser = new jDaoParser(); $parser->parse(simplexml_import_dom($doc)); global $gJConfig; if (!isset($gJConfig->_pluginsPathList_db[$selector->driver]) || !file_exists($gJConfig->_pluginsPathList_db[$selector->driver])) { throw new jException('jelix~db.error.driver.notfound', $selector->driver); } require_once $gJConfig->_pluginsPathList_db[$selector->driver] . $selector->driver . '.daobuilder.php'; $class = $selector->driver . 'DaoBuilder'; $generator = new $class($selector->getDaoClass(), $selector->getDaoRecordClass(), $parser); // génération des classes PHP correspondant à la définition de la DAO $compiled = '<?php ' . $generator->buildClasses() . "\n?>"; jFile::write($selector->getCompiledFilePath(), $compiled); return true; }
protected function _execute(InputInterface $input, OutputInterface $output) { $module = $input->getArgument('module'); $daoname = $input->getArgument('daoname'); $classname = $input->getArgument('classname'); /* * Computing some paths and filenames */ $modulePath = $this->getModulePath($module); $sourceDaoPath = $modulePath . 'daos/'; $sourceDaoPath .= strtolower($daoname) . '.dao.xml'; if (!file_exists($sourceDaoPath)) { throw new \Exception("The file {$sourceDaoPath} doesn't exist"); } $targetClassPath = $modulePath . 'classes/'; $targetClassPath .= strtolower($classname) . '.class.php'; /* * Parsing the dao xml file */ $selector = new \jSelectorDao($module . '~' . $daoname, ''); $tools = \jDb::getConnection()->tools(); $doc = new \DOMDocument(); if (!$doc->load($sourceDaoPath)) { throw new \jException('jelix~daoxml.file.unknown', $sourceDaoPath); } if ($doc->documentElement->namespaceURI != JELIX_NAMESPACE_BASE . 'dao/1.0') { throw new \jException('jelix~daoxml.namespace.wrong', array($sourceDaoPath, $doc->namespaceURI)); } $parser = new \jDaoParser($selector); $parser->parse(simplexml_import_dom($doc), $tools); $properties = $parser->GetProperties(); /* * Generating the class */ $classContent = ''; foreach ($properties as $name => $property) { $classContent .= " public \${$name};\n"; } $this->createFile($targetClassPath, 'module/classfromdao.class.tpl', array('properties' => $classContent, 'name' => $classname), "Class"); }
/** * compile the given class id. * @param jSelectorDao $selector */ public function compile($selector) { $daoPath = $selector->getPath(); // load the XML file $doc = new DOMDocument(); if (!$doc->load($daoPath)) { throw new jException('jelix~daoxml.file.unknown', $daoPath); } if ($doc->documentElement->namespaceURI != JELIX_NAMESPACE_BASE . 'dao/1.0') { throw new jException('jelix~daoxml.namespace.wrong', array($daoPath, $doc->namespaceURI)); } $tools = jApp::loadPlugin($selector->driver, 'db', '.dbtools.php', $selector->driver . 'DbTools'); if (is_null($tools)) { throw new jException('jelix~db.error.driver.notfound', $selector->driver); } $parser = new jDaoParser($selector); $parser->parse(simplexml_import_dom($doc), $tools); $class = $selector->dbType . 'DaoBuilder'; if (!jApp::includePlugin($selector->dbType, 'daobuilder', '.daobuilder.php', $class)) { throw new jException('jelix~dao.error.builder.notfound', $selector->dbType); } $generator = new $class($selector, $tools, $parser); // generation of PHP classes corresponding to the DAO definition $compiled = '<?php '; $compiled .= "\nif (jApp::config()->compilation['checkCacheFiletime']&&(\n"; $compiled .= "\n filemtime('" . $daoPath . '\') > ' . filemtime($daoPath); $importedDao = $parser->getImportedDao(); if ($importedDao) { foreach ($importedDao as $selimpdao) { $path = $selimpdao->getPath(); $compiled .= "\n|| filemtime('" . $path . '\') > ' . filemtime($path); } } $compiled .= ")){ return false;\n}\nelse {\n"; $compiled .= $generator->buildClasses() . "\n return true; }"; jFile::write($selector->getCompiledFilePath(), $compiled); return true; }
/** * get autoincrement PK field */ protected function getAutoIncrementPKField($using = null) { if ($using === null) { $using = $this->_dataParser->getProperties(); } $tb = $this->_dataParser->getTables(); $tb = $tb[$this->_dataParser->getPrimaryTable()]['realname']; foreach ($using as $id => $field) { if (!$field->isPK) { continue; } if ($field->autoIncrement) { return $field; } } return null; }
public function compile($selector) { $daoPath = $selector->getPath(); $doc = new DOMDocument(); if (!$doc->load($daoPath)) { throw new jException('jelix~daoxml.file.unknown', $daoPath); } if ($doc->documentElement->namespaceURI != JELIX_NAMESPACE_BASE . 'dao/1.0') { throw new jException('jelix~daoxml.namespace.wrong', array($daoPath, $doc->namespaceURI)); } $tools = jApp::loadPlugin($selector->driver, 'db', '.dbtools.php', $selector->driver . 'DbTools'); if (is_null($tools)) { throw new jException('jelix~db.error.driver.notfound', $selector->driver); } $parser = new jDaoParser($selector); $parser->parse(simplexml_import_dom($doc), $tools); require_once jApp::config()->_pluginsPathList_db[$selector->driver] . $selector->driver . '.daobuilder.php'; $class = $selector->driver . 'DaoBuilder'; $generator = new $class($selector, $tools, $parser); $compiled = '<?php ' . $generator->buildClasses() . "\n?>"; jFile::write($selector->getCompiledFilePath(), $compiled); return true; }
public function run() { $this->loadAppConfig(); #if ENABLE_OPTIMIZED_SOURCE require JELIX_LIB_PATH . 'dao/jDaoCompiler.class.php'; // jDaoParser is in jDaoCompiler file #endif $path = $this->getModulePath($this->_parameters['module']); $filename = $path . 'forms/'; $this->createDir($filename); $filename .= strtolower($this->_parameters['form']) . '.form.xml'; if ($this->getOption('-createlocales')) { $locale_content = ''; $locale_base = $this->_parameters['module'] . '~' . strtolower($this->_parameters['form']) . '.form.'; $locale_filename_fr = $path . 'locales/fr_FR/'; $this->createDir($locale_filename_fr); $locale_filename_fr .= strtolower($this->_parameters['form']) . '.UTF-8.properties'; $locale_filename_en = $path . 'locales/en_EN/'; $this->createDir($locale_filename_en); $locale_filename_en .= strtolower($this->_parameters['form']) . '.UTF-8.properties'; $submit = "\n\n<submit ref=\"_submit\">\n\t<label locale='" . $locale_base . "ok' />\n</submit>"; } else { $submit = "\n\n<submit ref=\"_submit\">\n\t<label>ok</label>\n</submit>"; } $dao = $this->getParam('dao'); if ($dao === null) { if ($this->getOption('-createlocales')) { $locale_content = "form.ok=OK\n"; $this->createFile($locale_filename_fr, 'locales.tpl', array('content' => $locale_content)); $this->createFile($locale_filename_en, 'locales.tpl', array('content' => $locale_content)); } $this->createFile($filename, 'module/form.xml.tpl', array('content' => '<!-- add control declaration here -->' . $submit)); return; } global $gJConfig; $gJConfig->startModule = $this->_parameters['module']; jContext::push($this->_parameters['module']); $tools = jDb::getConnection()->tools(); // we're going to parse the dao $selector = new jSelectorDao($dao, ''); $doc = new DOMDocument(); $daoPath = $selector->getPath(); if (!$doc->load($daoPath)) { throw new jException('jelix~daoxml.file.unknown', $daoPath); } if ($doc->documentElement->namespaceURI != JELIX_NAMESPACE_BASE . 'dao/1.0') { throw new jException('jelix~daoxml.namespace.wrong', array($daoPath, $doc->namespaceURI)); } $parser = new jDaoParser($selector); $parser->parse(simplexml_import_dom($doc), $tools); // now we generate the form file $properties = $parser->GetProperties(); $table = $parser->GetPrimaryTable(); $content = ''; foreach ($properties as $name => $property) { if (!$property->ofPrimaryTable) { continue; } if ($property->isPK && $property->autoIncrement) { continue; } $attr = ''; if ($property->required) { $attr .= ' required="true"'; } if ($property->defaultValue !== null) { $attr .= ' defaultvalue="' . htmlspecialchars($property->defaultValue) . '"'; } if ($property->maxlength !== null) { $attr .= ' maxlength="' . $property->maxlength . '"'; } if ($property->minlength !== null) { $attr .= ' minlength="' . $property->minlength . '"'; } $datatype = ''; $tag = 'input'; switch ($property->unifiedType) { case 'integer': case 'numeric': $datatype = 'integer'; break; case 'datetime': $datatype = 'datetime'; break; case 'time': $datatype = 'time'; break; case 'date': $datatype = 'date'; break; case 'double': case 'float': $datatype = 'decimal'; break; case 'text': case 'blob': $tag = 'textarea'; break; case 'boolean': $tag = 'checkbox'; break; } if ($datatype != '') { $attr .= ' type="' . $datatype . '"'; } if ($this->getOption('-createlocales')) { $locale_content .= 'form.' . $name . '=' . ucwords(str_replace('_', ' ', $name)) . "\n"; $content .= "\n\n<{$tag} ref=\"{$name}\"{$attr}>\n\t<label locale='" . $locale_base . $name . "' />\n</{$tag}>"; } else { $content .= "\n\n<{$tag} ref=\"{$name}\"{$attr}>\n\t<label>" . ucwords(str_replace('_', ' ', $name)) . "</label>\n</{$tag}>"; } } if ($this->getOption('-createlocales')) { $locale_content .= "form.ok=OK\n"; $this->createFile($locale_filename_fr, 'module/locales.tpl', array('content' => $locale_content)); $this->createFile($locale_filename_en, 'module/locales.tpl', array('content' => $locale_content)); } $this->createFile($filename, 'module/form.xml.tpl', array('content' => $content . $submit)); }
public function run() { jxs_init_jelix_env(); $path = $this->getModulePath($this->_parameters['module']); $filename = $path . 'forms/'; $this->createDir($filename); $filename .= strtolower($this->_parameters['form']) . '.form.xml'; $submit = "\n\n<submit ref=\"_submit\">\n\t<label>ok</label>\n</submit>"; if (($dao = $this->getParam('dao')) === null) { $this->createFile($filename, 'form.xml.tpl', array('content' => '<!-- add control declaration here -->' . $submit)); return; } global $gJConfig; $gJConfig->startModule = $this->_parameters['module']; jContext::push($this->_parameters['module']); // we're going to parse the dao $selector = new jSelectorDao($dao, '', false); jDaoCompiler::$daoId = $selector->toString(); jDaoCompiler::$daoPath = $selector->getPath(); jDaoCompiler::$dbType = $selector->driver; $doc = new DOMDocument(); if (!$doc->load(jDaoCompiler::$daoPath)) { throw new jException('jelix~daoxml.file.unknow', jDaoCompiler::$daoPath); } if ($doc->documentElement->namespaceURI != JELIX_NAMESPACE_BASE . 'dao/1.0') { throw new jException('jelix~daoxml.namespace.wrong', array(jDaoCompiler::$daoPath, $doc->namespaceURI)); } $parser = new jDaoParser(); $parser->parse(simplexml_import_dom($doc)); // know we generate the form file $properties = $parser->GetProperties(); $table = $parser->GetPrimaryTable(); $content = ''; foreach ($properties as $name => $property) { if (!$property->ofPrimaryTable) { continue; } if ($property->isPK && ($property->datatype == 'autoincrement' || $property->datatype == 'bigautoincrement')) { continue; } $attr = ''; if ($property->required) { $attr .= ' required="true"'; } if ($property->defaultValue !== null) { $attr .= ' defaultvalue="' . htmlspecialchars($property->defaultValue) . '"'; } if ($property->maxlength !== null) { $attr .= ' maxlength="' . $property->maxlength . '"'; } if ($property->minlength !== null) { $attr .= ' minlength="' . $property->minlength . '"'; } //if(false) // $attr.=' defaultvalue=""'; $datatype = ''; $tag = 'input'; switch ($property->datatype) { case 'autoincrement': case 'bigautoincrement': case 'int': case 'integer': case 'numeric': $datatype = 'integer'; break; case 'datetime': $datatype = 'datetime'; break; case 'time': $datatype = 'time'; break; case 'date': $datatype = 'date'; break; case 'double': case 'float': $datatype = 'decimal'; break; case 'text': $tag = 'textarea'; break; case 'boolean': $tag = 'checkbox'; break; } if ($datatype != '') { $attr .= ' type="' . $datatype . '"'; } $content .= "\n\n<{$tag} ref=\"{$name}\"{$attr}>\n\t<label>{$name}</label>\n</{$tag}>"; } $this->createFile($filename, 'form.xml.tpl', array('content' => $content . $submit)); }
protected function import($xml, $tools) { if (isset($xml['import'])) { $import = (string) $xml['import']; jApp::pushCurrentModule($this->selector->module); // Keep the same driver as current used $importSel = new jSelectorDaoDb($import, $this->selector->driver, $this->selector->dbType); jApp::popCurrentModule(); $doc = new DOMDocument(); if (!$doc->load($importSel->getPath())) { throw new jException('jelix~daoxml.file.unknown', $importSel->getPath()); } $parser = new jDaoParser($importSel); $parser->parse(simplexml_import_dom($doc), $tools); $this->_properties = $parser->getProperties(); $this->_tables = $parser->getTables(); $this->_primaryTable = $parser->getPrimaryTable(); $this->_methods = $parser->getMethods(); $this->_ojoins = $parser->getOuterJoins(); $this->_ijoins = $parser->getInnerJoins(); $this->_eventList = $parser->getEvents(); $this->_userRecord = $parser->getUserRecord(); $this->_importedDao = $parser->getImportedDao(); $this->hasOnlyPrimaryKeys = $parser->hasOnlyPrimaryKeys; if ($this->_importedDao) { $this->_importedDao[] = $importSel; } else { $this->_importedDao = array($importSel); } } }
function testSelectPattern() { $doc = '<?xml version="1.0"?> <dao xmlns="http://jelix.org/ns/dao/1.0"> <datasources> <primarytable name="product_test" primarykey="id" /> </datasources> <record> <property name="id" fieldname="id" datatype="autoincrement"/> <property name="name" fieldname="name" datatype="string" required="true" selectpattern="%s"/> <property name="price" fieldname="price" datatype="float"/> </record> </dao>'; $parser = new jDaoParser($this->_selector); $parser->parse(simplexml_load_string($doc), $this->_tools); $generator = new testMysqlDaoGenerator($this->_selector, $this->_tools, $parser); $result = $generator->GetSelectClause(); $this->assertEqualOrDiff('SELECT `product_test`.`id`, `product_test`.`name`, `product_test`.`price`', $result); $doc = '<?xml version="1.0"?> <dao xmlns="http://jelix.org/ns/dao/1.0"> <datasources> <primarytable name="product_test" primarykey="id" /> </datasources> <record> <property name="id" fieldname="id" datatype="autoincrement"/> <property name="name" fieldname="name" datatype="string" required="true" selectpattern="TOUPPER(%s)"/> <property name="price" fieldname="price" datatype="float"/> </record> </dao>'; $parser = new jDaoParser($this->_selector); $parser->parse(simplexml_load_string($doc), $this->_tools); $generator = new testMysqlDaoGenerator($this->_selector, $this->_tools, $parser); $result = $generator->GetSelectClause(); $this->assertEqualOrDiff('SELECT `product_test`.`id`, TOUPPER(`product_test`.`name`) as `name`, `product_test`.`price`', $result); $doc = '<?xml version="1.0"?> <dao xmlns="http://jelix.org/ns/dao/1.0"> <datasources> <primarytable name="p" realname="product_test" primarykey="id" /> </datasources> <record> <property name="id" fieldname="id" datatype="autoincrement"/> <property name="name" fieldname="name" datatype="string" required="true" selectpattern="TOUPPER(%s)"/> <property name="price" fieldname="price" datatype="float"/> </record> </dao>'; $parser = new jDaoParser($this->_selector); $parser->parse(simplexml_load_string($doc), $this->_tools); $generator = new testMysqlDaoGenerator($this->_selector, $this->_tools, $parser); $result = $generator->GetSelectClause(); $this->assertEqualOrDiff('SELECT `p`.`id`, TOUPPER(`p`.`name`) as `name`, `p`.`price`', $result); $doc = '<?xml version="1.0"?> <dao xmlns="http://jelix.org/ns/dao/1.0"> <datasources> <primarytable name="product_test" primarykey="id" /> </datasources> <record> <property name="id" fieldname="id" datatype="autoincrement"/> <property name="name" fieldname="name" datatype="string" required="true" selectpattern="TOUPPER(name)"/> <property name="price" fieldname="price" datatype="float"/> </record> </dao>'; $parser = new jDaoParser($this->_selector); $parser->parse(simplexml_load_string($doc), $this->_tools); $generator = new testMysqlDaoGenerator($this->_selector, $this->_tools, $parser); $result = $generator->GetSelectClause(); $this->assertEqualOrDiff('SELECT `product_test`.`id`, TOUPPER(name) as `name`, `product_test`.`price`', $result); $doc = '<?xml version="1.0"?> <dao xmlns="http://jelix.org/ns/dao/1.0"> <datasources> <primarytable name="product_test" primarykey="id" /> </datasources> <record> <property name="id" fieldname="id" datatype="autoincrement"/> <property name="name" fieldname="name" datatype="string" required="true" selectpattern="CONCAT(name,\' \',price)"/> <property name="price" fieldname="price" datatype="float"/> </record> </dao>'; $parser = new jDaoParser($this->_selector); $parser->parse(simplexml_load_string($doc), $this->_tools); $generator = new testMysqlDaoGenerator($this->_selector, $this->_tools, $parser); $result = $generator->GetSelectClause(); $this->assertEqualOrDiff('SELECT `product_test`.`id`, CONCAT(name,\\\' \\\',price) as `name`, `product_test`.`price`', $result); }
/** * @param simpleXmlElement $method the xml element describing the method to generate * @param jDaoParser $parser the parser on a dao file */ function __construct($method, $parser) { $this->_parser = $parser; $params = $parser->getAttr($method, array('name', 'type', 'call', 'distinct', 'eventbefore', 'eventafter', 'groupby')); if ($params['name'] === null) { throw new jDaoXmlException($this->_parser->selector, 'missing.attr', array('name', 'method')); } $this->name = $params['name']; $this->type = $params['type'] ? strtolower($params['type']) : 'select'; if (isset($method->parameter)) { foreach ($method->parameter as $param) { $attr = $param->attributes(); if (strpos($attr['name'], '$') !== false) { throw new jDaoXmlException($this->_parser->selector, 'method.parameter.invalidname', array($method->name, $attr['name'])); } if (!isset($attr['name'])) { throw new jDaoXmlException($this->_parser->selector, 'method.parameter.unknowname', array($this->name)); } $this->_parameters[] = (string) $attr['name']; if (isset($attr['default'])) { $this->_parametersDefaultValues[(string) $attr['name']] = (string) $attr['default']; } } } if ($this->type == 'sql') { if ($params['call'] === null) { throw new jDaoXmlException($this->_parser->selector, 'method.procstock.name.missing'); } $this->_procstock = $params['call']; return; } if ($this->type == 'php') { if (isset($method->body)) { $this->_body = (string) $method->body; } else { throw new jDaoXmlException($this->_parser->selector, 'method.body.missing'); } return; } $this->_conditions = new jDaoConditions(); if (isset($method->conditions)) { $this->_parseConditions($method->conditions[0], false); } if ($this->type == 'update' || $this->type == 'delete') { if ($params['eventbefore'] == 'true') { $this->eventBeforeEnabled = true; } if ($params['eventafter'] == 'true') { $this->eventAfterEnabled = true; } } if ($this->type == 'update') { if ($this->_parser->hasOnlyPrimaryKeys) { throw new jDaoXmlException($this->_parser->selector, 'method.update.forbidden', array($this->name)); } if (isset($method->values) && isset($method->values[0]->value)) { foreach ($method->values[0]->value as $val) { $this->_addValue($val); } } else { throw new jDaoXmlException($this->_parser->selector, 'method.values.undefine', array($this->name)); } return; } if (strlen($params['distinct'])) { if ($this->type == 'select') { $this->distinct = $this->_parser->getBool($params['distinct']); } elseif ($this->type == 'count') { $props = $this->_parser->getProperties(); if (!isset($props[$params['distinct']])) { throw new jDaoXmlException($this->_parser->selector, 'method.property.unknown', array($this->name, $params['distinct'])); } $this->distinct = $params['distinct']; } else { throw new jDaoXmlException($this->_parser->selector, 'forbidden.attr.context', array('distinct', '<method name="' . $this->name . '"')); } } if ($this->type == 'count') { return; } if (isset($method->order) && isset($method->order[0]->orderitem)) { foreach ($method->order[0]->orderitem as $item) { $this->_addOrder($item); } } if (strlen($params['groupby'])) { if ($this->type == 'select' || $this->type == 'selectfirst') { $this->_groupBy = preg_split("/[\\s,]+/", $params['groupby']); $props = $this->_parser->getProperties(); foreach ($this->_groupBy as $p) { if (!isset($props[$p])) { throw new jDaoXmlException($this->_parser->selector, 'method.property.unknown', array($this->name, $p)); } } } else { throw new jDaoXmlException($this->_parser->selector, 'forbidden.attr.context', array('groupby', '<method name="' . $this->name . '"')); } } if (isset($method->limit)) { if (isset($method->limit[1])) { throw new jDaoXmlException($this->_parser->selector, 'tag.duplicate', array('limit', $this->name)); } if ($this->type == 'select') { $this->_addLimit($method->limit[0]); } else { throw new jDaoXmlException($this->_parser->selector, 'method.limit.forbidden', $this->name); } } }
function testInsertQuery() { $doc = '<?xml version="1.0"?> <dao xmlns="http://jelix.org/ns/dao/1.0"> <datasources> <primarytable name="product_test" primarykey="code" /> </datasources> <record> <property name="code" fieldname="code" datatype="string" insertpattern="now()"/> <property name="name" fieldname="name" datatype="string" /> <property name="price" fieldname="price" datatype="float"/> </record> </dao>'; $parser = new jDaoParser($this->_selector); $parser->parse(simplexml_load_string($doc), $this->_tools); $generator = new testMysqlDaoGenerator($this->_selector, $this->_tools, $parser); $fieldList = $generator->GetPropertiesBy('PrimaryTable'); list($fields, $values) = $generator->PrepareValues($fieldList, 'insertPattern', 'record->'); $this->assertEqual("now()", $values['code']); $doc = '<?xml version="1.0"?> <dao xmlns="http://jelix.org/ns/dao/1.0"> <datasources> <primarytable name="product_test" primarykey="code" /> </datasources> <record> <property name="code" fieldname="code" datatype="string" insertpattern="now(%s)"/> <property name="name" fieldname="name" datatype="string" /> <property name="price" fieldname="price" datatype="float"/> </record> </dao>'; $parser = new jDaoParser($this->_selector); $parser->parse(simplexml_load_string($doc), $this->_tools); $generator = new testMysqlDaoGenerator($this->_selector, $this->_tools, $parser); $fieldList = $generator->GetPropertiesBy('PrimaryTable'); list($fields, $values) = $generator->PrepareValues($fieldList, 'insertPattern', 'record->'); $this->assertEqual('now(\'.($record->code === null ? \'NULL\' : $this->_conn->quote2($record->code,false)).\')', $values['code']); }
/** * constructor. * @param array $attributes list of attributes of a simpleXmlElement * @param jDaoParser $parser the parser on the dao file * @param jDbTools $tools */ function __construct($aAttributes, $parser, $tools) { $needed = array('name', 'fieldname', 'table', 'datatype', 'required', 'minlength', 'maxlength', 'regexp', 'sequence', 'default', 'autoincrement'); // Allowed attributes names $allowed = array('name', 'fieldname', 'table', 'datatype', 'required', 'minlength', 'maxlength', 'regexp', 'sequence', 'default', 'autoincrement', 'updatepattern', 'insertpattern', 'selectpattern', 'comment'); foreach ($aAttributes as $attributeName => $attributeValue) { if (!in_array($attributeName, $allowed)) { throw new jDaoXmlException($parser->selector, 'unknown.attr', array($attributeName, 'property')); } } $params = $parser->getAttr($aAttributes, $needed); if ($params['name'] === null) { throw new jDaoXmlException($parser->selector, 'missing.attr', array('name', 'property')); } $this->name = $params['name']; if (!preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $this->name)) { throw new jDaoXmlException($parser->selector, 'property.invalid.name', $this->name); } $this->fieldName = $params['fieldname'] !== null ? $params['fieldname'] : $this->name; $this->table = $params['table'] !== null ? $params['table'] : $parser->getPrimaryTable(); $tables = $parser->getTables(); if (!isset($tables[$this->table])) { throw new jDaoXmlException($parser->selector, 'property.unknown.table', $this->name); } $this->required = $this->requiredInConditions = $parser->getBool($params['required']); $this->maxlength = $params['maxlength'] !== null ? intval($params['maxlength']) : null; $this->minlength = $params['minlength'] !== null ? intval($params['minlength']) : null; $this->regExp = $params['regexp']; $this->autoIncrement = $parser->getBool($params['autoincrement']); if ($params['datatype'] === null) { throw new jDaoXmlException($parser->selector, 'missing.attr', array('datatype', 'property')); } $params['datatype'] = trim(strtolower($params['datatype'])); if ($params['datatype'] == '') { throw new jDaoXmlException($parser->selector, 'wrong.attr', array($params['datatype'], $this->fieldName, 'property')); } $this->datatype = strtolower($params['datatype']); $ti = $tools->getTypeInfo($this->datatype); $this->unifiedType = $ti[1]; if (!$this->autoIncrement) { $this->autoIncrement = $ti[6]; } if ($this->unifiedType == 'integer' || $this->unifiedType == 'numeric') { if ($params['sequence'] !== null) { $this->sequenceName = $params['sequence']; $this->autoIncrement = true; } } $this->isPK = in_array($this->fieldName, $tables[$this->table]['pk']); if (!$this->isPK && $this->table == $parser->getPrimaryTable()) { foreach ($tables as $table => $info) { if ($table == $this->table) { continue; } if (isset($info['fk']) && in_array($this->fieldName, $info['fk'])) { $this->isFK = true; break; } } } else { $this->required = true; $this->requiredInConditions = true; } if ($this->autoIncrement) { $this->required = false; $this->requiredInConditions = true; } if ($params['default'] !== null) { $this->defaultValue = $tools->stringToPhpValue($this->unifiedType, $params['default']); } // insertpattern is allowed on primary keys noy autoincremented if ($this->isPK && !$this->autoIncrement && isset($aAttributes['insertpattern'])) { $this->insertPattern = (string) $aAttributes['insertpattern']; } if ($this->isPK) { $this->updatePattern = ''; } // we ignore *pattern attributes on PK and FK fields if (!$this->isPK && !$this->isFK) { if (isset($aAttributes['updatepattern'])) { $this->updatePattern = (string) $aAttributes['updatepattern']; } if (isset($aAttributes['insertpattern'])) { $this->insertPattern = (string) $aAttributes['insertpattern']; } if (isset($aAttributes['selectpattern'])) { $this->selectPattern = (string) $aAttributes['selectpattern']; } } // no update and insert patterns for field of external tables if ($this->table != $parser->getPrimaryTable()) { $this->updatePattern = ''; $this->insertPattern = ''; $this->required = false; $this->requiredInConditions = false; $this->ofPrimaryTable = false; } else { $this->ofPrimaryTable = true; } // field comment if (isset($aAttributes['comment'])) { $this->comment = (string) $aAttributes['comment']; } }
protected function _execute(InputInterface $input, OutputInterface $output) { $module = $input->getArgument('module'); $formName = $input->getArgument('form'); $daoName = $input->getArgument('dao'); require_once JELIX_LIB_PATH . 'dao/jDaoParser.class.php'; $path = $this->getModulePath($module); $formdir = $path . 'forms/'; $this->createDir($formdir); $formfile = strtolower($formName) . '.form.xml'; if ($input->getOption('createlocales')) { $locale_content = ''; $locale_base = $module . '~' . strtolower($formName) . '.form.'; $locale_filename_fr = 'locales/fr_FR/'; $this->createDir($path . $locale_filename_fr); $locale_filename_fr .= strtolower($formName) . '.UTF-8.properties'; $locale_filename_en = 'locales/en_US/'; $this->createDir($path . $locale_filename_en); $locale_filename_en .= strtolower($formName) . '.UTF-8.properties'; $submit = "\n\n<submit ref=\"_submit\">\n\t<label locale='" . $locale_base . "ok' />\n</submit>"; } else { $submit = "\n\n<submit ref=\"_submit\">\n\t<label>ok</label>\n</submit>"; } if ($daoName === null) { if ($input->getOption('createlocales')) { $locale_content = "form.ok=OK\n"; $this->createFile($path . $locale_filename_fr, 'locales.tpl', array('content' => $locale_content), "Locales file"); $this->createFile($path . $locale_filename_en, 'locales.tpl', array('content' => $locale_content), "Locales file"); } $this->createFile($formdir . $formfile, 'module/form.xml.tpl', array('content' => '<!-- add control declaration here -->' . $submit), "Form"); return; } \Jelix\Core\App::config()->startModule = $module; \Jelix\Core\App::pushCurrentModule($module); $tools = \jDb::getConnection()->tools(); // we're going to parse the dao $selector = new \jSelectorDao($daoName, ''); $doc = new \DOMDocument(); $daoPath = $selector->getPath(); if (!$doc->load($daoPath)) { throw new \jException('jelix~daoxml.file.unknown', $daoPath); } if ($doc->documentElement->namespaceURI != JELIX_NAMESPACE_BASE . 'dao/1.0') { throw new \jException('jelix~daoxml.namespace.wrong', array($daoPath, $doc->namespaceURI)); } $parser = new \jDaoParser($selector); $parser->parse(simplexml_import_dom($doc), $tools); // now we generate the form file $properties = $parser->GetProperties(); $table = $parser->GetPrimaryTable(); $content = ''; foreach ($properties as $name => $property) { if (!$property->ofPrimaryTable) { continue; } if ($property->isPK && $property->autoIncrement) { continue; } $attr = ''; if ($property->required) { $attr .= ' required="true"'; } if ($property->defaultValue !== null) { $attr .= ' defaultvalue="' . htmlspecialchars($property->defaultValue) . '"'; } if ($property->maxlength !== null) { $attr .= ' maxlength="' . $property->maxlength . '"'; } if ($property->minlength !== null) { $attr .= ' minlength="' . $property->minlength . '"'; } $datatype = ''; $tag = 'input'; switch ($property->unifiedType) { case 'integer': case 'numeric': $datatype = 'integer'; break; case 'datetime': $datatype = 'datetime'; break; case 'time': $datatype = 'time'; break; case 'date': $datatype = 'date'; break; case 'double': case 'float': $datatype = 'decimal'; break; case 'text': case 'blob': $tag = 'textarea'; break; case 'boolean': $tag = 'checkbox'; break; } if ($datatype != '') { $attr .= ' type="' . $datatype . '"'; } // use database comment to create form's label if ($property->comment != '' && $input->getOption('usecomments')) { if ($input->getOption('createlocales')) { // replace special chars by dot $locale_content .= 'form.' . $name . '=' . htmlspecialchars(utf8_decode($property->comment)) . "\n"; $content .= "\n\n<{$tag} ref=\"{$name}\"{$attr}>\n\t<label locale='" . $locale_base . $name . "' />\n</{$tag}>"; } else { // encoding special chars $content .= "\n\n<{$tag} ref=\"{$name}\"{$attr}>\n\t<label>" . htmlspecialchars($property->comment) . "</label>\n</{$tag}>"; } } else { if ($input->getOption('createlocales')) { $locale_content .= 'form.' . $name . '=' . ucwords(str_replace('_', ' ', $name)) . "\n"; $content .= "\n\n<{$tag} ref=\"{$name}\"{$attr}>\n\t<label locale='" . $locale_base . $name . "' />\n</{$tag}>"; } else { $content .= "\n\n<{$tag} ref=\"{$name}\"{$attr}>\n\t<label>" . ucwords(str_replace('_', ' ', $name)) . "</label>\n</{$tag}>"; } } } if ($input->getOption('createlocales')) { $locale_content .= "form.ok=OK\n"; $this->createFile($path . $locale_filename_fr, 'module/locales.tpl', array('content' => $locale_content), "Locales file"); $this->createFile($path . $locale_filename_en, 'module/locales.tpl', array('content' => $locale_content), "Locales file"); } $this->createFile($formdir . $formfile, 'module/form.xml.tpl', array('content' => $content . $submit), "Form file"); }