<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=moderation.page.mfind
 * [END_COT_EXT]
 */

defined('COT_CODE') or die('Wrong URL');

if($m == 'portfolio' && in_array('portfolio', $moderauth)) {
  $mfind = true;

  require_once cot_incfile('portfolio', 'module');

  list($pn, $d, $d_url) = cot_import_pagenav('d', $cfg['maxrowsperpage']);
  $id = cot_import('id', 'G', 'INT');
  $state = cot_import('state', 'G', 'INT');
  $ajax = cot_import('ajax', 'G', 'INT');
  $ajax = empty($ajax) ? 0 : (int)$ajax;

  $sq = cot_import('sq', 'G', 'TXT');

  /* === Hook === */
  foreach (cot_getextplugins('portfolio.admin.list.first') as $pl)
  {
  	include $pl;
  }
  /* ===== */

  /* === Hook === */
  $extpl = cot_getextplugins('portfolio.admin.delete.done');
  /* ===== */

  if ($a == 'delete' && $id > 0)
  {
  	cot_portfolio_delete($id);
    cot_redirect(cot_url('moderation', 'm=portfolio','',true));
    exit;
  }
  if ($a == 'validate' && $id > 0)
  {
  	$sql = $db->query("SELECT * FROM $db_portfolio AS f LEFT JOIN $db_users AS u ON u.user_id=f.item_userid WHERE item_id='$id' LIMIT 1");
  	cot_die($sql->rowCount() == 0);
  	$item = $sql->fetch();

  	$db->update($db_portfolio, array('item_state' => 0), "item_id=?", array($id));

  	cot_redirect(cot_url('moderation', 'm=portfolio&state=2', '', true));
    exit;
  }
  if ($a == 'unvalidate' && $id > 0)
  {
  	$sql = $db->query("SELECT * FROM $db_portfolio AS f LEFT JOIN $db_users AS u ON u.user_id=f.item_userid WHERE item_id='$id' LIMIT 1");
  	cot_die($sql->rowCount() == 0);
  	$item = $sql->fetch();

  	$db->update($db_portfolio, array('item_state' => 2), "item_id=?", array($id));

  	cot_redirect(cot_url('moderation', 'm=portfolio&state=0', '', true));
    exit;
  }

  $t = new XTemplate(cot_tplfile('moderation.portfolio', 'module'));

  $where = array();

  if (!empty($sq))
  {
  	$words = explode(' ', $sq);
  	$sqlsearch = '%'.implode('%', $words).'%';

  	$where['search'] = "(item_title LIKE '".$db->prep($sqlsearch)."' OR item_text LIKE '".$db->prep($sqlsearch)."')";
  }

  if (!empty($state))
  {
  	$where['state'] = "item_state=".$state;
  }
  else
  {
  	$where['state'] = "item_state=0";
  }

  $list_url_path = array('m' => 'portfolio', 'sort' => $sort, 'sq' => $sq, 'state' => $state);

  $order['date'] = 'item_date DESC';

  /* === Hook === */
  foreach (cot_getextplugins('portfolio.admin.list.query') as $pl)
  {
  	include $pl;
  }
  /* ===== */

  $where = ($where) ? 'WHERE '.implode(' AND ', $where) : '';
  $order = ($order) ? 'ORDER BY '.implode(', ', $order) : '';

  $totalitems = $db->query("SELECT COUNT(*) FROM $db_portfolio
  	".$where."")->fetchColumn();

  $sqllist = $db->query("SELECT * FROM $db_portfolio AS m LEFT JOIN $db_users AS u ON u.user_id=m.item_userid
  	".$where."
  	".$order."
  	LIMIT $d, ".$cfg['maxrowsperpage']);

  $pagenav = cot_pagenav('moderation', $list_url_path, $d, $totalitems, $cfg['maxrowsperpage']);

  $t->assign(array(
  	"SEARCH_ACTION_URL" => cot_url('moderation', "m=portfolio&c=".$c, '', true),
  	"SEARCH_SQ" => cot_inputbox('text', 'sq', $sq, 'class="schstring"'),
    "SEARCH_STATE" => cot_radiobox($state, 'state', array(0, 2), array('опубликованные', 'на проверке')),
  	'PAGENAV_PAGES' => $pagenav['main'],
  	'PAGENAV_PREV' => $pagenav['prev'],
  	'PAGENAV_NEXT' => $pagenav['next'],
  	"PAGENAV_COUNT" => $totalitems,
  ));

  $sqllist_rowset = $sqllist->fetchAll();
  $sqllist_idset = array();
  foreach ($sqllist_rowset as $item)
  {
  	$sqllist_idset[$item['item_id']] = $item['item_alias'];
  }

  /* === Hook === */
  $extp = cot_getextplugins('portfolio.admin.list.loop');
  /* ===== */

  foreach ($sqllist_rowset as $item)
  {
  	$jj++;

  	$t->assign(cot_generate_usertags($item, 'PRD_ROW_OWNER_'));
  	$t->assign(cot_generate_portfoliotags($item, 'PRD_ROW_', 300, $usr['isadmin'], $cfg['homebreadcrumb']));

  	$t->assign(array(
  		'PRD_ROW_ODDEVEN' => cot_build_oddeven($jj),
  		'PRD_ROW_EDIT_URL' => cot_url('portfolio', 'm=edit&id='.$item['item_id']),
  		'PRD_ROW_DELETE_URL' => cot_url('moderation', 'm=portfolio&a=delete&id='.$item['item_id']),
      'PRD_ROW_VALIDATE_URL' => cot_url('moderation', 'm=portfolio&a=validate&id='.$item['item_id']),
      'PRD_ROW_UNVALIDATE_URL' => cot_url('moderation', 'm=portfolio&a=unvalidate&id='.$item['item_id']),
  	));

  	/* === Hook - Part2 : Include === */
  	foreach ($extp as $pl)
  	{
  		include $pl;
  	}
  	/* ===== */

  	$t->parse("MAIN.PRD_ROWS");
  }

  /* === Hook === */
  $extp = cot_getextplugins('portfolio.admin.list.tags');
  foreach ($extp as $pl)
  {
  	include $pl;
  }
  /* ===== */

  $t->parse("MAIN");
  echo $t->text("MAIN");
}

