<?php
/**
 * [BEGIN_COT_EXT]
 * Hooks=api.track_stack.main
 * [END_COT_EXT]
 */

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

if($usr['id'] > 0) {
  require_once cot_incfile('projects', 'module');

  $items = $db->query("SELECT p.*, o.* FROM $db_projects AS p LEFT JOIN $db_projects_offers AS o ON o.offer_pid=p.item_id WHERE item_userid=".$usr['id'].' AND item_state=0 AND offer_view=0')->fetchAll();

  foreach($items as $item) {
    if($item['offer_date'] > $lastdate) {
      $urlparams = empty($item['item_alias']) ?
          			array('c' => $item['item_cat'], 'id' => $item['item_id']) :
          			array('c' => $item['item_cat'], 'al' => $item['item_alias']);

      $return['push']['notify'][] = '<a class="bold fs13 db nowrap" href="'.cot_url('projects', $urlparams, '', true).'">
          Новое предложение по запросу "'.$item['item_title'].'"
        </a>';
      $return['push']['cntnew']['notify']++;
      if($item['offer_date'] > $return['lastdate']) $return['lastdate'] = $item['offer_date'];
    }
    $return['push']['cnt']['notify']++;
  }
}

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('projects', 'any', 'RWA');
if(!$usr['isadmin'] && in_array('projects', $moderauth)) $usr['isadmin'] = true;

if ($usr['isadmin'])
{
	$projectsql = $db->query("SELECT * FROM $db_projects WHERE item_state=2")->fetchAll();

  foreach($projectsql as $item) {
    if(($item['item_update_real'] > 0 && $lastdate < $item['item_update_real']) || ($lastdate < $item['item_date'])) {
      $return['push']['notify'][] = '<a class="bold fs13 db nowrap" href="'.cot_url('projects', 'm=edit&id='.$item['item_id']).'">
                                        Заявка #'.$item['item_id'].' ожидает модерации
                                     </a>';
      if($item['item_date'] > $return['lastdate']) $return['lastdate'] = $item['item_date'];
    }

    $return['push']['cnt']['notify']++;
    $return['push']['cntnew']['notify']++;
  }
}

?>