<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=notify.tags
 * [END_COT_EXT]
 */
defined('COT_CODE') or die('Wrong URL.');

$allMarketCats = cot_structure_children('market', '', true);
$notifyMarket = array();
$notifyMarket_titles = array();

if (!empty($usr['profile']['user_notifymarket']) && $usr['profile']['user_notifymarket'] != '-1') {
    $rcats = explode(',', $usr['profile']['user_notifymarket']);
} elseif ($usr['profile']['user_notifymarket'] == '-1') {
    $rcats = array();
} else {
    $rcats = $allMarketCats;
}

$i = 0;
foreach ($allMarketCats as $cat) {
    $id = explode(".", $structure['market'][$cat]['rpath']);
    $k = count($id) - 1;
    unset($id[$k]);

    $ms = array_search($cat, array_merge(array(0), $rcats), 1);

    $notifyMarket[$i]["id"] = $structure['market'][$cat]['rpath'];
    $notifyMarket[$i]["parent"] = implode(".", $id) ? : "#";
    $notifyMarket[$i]["text"] = $structure['market'][$cat]['title'];
    $notifyMarket[$i]["state"]["opened"] = true;
    $notifyMarket[$i]["state"]["selected"] = $ms ? true : false;
    $notifyMarket[$i]["li_attr"]["data-id"] = $cat;
    $i++;
}

if ($a == 'update') {
    $rcats = cot_import('marketCats', 'P', 'TXT');
    $db->update($db_users, array('user_notifymarket' => $rcats), "user_id=" . $usr['id']);
}

$t->assign(array(
    'MARKET_CATS' => json_encode($notifyMarket),
));
