<?php

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

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

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

  list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('projects', 'any');
  if(!$usr['isadmin'] && $usr['maingrp'] == 6) $usr['isadmin'] = true;
  cot_block($usr['isadmin']);

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

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

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

  $mass_act = cot_import('prj_action', 'P', 'TXT');
  $prj_arr = cot_import('prj_arr', 'P', 'ARR');
  /* === Hook === */
  foreach (cot_getextplugins('projects.admin.list.first') as $pl)
  {
  	include $pl;
  }
  /* ===== */

  if ($a == 'validate')
  {

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

  	$sql = $db->query("SELECT * FROM $db_projects AS p LEFT JOIN $db_users AS u ON u.user_id=p.item_userid WHERE item_id='$id' LIMIT 1");
  	cot_die($sql->rowCount() == 0);
  	$item = $sql->fetch();

  	$db->update($db_projects, array('item_state' => 0, 'item_ismoder' => $usr['id']), "item_id=?", array($id));

  	cot_projects_sync($item['item_cat']);

  	$rbody = cot_rc($L['project_added_mail_body'], array(
  		'user_name' => $item['user_name'],
  		'prj_name' => $item['item_title'],
  		'sitename' => $cfg['maintitle'],
  		'link' => COT_ABSOLUTE_URL.cot_url('projects', 'id='.$id, '', true)
  	));
  	cot_mail($item['user_email'], $L['project_added_mail_subj'], $rbody);

  	/* === Hook === */
      foreach (cot_getextplugins('projects.admin.validate.done') as $pl)
      {
          include $pl;
      }
      /* ===== */

  	cot_redirect(cot_url('moderation', 'm=projects'));
  }

  if ($a == 'delete')
  {

  	cot_projects_delete($id);
  }

  if(count($prj_arr)>0 && in_array($mass_act,array('delete','validate'))){
  		switch ($mass_act) {
  			case 'delete':
  				foreach ($prj_arr as $prj_id) {
  					cot_projects_delete($prj_id);
  				}
  				cot_redirect(cot_url('moderation', 'm=projects','',true));
  				break;
  			case 'validate':
  						/* === Hook === */
  						$extpl = cot_getextplugins('projects.admin.multiple.validate.first');
  						$extpl1 = cot_getextplugins('projects.admin.multiple.validate.done');
  						/* ===== */

  						foreach ($prj_arr as $prj_id) {
  							 		/* === Hook: Part 1 === */
  							 	    foreach ($extpl as $pl)
  							 	    {
  							 	        include $pl;
  							 	    }
  							 	    /* ===== */
  							 		$sql = $db->query("SELECT * FROM $db_projects AS p LEFT JOIN $db_users AS u ON u.user_id=p.item_userid WHERE item_id='$prj_id' LIMIT 1");
  							 		cot_die($sql->rowCount() == 0);
  							 		$item = $sql->fetch();

  							 		$db->update($db_projects, array('item_state' => 0, 'item_ismoder' => $usr['id']), "item_id=?", array($prj_id));

  							 		cot_projects_sync($item['item_cat']);

  							 		$rbody = cot_rc($L['project_added_mail_body'], array(
  							 			'user_name' => $item['user_name'],
  							 			'prj_name' => $item['item_title'],
  							 			'sitename' => $cfg['maintitle'],
  							 			'link' => COT_ABSOLUTE_URL.cot_url('projects', 'id='.$prj_id, '', true)
  							 		));
  							 		cot_mail($item['user_email'], $L['project_added_mail_subj'], $rbody);

  							 		/* === Hook: Part 2 === */
  							 	    foreach ($extpl1 as $pl)
  							 	    {
  							 	        include $pl;
  							 	    }
  							 	    /* ===== */
  					 	    }
  					 	cot_redirect(cot_url('moderation', 'm=projects','',true));
  				break;

  			default:
  				cot_redirect(cot_url('moderation', 'm=projects','',true));
  				break;
  		}
  }

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

  $where = array();

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

  if($uid > 0) {
    $where['userid'] = "item_userid=".$uid;
  }

  if (!empty($c))
  {
  	$catsub = cot_structure_children('market', $c);
  	$where['cat'] = "item_cat IN ('".implode("','", $catsub)."')";
  }

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

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

  $list_url_path = array('m' => 'projects', 'c' => $c, 'sort' => $sort, 'sq' => $sq, 'uid' => $uid);

  /* === Hook === */
  foreach (cot_getextplugins('projects.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_projects
  	".$where."")->fetchColumn();

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

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

  $users_sql = $db->query("SELECT user_id, user_name FROM $db_users WHERE user_maingrp>3")->fetchAll(PDO::FETCH_KEY_PAIR);

  $t->assign(array(
  	"SEARCH_ACTION_URL" => cot_url('moderation', "m=projects&c=".$c."&type=".$type, '', true),
  	"SEARCH_SQ" => cot_inputbox('text', 'sq', $sq, 'class="schstring"'),
    "SEARCH_USER" => cot_selectbox($uid, 'uid', array_keys($users_sql), array_values($users_sql), true),
  	"SEARCH_STATE" => cot_radiobox($state, 'state', array(0, 1, 2, 3), array('опубликованные', 'скрытые', 'на проверке', 'на исправлении')),
  	"SEARCH_CAT" => cot_projects_selectcat($c, 'c'),
  	"SEARCH_SORTER" => cot_selectbox($sort, "sort", array('', 'costasc', 'costdesc'), array($L['projects_mostrelevant'], $L['projects_costasc'], $L['projects_costdesc']), false),
  	'TYPES_EDIT' => cot_url('moderation', 'm=projects&p=types'),
  	'PAGENAV_PAGES' => $pagenav['main'],
  	'PAGENAV_PREV' => $pagenav['prev'],
  	'PAGENAV_NEXT' => $pagenav['next'],
  	'CATTITLE' => (!empty($c)) ? ' / '.(!empty($c)) ? ' / '.htmlspecialchars($structure['projects'][$c]['title']) : ''  : ''
  ));

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

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

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

  	$t->assign(cot_generate_usertags($item, 'PRJ_ROW_OWNER_'));
  	$t->assign(cot_generate_projecttags($item, 'PRJ_ROW_', $cfg['projects']['shorttextlen'], $usr['isadmin'], $cfg['homebreadcrumb']));

  	$t->assign(array(
  		'PRJ_ROW_ODDEVEN' => cot_build_oddeven($jj),
  		'PRJ_ROW_EDIT_URL' => cot_url('projects', 'm=edit&id='.$item['item_id']),
  		'PRJ_ROW_VALIDATE_URL' => cot_url('moderation', 'm=projects&a=validate&id='.$item['item_id']),
  		'PRJ_ROW_DELETE_URL' => cot_url('moderation', 'm=projects&a=delete&id='.$item['item_id'])
  	));

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

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

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

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