<?php
// $Id$
/**
 * @file
 * CRUD functions for FCKeditor settings.
 */

/**
 * Add fields and paths to exclude in the visibility settings
 *
 * @param $fields
 *   array of fields to exclude, use FALSE to keep current settings
 * @param $paths
 *   array of paths to exclude, use FALSE to keep current settings
 * @param $simple_fields
 *   array of fields to force the simplified toolbar, use FALSE to keep current settings
 * @param $simple_paths
 *   array of paths to force the simplified toolbar, use FALSE to keep current settings
 * @param $profile
 *   The name of the profile to edit Defaults to the global profile
 * 
 */
function install_fckeditor_visibility($fields=FALSE, $paths=FALSE, $simple_fields=FALSE, $simple_paths=FALSE, $profile='FCKeditor Global Profile'){
	$result = db_query("SELECT settings FROM {fckeditor_settings} WHERE name='%s'",$profile);
	$data = db_fetch_object($result);
  if (!empty($data->settings)) {
    $settings = unserialize($data->settings);
	}
	
	//check each field and add it to the exclude list if its not already there
	if($fields !== FALSE){
		foreach($fields as $key => $value){
			$exists = strpos($settings['excl_fields'],$value);
			if ($exists === FALSE){
				$settings['excl_fields'] .= "\n".$value;
			}
		}
	}
	
	//check each path and add it to the exclude list if its not already there
	if($paths !== FALSE){
		foreach($paths as $key => $value){
			$exists = strpos($settings['excl_paths'],$value);
			if ($exists === FALSE){
				$settings['excl_paths'] .= "\n".$value;
			}
		}
	}
	
	//check each field and add it to the force simple list if its not already there
	if($simple_fields !== FALSE){
		foreach($simple_fields as $key => $value){
			$exists = strpos($settings['simple_incl_fields'],$value);
			if ($exists === FALSE){
				$settings['simple_incl_fields'] .= "\n".$value;
			}
		}
	}
	
	//check each path and add it to the force simple list if its not already there
	if($simple_paths !== FALSE){
		foreach($simple_paths as $key => $value){
			$exists = strpos($settings['simple_incl_paths'],$value);
			if ($exists === FALSE){
				$settings['simple_incl_paths'] .= "\n".$value;
			}
		}
	}
	
	//save the settings
	db_query("DELETE FROM {fckeditor_settings} WHERE name = '%s'", 'FCKeditor Global Profile');
	db_query("INSERT INTO {fckeditor_settings} (name, settings) VALUES ('%s', '%s')", 'FCKeditor Global Profile', serialize($settings));
}