<?php /*

 ocPortal
 Copyright (c) ocProducts, 2004-2012

 See text/EN/licence.txt for full licencing information.


 NOTE TO PROGRAMMERS:
   Do not edit this file. If you need to make changes, save your changed file to the appropriate *_custom folder
   **** If you ignore this advice, then your website upgrades (e.g. for bug fixes) will likely kill your changes ****

*/

/**
 * @license		http://opensource.org/licenses/cpal_1.0 Common Public Attribution License
 * @copyright	ocProducts Ltd
 * @package		authors
 */

/**
 * Shows an HTML page of all authors clickably.
 */
function authors_script()
{
	require_lang('authors');

	global $NON_CANONICAL_PARAMS;
	$NON_CANONICAL_PARAMS[]='max';

	$start=get_param_integer('start',0);
	$max=get_param_integer('max',300);

	$author_fields=$GLOBALS['SITE_DB']->query('SELECT m_name,m_table FROM '.$GLOBALS['SITE_DB']->get_table_prefix().'db_meta WHERE m_name LIKE \''.db_encode_like('%author').'\'');
	$rows=array();
	foreach ($author_fields as $field)
	{
		if (($field['m_table']!='addons') && ($field['m_table']!='blocks') && ($field['m_table']!='modules'))
		{
			$rows_new=$GLOBALS['SITE_DB']->query('SELECT DISTINCT '.$field['m_name'].' AS author FROM '.$GLOBALS['SITE_DB']->get_table_prefix().$field['m_table'].' WHERE '.db_string_not_equal_to($field['m_name'],'').' ORDER BY '.$field['m_name'],$max+$start);
			foreach ($rows_new as $a)
			{
				if ((!array_key_exists($a['author'],$rows)) || ($field['m_table']=='authors'))
					$rows[$a['author']]=$field['m_table'];
			}
		}
	}

	$rows=array_unique($rows);

	$field_name=get_param('field_name');

	$content=new ocp_tempcode();
	$i=0;
	foreach ($rows as $author=>$table)
	{
		if (($i>=$start) && ($i<$start+$max))
		{
			if ($table=='authors')
			{
				$content->attach(do_template('AUTHOR_POPUP_WINDOW_DEFINED',array('_GUID'=>'cffa9926cebd3ec2920677266a3299ea','FIELD_NAME'=>$field_name,'AUTHOR'=>$author)));
			} else
			{
				$content->attach(do_template('AUTHOR_POPUP_WINDOW_UNDEFINED',array('_GUID'=>'6210be6d1eef4bc2bda7f49947301f97','FIELD_NAME'=>$field_name,'AUTHOR'=>$author)));
			}
		}

		$i++;
	}
	if ($content->is_empty()) $content=paragraph(do_lang_tempcode('NO_ENTRIES'),'dfids09fi;lk;3');

	if ($i>=$start+$max)
	{
		$keep=symbol_tempcode('KEEP');
		$next_link=find_script('authors').'?field_name='.urlencode($field_name).'&start='.strval($start+$max).'&max='.strval($max).$keep->evaluate();
	} else
	{
		$next_link=NULL;
	}

	$echo=do_template('STYLED_HTML_WRAP',array('_GUID'=>'ab8d8c9d276530d82ddd84202aacf32f','TITLE'=>do_lang_tempcode('CHOOSE_AUTHOR'),'NEXT_LINK'=>$next_link,'CONTENT'=>$content));
	$echo->evaluate_echo();
}

/**
 * Get a member ID from an author name. First by trying authors table, second by trying forum membernames.
 *
 * @param  ID_TEXT		The name of an author
 * @return ?MEMBER		The member ID (NULL: none found)
 */
function get_author_id_from_name($author)
{
	$handle=$GLOBALS['SITE_DB']->query_value_null_ok('authors','forum_handle',array('author'=>$author));
	if (!is_numeric($handle))
	{
		$handle=$GLOBALS['FORUM_DRIVER']->get_member_from_username($author);
	}
	return $handle;
}

/**
 * Adds an author (re-creating them if they already exist - thus it also serves to edit; the reason for this is the fluidity of authors - members are automatically authors even before an author profile is made)
 *
 * @param  ID_TEXT		The name of an author
 * @param  URLPATH		The URL to the authors home page
 * @param  ?MEMBER		The member ID of the author (NULL: no forum profile)
 * @param  LONG_TEXT		A description of the author
 * @param  LONG_TEXT		A terse string showing author skills
 * @param  ?SHORT_TEXT	Meta keywords for this resource (NULL: do not edit) (blank: implicit)
 * @param  ?LONG_TEXT	Meta description for this resource (NULL: do not edit) (blank: implicit)
 */
function add_author($author,$url,$forum_handle,$description,$skills,$meta_keywords='',$meta_description='')
{
	log_it('DEFINE_AUTHOR',$author,is_null($forum_handle)?'':strval($forum_handle));

	$rows=$GLOBALS['SITE_DB']->query_select('authors',array('description','skills'),array('author'=>$author),'',1);
	if (array_key_exists(0,$rows))
	{
		delete_lang($rows[0]['description']);
		delete_lang($rows[0]['skills']);
		$GLOBALS['SITE_DB']->query_delete('authors',array('author'=>$author),'',1);
	}

	require_code('seo2');
	if (($meta_keywords=='') && ($meta_description==''))
	{
		seo_meta_set_for_implicit('authors',$author,array($author,$description,$skills),$description);
	} else
	{
		seo_meta_set_for_explicit('authors',$author,$meta_keywords,$meta_description);
	}

	$GLOBALS['SITE_DB']->query_insert('authors',array('author'=>$author,'url'=>$url,'forum_handle'=>$forum_handle,'description'=>insert_lang_comcode($description,3),'skills'=>insert_lang_comcode($skills,3)));
}

/**
 * Delete an author
 *
 * @param  ID_TEXT		The name of an author
 */
function delete_author($author)
{
	$rows=$GLOBALS['SITE_DB']->query_select('authors',array('description','skills'),array('author'=>$author),'',1);
	if (array_key_exists(0,$rows))
	{
		delete_lang($rows[0]['description']);
		delete_lang($rows[0]['skills']);
		$GLOBALS['SITE_DB']->query_delete('authors',array('author'=>$author),'',1);
	} else warn_exit(do_lang_tempcode('MISSING_RESOURCE'));

	log_it('DELETE_AUTHOR',$author);
}

/**
 * Find if a member's usergroup has permission to edit an author
 *
 * @param  MEMBER			The member being checked whether to have the access
 * @param  ID_TEXT		An author
 * @return boolean		Whether the member can edit this author
 */
function has_edit_author_permission($member,$author)
{
	if (is_guest($member)) return false;
	if ((get_author_id_from_name($author)==$member) && (has_specific_permission($member,'set_own_author_profile'))) return true;
	if (has_specific_permission($member,'edit_midrange_content','cms_authors')) return true;
	return false;
}

/**
 * Find if a member's usergroup has permission to delete an author
 *
 * @param  MEMBER			The member being checked whether to have the access
 * @param  ID_TEXT		An author
 * @return boolean		Whether the member can edit this author
 */
function has_delete_author_permission($member,$author)
{
	if (is_guest($member)) return false;
	if ((get_author_id_from_name($author)==$member) && (has_specific_permission($member,'delete_own_midrange_content'))) return true;
	if (has_specific_permission($member,'delete_midrange_content','cms_authors')) return true;
	return false;
}

/**
 * Merge two authors.
 *
 * @param  ID_TEXT		The first author (being removed effectively)
 * @param  ID_TEXT		The second author (subsuming the first)
 */
function merge_authors($from,$to)
{
	$author_fields=$GLOBALS['SITE_DB']->query('SELECT m_name,m_table FROM '.$GLOBALS['SITE_DB']->get_table_prefix().'db_meta WHERE m_name LIKE \''.db_encode_like('%author').'\'');
	foreach ($author_fields as $field)
	{
		if ($field['m_table']!='authors')
		{
			$GLOBALS['SITE_DB']->query_update($field['m_table'],array($field['m_name']=>$to),array($field['m_name']=>$from));
		}
	}
	if ($from!=$to) $GLOBALS['SITE_DB']->query_delete('authors',array('author'=>$from),'',1);

	log_it('MERGE_AUTHORS',$from,$to);
}


