<?php

//No direct acesss
defined('_JEXEC') or die();
jimport('joomla.application.component.model');

class wishlogueModelwishlogueadspace extends JModelList {

    function display() {
   
    }

    function publishstatus($postdetails, $publishstatus) {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        foreach ($postdetails['cid'] as $pads) {
            $query = "UPDATE #__pin_wishlogueadspace set status = " . $publishstatus . " WHERE id=" . $pads; //update query for publish and unpublish
            $db->setQuery($query);
            $db->query();
        }
    }

    function deleteads($postdetails) {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        foreach ($postdetails['cid'] as $pads) {
            $query = 'SELECT imagename FROM `#__pin_wishlogueadspace` WHERE id=' . $pads;
            $db->setQuery($query);
            $deleteimage = $db->loadresult();
            JFile::delete(JPATH_ROOT . "/components/com_wishlogue/images/wishlogueadspace/" . $deleteimage);

            $query = "DELETE FROM #__pin_wishlogueadspace WHERE id=" . $pads; //update query for publish and unpublish
            $db->setQuery($query);
            $db->query();
        }
    }

    function getallads() {
        global $option, $mainframe;
        $app = JFactory::getApplication();
        $mainframe = JFactory::getApplication();
        $filter_order_wishlogueadspace = $app->getUserStateFromRequest($option . 'filter_order_wishlogueadspace', 'filter_order', 'id', 'cmd');
        $filter_order_Dir = $app->getUserStateFromRequest($option . 'filter_order_Dir', 'filter_order_Dir', 'asc', 'word');

        $data = new stdClass();
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select(array('id', 'url', 'createddate', 'imagename', 'status', 'description'));
        $query->from('#__pin_wishlogueadspace');
        $query->order($filter_order_wishlogueadspace . ' ' . $filter_order_Dir);
        $db->setQuery($query);
        $results = $db->loadObjectList();
        $results = array('getallads' => $results, 'order_Dir' => $filter_order_Dir, 'order' => $filter_order_wishlogueadspace);
        return $results;
    }

    function applynew($adImage, $adURL, $adStatus, $description) {
        $extension = JFile::getExt($adImage['name']);
        $extension = strtolower($extension);
        if ($extension == "jpg") {
            $extension = "jpeg";
        }
        if ($extension == "jpeg" || $extension == "png" || $extension == "gif") {
            $data = new stdClass();
            $db = JFactory::getDbo();
            $query = 'SELECT MAX(id) FROM `#__pin_wishlogueadspace`';
            $db->setQuery($query);
            $maxid = $db->loadresult();
            $src = $adImage['tmp_name'];
            $dest = JPATH_SITE . "/components/com_wishlogue/images/wishlogueadspace/" . ( intval($maxid) + 1 ) . "." . $extension;

            list($width, $height) = getimagesize($src);
            $newwidth = 200;
            $newheight = 200;
            $thumb = imagecreatetruecolor($newwidth, $newheight);

            switch ($extension) {
                case "jpeg": {
                        header('Content-Type: image/jpeg');
                        $source = imagecreatefromjpeg($src);
                        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                        imagejpeg($thumb, $dest);
                        break;
                    }
                case "png": {
                        header('Content-Type: image/png');
                        $source = imagecreatefrompng($src);
                        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                        imagepng($thumb, $dest);
                        break;
                    }

                case "gif": {
                        header('Content-Type: image/gif');
                        $source = imagecreatefromgif($src);
                        imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                        imagegif($thumb, $dest);
                        break;
                    }
            }
            //JFile::upload($src, $dest);
            $data->url = $adURL;
            $data->status = $adStatus;
            $data->imagename = ( intval($maxid) + 1 ) . "." . $extension;
            $data->description = $description;
            $db->insertObject('#__pin_wishlogueadspace', $data, id);
        }
    }
}

?>