<commit_before><?php

class TemplatesLib extends TikiLib {
	function TemplatesLib($db) {
		# this is probably uneeded now
		if (!$db) {
			die ("Invalid db object passed to TemplatesLib constructor");
		}

		$this->db = $db;
	}

	function list_all_templates($offset, $maxRecords, $sort_mode, $find) {
		$sort_mode = str_replace("_", " ", $sort_mode);

		if ($find) {
			$findesc = $this->qstr('%' . $find . '%');

			$mid = " where (content like $findesc)";
		} else {
			$mid = "";
		}

		$query = "select name,created,templateId from tiki_content_templates $mid order by $sort_mode limit $offset,$maxRecords";
		$query_cant = "select count(*) from tiki_content_templates $mid";
		$result = $this->query($query);
		$cant = $this->getOne($query_cant);
		$ret = array();

		while ($res = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
			$query2 = "select section from tiki_content_templates_sections where templateId=" . $res["templateId"];

			$result2 = $this->query($query2);
			$sections = array();

			while ($res2 = $result2->fetchRow(DB_FETCHMODE_ASSOC)) {
				$sections[] = $res2["section"];
			}

			$res["sections"] = $sections;
			$ret[] = $res;
		}

		$retval = array();
		$retval["data"] = $ret;
		$retval["cant"] = $cant;
		return $retval;
	}

	function replace_template($templateId, $name, $content) {
		$name = addslashes($name);

		$content = addslashes($content);
		// Check the name
		$now = date("U");

		if ($templateId) {
			$query
				= "update tiki_content_templates set content='$content', name='$name', created=$now where templateId=$templateId";
		} else {
			$query = "replace into tiki_content_templates(content,name,created)
                values('$content','$name',$now)";
		}

		$result = $this->query($query);
		$id = $this->getOne("select max(templateId) from tiki_content_templates where created=$now and name='$name'");
		return $id;
		return true;
	}

	function add_template_to_section($templateId, $section) {
		$query = "replace into tiki_content_templates_sections(templateId,section) values($templateId,'$section')";

		$result = $this->query($query);
	}

	function remove_template_from_section($templateId, $section) {
		$query = "delete from tiki_content_templates_sections where templateId=$templateId and section='$section'";

		$result = $this->query($query);
	}

	function template_is_in_section($templateId, $section) {
		$cant = $this->getOne(
			"select count(*) from tiki_content_templates_sections where templateId=$templateId and section='$section'");

		return $cant;
	}

	function remove_template($templateId) {
		$query = "delete from tiki_content_templates where templateId=$templateId";

		$result = $this->query($query);
		$query = "delete from tiki_content_templates_sections where templateId=$templateId";
		$result = $this->query($query);
		return true;
	}
}

$templateslib = new TemplatesLib($dbTiki);

?><commit_msg>db abstract<commit_after><?php

class TemplatesLib extends TikiLib {
	function TemplatesLib($db) {
		# this is probably uneeded now
		if (!$db) {
			die ("Invalid db object passed to TemplatesLib constructor");
		}

		$this->db = $db;
	}

	function list_all_templates($offset, $maxRecords, $sort_mode, $find) {
		$bindvars = array();
		if ($find) {
			$bindvars[] = '%' . $find . '%';
			$mid = " where (`content` like ?)";
		} else {
			$mid = "";
		}

		$query = "select `name`,`created`,`templateId` from `tiki_content_templates` $mid order by ".$this->convert_sortmode($sort_mode);
		$query_cant = "select count(*) from `tiki_content_templates` $mid";
		$result = $this->query($query,$bindvars,$maxRecords,$offset);
		$cant = $this->getOne($query_cant,$bindvars);
		$ret = array();

		while ($res = $result->fetchRow()) {
			$query2 = "select `section` from `tiki_content_templates_sections` where `templateId`=?";
			$result2 = $this->query($query2,array((int)$res["templateId"]));
			$sections = array();
			while ($res2 = $result2->fetchRow()) {
				$sections[] = $res2["section"];
			}
			$res["sections"] = $sections;
			$ret[] = $res;
		}

		$retval = array();
		$retval["data"] = $ret;
		$retval["cant"] = $cant;
		return $retval;
	}

	function replace_template($templateId, $name, $content) {
		$now = date("U");
		$bindvars = array($content,$name,(int)$now);
		if ($templateId) {
			$query = "update `tiki_content_templates` set `content`=?, `name`=?, `created`=? where `templateId`=?";
			$bindvars[] = (int) $templateId;
		} else {
			$query = "delete from `tiki_content_templates` where `content`=? and `name`=?";
			$this->query($query,array($content,$name),-1,-1,false);
			$query = "insert into `tiki_content_templates`(`content`,`name`,`created`) values(?,?,?)";
		}

		$result = $this->query($query,$bindvars);
		$id = $this->getOne("select max(`templateId`) from `tiki_content_templates` where `created`=? and `name`=?",array((int)$now,$name));
		return $id;
	}

	function add_template_to_section($templateId, $section) {
		$this->query("delete from `tiki_content_templates_sections` where `templateId`=? and `section`=?",array((int)$templateId,$section),-1,-1,false);
		$query = "insert into `tiki_content_templates_sections`(`templateId`,`section`) values(?,?)";
		$result = $this->query($query,array((int)$templateId,$section));
	}

	function remove_template_from_section($templateId, $section) {
		$result = $this->query("delete from `tiki_content_templates_sections` where `templateId`=? and `section`=?",array((int)$templateId,$section));
	}

	function template_is_in_section($templateId, $section) {
		$cant = $this->getOne("select count(*) from `tiki_content_templates_sections` where `templateId`=? and `section`=?",array((int)$templateId,$section));
		return $cant;
	}

	function remove_template($templateId) {
		$query = "delete from `tiki_content_templates` where `templateId`=?";
		$result = $this->query($query,array((int)$templateId));
		$query = "delete from `tiki_content_templates_sections` where `templateId`=?";
		$result = $this->query($query,array((int)$templateId));
		return true;
	}
}

$templateslib = new TemplatesLib($dbTiki);

?>

{'fraction_non_alphanumeric': 0.18249380677126342, 'fraction_numerical': 0.002642444260941371, 'mean_word_length': 5.341361256544503, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2988965', 'n_tokens_mistral': 2234, 'n_tokens_neox': 2041, 'n_words': 414}