<?php
/**
 * Copyright (C) LJCMS Co.,Ltd. All rights reserved.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2015.03.03
 */

class relatedlinkIModel extends X
{
    
    public function tagContent($mod, $content)
    {
        if (!empty($content))
        {
            $tags = $this->_getRelatedLink($mod);

            if (!empty($tags))
            {
                foreach ($tags as $key => $value)
                {
                    $content = preg_replace('/' . $value['name'] . '/', $value['tag'], $content, $value['count']);
                }
            }
        }
        return $content;
    }

    
    private function _getRelatedLink($mod)
    {
        $new_data[] = null;
        $sql = "SELECT * FROM " . DB_PREFIX . "relatedlink WHERE flag='1'";
        if ($mod == 'article')
        {
            $sql .= " AND article='1'";
        } elseif ($mod == 'product')
        {
            $sql .= " AND product='1'";
        } elseif ($mod == 'photo')
        {
            $sql .= " AND photo='1'";
        } elseif ($mod == 'download')
        {
            $sql .= " AND download='1'";
        } elseif ($mod == 'about')
        {
            $sql .= " AND about='1'";
        }
        $data = parent::$obj->getall($sql);
        foreach ($data as $key => $value)
        {
            $tag = null;
            $tag = "<a href='" . $value['url'] . "'";
            if ($value['target'] == 2)
            {
                $tag .= " target='_blank'";
            }
            if ($value['nofollow'] == 1)
            {
                $tag .= " rel='nofollow'";
            }
            if (!empty($value['color']))
            {
                $tag .= " style='color:" . $value['color'] . "'";
            }
            $tag .= ">" . $value['linktag'] . "</a>";
            $new_data[] = array('name' => $value['linktag'], 'tag' => $tag,'count'=>$value['count']);
        }

        return $new_data;
    }
}

?>
