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