<?php
//@*--


class Theme_Designer_Model_Export_ImageList
{
    /**
     * @var Theme_Designer_Model_Export_Image[]
     */
    private $_images = array();
    private $_cssReplaceInfo = array();
    private $_htmlReplaceInfo = array();

    public function add($id, $content)
    {
        $image = Mage::getModel('designer/export_image');
        $image->setId($id);
        $image->setContent($content);
        $image->save();
        $this->_images[$id] = $image;
    }

    public function export($exportDir)
    {
        if (!is_dir($exportDir)) {
            mkdir($exportDir, 0777, true);
        }

        foreach ($this->_images as $image) {
            $imagePath = $exportDir . '/' . $image->getFileName();
            $content = $image->getContent();
            if ($content) {
                file_put_contents($imagePath, base64_decode($content));
            }

            $this->_cssReplaceInfo[] = array(
                'from' => $image->getId(),
                'to' => 'images/designer/' . $image->getFileName()
            );

            $this->_htmlReplaceInfo[] = array(
                'from' => 'url(' . $image->getId() . ')',
                'to' => '<?php echo $this->getSkinUrl("images/designer/' . $image->getFileName() . '") ?>'
            );
        }
    }

    public function getCssReplaceInfo()
    {
        return Mage::helper('designer')->arrayColumns($this->_cssReplaceInfo, array('from', 'to'));
    }

    public function getHtmlReplaceInfo()
    {
        return Mage::helper('designer')->arrayColumns($this->_htmlReplaceInfo, array('from', 'to'));
    }
}

//--*@