<?php

(defined('COT_CODE') && defined('COT_PLUG')) or die('Wrong URL.');

global $structure, $cfg, $L, $usr, $sys;

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('plug', 'ads', 'RWA');
cot_block($usr['auth_write']);

$id = cot_import('id', 'G', 'INT');
$act = cot_import('act', 'G', 'ALP');

$ads = array();
if($id > 0) {
	$ads = $db->query("SELECT * FROM $db_ads WHERE item_id = ".(int)$id." AND item_userid=".$usr['id']." LIMIT 1")->fetch();
}

if($ads['item_id'] > 0) {
  if ($act == 'buy' && $ads['item_expire'] == 0 && $ads['item_paused'] == 0)
  {
    if($ads['item_marketid'] > 0) {
      require_once cot_incfile('market', 'module');
      $item = $db->query("SELECT * FROM $db_market WHERE item_id=".$ads['item_marketid']." AND item_userid=".$ads['item_userid'].' AND item_state=0 AND item_ismoder!=0 LIMIT 1')->fetch();
      if($item['item_id'] > 0) {
        $data = array(
          'market' => array(),
          'cost' => 0,
        );

        $data['market'] = ($ads['item_marketid'] > 0 ? $db->query("SELECT * FROM $db_market WHERE item_id=".$ads['item_marketid']." AND item_userid=".$usr['id'].' AND item_state=0 AND item_ismoder!=0 LIMIT 1')->fetch() : array('item_id' => 0));
        if($data['market']['item_id'] > 0) {
          $data['cost'] = (!empty($cfg['market']['cat_' . $ads['item_cat']]['adsprice']) ? $cfg['market']['cat_' . $ads['item_cat']]['adsprice'] : $cfg['market']['cat___default']['adsprice'])-0;
        } else {
          cot_error(cot_sitename('padeg', 'i').' '.cot_sitename('words', 'notfound').', либо '.cot_sitename('words', 'notactive'), '');
        }

        $data['cost'] = $data['cost'] * $ads['item_period'];

        if($ads['item_position'] > 0 && is_array($ads_positions[$ads['item_position']])) {
          $data['cost'] = $data['cost'] * $ads_positions[$ads['item_position']]['ratio'];
        } else {
          cot_error('Не корректно указано место размещения', '');
        }

        require_once cot_incfile('configuration');

        if($cfg['plugin']['ads']['purchase_period'] == 'day')
        {
      		$options['time'] = $ads['item_period'] * 24 * 60 * 60;
        }
        elseif($cfg['plugin']['ads']['purchase_period'] == 'week')
        {
      		$options['time'] = $ads['item_period'] * 7 * 24 * 60 * 60;
        }
        else
        {
      		$options['time'] = $ads['item_period'] * 30 * 24 * 60 * 60;
        }

        $summ = $data['cost'];
        $options['desc'] = $L['ads_payments_desc'];
      	$options['code'] = $ads['item_id'];

      	if ($db->fieldExists($db_payments, "pay_redirect")){
      		$options['redirect'] = $cfg['mainurl'].'/'.cot_url('plug', 'e=ads', '', true);
      	}

      	if (!cot_error_found())
      	{
          $pid = cot_payments_create_order('ads', $summ, $options);
          cot_redirect(cot_url('payments', 'm=billing&pid=' . $pid, '', true));
        }
      } else {
         cot_error(cot_sitename('padeg', 'i').' '.cot_sitename('words', 'notfound').', либо '.cot_sitename('words', 'notactive'), '');
      }
    }
  }
  elseif ($act == 'paused' && $ads['item_expire'] > $sys['now'] && $ads['item_paused'] == 0)
  {
     $db->update($db_ads,  array('item_expire' => 0, 'item_paused' => ($ads['item_expire'] - $sys['now'])), "item_id=".(int)$ads['item_id']);

     if($ads['item_position'] == $ads_positions_special['market_showonindex'] && $ads['item_marketid'] > 0) {
       require_once cot_incfile('market', 'module');
       $db->update($db_market, array('item_showonindex' => 0), 'item_id='.$ads['item_marketid']);
     }

     cot_message('Баннер остановлен');
  }
  elseif ($act == 'unpaused' && $ads['item_paused'] > 0)
  {
    if($ads['item_marketid'] > 0) {
     require_once cot_incfile('market', 'module');
     $item = $db->query("SELECT * FROM $db_market WHERE item_id=".$ads['item_marketid']." AND item_userid=".$ads['item_userid'].' AND item_state=0 AND item_ismoder!=0 LIMIT 1')->fetch();
     if($item['item_id'] > 0) {
       $db->update($db_ads,  array('item_expire' => ($ads['item_paused'] + $sys['now']), 'item_paused' => 0), "item_id=".(int)$ads['item_id']);

       if($ads['item_position'] == $ads_positions_special['market_showonindex'] && $ads['item_marketid'] > 0) {
         require_once cot_incfile('market', 'module');
         $db->update($db_market, array('item_showonindex' => 1), 'item_id='.$ads['item_marketid']);
       }

       cot_message('Баннер активирован');
     } else {
       cot_error(cot_sitename('padeg', 'i').' '.cot_sitename('words', 'notfound').', либо '.cot_sitename('words', 'notactive'), '');
     }
   }
  }
  elseif ($act == 'del' && $ads['item_expire'] < $sys['now'] && $ads['item_paused'] == 0)
  {
    $db->delete($db_ads, "item_id = ".(int)$ads['item_id']);

    foreach ($cot_extrafields[$db_ads] as $exfld)
    {
    	cot_extrafield_unlinkfiles($item['item_' . $exfld['field_name']], $exfld);
    }
    cot_message('Баннер удален');
  } else {
    cot_error('Невозможно выполнить действие', '');
  }
}

cot_redirect(cot_url('plug', 'e=ads', '', true));