예제 #1
0
 public static function add_template($template_name = null, $blocks = null, $wrappers = null)
 {
     $templates = HeadwayOption::get('list', 'templates', array());
     $last_template_id = HeadwayOption::get('last-id', 'templates', 0);
     /* These  two variables be used for when a blocks/wrappers imported ID is different than the one that it ends up with... i.g. skin importing to line up instances */
     $block_id_translations = array();
     $wrapper_id_translations = array();
     /* Build name */
     $id = $last_template_id + 1;
     $template_name = $template_name ? $template_name : 'Template ' . $id;
     /* Add template to templates array so it can be sent to DB */
     $templates[$id] = $template_name;
     /* Send array to DB */
     HeadwayOption::set('list', $templates, 'templates');
     HeadwayOption::set('last-id', $id, 'templates');
     /* Add blocks and wrappers */
     if ($blocks && $wrappers) {
         /* Format wrappers */
         foreach ($wrappers as $wrapper_id => $wrapper_settings) {
             $old_wrapper_id = $wrapper_id;
             $new_wrapper = HeadwayWrappers::add_wrapper('template-' . $id, $wrapper_settings);
             $wrapper_id_translations[str_replace('wrapper-', '', $old_wrapper_id)] = array('id' => $new_wrapper['id'], 'layout' => 'template-' . $id);
         }
         /* Add blocks */
         foreach ($blocks as $block) {
             $old_block_id = $block['id'];
             unset($block['id']);
             unset($block['layout']);
             /* Update block's wrapper ID to match the real ID of the imported wrapper because if you link to the old ID from the export file then it won't match up */
             $block['wrapper'] = 'wrapper-' . $wrapper_id_translations[str_replace('wrapper-', '', $block['wrapper'])]['id'];
             $new_block_id = HeadwayBlocksData::add_block('template-' . $id, $block);
             $block_id_translations[$old_block_id] = $new_block_id;
         }
     }
     /* End adding wrappers and blocks */
     return array('id' => $id, 'name' => $template_name, 'block-id-translations' => $block_id_translations, 'wrapper-id-translations' => $wrapper_id_translations);
 }