public function setupDefaults($navs)
 {
     // Create a new layout called 'Default'
     $defaultLayout = CpNav_LayoutRecord::model()->findById('1');
     if ($defaultLayout) {
         $layoutsRecord = $defaultLayout;
     } else {
         $layoutsRecord = new CpNav_LayoutRecord();
     }
     //$layoutsRecord->id = '1';
     $layoutsRecord->name = 'Default';
     $layoutsRecord->isDefault = '1';
     $layoutsRecord->save();
     // With this new layout in mind, populate the nav table with items from the default cp nav
     $i = 0;
     foreach ($navs as $key => $value) {
         $navRecord = new CpNav_NavRecord();
         $navRecord->layoutId = '1';
         $navRecord->handle = $key;
         $navRecord->currLabel = $value['label'];
         $navRecord->prevLabel = $value['label'];
         $navRecord->enabled = '1';
         $navRecord->order = $i;
         $navRecord->url = array_key_exists('url', $value) ? $value['url'] : $key;
         $navRecord->prevUrl = $navRecord->url;
         $navRecord->manualNav = '0';
         $navRecord->newWindow = '0';
         $navRecord->save();
         $i++;
     }
 }
 public function deleteLayout(CpNav_LayoutModel $layout)
 {
     $layoutRecord = CpNav_LayoutRecord::model()->findById($layout->id);
     // Delete all fields for this layout
     $navRecords = CpNav_NavRecord::model()->deleteAll('layoutId = :layoutId', array('layoutId' => $layout->id));
     $layoutRecord->delete();
     return true;
 }
 public function safeUp()
 {
     // Create the Layouts table
     craft()->db->createCommand()->createTable('cpnav_layouts', array('name' => array('column' => ColumnType::Varchar), 'isDefault' => array('column' => ColumnType::TinyInt)), null, true);
     // Create default layout
     $layoutsRecord = new CpNav_LayoutRecord();
     $layoutsRecord->id = '1';
     $layoutsRecord->name = 'Default';
     $layoutsRecord->isDefault = '1';
     $layoutsRecord->save();
     // Rename the old table
     craft()->db->createCommand()->renameTable('cpnav', 'cpnav_navs');
     // Add LayoutId column to main table
     craft()->db->createCommand()->addColumnAfter('cpnav_navs', 'layoutId', ColumnType::Int, 'id');
     craft()->db->createCommand()->addForeignKey('cpnav_navs', 'layoutId', 'cpnav_layouts', 'id', 'SET NULL', null);
     // Populate each nav with the default layoutId for now
     craft()->db->createCommand()->update('cpnav_navs', array('layoutId' => '1'));
     return true;
 }