*
 * @return grid_data_source
 */
function create_ds_header($manager)
{
    global $cats_cid;
    return new grid_data_source(new grid_header_item_array(new grid_header_item('u2cid', 'Id', type::STRING, true), new grid_header_item('url', 'URL', type::LINK_NEWWIN, true, null, true, true), new grid_header_item('cid', 'Категория', type::STRING, true, new cat_urls_formatter('category', $cats_cid, $manager)), new grid_header_item('actions', 'Действия', null, false, new cat_urls_formatter('actions', $cats_cid, $manager))));
}
$url_cats_unmatched_ds = create_ds_header($emanager->client_id());
$url_cats_unmatched_grid = new ajax_grid('url_cats_grid_unmatched', $url_cats_unmatched_ds, $ajaxbuf_url_cats);
$url_cats_unmatched_grid_pager = new ajax_grid_pager('url_cats_grid_pager_unmatched', $BILL->GetCategoriesUrlUnMatchedCount($url_cats_unmatched_grid->get_filterfield(), $url_cats_unmatched_grid->get_filtering()), 10);
$url_cats_unmatched_grid->attach_pager($url_cats_unmatched_grid_pager);
$url_cats_matched_ds = create_ds_header($emanager->client_id());
$url_cats_matched_grid = new ajax_grid('url_cats_grid_matched', $url_cats_matched_ds, $ajaxbuf_url_matched_cats);
$url_cats_matched_grid_pager = new ajax_grid_pager('url_cats_grid_pager_matched', $BILL->GetCategoriesUrlMatchedCount($url_cats_matched_grid->get_filterfield(), $url_cats_matched_grid->get_filtering()), 10);
$url_cats_matched_grid->attach_pager($url_cats_matched_grid_pager);
/**
 * Check if we need to make some actions
 */
if ($emanager->isAnyAction()) {
    //special actions
    if ($emanager->getAction() == 'changeCatByName') {
        $item = json_decode($emanager->getItem());
        $BILL->UpdateUrlCategoryMatchByName($item->url, $item->name);
    } elseif ($emanager->getAction() == 'recognizeAll') {
        $url_cats = $BILL->GetUrlCategoriesMatch($url_cats_unmatched_grid_pager->get_curpage(), 10, $url_cats_unmatched_grid->get_sorting(), $url_cats_unmatched_grid->get_sort_direction(), array(0), array());
        require_once dirname(__FILE__) . '/CADBiS/recognize.php';
        foreach ($url_cats as $url) {
            $catname = Recognizer::recognizeByUrlCheck($url['url']);
            if (!empty($catname)) {
                $BILL->UpdateUrlCategoryMatchByName($url['url'], $catname);
class conflicts_act_formatter extends grid_formatter
{
    public function format($data, $type, $number = 0, $columns = null)
    {
        return '<input type="checkbox" name="selected_kwds[' . $data['keyword'] . '][]" />';
    }
}
class conflicts_cat_formatter extends grid_formatter
{
    protected $_cats = null;
    public function __construct($cats)
    {
        $this->_cats = $cats;
    }
    public function format($data, $type, $number = 0, $columns = null)
    {
        return !empty($this->_cats[$data]['title_ru']) ? $this->_cats[$data]['title_ru'] : $this->_cats[$data]['title'];
    }
}
$ajaxbuffer = new ajax_buffer("update_buffer");
$datasource = new grid_data_source(new grid_header_item_array(new grid_header_item('id', '', null, false, new conflicts_act_formatter()), new grid_header_item('url', 'URL', type::LINK_NEWWIN, true, null, true, true), new grid_header_item('keyword', 'Слово', type::STRING, true, null, true, true), new grid_header_item('forcid', 'Категория', type::STRING, true, new conflicts_cat_formatter($cats)), new grid_header_item('incid', 'Конфликт с', type::STRING, true, new conflicts_cat_formatter($cats)), new grid_header_item('date', 'Дата', type::STRING, true)));
$grid = new ajax_grid('grid', $datasource, $ajaxbuffer);
$grid->no_data_message = 'Нет записей';
$grid->render_pager_top(true);
$grid->render_filter_bottom(true);
$grid_pager = new ajax_grid_pager('grid_pager', $BILL->GetRowsCount('url_categories_conflicts', $grid->get_filterfield(), $grid->get_filtering()), COUNT_ON_PAGE);
$grid->attach_pager($grid_pager);
$conflicts = $BILL->GetUrlCategoriesConflicts($grid_pager->get_curpage(), $grid_pager->get_pagesize(), $grid->get_sorting(), $grid->get_sort_direction(), $grid->get_filterfield(), $grid->get_filtering());
foreach ($conflicts as $conflict) {
    $datasource->add_row(array($conflict, $conflict['url'], $conflict['keyword'], $conflict['forcid'], $conflict['incid'], $conflict['date']));
}
    }
    public function format($data, $type, $number = 0, $columns = null)
    {
        switch ($this->_field) {
            case 'actions':
                return '<a href="javascript:deleteCat(' . $data['cid'] . ');">Удалить</a>,
						<a href="javascript:editCat(' . $data['cid'] . ',\'' . $data['title'] . '\',\'' . $data['title_ru'] . '\');">Изменить</a>';
            default:
                return parent::format($data, $type);
        }
    }
}
$cats_ds = new grid_data_source(new grid_header_item_array(new grid_header_item('cid', 'Id', type::STRING, true), new grid_header_item('title', 'Категория', type::STRING, true, null, true, true), new grid_header_item('title_ru', 'Название', type::STRING, true, null, true, true), new grid_header_item('actions', 'Действия', null, false, new cats_formatter('actions', $emanager))));
$cats_grid = new ajax_grid('cats_grid', $cats_ds, $ajaxbuf_cats);
$cats_grid_pager = new ajax_grid_pager('cats_grid_pager', $BILL->GetRowsCount('url_categories', $cats_grid->get_filterfield(), $cats_grid->get_filtering()), 20);
$cats_grid->attach_pager($cats_grid_pager);
/**
 * Check if we need to make some actions
 */
if ($emanager->isAnyAction() && $ajaxbuf_cats->is_post_back()) {
    switch ($emanager->getAction()) {
        case $emanager->action->ADD:
            $item = json_decode($emanager->getItem());
            $BILL->AddUrlCategory(array('title' => $item->title, 'title_ru' => $item->title_ru));
            break;
        case $emanager->action->UPD:
            $item = json_decode($emanager->getItem());
            $item->keywords = explode(',', $item->keywords);
            for ($i = 0; $i < count($item->keywords); ++$i) {
                $item->keywords[$i] = rtrim(ltrim($item->keywords[$i]));
                if (strlen($item->keywords[$i]) < 4 || empty($item->keywords[$i])) {