<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Tables_model extends CI_Model {

    public function __construct() {
        parent::__construct();
    }

    function getfloors() {
        $this->db->select('NAME,ID');
        $q = $this->db->get('FLOORS');
        return $q->result_array();
    }

    function getplaces() {
        $this->db->select('P.NAME,P.ID ,P.FLOOR, S.CONTENT');
        $this->db->join('SHAREDTICKETS AS S', 'S.ID = P.ID', 'LEFT');
        $q = $this->db->get('PLACES P');
        return $q->result_array();
    }

    function getsharedticket($tableid) {
        $this->db->select('ST.NAME,ST.ID ,ST.CONTENT');
        $this->db->where('ST.ID', $tableid);
        $q = $this->db->get('SHAREDTICKETS ST');
        return $q->result_array();
    }

    function addsharedticket($sharedticket) {
        $this->db->insert('SHAREDTICKETS', $sharedticket);
        if ($this->db->affected_rows() == '1') {
            return TRUE;
        }
        return FALSE;
    }

    public function getticketStatus() {
        $this->db->select('ST.NAME,ST.ID ,ST.CONTENT');

        $q = $this->db->get('SHAREDTICKETS ST');
        $results = $q->result_array();
        $ar = array();
        foreach ($results as $rs) {


//UPDATE QUANTITY IN XML       
            $xml = new DOMDocument('1.0');

            $xml->loadXml($rs['CONTENT']);
            $xml->saveXML();
//SELECT ROOT TAG TO UPDATE 
            $Xpath = new DOMXPath($xml);
            $result = $Xpath->query('/ticket');

            $baz_node_of_xml = $result->item(0);
            $root = $xml->documentElement;
            $i = 0;
            foreach ($result as $ticket) {
                $status = $ticket->getElementsByTagName('status')->item(0)->textContent;


                if ($status == 1) {
                    array_push($ar, $rs['ID']);
                }
            }
        }
        return $ar;
    }

}

