<?php
/**
 * Smarty plugin
 *
 * @category Smarty
 * @license /_licenses/untermStrich/agb_normal.pdf
 * @author Christian Wengg
 * @copyright 2009 untermStrich
 * @package Smarty
 * @subpackage plugins
 * @version $Id$
*/

/**
 * Smarty addi button function plugin
 *
 * Purpose:  Create new additional buttons in edit row of datatable
 *
 * @param   array   $params Parameters:
 *                  <ul>
 *                      <li>value   object     The stdClass object</li>
 *                      <li>type    string     A button type</li>
 *                  </ul>
 * @return  string  The field source
 */
function smarty_function_additional_button(array $params)
{
    $ret = '';
    if (isset($params['value']) && isset($params['type'])) {
        $CI = CI_Controller::get_instance();
        $item = $params['value'];
        $type = $params['type'];
        $icon = '<span title="%s" class="dialog_make_btn16 additional_button %s mobile_big smallbtns ui-corner-all" onclick="%s">%s</span>';
        
        switch ($type) {
            case 'work_input_set_confirmed':
                $my_id = $params['portlet_id'];
                $ret = sprintf(
                $icon,
                ($item->already_confirmed) ? $CI->lang->line('work_input_confirm_entry_set_notdone') : $CI->lang->line('work_input_confirm_entry_set_done'),//TITLE
                ($item->already_confirmed) ? 'done is_done' : 'ok not_done is_not_done',//CSS ICON
                'portlet_times_project_staff.confirm_entry(\''.$my_id.'\', \''.$item->id.'\', '.intval(!$item->already_confirmed).');',//ACTION
                ($item->already_confirmed) ? 'x' : ''//VALUE
                );
                break;
            case 'opentasks_set_done':
                $ret = sprintf(
                $icon,
                ($item->is_done) ? $CI->lang->line('opentasks_set_notdone') : $CI->lang->line('opentasks_confirm_done'),//TITLE
                ($item->is_done) ? 'done is_done' : 'not_done is_not_done',//CSS ICON
                'opentasks.set_done(\''.$item->id.'\', '.intval(!$item->is_done).');',//ACTION
                ($item->is_done) ? 'x' : ''//VALUE
                );
                break;
            case 'correspondence_edit_document':
                if (!isset($item->object_is_editable) || $item->object_is_editable) {
                    $ret = sprintf(
                        $icon,
                        us_htmlentities($item->file_path, ENT_COMPAT).' - ID:'.trim($item->id),
                        'open_win',
                        'usComm.edit_document(\''.$item->id.'\', \'edit\')',
                        (isset($params['dot3p']) && $params['dot3p']) ? $CI->lang->line('correspondence_edit_document') : ''
                    );
                }
                break;
            case 'email_quick_allocation':
                if (empty($item->project_id) || empty($item->project) || empty($item->address_id) || empty($item->address_name)) return '';
                if ((!isset($item->rowinfo) || !isset($item->rowinfo->class)) || ($item->rowinfo->class !== 'inforedrow' && $item->rowinfo->class !== 'infobluerow')) return '';
                
                $ret = sprintf(
                $icon,
                $CI->lang->line('inoutbox_quick_allocation')."\r\nP: ".mb_substr($item->project,0,40).((mb_strlen($item->project) > 40)?'...':'')."\r\nA: ".mb_substr($item->address_name,0,40).((mb_strlen($item->address_name) > 40)?'...':''),//TITLE
                'edit',//CSS ICON
                'email_allocation.quick_allocation(\''.$item->id.'\');',//ACTION
                ''//VALUE
                );
                break;                
            default:
                break;
        }
    } 
    return $ret;
}