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 checkIfUpdateNeeded($allNavs, $navs)
 {
     $layoutId = '1';
     // We're actually looping through each layout in our system, but only returning the one we asked for!
     // That way, we can easily handle re-generating all layouts
     $allLayouts = craft()->cpNav_layout->getAllLayouts();
     foreach ($allLayouts as $layout) {
         // Firstly, a quick size check between the default nav and our copy (manual links not included)
         // will tell us if we need to look at whats been added or missing
         // Get all records that are not manually created by user
         $manualNav = CpNav_NavRecord::model()->findAll(array('condition' => 'layoutId = ' . $layout->id . ' AND (manualNav IS NULL OR manualNav <> 1)', 'index' => 'handle'));
         // If not equal, looks like something has changed!
         if (count($manualNav) != count($navs)) {
             if (count($manualNav) < count($navs)) {
                 // There are new menu items that have been added
                 $i = 0;
                 foreach ($navs as $key => $value) {
                     if (!array_key_exists($key, $manualNav)) {
                         // This is the menu item to add to our DB
                         // Create new menu item
                         craft()->cpNav_nav->createNav(array('layoutId' => $layout->id, 'handle' => $key, 'label' => $value['label'], 'url' => array_key_exists('url', $value) ? $value['url'] : $key, 'order' => $i));
                         if ($layoutId == $layout->id) {
                             $allNavs = craft()->cpNav_nav->getNavsByLayoutId($layoutId);
                         }
                     }
                     $i++;
                 }
             } else {
                 // Some menu items have been deleted, we need to as well
                 foreach ($manualNav as $nav) {
                     if (!array_key_exists($nav->handle, $navs)) {
                         // This is the menu item to delete from our DB
                         $navModel = craft()->cpNav_nav->getNavById($nav->id);
                         // Remove from DB
                         craft()->cpNav_nav->deleteNav($navModel);
                         if ($layoutId == $layout->id) {
                             $allNavs = craft()->cpNav_nav->getNavsByLayoutId($layoutId);
                         }
                     }
                 }
             }
         }
     }
     return $allNavs;
 }
 public function restoreDefaults($layoutId)
 {
     $navRecords = CpNav_NavRecord::model()->deleteAll('layoutId = :layoutId', array('layoutId' => $layoutId));
 }