<?php
/**
 * WBEvent_Handler_TableNLSAddWord
 *
 *
 * $Id$
 *
 * @author gERD Schaufelberger <gerd@php-tools.net>
 * @license PHP License
 * @package WB
 * @subpackage base
 */

WBClass::load('WBEvent_Handler');

/**
 * WBEvent_Handler_TableNLSAddWord
 *
 * Adds words to NLS module on save
 *
 * @version 1.0.0
 * @package Wombat
 * @subpackage base
 */
class WBEvent_Handler_TableNLSUpdateOrAddWord extends WBEvent_Handler
{
    /**
     * table access
     * @var WBDatasource_Table
     */
    static private $table;

    /**
     * text importer
     * @var patI18n_Importer
     */
    static private $imp;

    /**
     * handler config
     *
     * - table name of table to update
     *
     * @var array
     */
    protected $config   =   array(
                                'table'         =>  ''
                                );


    /**
     * Import translatable strings
     *
     * Select all translatable columns and call import for each of them.
     *
     * @param WBEvent $e
     * @return bool true to continue, false to stop processing
     * @throws Exception in case of error
     */
    public function process(WBEvent $e)
    {
        $table  =   $this->config['table'];
        if (empty($this->config['table'])) {
            $table  =   $e->get('table');
        }

        // primary key or "__new" is required
        $id =   $e->get('id', '');
        if (empty($id)) {
            return true;
        }

        $save   =   $e->get('save');
        if (empty($save)) {
            return true;
        }

        if (!self::$table) {
            self::$table    =   WBClass::create('WBDatasource_Table');
            self::$table->switchTranslation(false);
        }

        $info   =   self::$table->getTableInfo($table);
        if (!isset($info['translatable']) || empty($info['translatable'])) {
            return true;
        }

        // load old message strings
        $old    =   false;
        if ('__new' != $id) {
            $old    =   self::$table->get($table, $id);
            if (!is_array($old) || 1 != count($old)) {
                return true;
            }
            $old    =   $old[0];
        }

        foreach ($info['translatable'] as $t) {
            if (!isset($save[$t]) || empty($save[$t])) {
                continue;
            }

            $this->createImporter();
            $mid    =   0;
            if (is_array($old) && !empty($old[$t])) {
                $mid    =   self::$imp->updateMsg($old[$t], $save[$t]);
            } else {
                $mid    =   self::$imp->addMsg($save[$t]);
            }

            if ($id != '__new' && !empty($mid)) {
                self::$imp->setXid($mid, $table, $id);
            }
        }

        return true;
    }

    /**
     * Initialize importer
     *
     * Load locale config and create patI18n_Importer_Wombat
     */
    private function createImporter()
    {
        if (self::$imp) {
            return;
        }

        self::$imp  =  WBClass::create('patI18n_Importer');
    }
}