<?php

/* CLASS FILE
----------------------------------*/

class page extends db {

  public $dt;
  public $settings;
  
  public function admin_add($fm) {
    db::db_query("INSERT INTO `" . DB_PREFIX . "admin_pages` (
    `name`,
    `info`,
    `en`,
    `tmp`
    ) VALUES (
    '" . db::db_escape($fm['name']) . "',
    '" . db::db_escape($fm['info']) . "',
    '{$fm['en']}',
    '" . db::db_escape($fm['tmp']) . "'
    )");
    $ID = db::db_last_insert_id();
    // If custom ordering for menu exists, add new page to the mix
    if (db::db_rowcount('cfg', 'WHERE `op1` = \'admin_links\'', false) > 0) {
      page::cfg_links($ID);
    }
    return $ID;
  }

  public function admin_update($fm) {
    db::db_query("UPDATE `" . DB_PREFIX . "admin_pages` SET
    `name`    = '" . db::db_escape($fm['name']) . "',
    `info`    = '" . db::db_escape($fm['info']) . "',
    `en`      = '{$fm['en']}',
    `tmp`     = '" . db::db_escape($fm['tmp']) . "'
    WHERE `id` = '{$fm['id']}'
    ");
    // If custom ordering for menu exists, check all exist
    if (db::db_rowcount('cfg', 'WHERE `op1` = \'admin_links\'', false) > 0) {
      page::cfg_links();
    }
    return $fm['id'];
  }

  public function admin_delete($fm) {
    db::db_query("DELETE FROM `" . DB_PREFIX . "admin_pages` WHERE `id` IN(" . db::db_escape(implode(',', $fm['ids'])) . ")");
    page::cfg_links();
    db::db_truncate(array('admin_pages'));
  }
  
  private function cfg_links($id = 0) {
    $q = db::db_query("SELECT * FROM `" . DB_PREFIX . "cfg` WHERE `op1` = 'admin_links' AND `op2` = 'eight'");
    // If no admin pages, remove link..
    if (db::db_rows($q) > 0 && db::db_rowcount('admin_pages', '', false) == 0) {
      db::db_query("DELETE FROM `" . DB_PREFIX . "cfg` WHERE `op1` = 'admin_links' AND `op2` = 'eight'");
    }
    // Add link if it doesn't exist..
    if ($id > 0 && db::db_rows($q) == 0 && db::db_rowcount('admin_pages', '', false) > 0) {
      db::db_query("INSERT INTO `" . DB_PREFIX . "cfg` (
      `op1`,
      `op2`,
      `op6`
      ) VALUES (
      'admin_links',
      'eight',
      'apg&id=" . $id . "'
      )"); 
    }
    // Add other links to array..
    if (db::db_rows($q) > 0 && db::db_rowcount('admin_pages', '', false) > 0) {
      $ar = [];
      $Q_APG = db::db_query("SELECT `id` FROM `" . DB_PREFIX . "admin_pages`
               WHERE `en` = 'yes'
               ORDER BY `ordr`
               ");
      while ($APG = db::db_object($Q_APG)) {
        $ar[] = 'apg&id=' . $APG->id;
      }
      if (!empty($ar)) {
        db::db_query("UPDATE `" . DB_PREFIX . "cfg` SET
        `op6` = '" . db::db_escape(implode(',', $ar)) . "'
        WHERE `op1` = 'admin_links'
        AND `op2` = 'eight'
        ");
      }
    }
  }

  public function add($fm) {
    db::db_query("INSERT INTO `" . DB_PREFIX . "pages` (
    `name`,
    `metat`,
    `info`,
    `en`,
    `tmp`
    ) VALUES (
    '" . db::db_escape($fm['name']) . "',
    '" . db::db_escape($fm['metat']) . "',
    '" . db::db_escape($fm['info']) . "',
    '{$fm['en']}',
    '" . db::db_escape($fm['tmp']) . "'
    )");
    $ID = db::db_last_insert_id();
    db::db_query("UPDATE `" . DB_PREFIX . "pages` SET `ordr` = '{$ID}' WHERE `id` = '{$ID}'");
    return $ID;
  }

  public function update($fm) {
    db::db_query("UPDATE `" . DB_PREFIX . "pages` SET
    `name`    = '" . db::db_escape($fm['name']) . "',
    `metat`   = '" . db::db_escape($fm['metat']) . "',
    `info`    = '" . db::db_escape($fm['info']) . "',
    `en`      = '{$fm['en']}',
    `tmp`     = '" . db::db_escape($fm['tmp']) . "'
    WHERE `id` = '{$fm['id']}'
    ");
  }

  public function delete($fm) {
    if (!empty($fm['ids'])) {
      db::db_query("DELETE FROM `" . DB_PREFIX . "pages` WHERE `id` IN(" . db::db_escape(implode(',', $fm['ids'])) . ")");
      db::db_truncate(array('pages'));
    }
  }

  public function order($fm, $table = 'pages') {
    $cnt = 0;
    foreach ($fm['sort'] AS $id) {
      db::db_query("UPDATE `" . DB_PREFIX . $table . "` SET
      `ordr`  = '" . (++$cnt) . "'
      WHERE `id` = '{$id}'
      ");
    }
  }

}

?>