CREATE OR REPLACE FUNCTION resource_copy_trigger() RETURNS trigger AS $$
 DECLARE
  lng TEXT;
  nme TEXT;
 BEGIN
  lng := TG_ARGV[1];
  IF (TG_ARGV[0] = 'object') THEN
    PERFORM resource_copy(NEW.name, NEW.id, TG_ARGV[0], lng);
  ELSIF (TG_ARGV[0] = 'region' OR TG_ARGV[0] = 'channel') THEN
    CASE lng
      WHEN 'en' THEN
        nme := NEW.name_en;
      WHEN 'de' THEN
        nme := NEW.name_de;
      WHEN 'es' THEN
        nme := NEW.name_es;
      WHEN 'it' THEN
        nme := NEW.name_it;
      WHEN 'pl' THEN
        nme := NEW.name_pl;
      WHEN 'fr' THEN
        nme := NEW.name_fr;
      WHEN 'fi' THEN
        nme := NEW.name_fi;
      WHEN 'sv' THEN
        nme := NEW.name_sv;
      WHEN 'ru' THEN
        nme := NEW.name_ru;
      WHEN 'ja' THEN
        nme := NEW.name_ja;
      WHEN 'da' THEN
        nme := NEW.name_da;
      WHEN 'nl' THEN
        nme := NEW.name_nl;
      WHEN 'zh' THEN
        nme := NEW.name_zh;
      WHEN 'ko' THEN
        nme := NEW.name_ko;
      WHEN 'tr' THEN
        nme := NEW.name_tr;
      WHEN 'id' THEN
        nme := NEW.name_id;
      WHEN 'pt' THEN
        nme := NEW.name_pt;
      WHEN 'ar' THEN
        nme := NEW.name_ar;
      WHEN 'ct' THEN
        nme := NEW.name_ct;
      WHEN 'hi' THEN
        nme := NEW.name_hi;
      WHEN 'th' THEN
        nme := NEW.name_th;
      WHEN 'bg' THEN
        nme := NEW.name_bg;
      WHEN 'cs' THEN
        nme := NEW.name_cs;
      WHEN 'hu' THEN
        nme := NEW.name_hu;
      WHEN 'ro' THEN
        nme := NEW.name_ro;
      WHEN 'ca' THEN
        nme := NEW.name_ca;
      WHEN 'et' THEN
        nme := NEW.name_et;
      WHEN 'lv' THEN
        nme := NEW.name_lv;
      WHEN 'lt' THEN
        nme := NEW.name_lt;
      WHEN 'vi' THEN
        nme := NEW.name_vi;
      WHEN 'hr' THEN
        nme := NEW.name_hr;
      WHEN 'sr' THEN
        nme := NEW.name_sr;
      WHEN 'el' THEN
        nme := NEW.name_el;
      WHEN 'uk' THEN
        nme := NEW.name_uk;
      WHEN 'he' THEN
        nme := NEW.name_he;
      WHEN 'fa' THEN
        nme := NEW.name_fa;
      WHEN 'tl' THEN
        nme := NEW.name_tl;
      WHEN 'sl' THEN
        nme := NEW.name_sl;
      WHEN 'ta' THEN
        nme := NEW.name_ta;
      WHEN 'sw' THEN
        nme := NEW.name_sw;
      WHEN 'ur' THEN
        nme := NEW.name_ur;
      WHEN 'sq' THEN
        nme := NEW.name_sq;
      WHEN 'ka' THEN
        nme := NEW.name_ka;
      WHEN 'hy' THEN
        nme := NEW.name_hy;
      WHEN 'bn' THEN
        nme := NEW.name_bn;
      WHEN 'gu' THEN
        nme := NEW.name_gu;
      WHEN 'mr' THEN
        nme := NEW.name_mr;
      WHEN 'te' THEN
        nme := NEW.name_te;
    END CASE;
    EXECUTE 'SELECT resource_copy('||quote_nullable(nme)||', '||cast(NEW.id AS TEXT)||', '''||TG_ARGV[0]||''', '''||lng||''')';    
  END IF;

  RETURN NULL;
 END
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION region_resource_update_trigger() RETURNS trigger AS $$
 BEGIN
  PERFORM regionid FROM region_updates WHERE regionid = NEW.objectid AND updatetype = 'object';
  IF NOT FOUND THEN
    INSERT INTO region_updates(regionid, updatetype) VALUES (NEW.objectid, 'object');
  END IF;
  RETURN NULL;
 END
$$ LANGUAGE plpgsql;

DROP FUNCTION resource_copy(TEXT, INT, TEXT, TEXT);
CREATE OR REPLACE FUNCTION resource_copy(nimi TEXT, id INT, target TEXT, lng TEXT) RETURNS BOOLEAN AS $$
  nimi = args[0]
  tname = str(nimi)
  id_str = str(id) 

  # loading script equivalent language
  se_query = plpy.prepare("SELECT script_equivalent AS se FROM language WHERE isocode = $1", ["text"])
  selngs = plpy.execute(se_query, [lng])
  selng = selngs[0]["se"]

  # loading object name if not given
  if nimi == None:
    if (target == "region"):
      name_query = plpy.prepare("SELECT COALESCE(name_"+lng+", name_"+selng+", name_en) AS name FROM region WHERE id = $1", ["int"])
      names = plpy.execute(name_query, [id])
      tname = names[0]["name"]
    else:
      name_query = plpy.prepare("SELECT language, value FROM data WHERE objectid = $1 AND datatypeid = 'name' AND visible AND language IN ('"+lng+"', '"+selng+"', 'en') ", ["int"])
      names = plpy.execute(name_query, [id])
      namedic = {}
      i = 0
      row = True
      while row:
        try:
          row = names[i]
          namedic[row["language"]] = row["value"]
        except:
          row = False
        i += 1
        
      if lng in namedic:
        tname = namedic[lng]
      elif selng in namedic:
        tname = namedic[selng]
      elif "en" in namedic:
        tname = namedic["en"]
      else:
        plpy.notice("could not find structure name for id:"+str(id))
        return False


  # if region, we load the groupid and parents
  if (target in ["region", "street"]):
    group_sql = "WITH RECURSIVE parents(id, regiongroupid, parent_id, name) AS "
    if (target == "region"):
      group_sql += "(SELECT id, regiongroupid, parent_id, COALESCE(name_"+lng+", name_"+selng+", name_en) AS name FROM region WHERE id = $1 "
    else:
      group_sql += "(SELECT id, 127 AS regiongroupid, parent_id, COALESCE(name_"+lng+", name_"+selng+", name) AS name FROM street WHERE id = $1 "
    group_sql += "UNION ALL SELECT b.id, b.regiongroupid, b.parent_id, COALESCE(name_"+lng+", name_en) AS name FROM region b, parents a "
    group_sql += "WHERE a.parent_id = b.id AND NOT b.deleted ) SELECT id, regiongroupid, name FROM parents ORDER BY regiongroupid DESC"
    group_query = plpy.prepare(group_sql, ["int"])
    groups = plpy.execute(group_query, [id])

    # looping through parent regions and picking city, state, country data
    cityname = None
    statename = None
    countryname = None
    for region in groups:
      if region["regiongroupid"] == 6:
        cityid = region["id"]
        cityname = region["name"]
      elif region["regiongroupid"] == 4:
        stateid = region["id"]
        statename = region["name"]
      elif region["regiongroupid"] == 3:
        countryid = region["id"]
        countryname = region["name"]

    groupid = groups[0]["regiongroupid"]
    if groupid > 6 and groupid != 13 and countryname:
      if cityname:
        tname += ", "+cityname
      if statename:
        tname += ", "+statename
      tname += ", "+countryname
    elif groupid > 4 and groupid != 13 and countryname:
      if statename:
        tname += ", "+statename
      tname += ", "+countryname
    elif groupid > 3 and countryname:
      tname += ", "+countryname

  if (target == "object"):
    name = "name "
  elif (target in ["region", "street", "channel"]):
    name = "name_"+lng+" "

  if (target == "object"):
    id_str = "object.objectid "
  elif (target == "region"):
    id_str = "region.id "
  elif (target == "street"):
    id_str = "street.id "
  elif (target == "channel"):
    id_str = "channel.id "

  res_query = "SELECT regexp_replace(translate(regexp_replace(lower($1), '&[#0-9]*;',''),' &+/.\"<','_--'), '_*-_*','-') AS newname, "
  if (target not in ["region", "street", "channel"]):
    res_query += "regexp_replace(translate(regexp_replace(lower(', ' || city.name_"+lng
    res_query += " || ', ' || country.name_"+lng+"), '&[#0-9]*;',''),' &+/.\"<','_--'), '_*-_*','-') AS newreg, "
  if (target == "street"):
    res_query += "'?t=region&id=' || cast("+id_str+"AS TEXT) AS dest "
  else:
    res_query += "'?t="+target+"&id=' || cast("+id_str+"AS TEXT) AS dest "
  res_query += "FROM "+target+" "
  if (target not in ["region", "street", "channel"]):
    res_query += "LEFT JOIN object_region AS citycon ON "+id_str+" = citycon.objectid "
    res_query += "INNER JOIN region AS city ON (citycon.regionid = city.id AND city.regiongroupid = 6) "
    res_query += "LEFT JOIN object_region AS countrycon ON "+id_str+" = countrycon.objectid "
    res_query += "INNER JOIN region AS country ON (country.id = countrycon.regionid AND country.regiongroupid = 3) "
  res_query += "WHERE "+id_str+" = $2 "
  if (target not in ["channel"]):
    res_query += "AND NOT "+target+".deleted "
  #plpy.notice(res_query)

  resource_sel = plpy.prepare(res_query, ["text", "int"])
  res = plpy.execute(resource_sel, [tname, id])
  if not res:
    # structure is deleted most likely
    plpy.notice("no results: "+res_query+" \n tname:"+str(tname)+" - id:"+str(id))
    return False
  newname = res[0]["newname"]
  if not newname or newname == "none":
    # no name found, skipping
    plpy.notice("newname empty with tname "+str(tname)+" - id:"+str(id)+" - query:"+res_query)
    return False
  if (target not in ["region", "street", "channel"]):
    regname = res[0]["newreg"]
    if not regname:
      plpy.notice("no region name found, exiting")
      return False
  else:
    regname = ""
  dest = res[0]["dest"]
  #called = (newname+regname).replace("\x80","")
  called = newname+regname

  found_unique_uri = False
  newchar = '0'

  # checking existing uris
  while not found_unique_uri:
    called_check = plpy.prepare("SELECT linkedprimarykey FROM resource WHERE called = $1 AND destinationtype = 'embed' AND language = $3 ORDER BY called ASC", ["text", "int", "text"])
    try:
      res = plpy.execute(called_check, [called, id, lng])
    except:
      plpy.notice("failed with "+str(id)+" - "+lng)
      plpy.notice("name was "+called)
      return False

    if res:
      results = reduce(lambda x,y: x+y, map(lambda x: x.values(), res))
      if id in results:
        return True
      newchar = chr(ord(newchar)+1)
      called = newname+"-"+newchar+regname
    else:
      found_unique_uri = True

  old_update = plpy.prepare("UPDATE resource SET destinationtype = 'redirect' WHERE linkedtable = $1 AND linkedprimarykey = $2 AND language = $3", ["text","int","text"])
  plpy.execute(old_update, [target, id, lng])

  ins_sql = "INSERT INTO resource (called, destination, destinationtype, linkedtable, linkedprimarykey, language) "
  ins_sql += "VALUES ($1, $2, 'embed', $3, $4, $5)"
  ins_query = plpy.prepare(ins_sql, ["text", "text", "text", "int", "text"])
  plpy.execute(ins_query, [called, dest, target, id, lng])

  return True;
$$ LANGUAGE plpython3u;

DROP TRIGGER resource_updates_region ON region;
CREATE TRIGGER resource_updates_region AFTER UPDATE OF name_en ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'en');
CREATE TRIGGER resource_updates_region_de AFTER UPDATE OF name_de ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'de');
CREATE TRIGGER resource_updates_region_fr AFTER UPDATE OF name_fr ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'fr');
CREATE TRIGGER resource_updates_region_es AFTER UPDATE OF name_es ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'es');
CREATE TRIGGER resource_updates_region_ru AFTER UPDATE OF name_ru ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'ru');
CREATE TRIGGER resource_updates_region_he AFTER UPDATE OF name_he ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'he');
CREATE TRIGGER resource_updates_region_tr AFTER UPDATE OF name_tr ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'tr');
CREATE TRIGGER resource_updates_region_ko AFTER UPDATE OF name_ko ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'ko');
CREATE TRIGGER resource_updates_region_it AFTER UPDATE OF name_it ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'it');
CREATE TRIGGER resource_updates_region_hu AFTER UPDATE OF name_hu ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'hu');
CREATE TRIGGER resource_updates_region_fi AFTER UPDATE OF name_fi ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'fi');
CREATE TRIGGER resource_updates_region_ca AFTER UPDATE OF name_ca ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'ca');
CREATE TRIGGER resource_updates_region_ar AFTER UPDATE OF name_ar ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'ar');
CREATE TRIGGER resource_updates_region_ja AFTER UPDATE OF name_ja ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'ja');
CREATE TRIGGER resource_updates_region_pt AFTER UPDATE OF name_pt ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'pt');
CREATE TRIGGER resource_updates_region_ct AFTER UPDATE OF name_ct ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'ct');
CREATE TRIGGER resource_updates_region_zh AFTER UPDATE OF name_zh ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'zh');
CREATE TRIGGER resource_updates_region_th AFTER UPDATE OF name_th ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'th');
CREATE TRIGGER resource_updates_region_id AFTER UPDATE OF name_id ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'id');
CREATE TRIGGER resource_updates_region_pl AFTER UPDATE OF name_pl ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'pl');
CREATE TRIGGER resource_updates_region_sv AFTER UPDATE OF name_sv ON region FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('region', 'sv');


DROP TRIGGER resource_updates_object ON structure;
--CREATE TRIGGER resource_updates_object AFTER UPDATE OF name, name_en ON structure FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('object','en');
CREATE TRIGGER resource_updates_object AFTER INSERT ON object_region FOR EACH ROW EXECUTE PROCEDURE region_resource_update_trigger();

CREATE TRIGGER resource_updates_channel AFTER UPDATE OF name_en ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'en');
CREATE TRIGGER resource_updates_channel_de AFTER UPDATE OF name_de ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'de');
CREATE TRIGGER resource_updates_channel_fr AFTER UPDATE OF name_fr ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'fr');
CREATE TRIGGER resource_updates_channel_es AFTER UPDATE OF name_es ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'es');
CREATE TRIGGER resource_updates_channel_it AFTER UPDATE OF name_it ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'it');
CREATE TRIGGER resource_updates_channel_sv AFTER UPDATE OF name_sv ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'sv');
CREATE TRIGGER resource_updates_channel_pl AFTER UPDATE OF name_pl ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'pl');
CREATE TRIGGER resource_updates_channel_fi AFTER UPDATE OF name_fi ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'fi');
CREATE TRIGGER resource_updates_channel_ru AFTER UPDATE OF name_ru ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'ru');
CREATE TRIGGER resource_updates_channel_pt AFTER UPDATE OF name_pt ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'pt');
CREATE TRIGGER resource_updates_channel_nl AFTER UPDATE OF name_nl ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'nl');
CREATE TRIGGER resource_updates_channel_da AFTER UPDATE OF name_da ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'da');
CREATE TRIGGER resource_updates_channel_ar AFTER UPDATE OF name_ar ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'ar');
CREATE TRIGGER resource_updates_channel_ja AFTER UPDATE OF name_ja ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'ja');
CREATE TRIGGER resource_updates_channel_zh AFTER UPDATE OF name_zh ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'zh');
CREATE TRIGGER resource_updates_channel_ct AFTER UPDATE OF name_ct ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'ct');
CREATE TRIGGER resource_updates_channel_ko AFTER UPDATE OF name_ko ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'ko');
CREATE TRIGGER resource_updates_channel_hi AFTER UPDATE OF name_hi ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'hi');
CREATE TRIGGER resource_updates_channel_id AFTER UPDATE OF name_id ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'id');
CREATE TRIGGER resource_updates_channel_tr AFTER UPDATE OF name_tr ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'tr');
CREATE TRIGGER resource_updates_channel_th AFTER UPDATE OF name_th ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'th');
CREATE TRIGGER resource_updates_channel_bg AFTER UPDATE OF name_bg ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'bg');
CREATE TRIGGER resource_updates_channel_cs AFTER UPDATE OF name_cs ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'cs');
CREATE TRIGGER resource_updates_channel_hu AFTER UPDATE OF name_hu ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'hu');
CREATE TRIGGER resource_updates_channel_ro AFTER UPDATE OF name_ro ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'ro');
CREATE TRIGGER resource_updates_channel_ca AFTER UPDATE OF name_ca ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'ca');
CREATE TRIGGER resource_updates_channel_et AFTER UPDATE OF name_et ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'et');
CREATE TRIGGER resource_updates_channel_lv AFTER UPDATE OF name_lv ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'lv');
CREATE TRIGGER resource_updates_channel_lt AFTER UPDATE OF name_lt ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'lt');
CREATE TRIGGER resource_updates_channel_vi AFTER UPDATE OF name_vi ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'vi');
CREATE TRIGGER resource_updates_channel_hr AFTER UPDATE OF name_hr ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'hr');
CREATE TRIGGER resource_updates_channel_sr AFTER UPDATE OF name_sr ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'sr');
CREATE TRIGGER resource_updates_channel_el AFTER UPDATE OF name_el ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'el');
CREATE TRIGGER resource_updates_channel_uk AFTER UPDATE OF name_uk ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'uk');
CREATE TRIGGER resource_updates_channel_he AFTER UPDATE OF name_he ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'he');
CREATE TRIGGER resource_updates_channel_fa AFTER UPDATE OF name_fa ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'fa');
CREATE TRIGGER resource_updates_channel_tl AFTER UPDATE OF name_tl ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'tl');
CREATE TRIGGER resource_updates_channel_ta AFTER UPDATE OF name_ta ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'ta');
CREATE TRIGGER resource_updates_channel_sl AFTER UPDATE OF name_sl ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'sl');
CREATE TRIGGER resource_updates_channel_sw AFTER UPDATE OF name_sw ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'sw');
CREATE TRIGGER resource_updates_channel_ur AFTER UPDATE OF name_ur ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'ur');
CREATE TRIGGER resource_updates_channel_sq AFTER UPDATE OF name_sq ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'sq');
CREATE TRIGGER resource_updates_channel_ka AFTER UPDATE OF name_ka ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'ka');
CREATE TRIGGER resource_updates_channel_hy AFTER UPDATE OF name_hy ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'hy');
CREATE TRIGGER resource_updates_channel_bn AFTER UPDATE OF name_bn ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'bn');
CREATE TRIGGER resource_updates_channel_gu AFTER UPDATE OF name_gu ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'gu');
CREATE TRIGGER resource_updates_channel_mr AFTER UPDATE OF name_mr ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'mr');
CREATE TRIGGER resource_updates_channel_te AFTER UPDATE OF name_te ON channel FOR EACH ROW EXECUTE PROCEDURE resource_copy_trigger('channel', 'te');

