1: <?php
2:
3: namespace Laravella\Crud;
4:
5: use Laravella\Crud\Log;
6: use \Seeder;
7: use \Model;
8: use \DB;
9: use \Hash;
10:
11: 12: 13:
14: class CrudSeeder extends Seeder {
15:
16: private $idCache = array();
17: private $pkTypeId = null;
18: private $fkTypeId = null;
19:
20: 21: 22:
23: public function setTitle($slug, $title)
24: {
25: $recs = DB::table('_db_pages')
26: ->where('slug', strtolower($slug))
27: ->update(array('title' => $title));
28: return $recs;
29: }
30:
31: 32: 33:
34: public function setHeroCaption($mediaId, $caption)
35: {
36:
37: }
38:
39: 40: 41:
42: public function setPageType() {
43:
44: }
45:
46: 47: 48:
49: public function setAssetType() {
50:
51: }
52:
53: 54: 55: 56: 57:
58: public function linkAssetPageGroups($assetGroup, $pageGroup){
59:
60: }
61:
62:
63: 64: 65: 66: 67: 68:
69: public function tableActionViewId($tableName, $actionName, $viewName)
70: {
71: $tableId = $this->getId('_db_tables', 'name', $tableName);
72: $actionId = $this->getId('_db_actions', 'name', $actionName);
73: $viewId = $this->getId('_db_views', 'name', $viewName);
74:
75: $recs = DB::table('_db_pages')->where('table_id', $tableId)
76: ->where('action_id', $actionId)
77: ->where('view_id', $viewId);
78: return $recs;
79: }
80:
81: 82: 83: 84: 85: 86: 87: 88:
89: public function addPage($tableName, $actionName, $viewName, $values)
90: {
91: return $this->tableActionView($tableName, $actionName, $viewName, $values);
92: }
93:
94: 95: 96: 97: 98: 99: 100: 101: 102: 103:
104: public function tableActionView($tableName, $actionName, $viewName, $values)
105: {
106: $tableId = $this->getId('_db_tables', 'name', $tableName);
107: $actionId = $this->getId('_db_actions', 'name', $actionName);
108: $slug = strtolower($tableName . '_' . $actionName);
109: $values['slug'] = $slug;
110: $id = null;
111: if (!is_null($viewName) && !empty($viewName))
112: {
113: $viewId = $this->getId('_db_views', 'name', $viewName);
114: $id = $this->updateOrInsert('_db_pages', array('table_id' => $tableId, 'action_id' => $actionId, 'view_id' => $viewId), $values);
115: }
116: else
117: {
118: $id = $this->updateOrInsert('_db_pages', array('table_id' => $tableId, 'action_id' => $actionId), $values);
119: }
120: return $id;
121: }
122:
123: 124: 125: 126: 127: 128: 129:
130: public function addAsset($url, $type = '', $assetGroup='', $position='top', $vendor = '', $version = '')
131: {
132: $optionTypes = $this->getOptionType($assetGroup);
133: $assetTypeID = $optionTypes[0]['id'];
134: $values = array('url' => $url, 'type' => $type, 'asset_type_id' => $assetTypeID,
135: 'vendor' => $vendor, 'version' => $version, 'position' => $position);
136: $id = $this->updateOrInsert('_db_assets', array('url' => $url), $values);
137: return $id;
138: }
139:
140: 141: 142: 143: 144:
145: public function linkAssetPage($id, $slugs)
146: {
147: $pages = array();
148: if (!is_array($slugs))
149: {
150: if ($slugs == '*')
151: {
152:
153: $this->info("linking asset id $id with *");
154: $pageTypes = DB::table('_db_option_types as ot1')
155: ->join('_db_option_types as ot2', 'ot1.parent_id', '=', 'ot2.id')
156: ->select('ot1.id')
157: ->where('ot2.name', 'pages')->get();
158: foreach ($pageTypes as $pageType)
159: {
160: $this->updateOrInsert('_db_page_assets', array('asset_type_id' => $id, 'page_type_id' => $pageType->id));
161: }
162: }
163: else
164: {
165:
166: $this->info("linking asset id $id with $slugs");
167: $pageTypes = DB::table('_db_option_types as ot1')
168: ->join('_db_option_types as ot2', 'ot1.parent_id', '=', 'ot2.id')
169: ->where('ot2.name', 'pages')
170: ->where('ot1.name', $slugs)
171: ->select('ot1.id')
172: ->get();
173: foreach ($pageTypes as $pageType)
174: {
175: $this->updateOrInsert('_db_page_assets', array('asset_type_id' => $id, 'page_id' => $pageType->id));
176: }
177: }
178: }
179: else
180: {
181: foreach ($slugs as $slug)
182: {
183:
184: $pageTypes = DB::table('_db_option_types as ot1')
185: ->join('_db_option_types as ot2', 'ot1.parent_id', '=', 'ot2.id')
186: ->where('ot2.name', 'pages')
187: ->where('ot1.name', $slug)
188: ->select('ot1.id')
189: ->get();
190: foreach ($pageTypes as $pageType)
191: {
192: $this->info("linking asset id $id with $pageType");
193: $this->updateOrInsert('_db_page_assets', array('asset_type_id' => $id, 'page_id' => $pageType->id));
194: }
195: }
196: }
197: }
198:
199: public function info($message)
200: {
201: Log::write(Log::INFO, $message);
202: }
203:
204: 205: 206: 207: 208: 209: 210: 211:
212: public function updateReference($fkTableName, $fkFieldName, $pkTableName, $pkFieldName, $pkDisplayFieldName)
213: {
214:
215: $fkTableId = DB::table('_db_tables')->where('name', $fkTableName)->pluck('id');
216:
217: $pkTableId = DB::table('_db_tables')->where('name', $pkTableName)->pluck('id');
218:
219: if (!isset($this->pkTypeId))
220: {
221: $this->pkTypeId = DB::table('_db_key_types')->where('name', 'primary')->pluck('id');
222: }
223:
224: if (!isset($this->fkTypeId))
225: {
226: $this->fkTypeId = DB::table('_db_key_types')->where('name', 'foreign')->pluck('id');
227: }
228:
229:
230:
231: $pkFieldId = DB::table('_db_fields')
232: ->where('table_id', $pkTableId)
233: ->where('name', $pkFieldName)
234: ->pluck('id');
235:
236: $pkDisplayFieldId = DB::table('_db_fields')
237: ->where('table_id', $pkTableId)
238: ->where('name', $pkDisplayFieldName)
239: ->pluck('id');
240:
241: $fkFieldId = DB::table('_db_fields')
242: ->where('table_id', $fkTableId)
243: ->where('name', $fkFieldName)
244: ->pluck('id');
245:
246: $message = "inserting into _db_fields : where " .
247: "pkTableName = $pkTableName, " .
248: "pkFieldName = $pkFieldName, " .
249: "pkTableId = $pkTableId, " .
250: "pkFieldId = $pkFieldId, " .
251: "fkTableName = $fkTableName, " .
252: "fkFieldName = $fkFieldName, " .
253: "fkTableId = $fkTableId, " .
254: "fkFieldId = $fkFieldId";
255:
256: Log::write("success", $message);
257:
258:
259:
260: 261: 262: 263: 264: 265:
266:
267: if (empty($fkFieldId) || empty($pkFieldId))
268: {
269: echo "\n There was an error finding the keys from the database. \n
270: You might have a problem in Laravella\Crud\UpdateReferences \n
271: or another class that extends Laravella\Crud\CrudSeeder and calls updateReference() \n";
272: echo $message . "\n";
273: die;
274: }
275:
276: $keyName = "{$pkTableName}.{$pkFieldName} - {$fkTableName}.{$fkFieldName}";
277:
278: $keyId = DB::table('_db_keys')->insertGetId(array('name' => $keyName));
279:
280: $insertValues = array(
281: 'order' => 0,
282: 'key_id' => $keyId,
283: 'pk_field_id' => $pkFieldId,
284: 'pk_display_field_id' => $pkDisplayFieldId,
285: 'fk_field_id' => $fkFieldId,
286: 'fk_display_field_id' => $fkFieldId,
287: 'key_type_id' => $this->pkTypeId);
288:
289: $this->updateOrInsert('_db_key_fields', $insertValues, $insertValues);
290:
291:
292:
293: 294: 295: 296: 297: 298: 299: 300:
301: }
302:
303: 304: 305: 306: 307: 308:
309: public function getId($table, $whereField, $whereValue = null)
310: {
311: $key = $table . ':' . $whereField . ':' . $whereValue;
312: if (!isset($this->idCache[$key]))
313: {
314: $m = Model::getInstance(array(), $table);
315: $query = $table . ' ';
316: if (is_array($whereField))
317: {
318:
319: foreach ($whereField as $key => $value)
320: {
321: $m = $m->where($key, $value);
322: $query .= $key . '=\'' . $value . '\' ';
323: }
324: }
325: else
326: {
327: $m = $m->where($whereField, $whereValue);
328: $query .= $whereField . '=\'' . $whereValue . '\' ';
329: }
330: $recs = $m->get();
331: if (count($recs) != 1)
332: {
333:
334: return null;
335: }
336: else
337: {
338: $idCache[$key] = $recs[0]->id;
339: }
340: }
341: return $idCache[$key];
342: }
343:
344: 345: 346: 347: 348:
349: public function addSeverity($severity)
350: {
351: $arr = array("name" => $severity);
352: $viewId = DB::table('_db_severities')->insertGetId($arr);
353: Log::write("success", " - $severity severity inserted");
354: }
355:
356: public function makeApiKey()
357: {
358: $password = rand(23450987, 234509870);
359: $password = md5($password);
360: return $password;
361: }
362:
363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373:
374: public function createUser($groupName, $name, $password, $email, $firstName, $lastName)
375: {
376:
377: $this->command->info($groupName . ' ' . $name);
378:
379: Log::write(Log::INFO, '[' . $groupName . '] ' . $name);
380:
381: $hashPass = Hash::make($password);
382:
383:
384: $userGroup = DB::table('usergroups')->where('group', $groupName)->first();
385:
386: $adminUser = array('username' => $name, 'password' => $hashPass, 'email' => $email, 'first_name' => $firstName, 'last_name' => $lastName);
387: $adminUser['activated'] = true;
388: $adminUser['api_token'] = $this->makeApiKey();
389: if (is_object($userGroup))
390: {
391: $adminUser['usergroup_id'] = $userGroup->id;
392: $this->command->info('usergroup id is : ' . $userGroup->id);
393: }
394:
395: $userId = DB::table('users')->insertGetId($adminUser);
396:
397: if (is_object($userGroup))
398: {
399:
400: }
401:
402: return $userId;
403: }
404:
405: 406: 407: 408: 409: 410: 411: 412: 413:
414: public function addOption($optionTypeId, $name, $value)
415: {
416: $option = array('option_type_id' => $optionTypeId, 'name' => $name, 'value' => $value);
417: $optionId = DB::table('_db_options')->insertGetId($option);
418: Log::write(Log::INFO, $name . ' option created');
419: return $optionId;
420: }
421:
422: 423: 424: 425: 426: 427: 428:
429: public function addOptionType($name, $parentId = null)
430: {
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443: $optionType = array('name' => $name, 'parent_id' => $parentId);
444: $optionTypeId = $this->updateOrInsert('_db_option_types', array('name' => $name, 'parent_id' => $parentId));
445:
446: Log::write(Log::INFO, $name . ' option type created');
447: return $optionTypeId;
448: }
449:
450: 451: 452: 453: 454: 455: 456:
457: public function getOptionType($name, $parentName = null)
458: {
459: $optionTypeA = array();
460: $optionTypes = null;
461: if (!is_null($parentName) && !empty($parentName))
462: {
463: $optionTypes = DB::table('_db_option_types as ot1')
464: ->join('_db_option_types as ot2', 'ot1.id', '=', 'ot2.parent_id')
465: ->where('ot1.name', $parentName)
466: ->where('ot2.name', $name)
467: ->select('ot1.name', 'ot1.id')
468: ->get();
469: }
470: else
471: {
472: $optionTypes = DB::table('_db_option_types as ot1')
473: ->where('ot1.name', $name)
474: ->select('ot1.name', 'ot1.id')
475: ->get();
476: }
477: foreach($optionTypes as $optionType)
478: {
479: $optionTypeA[] = array('id' => $optionType->id, 'name' => $optionType->name);
480: }
481: return $optionTypeA;
482: }
483:
484: 485: 486: 487: 488: 489:
490: public function addAssetType($typeName)
491: {
492: $crudId = $this->addOptionType('crud');
493: $assetsId = $this->addOptionType('assets', $crudId);
494: $assetTypeId = $this->addOptionType($typeName, $assetsId);
495: return $assetTypeId;
496: }
497:
498: 499: 500: 501: 502:
503: public function addPageType($pageType) {
504: $crudId = $this->addOptionType('crud');
505: $pagesId = $this->addOptionType('pages', $crudId);
506: $pageTypeId = $this->addOptionType($pageType, $pagesId);
507: return $pageTypeId;
508: }
509:
510: 511: 512: 513: 514:
515: public function addGroup($groupName)
516: {
517: $group = array('name' => $groupName);
518: DB::table('groups')->insert($group);
519: Log::write('info', $groupName . ' usergroup created');
520: }
521:
522: 523: 524: 525: 526: 527:
528: public function delete($updateTable, $whereValues)
529: {
530: $m = Model::getInstance($updateTable);
531: if (is_array($whereValues))
532: {
533:
534: foreach ($whereValues as $key => $value)
535: {
536: $m = $m->where($key, $value);
537: }
538: }
539: else
540: {
541: $m = $m->where('id', $whereValues);
542: }
543: $m->delete();
544: }
545:
546: 547: 548: 549: 550: 551: 552:
553: public function updateOrInsert($updateTable, $whereValues, array $insertValues = null)
554: {
555:
556: if (is_null($insertValues))
557: {
558: $insertValues = $whereValues;
559: }
560:
561: $m = Model::getInstance($updateTable);
562: if (is_array($whereValues))
563: {
564:
565: foreach ($whereValues as $key => $value)
566: {
567: $m = $m->where($key, $value);
568: }
569: }
570: else
571: {
572: $m = $m->where('id', $whereValues);
573: }
574: $recs = $m->distinct()->get();
575:
576:
577:
578:
579:
580:
581: $ids = array();
582:
583: if (is_object($recs) && count($recs->modelKeys()) > 0)
584: {
585:
586: foreach ($recs as $rec)
587: {
588:
589: $updateM = DB::table($updateTable)->where('id', $rec->id)->update($insertValues);
590: $ids = $rec->id;
591: }
592: }
593: else
594: {
595:
596: $id = DB::table($updateTable)->insertGetId($insertValues);
597: $ids = $id;
598:
599: }
600:
601: return $ids;
602: }
603:
604: public function addDivider($parentId)
605: {
606: $rec = array('label' => 'divider', 'href' => '', 'parent_id' => $parentId, 'icon_class' => '');
607: $menuId = DB::table('_db_menus')->insertGetId($rec);
608: }
609:
610: 611: 612: 613: 614: 615: 616: 617: 618:
619: public function ($label, $href, $iconClass = 'icon-file', $parentId = null)
620: {
621: if (is_string($parentId))
622: {
623: $parentId = DB::table('_db_menus')->where('label', $parentId)->first()->id;
624: }
625:
626: $rec = array('label' => $label, 'href' => $href, 'parent_id' => $parentId, 'icon_class' => $iconClass);
627: $menuId = $this->updateOrInsert('_db_menus', $rec, $rec);
628: if (is_array($menuId))
629: {
630: $menuId = $menuId[0];
631: }
632:
633: Log::write('info', $label . ' menu created');
634: return $menuId;
635: }
636:
637: 638: 639: 640: 641: 642:
643: public function ($menuId = null, $groupName = '')
644: {
645: if (!is_null($menuId))
646: {
647: $usergroup = DB::table('usergroups')->where('group', $groupName)->first();
648: if (is_object($usergroup))
649: {
650: $usergroupId = $usergroup->id;
651:
652: $this->updateOrInsert('_db_menu_permissions', array('menu_id' => $menuId, 'usergroup_id' => $usergroupId));
653:
654:
655: }
656: }
657: }
658:
659: 660: 661: 662: 663: 664:
665: public function addWidgetType($name)
666: {
667: $widgetTypeId = DB::table('_db_widget_types')->insertGetId(array('name' => $name));
668: Log::write(Log::INFO, $name . ' widget type created');
669: return $widgetTypeId;
670: }
671:
672: 673: 674: 675: 676: 677:
678: public function addDisplayType($name, $id = null)
679: {
680: $displayTypes = array('name' => $name);
681: if (!is_null($id))
682: {
683: $displayTypes['id'] = $id;
684: }
685: $id = DB::table('_db_display_types')->insertGetId($displayTypes);
686: Log::write(Log::INFO, $name . ' display types created');
687: return $id;
688: }
689:
690: 691: 692: 693: 694: 695:
696: public function addKeyType($name, $id = null)
697: {
698: $keyTypes = array('name' => $name);
699: if (!is_null($id))
700: {
701: $keyTypes['id'] = $id;
702: }
703: $id = DB::table('_db_key_types')->insertGetId($keyTypes);
704: Log::write(Log::INFO, $name . ' key type created');
705: return $id;
706: }
707:
708: 709: 710: 711: 712: 713:
714: public function addAction($actionName)
715: {
716: $arr = array("name" => $actionName);
717: $id = DB::table('_db_actions')->insertGetId($arr);
718: Log::write("success", " - $actionName action created");
719: return $id;
720: }
721:
722: 723: 724: 725: 726: 727:
728: public function addObject($objectName)
729: {
730: $arr = array("name" => $objectName);
731: $id = DB::table('_db_objects')->insertGetId($arr);
732: Log::write("success", " - $objectName object created");
733: return $id;
734: }
735:
736: 737: 738: 739: 740:
741: public function addView($viewName)
742: {
743: $arr = array("name" => $viewName);
744: $viewId = DB::table('_db_views')->insertGetId($arr);
745: Log::write("success", " - $viewName view inserted");
746: }
747:
748: 749: 750: 751: 752: 753: 754:
755: public function populateTableActions($doPermissions = false)
756: {
757: try
758: {
759: DB::table('_db_pages')->delete();
760:
761: $tables = DB::table('_db_tables')->get();
762: $actions = DB::table('_db_actions')->get();
763: $views = DB::table('_db_views')->get();
764:
765: if ($doPermissions)
766: {
767: $users = DB::table('users')->get();
768: $usergroups = DB::table('usergroups')->get();
769: }
770:
771: foreach ($views as $view)
772: {
773: echo $view->name."\n";
774: $pageTypes = $this->getOptionType($view->name);
775: echo var_dump($pageTypes);
776: $pageTypeId = $pageTypes[0]['id'];
777: foreach ($tables as $table)
778: {
779: foreach ($actions as $action)
780: {
781: $slug = strtolower($table->name . '_' . $action->name);
782: Log::write("info", "Linking table " . $table->name . ", \t view " . $view->name . ", \t action " . $action->name);
783: $arrTav = array('table_id' => $table->id, 'action_id' => $action->id,
784: 'view_id' => $view->id, 'page_size' => 10, 'title' => $table->name, 'slug' => $slug,
785: 'page_type_id'=>$pageTypeId);
786:
787: DB::table('_db_pages')->insert($arrTav);
788: 789: 790: 791: 792: 793: 794: 795: 796: 797: 798: 799: 800: 801: 802: 803: 804: 805:
806: }
807: }
808: }
809: }
810: catch (Exception $e)
811: {
812: Log::write("success", $e->getMessage());
813: $message = "Error inserting record into table.";
814: Log::write("success", $message);
815: throw new Exception($message, 1, $e);
816: }
817: }
818:
819: 820: 821: 822: 823:
824: public function makeLabel($name)
825: {
826: return ucwords(str_replace('_', ' ', $name));
827: }
828:
829: 830: 831:
832: public function getFieldType($fieldType)
833: {
834: $start = strpos($fieldType, '(');
835: if ($start > 0)
836: {
837: $fieldType = substr($fieldType, 0, $start);
838: Log::write("success", "fieldtype : $fieldType");
839: }
840: return $fieldType;
841: }
842:
843: 844: 845:
846: public function getFieldLength($fieldType)
847: {
848: $start = strpos($fieldType, '(') + 1;
849: $len = null;
850: if ($start > 0)
851: {
852: $count = strpos($fieldType, ')') - $start;
853: $len = substr($fieldType, $start, $count);
854:
855: }
856:
857: return $len;
858: }
859:
860: 861: 862:
863: public function getFieldWidth($fieldType, $fieldLength)
864: {
865: return 220;
866: }
867:
868: 869: 870:
871: public function getFieldWidget($fieldType, $fieldLength)
872: {
873: return "";
874: }
875:
876: public function getDisplayType($colRec, $types)
877: {
878:
879: $displayTypeId = $types['edit'];
880:
881: if ($colRec['name'] == "created_at" || $colRec['name'] == "updated_at")
882: {
883: $displayTypeId = $types['nodisplay'];
884: }
885: return $displayTypeId;
886: }
887:
888: }
889:
890: ?>
891: