public function execute()
 {
     if (!$this->getUser()->getRights('shop', 'settings')) {
         throw new waRightsException(_w('Access denied'));
     }
     $model = new shopTypeModel();
     $data = array();
     $data['id'] = waRequest::post('id', 0, waRequest::TYPE_INT);
     switch (waRequest::post('source', 'custom')) {
         case 'custom':
             $data['name'] = waRequest::post('name');
             $data['icon'] = waRequest::post('icon_url', false, waRequest::TYPE_STRING_TRIM);
             if (empty($data['icon'])) {
                 $data['icon'] = waRequest::post('icon', 'icon.box', waRequest::TYPE_STRING_TRIM);
             }
             if (!empty($data['id'])) {
                 $model->updateById($data['id'], $data);
             } else {
                 $data['sort'] = $model->select('MAX(sort)+1 as max_sort')->fetchField('max_sort');
                 $data['id'] = $model->insert($data);
             }
             break;
         case 'template':
             $data = $model->insertTemplate(waRequest::post('template'), true);
             break;
     }
     if ($data) {
         $data['icon_html'] = shopHelper::getIcon($data['icon'], 'icon.box');
         $data['name_html'] = '<span class="js-type-icon">' . $data['icon_html'] . '</span>
                 <span class="js-type-name">' . htmlspecialchars($data['name'], ENT_QUOTES, 'utf-8') . '</span>';
     }
     $this->response = $data;
 }
    private function typesAutocomplete($q)
    {
        $result = array();
        $model = new shopTypeModel();
        $value = $model->escape($q, 'like');
        $table = $model->getTableName();
        $sql = <<<SQL
SELECT `icon`,`id`,`name`,`count` FROM {$table}
WHERE
  `name` LIKE '%{$value}%'
ORDER BY `count` DESC
LIMIT 20
SQL;
        foreach ($model->query($sql)->fetchAll('id', true) as $id => $t) {
            $icon = shopHelper::getIcon($t['icon']);
            unset($t['icon']);
            $t['count'] = _w('%d product', '%d products', $t['count']);
            $result[] = array('id' => $id, 'value' => $id, 'name' => $t['name'], 'label' => $icon . implode('; ', array_filter($t)));
        }
        return $result;
    }
    /**
     * UI event handler
     * @return array
     */
    public function backendProductsEvent()
    {
        $icon = shopHelper::getIcon($this->getPluginStaticUrl() . 'img/yandexmarket.png');
        $html = <<<HTML
 <li data-action="export" data-plugin="yandexmarket" title="Экспорт выбранных товаров в Яндекс.Маркет">
    <a href="#">{$icon}Яндекс.Маркет</a>
 </li>
HTML;
        return array('toolbar_export_li' => $html);
    }