PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/index.php

https://github.com/aalionte/Magento-Module-Creator
PHP | 546 lines | 455 code | 28 blank | 63 comment | 20 complexity | 1b3533b55010474c4bc68c339fd501f7 MD5 | raw file
  1. <?php
  2. /**
  3. * Module Creator
  4. *
  5. * @category Automator
  6. * @package Standalone
  7. * @version 0.0.9.1
  8. * @author Daniel Nitz <n.nitz@netz98.de>
  9. * @copyright Copyright (c) 2008 netz98 new media GmbH (http://www.netz98.de)
  10. * Credits for blank files go to alistek (adam) from the community:
  11. * http://www.magentocommerce.com/wiki/custom_module_with_custom_database_table
  12. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  13. *
  14. *
  15. * Patched by OggettoWeb team(http://oggettoweb.com)
  16. * @author Vladimir Penkin(penkinv@gmail.com)
  17. * @version 0.0.1
  18. *
  19. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  20. *
  21. * $Id$
  22. */
  23. $root = substr($_SERVER['SCRIPT_FILENAME'], 0, strrpos($_SERVER['SCRIPT_FILENAME'], '/') + 1);
  24. $shop = null;
  25. /**
  26. * Enter description here...
  27. *
  28. * @param string|array $from
  29. * @param string|array $to
  30. * @return boolean
  31. */
  32. function copyBlankoFiles($from, $to, $shop = null)
  33. {
  34. global $root;
  35. if (!is_array($from)) {
  36. $from = array($from);
  37. }
  38. if (!is_array($to)) {
  39. $to = array($to);
  40. }
  41. if ($shop === null) {
  42. $shop = $root . 'new/';
  43. if (!is_dir($shop)) {
  44. mkdir($shop);
  45. }
  46. }
  47. if (count($from) !== count($to)) {
  48. throw new Exception('Count of from -> to files do not match.');
  49. }
  50. foreach ($to as $file) {
  51. $newPath = substr($file, 0, strrpos($file, '/'));
  52. createFolderPath($newPath, $shop);
  53. }
  54. for ($i = 0; $i < count($to); $i++) {
  55. if (copy($root.$from[$i], $shop.$to[$i]) === false) {
  56. throw new Exception('Could not copy blanko files.');
  57. }
  58. }
  59. return true;
  60. }
  61. /**
  62. * Enter description here...
  63. *
  64. * @param string|array $paths
  65. * @return bolean
  66. */
  67. function createFolderPath($paths, $shop = null)
  68. {
  69. global $root;
  70. if (!is_array($paths)) {
  71. $paths = array($paths);
  72. }
  73. if ($shop === null) {
  74. $shop = $root;
  75. }
  76. foreach ($paths as $path) {
  77. $folders = explode('/', $path);
  78. $current = '';
  79. foreach ($folders as $folder) {
  80. $fp = $current . DIRECTORY_SEPARATOR . $folder;
  81. if (!is_dir($shop.$fp)) {
  82. if (mkdir($shop.$fp) === false) {
  83. throw new Exception('Could not create new path: '. $shop.$fp);
  84. }
  85. }
  86. $current = $fp;
  87. }
  88. }
  89. return true;
  90. }
  91. /**
  92. * Enter description here...
  93. *
  94. * @param array|string $files
  95. */
  96. function insertCustomVars($files, $shop = null)
  97. {
  98. global $root;
  99. if (!is_array($files)) {
  100. $files = array($files);
  101. }
  102. if ($shop === null) {
  103. $shop = $root . 'new'.DIRECTORY_SEPARATOR;
  104. }
  105. foreach ($files as $file) {
  106. $handle = fopen ($shop.$file, 'r+');
  107. $content = '';
  108. while (!feof($handle)) {
  109. $content .= fgets($handle);
  110. }
  111. fclose($handle);
  112. $type = strrchr($file, '.');
  113. switch ($type) {
  114. case '.xml':
  115. $content = replaceXml($content);
  116. break;
  117. case '.php':
  118. case '.phtml':
  119. $content = replacePhp($content);
  120. break;
  121. default:
  122. throw new Exception('Unknown file type found: '.$type);
  123. }
  124. $handle = fopen ($shop.$file, 'w');
  125. fputs($handle, $content);
  126. fclose($handle);
  127. }
  128. }
  129. /**
  130. * Enter description here...
  131. *
  132. * @param string $content
  133. * @return string
  134. */
  135. function replacePhp($content)
  136. {
  137. global $capNamespace, $lowNamespace, $capModule, $lowModule, $capModuleadmin, $lowModuleadmin;
  138. $search = array(
  139. '/<Namespace>/',
  140. '/<namespace>/',
  141. '/<Module>/',
  142. '/<module>/',
  143. '/<Moduleadmin>/',
  144. '/<moduleadmin>/',
  145. );
  146. $replace = array(
  147. $capNamespace,
  148. $lowNamespace,
  149. $capModule,
  150. $lowModule,
  151. $capModuleadmin,
  152. $lowModuleadmin,
  153. );
  154. return preg_replace($search, $replace, $content);
  155. }
  156. /**
  157. * Enter description here...
  158. *
  159. * @param string $content
  160. * @return string
  161. */
  162. function replaceXml($content)
  163. {
  164. global $capNamespace, $lowNamespace, $capModule, $lowModule, $capModuleadmin, $lowModuleadmin;
  165. $search = array(
  166. '/\[Namespace\]/',
  167. '/\[namespace\]/',
  168. '/\[Module\]/',
  169. '/\[module\]/',
  170. '/\[Moduleadmin\]/',
  171. '/\[moduleadmin\]/',
  172. );
  173. $replace = array(
  174. $capNamespace,
  175. $lowNamespace,
  176. $capModule,
  177. $lowModule,
  178. $capModuleadmin,
  179. $lowModuleadmin,
  180. );
  181. return preg_replace($search, $replace, $content);
  182. }
  183. /**
  184. * Enter description here...
  185. *
  186. * @param string $dir
  187. * @return boolean|string
  188. */
  189. function checkShopRoot($dir)
  190. {
  191. $dir = replaceDirSeparator($dir);
  192. if (substr($dir, strlen($dir) - 1, 1) !== DIRECTORY_SEPARATOR) {
  193. $dir .= DIRECTORY_SEPARATOR;
  194. }
  195. if (is_dir($dir . 'app')) {
  196. return $dir;
  197. }
  198. return false;
  199. }
  200. /**
  201. * Enter description here...
  202. *
  203. * @param unknown_type $dir
  204. * @return unknown
  205. */
  206. function replaceDirSeparator($dir)
  207. {
  208. $search = array('\\\\', '/');
  209. $dir = str_replace($search, DIRECTORY_SEPARATOR, $dir);
  210. return $dir;
  211. }
  212. /**
  213. * Enter description here...
  214. *
  215. * @param unknown_type $dir
  216. * @param unknown_type $module
  217. * @return unknown
  218. */
  219. function uninstallModule($dir, $module)
  220. {
  221. if (is_dir($dir.$module)) {
  222. $folder = rmRecurse($dir.$module);
  223. $sql = deleteSql($dir, $module);
  224. if ($folder and $sql) {
  225. return true;
  226. }
  227. }
  228. return false;
  229. }
  230. /**
  231. * Enter description here...
  232. *
  233. * @param unknown_type $dir
  234. * @return unknown
  235. */
  236. function getMagentoDatabaseSettings($dir)
  237. {
  238. $xml = simplexml_load_file($dir.'app/etc/local.xml', null, LIBXML_NOCDATA);
  239. $settings = array();
  240. $settings['dbUser'] = (string)$xml->global->resources->default_setup->connection->username;
  241. $settings['dbHost'] = (string)$xml->global->resources->default_setup->connection->host;
  242. $settings['dbPassword'] = (string)$xml->global->resources->default_setup->connection->password;
  243. $settings['dbName'] = (string)$xml->global->resources->default_setup->connection->dbname;
  244. return $settings;
  245. }
  246. /**
  247. * Enter description here...
  248. *
  249. * @param unknown_type $dir
  250. * @param unknown_type $module
  251. */
  252. function deleteSql($dir, $module)
  253. {
  254. $settings = getMagentoDatabaseSettings($dir);
  255. $connection = dbConnect($settings);
  256. $module = preg_replace('/\/$/', '', $module);
  257. $module = strtolower(substr(strrchr($module, '/'), 1));
  258. $tblPrefix = getTablePrefix($dir);
  259. $sql = "DELETE FROM ".$tblPrefix."core_resource WHERE code = '".$module."_setup'";
  260. $delete = mysql_query($sql);
  261. $sql = "DROP TABLE ".$tblPrefix.$module;
  262. $drop = mysql_query($sql);
  263. dbDisconnect($connection);
  264. if ($delete and $drop) {
  265. return true;
  266. }
  267. return false;
  268. }
  269. /**
  270. * Enter description here...
  271. *
  272. * @return unknown
  273. */
  274. function getTablePrefix($dir)
  275. {
  276. $xml = simplexml_load_file($dir.'app/etc/local.xml', null, LIBXML_NOCDATA);
  277. $prefix = (string)$xml->global->resources->db->table_prefix;
  278. if ($prefix != '') {
  279. return $prefix.'.';
  280. }
  281. return $prefix;
  282. }
  283. /**
  284. * Enter description here...
  285. *
  286. * @param array $settings
  287. * @return boolean
  288. */
  289. function dbConnect(array $settings)
  290. {
  291. $connection = mysql_connect($settings['dbHost'], $settings['dbUser'], $settings['dbPassword']) or die
  292. ('Could not connect to host.');
  293. mysql_select_db($settings['dbName']) or die
  294. ('Database does not exsist.');
  295. return $connection;
  296. }
  297. /**
  298. * Enter description here...
  299. *
  300. * @param unknown_type $connection
  301. */
  302. function dbDisconnect($connection)
  303. {
  304. mysql_close($connection);
  305. }
  306. /**
  307. * http://de3.php.net/manual/de/function.rmdir.php
  308. * ornthalas at NOSPAM dot gmail dot com
  309. *
  310. * @param string $filepath
  311. * @return unknown
  312. */
  313. function rmRecurse($filepath)
  314. {
  315. if (is_dir($filepath) && !is_link($filepath)) {
  316. if ($dh = opendir($filepath)) {
  317. while (($sf = readdir($dh)) !== false) {
  318. if ($sf == '.' || $sf == '..') {
  319. continue;
  320. }
  321. if (!rmRecurse($filepath.'/'.$sf)) {
  322. throw new Exception($filepath.'/'.$sf.' could not be deleted.');
  323. }
  324. }
  325. closedir($dh);
  326. }
  327. return rmdir($filepath);
  328. }
  329. return unlink($filepath);
  330. }
  331. //--------------------------------------------------------------
  332. /*$form = ' <h1>Magento Module Creator</h1>
  333. <form name="newmodule" method="POST" action="" />
  334. <div class="element">
  335. <div class="description">Namespace:<br /><span class="annotation">(e.g. your Company Name)</span></div>
  336. <input name="namespace" class="text" type="text" length="50" value="'.$_POST['namespace'].'" />
  337. </div>
  338. <div id="module" class="element">
  339. <div class="description">Module:<br /><span class="annotation">(e.g. Blog, News, Forum)</span></div>
  340. <input name="module" class="text" type="text" length="50" value="'.$_POST['module'].'" />
  341. </div>
  342. <div id="moduleadmin" class="element">
  343. <div class="description">Moduleadmin:<br /><span class="annotation">(e.g. Blogadmin, NewsAdmin, Control)</span></div>
  344. <input name="moduleadmin" class="text" type="text" length="50" value="'.$_POST['moduleadmin'].'" />
  345. </div>
  346. <div id="magento_root" class="element">
  347. <div class="description">Magento Root Directory:<br /><span class="annotation">(optional, required for uninstall)</span></div>
  348. <input name="magento_root" class="text" type="text" length="255" value="'.replaceDirSeparator($_POST['magento_root']).'" />
  349. </div>
  350. <div id="interface" class="element">
  351. <div class="description">Design:<br /><span class="annotation">(interface, default is \'default\')</span></div>
  352. <input name="interface" class="text" type="text" length="100" value="'.$_POST['interface'].'" />
  353. </div>
  354. <div id="theme" class="element">
  355. <div class="description">Design:<br /><span class="annotation">(theme, default is \'default\')</span></div>
  356. <input name="theme" class="text" type="text" length="100" value="'.$_POST['theme'].'" />
  357. </div>
  358. <div class="element">
  359. <div class="description">Generage admin module?:<br /><span class="annotation">(default is \'yes\')</span></div>
  360. <input name="generate-admin" class="checkbox" type="checkbox" checked /> <a href="#" title="The admin module will be separated from main module">?</a>
  361. </div>
  362. <div id="submit">
  363. <input type="submit" value="create" name="create" id="create" /> <input type="submit" value="uninstall" name="uninstall" id="uninstall" />
  364. </div>
  365. </form>';*/
  366. if(!empty($_POST)) {
  367. $namespace = $_POST['namespace'];
  368. $module = $_POST['module'];
  369. $moduleadmin = $_POST['moduleadmin'];
  370. $interface = $_POST['interface'];
  371. $theme = $_POST['theme'];
  372. if ($interface == '') {
  373. $interface = 'default';
  374. }
  375. if ($theme == '') {
  376. $theme = 'default';
  377. }
  378. if ($_POST['magento_root'] != '') {
  379. if (checkShopRoot($_POST['magento_root']) !== false) {
  380. $shop = checkShopRoot($_POST['magento_root']);
  381. } else {
  382. throw new Exception('This is not a valid Magento install dir: ' . $_POST['magento_root']);
  383. }
  384. }
  385. $capNamespace = ucfirst($namespace);
  386. $lowNamespace = strtolower($namespace);
  387. $capModule = ucfirst($module);
  388. $lowModule = strtolower($module);
  389. $capModuleadmin = ucfirst($moduleadmin);
  390. $lowModuleadmin = strtolower($moduleadmin);
  391. $fromFiles = array(
  392. 'blank/app/etc/modules/Namespace_Module.xml',
  393. 'blank/app/code/local/Namespace/Module/Block/Module.php',
  394. 'blank/app/code/local/Namespace/Module/controllers/IndexController.php',
  395. 'blank/app/code/local/Namespace/Module/etc/config.xml',
  396. 'blank/app/code/local/Namespace/Moduleadmin/etc/config.xml',
  397. 'blank/app/code/local/Namespace/Module/Model/Module.php',
  398. 'blank/app/code/local/Namespace/Module/Model/Mysql4/Module.php',
  399. 'blank/app/code/local/Namespace/Module/Model/Mysql4/Module/Collection.php',
  400. 'blank/app/code/local/Namespace/Module/Model/Status.php',
  401. 'blank/app/code/local/Namespace/Module/sql/module_setup/mysql4-install-0.1.0.php',
  402. 'blank/app/design/frontend/interface/theme/layout/module.xml',
  403. 'blank/app/design/frontend/interface/theme/template/module/module.phtml',
  404. 'blank/app/code/local/Namespace/Module/Helper/Data.php',
  405. 'blank/app/design/adminhtml/interface/theme/layout/module.xml',
  406. );
  407. $toFiles = array(
  408. 'app/etc/modules/'.$capNamespace.'_'.$capModule.'.xml',
  409. 'app/code/local/'.$capNamespace.'/'.$capModule.'/Block/'.$capModule.'.php',
  410. 'app/code/local/'.$capNamespace.'/'.$capModule.'/controllers/IndexController.php',
  411. 'app/code/local/'.$capNamespace.'/'.$capModule.'/etc/config.xml',
  412. 'app/code/local/'.$capNamespace.'/'.$capModuleadmin.'/etc/config.xml',
  413. 'app/code/local/'.$capNamespace.'/'.$capModule.'/Model/'.$capModule.'.php',
  414. 'app/code/local/'.$capNamespace.'/'.$capModule.'/Model/Mysql4/'.$capModule.'.php',
  415. 'app/code/local/'.$capNamespace.'/'.$capModule.'/Model/Mysql4/'.$capModule.'/Collection.php',
  416. 'app/code/local/'.$capNamespace.'/'.$capModule.'/Model/Status.php',
  417. 'app/code/local/'.$capNamespace.'/'.$capModule.'/sql/'.$lowModule.'_setup/mysql4-install-0.1.0.php',
  418. 'app/design/frontend/'.$interface.'/'.$theme.'/layout/'.$lowModule.'.xml',
  419. 'app/design/frontend/'.$interface.'/'.$theme.'/template/'.$lowModule.'/'.$lowModule.'.phtml',
  420. 'app/code/local/'.$capNamespace.'/'.$capModule.'/Helper/Data.php',
  421. 'app/design/adminhtml/'.$interface.'/'.$theme.'/layout/'.$lowModule.'.xml'
  422. );
  423. /* generatin admin module*/
  424. if ($_POST['generate-admin']) {
  425. $fromFiles[] = 'blank/app/etc/modules/Namespace_Moduleadmin.xml';
  426. $fromFiles[] = 'blank/app/code/local/Namespace/Moduleadmin/Block/Module.php';
  427. $fromFiles[] = 'blank/app/code/local/Namespace/Moduleadmin/Block/Module/Edit.php';
  428. $fromFiles[] = 'blank/app/code/local/Namespace/Moduleadmin/Block/Module/Grid.php';
  429. $fromFiles[] = 'blank/app/code/local/Namespace/Moduleadmin/Block/Module/Edit/Form.php';
  430. $fromFiles[] = 'blank/app/code/local/Namespace/Moduleadmin/Block/Module/Edit/Tabs.php';
  431. $fromFiles[] = 'blank/app/code/local/Namespace/Moduleadmin/Block/Module/Edit/Tab/Form.php';
  432. $fromFiles[] = 'blank/app/code/local/Namespace/Moduleadmin/controllers/ModuleController.php';
  433. $fromFiles[] = 'blank/app/code/local/Namespace/Moduleadmin/Helper/Data.php';
  434. $toFiles[] = 'app/etc/modules/'.$capNamespace.'_'.$capModuleadmin.'.xml';
  435. $toFiles[] = 'app/code/local/'.$capNamespace.'/'.$capModuleadmin.'/Block/'.$capModule.'.php';
  436. $toFiles[] = 'app/code/local/'.$capNamespace.'/'.$capModuleadmin.'/Block/'.$capModule.'/Edit.php';
  437. $toFiles[] = 'app/code/local/'.$capNamespace.'/'.$capModuleadmin.'/Block/'.$capModule.'/Grid.php';
  438. $toFiles[] = 'app/code/local/'.$capNamespace.'/'.$capModuleadmin.'/Block/'.$capModule.'/Edit/Form.php';
  439. $toFiles[] = 'app/code/local/'.$capNamespace.'/'.$capModuleadmin.'/Block/'.$capModule.'/Edit/Tabs.php';
  440. $toFiles[] = 'app/code/local/'.$capNamespace.'/'.$capModuleadmin.'/Block/'.$capModule.'/Edit/Tab/Form.php';
  441. $toFiles[] = 'app/code/local/'.$capNamespace.'/'.$capModuleadmin.'/controllers/'.$capModule.'Controller.php';
  442. $toFiles[] = 'app/code/local/'.$capNamespace.'/'.$capModuleadmin.'/Helper/Data.php';
  443. }
  444. if ($_POST['create']) {
  445. if (!empty($module) && !empty($namespace)) {
  446. copyBlankoFiles($fromFiles, $toFiles, $shop);
  447. insertCustomVars($toFiles, $shop);
  448. $message = '<div id="message"><p><strong>New Module successfully created!</strong></p>
  449. <p>Go to the folder where this file is located. You\'ll find a new folder called \'new\'.</p>
  450. <p>Within are all required files for your new module. This folder has the same structure as your Magento Installation.
  451. Just make sure you replace the \'interface\' and \'theme\' folder with your current design path. If you want to add custom
  452. DB-fields go to /new/local/'.$capNamespace.'/'.$capModule.'/sql/module_setup/mysql4-install-0.1.0.php
  453. and make your changes for line 12 to 14.</p><p>Copy /new/'.$capNamespace.'_'.$capModule.'.xml to /app/etc/modules/. If you
  454. chose a Magento Install dir, all files can be found in their according directory.
  455. Implement your module functionallity and you\'re done!</p>
  456. <p><strong>List of created files:</strong></p>';
  457. foreach ($toFiles as $file) {
  458. $message .= '<p class="file">' . $file . '</p>';
  459. }
  460. $message .= '</div>';
  461. } else {
  462. $message = '<div id="message"><p>Please fill out out required fields.</p></div>';
  463. }
  464. }
  465. if ($_POST['uninstall']) {
  466. if (uninstallModule($shop, 'app/code/local/'.$capNamespace.'/'.$capModule.'/') === true) {
  467. $message = '<div id="message"><p><strong>Module successfully uninstalled!</strong></p></div>';
  468. } else {
  469. $message = '<div id="message"><p><strong>Couldn\'t find module in Magento installation.</strong></p>
  470. <p>After creating a module, you need to run Magento to install all new required tables
  471. automatically. Also make sure you deactivate/refresh all Magento caches. Otherwise
  472. no new modules will be recognized.</p></div>';
  473. }
  474. }
  475. } else {
  476. $message = '<div id="message">To create a new module, insert Namespace and a Module name (e.g. Blog, Forum, etc.) as well as
  477. your design above. If you want it to be installed right away into your Magento, enter your Magento install path.
  478. This script will create a simple news module on which you can build your own module.</div>';
  479. }
  480. include('header.php');
  481. //print $form;
  482. print $message;
  483. include('footer.php');