get_results("SELECT * FROM ".TEMPLATES_TABLE." WHERE `gall_type` = ".$gall_type." ORDER BY created DESC", 'ARRAY_A'); return $templates; } /** * Returns gallery settings by template id * @global type $wpdb * @param int $id * @return array */ function fgallery_get_template_settings($id) { global $wpdb; $settings = $wpdb->get_var("SELECT `gall_settings` FROM ".TEMPLATES_TABLE." WHERE id = ".$id); return unserialize($settings); } /** * Saves the template to database * @global type $wpdb */ function fgallery_save_template(){ global $wpdb; if (!empty($_POST) && wp_verify_nonce($_POST['fgallery_templates_field'],'fgallery_templates')) { if (isset($_POST['gall_id']) && is_numeric($_POST['gall_id'])) { $gall_id = $_POST['gall_id']; } else { die('Invalid gallery'); } if (isset($_POST['gall_type']) && is_numeric($_POST['gall_type'])) { $gall_type = $_POST['gall_type']; } else { die('Invalid gallery'); } $settings = fgallery_get_album_settings($gall_id); if ($_POST['templ_title'] != '') { $wpdb->insert(TEMPLATES_TABLE, array('gall_type' => $gall_type, 'gall_settings' => serialize($settings), 'created' => date("Y-m-d"), 'templ_title' => $_POST['templ_title'], 'templ_description'=>$_POST['templ_description'])); if ($wpdb->insert_id > 0) { _e('Template was saved successfully','fgallery'); } } elseif (isset($_POST['update_ex']) && is_numeric($_POST['update_ex'])) { $templ_id = $_POST['update_ex']; $to_update = array('gall_type' => $gall_type, 'gall_settings' => serialize($settings)); if ($_POST['templ_description'] != '') { $templ_description = $_POST['templ_description']; $to_update['templ_description'] = $templ_description; } $wpdb->update(TEMPLATES_TABLE, $to_update, array('id'=>$templ_id)); _e('Template was saved successfully','fgallery'); } } die(); } /** * Loads the template */ function fgallery_load_template(){ if (!empty($_POST) && wp_verify_nonce($_POST['fgallery_load_settings_field'],'fgallery_load_settings')) { if (isset($_POST['gall_id']) && is_numeric($_POST['gall_id'])) { $gall_id = $_POST['gall_id']; } else { die('Invalid gallery'); } if (isset($_POST['template']) && is_numeric($_POST['template'])) { $templ_id = $_POST['template']; $settings = fgallery_get_template_settings($templ_id); } else { if (!empty($_FILES)) { $to_store = simplexml_load_file($_FILES['settings_file']['tmp_name']); $settings_array = array(); foreach ($to_store as $key=>$value) { $name = 'sc_'.$key.'__'; foreach ($value as $key_2=>$value_2) { $attr_name = $name.$key_2; $new_value = (string)$value_2; $settings_array[$attr_name] = str_replace('0x','',$new_value); } } if (empty($settings_array)) { die('Invalid settings'); } $settings = fgallery_prepare_settings($settings_array); } else { die('Invalid template'); } } if(fgallery_save_album_settings($gall_id, $settings)) { echo '1'; } else { _e('There was error while loading template. Please try again', 'fgallery'); } } die(); } /** * Deletes the template from db */ function fgallery_delete_template(){ global $wpdb; if(isset($_GET['templ_id']) && is_numeric($_GET['templ_id'])) { $templ_id = $_GET['templ_id']; $wpdb->query("DELETE FROM ".TEMPLATES_TABLE." WHERE `id` = ".$templ_id); } die(); } /** * Returns the rendered form to save template for given gallery * @param array $gallery */ function fgallery_render_template_form($gallery) { $templates = fgallery_get_templates($gallery['gall_type']); ?>


'.__('You can update existing template','fgallery').'

'; echo ''; } ?>


'.__('Choose template from the list','fgallery').''; foreach ($templates as $template) { echo '
'; echo ''; echo '
'.$template['templ_title'].'
'; echo '
'.$template['templ_description'].'
'; echo '
'; } echo '


'; } else { echo '

'.sprintf(__('There are no templates for %s gallery type yet. Please create new or'), fgallery_get_flash_type($gallery)).'

'; } ?>



%s gallery type','fgallery'), fgallery_get_flash_type($gallery)); ?>