Skip to content

Commit

Permalink
Debug modulebuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Jul 12, 2017
1 parent 11cc1f0 commit 40b3ae2
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 125 deletions.
6 changes: 3 additions & 3 deletions htdocs/core/class/html.formfile.class.php
Expand Up @@ -314,7 +314,7 @@ function showdocuments($modulepart,$modulesubdir,$filedir,$urlsource,$genallowed

if (preg_match('/massfilesarea_/', $modulepart))
{
$out.='<div id="show_files"><br></div>';
$out.='<div id="show_files"><br></div>'."\n";
$title=$langs->trans("MassFilesArea").' <a href="" id="togglemassfilesarea" ref="shown">('.$langs->trans("Hide").')</a>';
$title.='<script type="text/javascript" language="javascript">
jQuery(document).ready(function() {
Expand Down Expand Up @@ -788,14 +788,14 @@ function showdocuments($modulepart,$modulesubdir,$filedir,$urlsource,$genallowed
$out.=dol_print_date($file->datea,'dayhour');
$out.='</td>';
if ($delallowed || $printer || $morepicto) $out.='<td></td>';
$out.='</tr>';
$out.='</tr>'."\n";
}
$this->numoffiles++;
}

if (count($file_list) == 0 && count($link_list) == 0 && $headershown)
{
$out.='<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>';
$out.='<tr class="oddeven"><td colspan="3" class="opacitymedium">'.$langs->trans("None").'</td></tr>'."\n";
}

}
Expand Down
164 changes: 138 additions & 26 deletions htdocs/core/lib/modulebuilder.lib.php
Expand Up @@ -22,6 +22,8 @@
*/




/**
* Save data into a memory area shared by all users, all sessions on server
*
Expand All @@ -31,63 +33,173 @@
* @param string $newmask New mask
* @return int <0 if KO, >0 if OK
*/
function rebuildobjectsql($destdir, $module, $objectname, $newmask)
function rebuildObjectClass($destdir, $module, $objectname, $newmask)
{
global $db;
global $db, $langs;

if (empty($objectname)) return -1;

dol_include_once(strtolower($module).'/class/'.strtolower($objectname).'.class.php');
$object=new $objectname($db);
$pathoffiletoeditsrc=$destdir.'/class/'.strtolower($objectname).'.class.php';
$pathoffiletoedittarget=$destdir.'/class/'.strtolower($objectname).'.class.php';
if (! dol_is_file($pathoffiletoeditsrc))
{
//$pathoffiletoeditsrc=DOL_DOCUMENT_ROOT.'/modulebuilder/template/class/myobject.class.php';
setEventMessages($langs->trans("ErrorFileNotFound", $pathoffiletoeditsrc), null, 'errors');
return -1;
}

// Edit sql files
$pathoffiletoedit=dol_osencode($destdir.'/sql/llx_'.strtolower($objectname).'.sql');
//$pathoffiletoedittmp=$destdir.'/class/'.strtolower($objectname).'.class.php.tmp';
//dol_delete_file($pathoffiletoedittmp, 0, 1, 1);

$contentsql = file_get_contents($pathoffiletoedit, 'r');
try
{
include_once $pathoffiletoeditsrc;
if (class_exists($objectname)) $object=new $objectname($db);
}
catch(Exception $e)
{
print $e->getMessage();
}

// Edit class files

$contentclass = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r');

$i=0;
$texttoinsert = '// BEGIN MODULEBUILDER PROPERTIES'."\n";
$texttoinsert.= "\t".'/**'."\n";
$texttoinsert.= "\t".' * @var array Array with all fields and their property'."\n";
$texttoinsert.= "\t".' */'."\n";
$texttoinsert.= "\t".'public $fields=array('."\n";

if (count($object->fields))
{
foreach($object->fields as $key => $val)
{
$i++;
$typephp='';
$texttoinsert.= "\t\t'".$key."' => array('type'=>'".$val['type']."', 'label'=>'".$val['label']."',";
if ($val['position']) $texttoinsert.= " 'position'=>".$val['position'].",";
if ($val['notnull']) $texttoinsert.= " 'notnull'=>".$val['notnull'].",";
if ($val['index']) $texttoinsert.= " 'index'=>".$val['index'].",";
if ($val['searchall']) $texttoinsert.= " 'searchall'=>".$val['searchall'].",";
if ($val['comment']) $texttoinsert.= " 'comment'=>'".$val['comment']."',";
$texttoinsert.= "),\n";
}
}
$texttoinsert.= "\t".');'."\n";

$texttoinsert.= "\n";

if (count($object->fields))
{
foreach($object->fields as $key => $val)
{
$i++;
$typephp='';
$texttoinsert.= "\t".'public $'.$key.$typephp.";";
//if ($key == 'rowid') $texttoinsert.= ' AUTO_INCREMENT PRIMARY KEY';
//if ($key == 'entity') $texttoinsert.= ' DEFAULT 1';
//$texttoinsert.= ($val['notnull']?' NOT NULL':'');
//if ($i < count($object->fields)) $texttoinsert.=";";
$texttoinsert.= "\n";
}
}

$texttoinsert.= "\t".'// END MODULEBUILDER PROPERTIES';

$contentclass = preg_replace('/\/\/ BEGIN MODULEBUILDER PROPERTIES.*END MODULEBUILDER PROPERTIES/ims', $texttoinsert, $contentclass);

//file_put_contents($pathoffiletoedittmp, $contentclass);
file_put_contents(dol_osencode($pathoffiletoedittarget), $contentclass);
@chmod($pathoffiletoedit, octdec($newmask));


return 1;
}

/**
* Save data into a memory area shared by all users, all sessions on server
*
* @param string $destdir Directory
* @param string $module Module name
* @param string $objectname Name of object
* @param string $newmask New mask
* @return int <0 if KO, >0 if OK
*/
function rebuildObjectSql($destdir, $module, $objectname, $newmask)
{
global $db, $langs;

if (empty($objectname)) return -1;

try
{
dol_include_once(strtolower($module).'/class/'.strtolower($objectname).'.class.php');
if (class_exists($objectname)) $object=new $objectname($db);
}
catch(Exception $e)
{
print $e->getMessage();
}

// Edit .sql file
$pathoffiletoeditsrc=dol_osencode($destdir.'/sql/llx_'.strtolower($objectname).'.sql');
$pathoffiletoedittarget=dol_osencode($destdir.'/sql/llx_'.strtolower($objectname).'.sql');

$contentsql = file_get_contents($pathoffiletoeditsrc, 'r');

$i=0;
$texttoinsert = '-- BEGIN MODULEBUILDER FIELDS'."\n";
foreach($object->fields as $key => $val)
if (count($object->fields))
{
$i++;
$texttoinsert.= "\t".$key." ".$val['type'];
if ($key == 'rowid') $texttoinsert.= ' AUTO_INCREMENT PRIMARY KEY';
if ($key == 'entity') $texttoinsert.= ' DEFAULT 1';
$texttoinsert.= ($val['notnull']?' NOT NULL':'');
if ($i < count($object->fields)) $texttoinsert.=", ";
$texttoinsert.= "\n";
foreach($object->fields as $key => $val)
{
$i++;
$texttoinsert.= "\t".$key." ".$val['type'];
if ($key == 'rowid') $texttoinsert.= ' AUTO_INCREMENT PRIMARY KEY';
if ($key == 'entity') $texttoinsert.= ' DEFAULT 1';
$texttoinsert.= ($val['notnull']?' NOT NULL':'');
if ($i < count($object->fields)) $texttoinsert.=", ";
$texttoinsert.= "\n";
}
}
$texttoinsert.= "\t".'-- END MODULEBUILDER FIELDS';

$contentsql = preg_replace('/-- BEGIN MODULEBUILDER FIELDS.*END MODULEBUILDER FIELDS/ims', $texttoinsert, $contentsql);

file_put_contents($pathoffiletoedit, $contentsql);
file_put_contents($pathoffiletoedittarget, $contentsql);
@chmod($pathoffiletoedit, octdec($newmask));


// Edit .key.sql file
$pathoffiletoeditsrc=dol_osencode($destdir.'/sql/llx_'.strtolower($objectname).'.key.sql');
$pathoffiletoedittarget=dol_osencode($destdir.'/sql/llx_'.strtolower($objectname).'.key.sql');

// Edit sql files
$pathoffiletoedit=dol_osencode($destdir.'/sql/llx_'.strtolower($objectname).'.key.sql');

$contentsql = file_get_contents($pathoffiletoedit, 'r');
$contentsql = file_get_contents($pathoffiletoeditsrc, 'r');

$i=0;
$texttoinsert = '-- BEGIN MODULEBUILDER INDEXES'."\n";
foreach($object->fields as $key => $val)
if (count($object->fields))
{
$i++;
if ($val['index'])
foreach($object->fields as $key => $val)
{
$texttoinsert.= "ALTER TABLE llx_".strtolower($objectname)." ADD INDEX idx_".strtolower($objectname)."_".$key." (".$key.");";
$texttoinsert.= "\n";
$i++;
if ($val['index'])
{
$texttoinsert.= "ALTER TABLE llx_".strtolower($objectname)." ADD INDEX idx_".strtolower($objectname)."_".$key." (".$key.");";
$texttoinsert.= "\n";
}
}
}
$texttoinsert.= '-- END MODULEBUILDER INDEXES';

$contentsql = preg_replace('/-- BEGIN MODULEBUILDER INDEXES.*END MODULEBUILDER INDEXES/ims', $texttoinsert, $contentsql);

file_put_contents($pathoffiletoedit, $contentsql);
file_put_contents($pathoffiletoedittarget, $contentsql);
@chmod($pathoffiletoedit, octdec($newmask));

return 1;
}


2 changes: 1 addition & 1 deletion htdocs/langs/en_US/modulebuilder.lang
Expand Up @@ -10,7 +10,7 @@ ModuleKey=Module key
ObjectKey=Object key
ModuleInitialized=Module initialized
FilesForObjectInitialized=Files for new object '%s' initialized
FilesForObjectUpdated=Files for object '%s' updated
FilesForObjectUpdated=Files for object '%s' updated (.sql files and .class.php file)
ModuleBuilderDescdescription=Enter here all general information that describe your module
ModuleBuilderDescspecifications=You can enter here a long text to describe the specifications of your module that is not already structured into other tabs. So you have on hand the rules to develop. Also this text content will be included into the generated documentation (see last tab).
ModuleBuilderDescobjects=Define here the objects you want to manage with your module. A sql file, a page to list them, to create/edit/view a card and an API will be generated.
Expand Down
16 changes: 10 additions & 6 deletions htdocs/modulebuilder/index.php
Expand Up @@ -231,12 +231,11 @@

if (! $error)
{
// Edit sql with new properties
rebuildobjectsql($destdir, $module, $objectname, $newmask);

// Edit the class file to write properties
rebuildObjectClass($destdir, $module, $objectname, $newmask);


// Edit sql with new properties
rebuildObjectSql($destdir, $module, $objectname, $newmask);
}

if (! $error)
Expand All @@ -251,8 +250,13 @@

$destdir = $dirins.'/'.strtolower($module);

// TODO Complete list of fields with new one

// Edit the class file to write properties
rebuildObjectClass($destdir, $module, $objectname, $newmask);

// Edit sql with new properties
rebuildobjectsql($destdir, $module, $objectname, $newmask);
rebuildObjectSql($destdir, $module, $objectname, $newmask);

if (! $error)
{
Expand Down Expand Up @@ -973,7 +977,7 @@
print '<br><br><br>';

$result = dol_include_once($pathtoclass);
$tmpobjet = new $tabobj($db);
if (class_exists($tabobj)) $tmpobjet = new $tabobj($db);

$reflector = new ReflectionClass($tabobj);
$properties = $reflector->getProperties(); // Can also use get_object_vars
Expand Down
4 changes: 2 additions & 2 deletions htdocs/modulebuilder/template/class/myobject.class.php
Expand Up @@ -58,7 +58,7 @@ class MyObject extends CommonObject
public $picto = 'myobject';


// BEGIN MODULEBUILDER PROPERTIES - Do not remove this comment
// BEGIN MODULEBUILDER PROPERTIES
/**
* @var array Array with all fields and their property
*/
Expand All @@ -70,7 +70,7 @@ class MyObject extends CommonObject
'tms' =>array('type'=>'timestamp', 'label'=>'DateModification', 'notnull'=>true, 'position'=>500),
'status'=>array('type'=>'integer', 'label'=>'Status', 'index'=>true, 'position'=>1000),
);
// Do not remove this comment - END MODULEBUILDER PROPERTIES
// END MODULEBUILDER PROPERTIES



Expand Down

0 comments on commit 40b3ae2

Please sign in to comment.