<?php
/*
Plugin Name: DuplicateKiller
Plugin URI: http://www.osclass.org/
Description: Marking the ad as spam automatically based on similarity percentage.
Version: 1.0
Author: Frosticek
Author URI: http://www.vbazare.sk
Short Name: DuplicateKiller
*/

function duplicatekiller_install() {
  $conn = getConnection();
  $conn->autocommit(false);
  try {
    $path = osc_plugin_resource('duplicatekiller/struct.sql');
    $sql = file_get_contents($path);
    $conn->osc_dbImportSQL($sql);
    $conn->commit();
  }
  catch (Exception $e) {
    $conn->rollback();
    echo $e->getMessage();
  }
  $conn->autocommit(true);
}

function duplicatekiller_uninstall() {
    // Insert here the code you want to execute after the plugin's uninstall
    // for example you might want to drop/remove a table or modify some values

    // In this case we'll remove the table we created to store Example attributes
    $conn = getConnection();
    $conn->autocommit(false);
    try {
        $conn->osc_dbExec('DROP TABLE %st_duplicatekiller', DB_TABLE_PREFIX);
        //$conn->osc_dbExec('DROP TABLE %st_duplicatekiller_ad_id', DB_TABLE_PREFIX);
        $conn->commit();
    } catch (Exception $e) {
        $conn->rollback();
        echo $e->getMessage();
    }
    $conn->autocommit(true);
}


function duplicatekiller_admin_menu() {
  echo '<h3><a href="#">Duplicatekiller</a></h3>
    <ul>
        <li><a href="' . osc_admin_render_plugin_url("duplicatekiller/settings.php") . '?section=types">&raquo; ' . __('Settings', 'duplicatekiller') . '</a></li>
    </ul>';
}
function get_percentage() {
  $conn = getConnection();
  $percentage = $conn->osc_dbFetchResults("SELECT percent FROM %st_duplicatekiller WHERE id=1", DB_TABLE_PREFIX);
  foreach ($percentage as $per) {
    echo $per[percent];
  }  
}

function post_percentage() {
  $conn = getConnection();
  $percentage = $conn->osc_dbFetchResult("SELECT percent FROM %st_duplicatekiller WHERE id=1", DB_TABLE_PREFIX);
  return $percentage['percent'];
}

function after_item_posted_duplicate() {

    $itemId = Params::getParam('itemId');
    $item = Item::newInstance()->findByPrimaryKey($itemId);
    $conn = getConnection();
    $percentage = post_percentage();

    $same_emails_id = $conn->osc_dbFetchResults("SELECT pk_i_id FROM %st_item WHERE s_contact_email= '%s' AND b_spam= 0", DB_TABLE_PREFIX, $item['s_contact_email']);

    foreach ($same_emails_id as $same_email_id) {
       if($itemId != $same_email_id['pk_i_id']) {
          $result_des_tit = $conn->osc_dbFetchResult("SELECT * FROM %st_item_description WHERE fk_i_item_id= %d", DB_TABLE_PREFIX, $same_email_id['pk_i_id']);
          $same_desc  = $result_des_tit['s_description'];
          $same_title = $result_des_tit['s_title'];

          similar_text(strtolower($same_desc),  strtolower($item['s_description']), $per_desc);
          //similar_text(strtolower($same_title), strtolower($item['s_title']), $per_title);

          if ($per_desc > $percentage && $per_title > $percentage) {
             $conn->osc_dbExec("UPDATE %st_item SET b_spam = 1 WHERE pk_i_id = %d", DB_TABLE_PREFIX, $same_email_id['pk_i_id']);
             $conn->osc_dbExec("INSERT INTO %st_duplicatekiller_ad_id (id) VALUES (%d)", DB_TABLE_PREFIX, $same_email_id['pk_i_id']);
             osc_add_flash_error_message(__('Добавлен дубликат объявления, более старое объявление обнавлено!', 'duplicatekiller'));

      }
    }
  }
} 

function after_item_validate_duplicate($id) {
$conn = getConnection();
$result = $conn->osc_dbFetchResults("SELECT * FROM %st_duplicatekiller_ad_id WHERE id='%s'",DB_TABLE_PREFIX,$id);
$number_of_rows = count($result);
echo $number_of_rows;
if ($number_of_rows > 0)
{
$conn->osc_dbExec("UPDATE %st_item SET b_spam = 1 WHERE pk_i_id = '%s'", DB_TABLE_PREFIX,$id);
osc_add_flash_error_message(__('Объявление удалено потому что вы продублировали свое объявление!', 'duplicatekiller'));
}
}

//osc_register_plugin(osc_plugin_path(__FILE__), 'duplicatekiller_install');
//osc_add_hook(osc_plugin_path(__FILE__) . '_uninstall','duplicatekiller_uninstall');
osc_add_hook('after_item_post', 'after_item_posted_duplicate');
osc_add_hook('activate_item', 'after_item_validate_duplicate');
osc_add_hook('admin_menu', 'duplicatekiller_admin_menu');
?>