<?php

/**
 *
 * @copyright  2016-2017 izend.org
 * @version    3
 * @link       http://www.izend.org
 */

require_once 'models/thread.inc';

function sms_get_user_campaign_id($user_id) {
	$tabthread=db_prefix_table('thread');

	$sql="SELECT thread_id FROM $tabthread WHERE thread_type='campaign' AND user_id=$user_id";

	$r = db_query($sql);

	return $r ? $r[0]['thread_id'] : false;
}

function sms_list_campaign_users($lang) {
	$sqllang=db_sql_arg($lang, false);

	$tabthread=db_prefix_table('thread');
	$tabthreadlocale=db_prefix_table('thread_locale');

	$sql="SELECT t.thread_id, tloc.name AS thread_name, tloc.title AS thread_title, tloc.abstract AS thread_abstract, tloc.cloud AS thread_cloud, t.number AS thread_number FROM $tabthread t JOIN $tabthreadlocale tloc ON tloc.thread_id=t.thread_id AND tloc.locale=$sqllang WHERE t.thread_type='campaign' ORDER BY t.number";

	$r = db_query($sql);

	if (!$r) {
		return false;
	}

	$thread_list = array();
	$url = url('campaign', $lang);
	foreach ($r as $thread) {
		extract($thread);	/* thread_id thread_name thread_title thread_abstract thread_cloud thread_number */
		$thread_url = $url . '/' . $thread_id;
		$thread_list[] = compact('thread_number', 'thread_id', 'thread_title', 'thread_abstract', 'thread_cloud', 'thread_url');
	}

	return $thread_list;
}

function sms_create_campaign_thread($user_id, $lang, $thread_name, $thread_title) {
	global $supported_languages;

	$thread_type='campaign';

	$r = thread_create($lang, $user_id, $thread_name, $thread_title, $thread_type);

	if (!$r) {
		return false;
	}
	extract($r);	/* thread_id thread_number */

	foreach ($supported_languages as $clang) {
		$r = thread_set($clang, $thread_id, $thread_name, $thread_title, $thread_type, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false);

		if (!$r) {
			$sql="DELETE FROM $tabthread WHERE thread_id=$thread_id";

			$r = db_delete($sql);

			return false;
		}
	}

	return true;
}

function sms_delete_campaign_thread($user_id) {
	$thread_id=sms_get_user_campaign_id($user_id);

	if (!$thread_id) {
		return false;
	}

	if (!thread_delete($thread_id)) {
		return false;
	}

	return true;
}

function sms_rename_campaign_thread($user_id, $thread_name, $thread_title) {
	$sqlname=db_sql_arg($thread_name, true);
	$sqltitle=db_sql_arg($thread_title, true, true);

	$tabthread=db_prefix_table('thread');
	$tabthreadlocale=db_prefix_table('thread_locale');

	$sql="UPDATE $tabthreadlocale SET title=$sqltitle, name=$sqlname WHERE thread_id=(SELECT thread_id FROM $tabthread WHERE thread_type='campaign' AND user_id=$user_id)";

	$r = db_update($sql);

	if ($r === false) {
		return false;
	}

	return true;
}

function sms_get_campaign($lang, $node_id) {
	$sqllang=db_sql_arg($lang, false);

	$tabnode=db_prefix_table('node');
	$tabnodelocale=db_prefix_table('node_locale');
	$tabnodecontent=db_prefix_table('node_content');
	$tabcontentsms=db_prefix_table('content_sms');

	$sql="SELECT nloc.name AS sms_name, nloc.title AS sms_title, nloc.abstract AS sms_subject, nloc.cloud AS sms_cloud, UNIX_TIMESTAMP(n.modified) AS sms_modified, c.text AS sms_text FROM $tabnode n JOIN $tabnodelocale nloc ON nloc.node_id=n.node_id AND nloc.locale=$sqllang JOIN $tabnodecontent nc ON nc.node_id=n.node_id AND nc.content_type='sms' AND nc.ignored=FALSE LEFT JOIN $tabcontentsms c ON c.content_id=nc.content_id AND c.locale=nloc.locale WHERE n.node_id=$node_id";

	$r = db_query($sql);

	return $r ? $r[0] : false;
}

function sms_set_campaign($lang, $node_id, $sms_title, $sms_name, $sms_cloud, $sms_subject, $sms_text) {
	$tabnode=db_prefix_table('node');

	$sql="UPDATE $tabnode SET modified=NOW() WHERE node_id=$node_id";

	$r = db_update($sql);

	if ($r === false) {
		return false;
	}

	$sqllang=db_sql_arg($lang, false);
	$sqlname=db_sql_arg($sms_name, true);
	$sqltitle=db_sql_arg($sms_title, true, true);
	$sqlcloud=db_sql_arg($sms_cloud, true, true);
	$sqlabstract=db_sql_arg($sms_subject, true, true);

	$tabnodelocale=db_prefix_table('node_locale');

	$sql="INSERT INTO $tabnodelocale (node_id, locale, name, title, abstract, cloud) SELECT * FROM (SELECT $node_id AS node_id, $sqllang AS locale, $sqlname AS name, $sqltitle AS title, $sqlabstract AS abstract, $sqlcloud AS cloud) s WHERE NOT EXISTS (SELECT node_id FROM $tabnodelocale WHERE node_id=$node_id AND locale=$sqllang)";

	$r = db_insert($sql);

	if ($r === false) {
		return false;
	}

	if ($r === 0) {
		$sql="UPDATE $tabnodelocale SET name=$sqlname, title=$sqltitle, abstract=$sqlabstract, cloud=$sqlcloud WHERE node_id=$node_id AND locale=$sqllang";

		$r = db_update($sql);

		if ($r === false) {
			return false;
		}
	}

	$r = cloud_tag_node($lang, $node_id, $sms_cloud);

	if (!$r) {
		return false;
	}

	$tabnodecontent=db_prefix_table('node_content');

	$sql="SELECT nc.content_id FROM $tabnodecontent nc WHERE nc.node_id=$node_id AND nc.content_type='sms'";

	$r = db_query($sql);

	if (!$r) {
		return false;
	}

	$content_id=$r[0]['content_id'];

	$sqltext=db_sql_arg($sms_text, true, true);

	$tabcontentsms=db_prefix_table('content_sms');

	$sql="INSERT INTO $tabcontentsms (content_id, locale, text) SELECT * FROM (SELECT $content_id AS user_id, $sqllang AS locale, $sqltext AS text) s WHERE NOT EXISTS (SELECT $content_id FROM $tabcontentsms WHERE content_id=$content_id AND locale=$sqllang)";

	$r = db_insert($sql);

	if ($r) {
		return true;
	}

	$sql="UPDATE $tabcontentsms SET text=$sqltext WHERE content_id=$content_id AND locale=$sqllang";

	$r = db_update($sql);

	if ($r === false) {
		return false;
	}

	return true;
}
