View file Upload/src/addons/Brivium/CustomNodeIcon/XF/Entity/Node.php

File size: 3.77Kb
<?php
namespace Brivium\CustomNodeIcon\XF\Entity;

class Node extends XFCP_Node
{

    public function getAbstractedCustomNodeIconPath($type)
    {
        $nodeId = $this->node_id;
        return sprintf('data://brivium/CustomNodeIcons/%s/%d/%d.jpg',
            $type,
            floor($nodeId / 1000),
            $nodeId
        );
    }

    public function getIconUrlByType($type, $ignoreDefault = false)
    {
        $nodeIconData = $this->node_icon_data;
        if(empty($nodeIconData[$type]))
        {
            return false;
        }

        switch ($nodeIconData[$type]['type'])
        {
            case 'default';
                if(empty($ignoreDefault))
                {
                    break;
                }
            case 'image';
                if(empty($nodeIconData[$type]['date']))
                {
                    return false;
                }

                $date = $nodeIconData[$type]['date'];
                $app = $this->app();
                $nodeId = $this->node_id;
                return $app->applyExternalDataUrl(
                    sprintf('brivium/CustomNodeIcons/%s/%d/%d.jpg?%d',
                        $type,
                        floor($nodeId / 1000),
                        $nodeId,
                        $date
                    )
                );
        }
        return  false;
    }

    public function hasNodeIcon($type = 'read')
    {
        $nodeIconData = $this->node_icon_data;
        if(empty($nodeIconData[$type]))
        {
            return false;
        }

        switch ($nodeIconData[$type]['type'])
        {
            case 'image';
                if(empty($nodeIconData[$type]['date']))
                {
                    return false;
                }
                return $nodeIconData[$type]['date'];
        }
        return  false;
    }

    public function getNodeIconData()
    {
        $nodeIconData = !empty($this->brcni_icon_data) ? $this->brcni_icon_data : [];

        $nodeIconData += array(
            'read' => ['type' => 'default'],
            'unread' => ['type' => 'default'],
        );
        foreach ($nodeIconData as $type => &$val)
        {
            if(is_array($val))
            {
                continue;
            }

            $val = [
                'type' => !empty($this->brcni_icon_type) ? $this->brcni_icon_type : 'image',
                'date' => $val,
            ];
        }

        return $nodeIconData;
    }

    public function isUseNodeIcon()
    {
        $nodeIconData = $this->node_icon_data;

        $notUseNodeIcon = true;
        foreach ($nodeIconData as $type => $val)
        {
            if(!is_array($val) || empty($val['type']) ||  $val['type'] != 'default')
            {
                $notUseNodeIcon = false; break;
            }
        }
        return !$notUseNodeIcon;
    }

    public function setNodeIconData(array $nodeIconData)
    {
        $originalNodeIconData = $this->node_icon_data;
        foreach ($nodeIconData as $type => &$data)
        {
            if(!empty($originalNodeIconData[$type]) && is_array($originalNodeIconData[$type]))
            {
                $data += $originalNodeIconData[$type];
            }
        }

        $nodeIconData += $originalNodeIconData;
        $this->set('brcni_icon_data', $nodeIconData);
    }

	public static function getStructure(\XF\Mvc\Entity\Structure $structure)
	{
        $structure = parent::getStructure($structure);
        $structure->columns += [
            'brcni_icon_type' => ['type' => self::STR, 'default' => '',
                'allowedValues' => ['', 'image']
            ],
            'brcni_icon_data' => ['type' => self::JSON_ARRAY, 'default' => []],
        ];

        $structure->getters += [
            'node_icon_data' => true
        ];

        return $structure;
	}
}