ACC SHELL

Path : /srv/www/vhosts/tsisystem/app/models/
File Upload :
Current File : //srv/www/vhosts/tsisystem/app/models/product.php

<?php

/* $Id$ */

class Product extends AppModel
{
	var $name = 'Product';

    var $_words = array();
    var $_docs = array();

    /**
     *
     */
    function beforeSave()
    {
        if (empty($this->data['Product']['name'])) {
            return $this->invalidate('name');
        }

        $this->data['Product']['descr'] = $this->_cleanHTML($this->data['Product']['descr']);

        return true;
    }

    /**
     *
     */
    function afterSave()
    {
        // Update index field
        $idx = strtolower(__latin2ascii($this->data['Product']['name']));
        $idx = preg_replace('/[ _]+/', '-', $idx);
        $idx = preg_replace('/[^a-z0-9-]/', '', $idx);
        $idx .= '--' . $this->id;

        $this->query("update {$this->table} set name_idx = '$idx' where id = {$this->id}");
        $this->_clearCache();

        return;

        $this->query("update {$this->table} t1 set is_cat = 0 where id <> $this->id");

        $ids = array();
        foreach ($this->query("
            select distinct t1.id from {$this->table} t1, {$this->table} t2
            where t1.id = t2.parent"
            ) as $row) {
            $ids[] = $row['t1']['id'];
        }

        if ($ids = join(',', $ids)) {
            $this->query("update {$this->table} set is_cat = 1 where id in ($ids)");
        }

    }

    /**
     *
     */
    function afterDelete()
    {
        $this->_clearCache();
    }


    /**
     *
     */
    function menuTree($current)
    {
        // which nodes to expand
        $this->expandedNodes[$selected = $current['Product']['id']] = 1;

        $limit = 10;
        $parent = $current['Product']['parent'];

        while ($parent && $limit--) {
            $tmp = $this->find('id = ' . (int) $parent, 'id, name, name_idx, is_cat, parent');
            $this->current_path[$tmp['Product']['name']] = $tmp['Product']['name_idx'];

            if (! $parent = (int) $tmp['Product']['parent']) {
                break;
            }
            $last = $tmp;
            $this->expandedNodes[$tmp['Product']['id']] = 1;
        }

        if (empty($this->current_path)) {
            $this->current_path[$current['Product']['name']] = $current['Product']['name_idx'];
        }
        else if (count($this->current_path) == 1) {
            $this->current_path = array_merge(
                array($current['Product']['name'] => $current['Product']['name_idx']),
                $this->current_path
            );
        }

        if (! isset($tmp)) {
            return $this->buildTreeMenu($current['Product']['id']);
        }

        return $this->buildTreeMenu($tmp['Product']['id']);
    }

    /**
     *
     */
    function buildTreeMenu($id)
    {
        $menu = array();

        foreach ($this->findAll('parent = ' . (int) $id,
                'id, name, name_idx, is_cat', 'prod_sort') as $ary) {
            $menu[$ary['Product']['id']] = array('data' => $ary['Product']);

            if (isset($this->expandedNodes[$ary['Product']['id']])
                && $ary['Product']['is_cat']) {
                $menu[$ary['Product']['id']]['child']
                    = $this->buildTreeMenu($ary['Product']['id']);
            }
        }

        return $menu;
    }

    /**
     *
     */
    function sortUp($id)
    {
        if (! $row = $this->findById($id)) {
            return;
        }
        if (! ($parent = (int) $row['Product']['parent'])) {
            return;
        }

        $ary = array();
        $sort = 1;
        $previous = $last = NULL;
        foreach ($this->findAll("parent = $parent", 'id', 'prod_sort asc') as $row) {
            if ($row['Product']['id'] == $id) {
                $previous = $last;
            }
            $ary[$row['Product']['id']] = $sort++;
            $last = $row['Product']['id'];
        }

        if ($previous) {
            $ary[$previous]++;
            $ary[$id]--;
            $this->sortSave($ary);
        }
    }

    /**
     *
     */
    function sortDown($id)
    {
        if (! $row = $this->findById($id)) {
            return;
        }
        if (! ($parent = (int) $row['Product']['parent'])) {
            return;
        }

        $ary = array();
        $sort = 1;
        $next = $last = NULL;
        foreach ($this->findAll("parent = $parent", 'id', 'prod_sort asc') as $row) {
            if ($last == $id) {
                $next = $row['Product']['id'];
            }
            $ary[$row['Product']['id']] = $sort++;
            $last = $row['Product']['id'];
        }

        if ($next) {
            $ary[$next]--;
            $ary[$id]++;
            $this->sortSave($ary);
        }

    }

    /**
     *
     */
    function sortSave($ary)
    {
        foreach ($ary as $id => $sort) {
            $this->query(sprintf("update %s set prod_sort = %d where id = %d",
                $this->table, $sort, $id));
        }

        $this->_clearCache();
    }
}

?>

ACC SHELL 2018