Overview

Namespaces

  • Composer
    • Autoload
  • Illuminate
    • Support
      • Contracts
      • Facades
  • Laravella
    • Crud
      • Exceptions
      • Facades
  • None
  • PHP

Classes

  • CrudDatabaseSeeder
  • CrudInstallCommand
  • CrudSeeder
  • CrudServiceProvider
  • CrudUpdateCommand
  • DbGopher
  • Log
  • Options
  • Params
  • PostCrudSeeder
  • SeedActions
  • SeedAssets
  • SeedGroups
  • SeedLogs
  • SeedMenus
  • SeedObjects
  • SeedOptions
  • SeedPageTypes
  • SeedSeverities
  • SeedTables
  • SeedUsergroups
  • SeedUsers
  • SeedViews
  • UpdateCMSFields
  • UpdateReferences
  • Overview
  • Namespace
  • Class
  • Tree
  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:  * Basic functions to manipulate meta data
 13:  */
 14: class CrudSeeder extends Seeder {
 15: 
 16:     private $idCache = array();
 17:     private $pkTypeId = null;
 18:     private $fkTypeId = null;
 19: 
 20:     /**
 21:      * Set the title of a page
 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:      * Set the Caption of an image (_db_medias.caption)
 33:      */
 34:     public function setHeroCaption($mediaId, $caption)
 35:     {
 36:         
 37:     }
 38: 
 39:     /*
 40:      * Set the page's type
 41:      */
 42:     public function setPageType() {
 43:         
 44:     }
 45:     
 46:     /*
 47:      * Set the asset's type
 48:      */
 49:     public function setAssetType() {
 50:         
 51:     }
 52:     
 53:     /**
 54:      * 
 55:      * @param type $id
 56:      * @param type $slugs
 57:      */
 58:     public function linkAssetPageGroups($assetGroup, $pageGroup){
 59:         
 60:     }
 61:     
 62:     
 63:     /**
 64:      * 
 65:      * @param type $tableName The name of the table 
 66:      * @param type $actionName The name of the action
 67:      * @param type $viewName The name of the view
 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:      * @param type $tableName
 84:      * @param type $actionName
 85:      * @param type $viewName
 86:      * @param type $values
 87:      * @return type
 88:      */
 89:     public function addPage($tableName, $actionName, $viewName, $values)
 90:     {
 91:         return $this->tableActionView($tableName, $actionName, $viewName, $values);
 92:     }
 93: 
 94:     /**
 95:      * Deprecated. Use addPage.
 96:      * 
 97:      * @param type $tableName
 98:      * @param type $actionName
 99:      * @param type $viewName
100:      * @param array $values
101:      * 
102:      * @deprecated 
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:      * @param type $url
126:      * @param type $type
127:      * @param type $vendor
128:      * @param type $version
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:      * @param type $id
143:      * @param type $slugs Use '*' for all pages
144:      */
145:     public function linkAssetPage($id, $slugs)
146:     {
147:         $pages = array();
148:         if (!is_array($slugs))
149:         {
150:             if ($slugs == '*')
151:             {
152:                 //all slugs
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:                 //specific slug
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:                 //an array of slugs
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:      * Update a reference to primary keys in _db_fields
206:      * 
207:      * @param type $fkTableName
208:      * @param type $fkFieldName
209:      * @param type $pkTableName
210:      * @param type $pkFieldName
211:      */
212:     public function updateReference($fkTableName, $fkFieldName, $pkTableName, $pkFieldName, $pkDisplayFieldName)
213:     {
214:         //get the id of the pkTableName in _db_tables
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:         //get the id of the primary key field in _db_fields
230:         //for each field in the _db_fields table there will thus be a reference to 
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: //KEEP THIS
259: //set the reference on the fk field
260:         /*
261:           DB::table('_db_fields')
262:           ->where('table_id', $fkTableId)
263:           ->where('name', $fkFieldName)
264:           ->update(array('pk_field_id' => $pkFieldId, 'pk_display_field_id' => $pkDisplayFieldId));
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: //        DB::table('_db_key_fields')->insert($insertValues);
292: 
293:         /*
294:           $this->__log("success", "updating record : {$fkRec->id}");
295: 
296:           DB::table('_db_fields')
297:           ->where('table_id', $fkTableId)
298:           ->where('name', $fkFieldName)
299:           ->update(array('pk_field_id' => $fieldId));
300:          */
301:     }
302: 
303:     /**
304:      * Get the id of a record based on the value of another field
305:      * 
306:      * @param type $whereField
307:      * @param type $whereValue
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:                 //$whereField is an array of key-value pairs
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: //                throw new DBException(count($recs) . ' Unique id not found where ' . $query);
334:                 return null;
335:             }
336:             else
337:             {
338:                 $idCache[$key] = $recs[0]->id;
339:             }
340:         }
341:         return $idCache[$key];
342:     }
343: 
344:     /**
345:      * Add a severity
346:      * 
347:      * @param type $severity
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:      * Add a new user
365:      * 
366:      * @param type $groupName
367:      * @param type $name
368:      * @param type $password
369:      * @param type $email
370:      * @param type $firstName
371:      * @param type $lastName
372:      * @return type
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:         //$group = DB::table('groups')->where('name', $groupName)->first();
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); //Config::get('crud::app.setup_user');
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: //            DB::table('users_groups')->insert(array('user_id' => $userId, 'usergroup_id' => $userGroup->id));
400:         }
401: 
402:         return $userId;
403:     }
404: 
405:     /**
406:      * 
407:      * Add an option
408:      * 
409:      * @param type $optionTypeId
410:      * @param type $name
411:      * @param type $value
412:      * @return type
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:      * Add an option type 
424:      * 
425:      * @param type $name
426:      * @param type $parentId
427:      * @return type
428:      */
429:     public function addOptionType($name, $parentId = null)
430:     {
431:         // an option with name "grand.parent.name"
432: //        if (is_null($parentId)) {
433: //            $parentA = explode('.', $name);
434: //            foreach($parentA as $parent) {
435: //                $optionTypes = DB::table('_db_option_types as ot1')
436: //                        ->join('_db_option_types as ot2', 'ot1.id', '=', 'ot2.parent_id')
437: //                        ->where('ot1.name', $parentName)
438: //                        ->where('ot2.name', $name)
439: //                        ->select('ot1.name', 'ot1.id')
440: //                        ->get();
441: //            }
442: //        }
443:         $optionType = array('name' => $name, 'parent_id' => $parentId);
444:         $optionTypeId = $this->updateOrInsert('_db_option_types', array('name' => $name, 'parent_id' => $parentId));
445: //        $optionTypeId = DB::table('_db_option_types')->insertGetId($optionType);
446:         Log::write(Log::INFO, $name . ' option type created');
447:         return $optionTypeId;
448:     }
449: 
450:     /**
451:      * Add an option type 
452:      * 
453:      * @param type $name
454:      * @param type $parentId
455:      * @return type
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:      * Add an asset type
486:      * 
487:      * @param type $typeName
488:      * @return type
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:      * @param type $pageType
501:      * @return type
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:      * Add a new usergroup
512:      * 
513:      * @param type $groupName
514:      */
515:     public function addGroup($groupName)
516:     {
517:         $group = array('name' => $groupName);     //can change permissions
518:         DB::table('groups')->insert($group);
519:         Log::write('info', $groupName . ' usergroup created');
520:     }
521: 
522:     /**
523:      * Updates a field or inserts a record if key does not exist
524:      * 
525:      * @param type $updateTable
526:      * @param type $whereValues an array of key value pairs , or an id
527:      */
528:     public function delete($updateTable, $whereValues)
529:     {
530:         $m = Model::getInstance($updateTable);
531:         if (is_array($whereValues))
532:         {
533:             //$whereField is an array of key-value pairs
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:      * Updates a field or inserts a record if key does not exist
548:      * 
549:      * @param type $updateTable The table that is to be updated
550:      * @param type $whereValues an array of key value pairs , or an id
551:      * @param type $insertValues Used as a single value if whereField is a string, else excluded
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:             //$whereField is an array of key-value pairs
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: //$queries = DB::getQueryLog();
577: //$last_query = end($queries);
578: //echo var_dump($last_query);
579: //echo 'last query';
580: 
581:         $ids = array();
582: 
583:         if (is_object($recs) && count($recs->modelKeys()) > 0)
584:         {
585:             //records exist so update
586:             foreach ($recs as $rec)
587:             {
588: //                $this->info('updating ' . $updateTable . ' ' . $rec->id . ' ' . PHP_EOL);
589:                 $updateM = DB::table($updateTable)->where('id', $rec->id)->update($insertValues);
590:                 $ids = $rec->id;
591:             }
592:         }
593:         else
594:         {
595: //            $this->info('inserting ' . $updateTable . ' ' . PHP_EOL);
596:             $id = DB::table($updateTable)->insertGetId($insertValues);
597:             $ids = $id;
598: //            $this->info($updateTable . ' inserted with id ' . $id . "\n");
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:      * Add a menu item
612:      * 
613:      * @param type $label
614:      * @param type $href
615:      * @param type $iconClass
616:      * @param type $parentId Can be the id or the label of the parent menu item
617:      * @return type
618:      */
619:     public function addMenu($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: //        $menuId = DB::table('_db_menus')->insertGetId($rec);
633:         Log::write('info', $label . ' menu created');
634:         return $menuId;
635:     }
636: 
637:     /**
638:      * Add menu permissions
639:      * 
640:      * @param type $menuId
641:      * @param type $groupName
642:      */
643:     public function addMenuPermissions($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: //                DB::table('_db_menu_permissions')->insertGetId(array('menu_id' => $menuId, 'usergroup_id' => $usergroupId));
655:             }
656:         }
657:     }
658: 
659:     /**
660:      * Add widget type
661:      * 
662:      * @param type $name
663:      * @return type
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:      * Add display_type
674:      * 
675:      * @param type $id
676:      * @param type $name
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:      * Add display_type
692:      * 
693:      * @param type $id
694:      * @param type $name
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:      * Add action to _db_actions
710:      * 
711:      * @param type $actionName
712:      * @return type
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:      * Add objects to _db_actions
724:      * 
725:      * @param type $actionName
726:      * @return type
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:      * populate _db_views
738:      * 
739:      * @param type $viewName
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:      * Populate table _db_pages
750:      * 
751:      * @param type $viewId
752:      * @param type $doPermissions Will also populate permissions tables if true
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); //frontendpages
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:                           if ($doPermissions)
790:                           {
791:                           foreach ($users as $user)
792:                           {
793:                           $arr = array('table_id' => $table->id, 'action_id' => $action->id, 'user_id' => $user->id);
794:                           DB::table('_db_user_permissions')->insert($arr);
795:                           Log::write("success", "Granted user " . $user->username . " \t action " . $action->name . " on \t table " . $table->name);
796:                           }
797:                           foreach ($usergroups as $usergroup)
798:                           {
799:                           $arr = array('table_id' => $table->id, 'action_id' => $action->id, 'usergroup_id' => $usergroup->id);
800:                           DB::table('_db_usergroup_permissions')->insert($arr);
801:                           Log::write("success", "Granted usergroup " . $usergroup->group . " \t action " . $action->name . " on table \t" . $table->name);
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:      * Replace _ with spaces and make first character of each word uppercase
821:      * 
822:      * @param type $name
823:      */
824:     public function makeLabel($name)
825:     {
826:         return ucwords(str_replace('_', ' ', $name));
827:     }
828: 
829:     /**
830:      * Returns varchar if fieldType = varchar(100) etc.
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:      * Returns 100 if fieldType = varchar(100) etc.
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:             //$this->__log("success", "fieldtype : $fieldType, start : $start, count : $count, len : $len");
855:         }
856: 
857:         return $len;
858:     }
859: 
860:     /**
861:      * Try and calculate the width of the widget to display the field in 
862:      */
863:     public function getFieldWidth($fieldType, $fieldLength)
864:     {
865:         return 220;
866:     }
867: 
868:     /**
869:      * Try and calculate the best widget to display the field in. Define the widget in json
870:      */
871:     public function getFieldWidget($fieldType, $fieldLength)
872:     {
873:         return ""; //'{widget" : "input", "attributes" : {"type" : "text"}}';
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: 
crud API documentation generated by ApiGen 2.8.0