/**
 * Set page title to $content value
 * 
 * Parameters:
 * 
 * - not_lang - Use raw value...
 *
 * @param array $params
 * @param string $content
 * @param Smarty $smarty
 * @param boolean $repeat
 * @return string
 */
function smarty_block_title($params, $content, &$smarty, &$repeat)
{
    if (!isset($params['not_lang'])) {
        //$content = lang($content, $params, false); // Params will be cleaned by page construction
        $content = lang($content, $params, false, null, true);
        // Params will be cleaned by page construction
    }
    // if
    $construction =& PageConstruction::instance();
    $construction->setPageTitle($content);
    return '';
}
/**
 * Add stylesheet to selected page
 * 
 * Parameters:
 * 
 * - name - name of the CSS file
 * - module - name of the module, if null global stylesheet is used
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_add_stylesheet($params, &$smarty)
{
    $name = array_var($params, 'name');
    if (empty($name)) {
        return new InvalidParamError('name', $name, "'name' parameter is required for 'add_stylesheet' helper", true);
    }
    // if
    $module = array_var($params, 'module');
    if (!isset($params['type'])) {
        $params['type'] = 'text/css';
    }
    // if
    unset($params['name']);
    if (isset($params['module'])) {
        unset($params['module']);
    }
    // if
    PageConstruction::addLink(get_stylesheet_url($name, $module), 'stylesheet', $params);
    return '';
}
/**
 * Set page title
 * 
 * Parameters:
 * 
 * - to - new title
 *
 * @param array $params
 * @param Smarty $smarty
 * @return void
 */
function smarty_function_page_title_set($params, &$smarty)
{
    $construction =& PageConstruction::instance();
    $construction->setPageTitle(array_var($params, 'to'));
    return '';
}
/**
 * Display page title
 *
 * Parameters:
 * 
 * - default - default page title, if no page title is present. If there is no 
 *   default value 'Index' is used
 * 
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_page_title($params, &$smarty)
{
    $default = array_var($params, 'default', 'Index');
    $page_title = PageConstruction::getPageTitle();
    return empty($page_title) ? clean($default) : clean($page_title);
}
/**
 * Set page properties with following object
 *
 * Parameters:
 *
 * - object - Application object instance
 *
 * @param array $params
 * @param Smarty $smarty
 * @return null
 */
function smarty_function_page_object($params, &$smarty)
{
    static $private_roles = false;
    $object = array_var($params, 'object');
    if (!instance_of($object, 'ApplicationObject')) {
        return new InvalidParamError('object', $object, '$object is expected to be an instance of ApplicationObject class', true);
    }
    // if
    require_once SMARTY_DIR . '/plugins/modifier.datetime.php';
    $wireframe =& Wireframe::instance();
    $logged_user =& get_logged_user();
    $construction =& PageConstruction::instance();
    if ($construction->page_title == '') {
        $construction->setPageTitle($object->getName());
    }
    // if
    if (instance_of($object, 'ProjectObject') && $wireframe->details == '') {
        $in = $object->getParent();
        $created_on = $object->getCreatedOn();
        $created_by = $object->getCreatedBy();
        if (instance_of($created_by, 'User') && instance_of($in, 'ApplicationObject') && instance_of($created_on, 'DateValue')) {
            //BOF:mod 20120913
            /*
            //EOF:mod 20120913
            $wireframe->details = lang('By <a href=":by_url">:by_name</a> in <a href=":in_url">:in_name</a> on <span>:on</span>', array(
            		    //BOF:mod 20120913
            */
            $wireframe->details = lang('Created on <span>:on</span>', array('by_url' => $created_by->getViewUrl(), 'by_name' => $created_by->getDisplayName(), 'in_url' => $in->getViewUrl(), 'in_name' => $in->getName(), 'on' => smarty_modifier_datetime($created_on)));
        } elseif (instance_of($created_by, 'User') && instance_of($created_on, 'DateValue')) {
            //BOF:mod 20120913
            /*
            //EOF:mod 20120913
            $wireframe->details = lang('By <a href=":by_url">:by_name</a> on <span>:on</span>', array(
            		    //BOF:mod 20120913
            */
            $wireframe->details = lang('Created on <span>:on</span>', array('by_url' => $created_by->getViewUrl(), 'by_name' => $created_by->getDisplayName(), 'on' => smarty_modifier_datetime($created_on)));
        } elseif (instance_of($created_by, 'User')) {
            //BOF:mod 20120913
            /*
            //EOF:mod 20120913
            $wireframe->details = lang('By <a href=":by_url">:by_name</a>', array(
            		    //BOF:mod 20120913
            */
            $wireframe->details = lang('Created on <span>:on</span>', array('by_url' => $created_by->getViewUrl(), 'by_name' => $created_by->getDisplayName(), 'on' => smarty_modifier_datetime($created_on)));
        } elseif (instance_of($created_by, 'AnonymousUser') && instance_of($created_on, 'DateValue')) {
            //BOF:mod 20120913
            /*
            //EOF:mod 20120913
            $wireframe->details = lang('By <a href=":by_url">:by_name</a> on <span>:on</span>', array(
            		    //BOF:mod 20120913
            */
            $wireframe->details = lang('Created on <span>:on</span>', array('by_url' => 'mailto:' . $created_by->getEmail(), 'by_name' => $created_by->getName(), 'on' => smarty_modifier_datetime($created_on)));
        } elseif (instance_of($created_by, 'AnonymousUser')) {
            //BOF:mod 20120913
            /*
            //EOF:mod 20120913
            $wireframe->details = lang('By <a href=":by_url">:by_name</a>', array(
            		    //BOF:mod 20120913
            */
            $wireframe->details = lang('Created on <span>:on</span>', array('by_url' => 'mailto:' . $created_by->getEmail(), 'by_name' => $created_by->getName(), 'on' => smarty_modifier_datetime($created_on)));
        }
        // if
    }
    // if
    $smarty->assign('page_object', $object);
    // Need to do a case sensitive + case insensitive search to have PHP4 covered
    $class_methods = get_class_methods($object);
    if (in_array('getOptions', $class_methods) || in_array('getoptions', $class_methods)) {
        $options = $object->getOptions($logged_user);
        if (instance_of($options, 'NamedList') && $options->count()) {
            $wireframe->addPageAction(lang('Options'), '#', $options->data, array('id' => 'project_object_options'), 1000);
        }
        // if
        if (instance_of($object, 'ProjectObject')) {
            if ($object->getState() > STATE_DELETED) {
                if ($object->getVisibility() <= VISIBILITY_PRIVATE) {
                    //Ticket ID #362 - modify Private button (SA) 14March2012 BOF
                    $users_table = TABLE_PREFIX . 'users';
                    $assignments_table = TABLE_PREFIX . 'assignments';
                    $subscription_table = TABLE_PREFIX . 'subscriptions';
                    //					$rows = db_execute_all("SELECT $assignments_table.is_owner AS is_assignment_owner, $users_table.id AS user_id, $users_table.company_id, $users_table.first_name, $users_table.last_name, $users_table.email FROM $users_table, $assignments_table WHERE $users_table.id = $assignments_table.user_id AND $assignments_table.object_id = ? ORDER BY $assignments_table.is_owner DESC", $object->getId());
                    $rows = db_execute_all("SELECT {$assignments_table}.is_owner AS is_assignment_owner, {$users_table}.id AS user_id, {$users_table}.company_id, {$users_table}.first_name, {$users_table}.last_name, {$users_table}.email FROM {$users_table}, {$assignments_table} WHERE {$users_table}.id = {$assignments_table}.user_id AND {$assignments_table}.object_id = " . $object->getId() . " UNION SELECT '0' AS is_assignment_owner, {$users_table}.id AS user_id, {$users_table}.company_id, {$users_table}.first_name, {$users_table}.last_name, {$users_table}.email FROM {$users_table}, {$subscription_table} WHERE {$users_table}.id = {$subscription_table}.user_id AND {$subscription_table}.parent_id = " . $object->getId());
                    if (is_foreachable($rows)) {
                        $owner = null;
                        $other_assignees = array();
                        $users_dropdown_for_tickets = '';
                        foreach ($rows as $row) {
                            if (empty($row['first_name']) && empty($row['last_name'])) {
                                $user_link = clean($row['email']);
                            } else {
                                $user_link = clean($row['first_name'] . ' ' . $row['last_name']);
                            }
                            // if
                            if ($row['is_assignment_owner']) {
                                $owner = $user_link;
                            } else {
                                $other_assignees[] = $user_link;
                            }
                            if ($owner) {
                                if (instance_of($object, 'Ticket')) {
                                    if (count($other_assignees) > 0) {
                                        $users = $owner;
                                        if (!empty($users)) {
                                            $users .= ', ';
                                        }
                                        $users .= implode(', ', $other_assignees);
                                    } else {
                                        $users = $owner;
                                    }
                                    // if
                                } else {
                                    if (count($other_assignees) > 0) {
                                        $users = $owner . ' ' . lang('is responsible', null, true, $language) . '. ' . lang('Other assignees', null, true, $language) . ': ' . implode(', ', $other_assignees) . '.';
                                    } else {
                                        $users = $owner . ' ' . lang('is responsible', null, true, $language) . '.';
                                    }
                                    // if
                                }
                            } elseif (count($other_assignees) > 0) {
                                $users = implode(', ', $other_assignees);
                            }
                        }
                    }
                    $wireframe->addPageMessage(lang('<b>Private</b> - This Ticket has been marked as "Private" and is Visible by these Users:  :users', array('users' => $users)), PAGE_MESSAGE_PRIVATE);
                    //Ticket ID #362 - modify Private button (SA) 14March2012 EOF
                }
                // if
            } else {
                $wireframe->addPageMessage(lang('<b>Trashed</b> - this :type is located in trash.', array('type' => $object->getVerboseType(true))), PAGE_MESSAGE_TRASHED);
            }
            // if
        }
        // if
    }
    // if
    return '';
}
/**
 * Render HTML editor
 *
 * @param array $params
 * @param string $content
 * @param Smarty $smarty
 * @param boolean $repeat
 * @return string
 */
function smarty_block_editor_field($params, $content, &$smarty, &$repeat)
{
    static $ids = array(), $files_included = false;
    // if browser is ipad, we need to turn visual editor off
    if (USER_AGENT == USER_AGENT_IPAD) {
        $visual = false;
    } else {
        if (isset($params['visual'])) {
            $visual = array_var($params, 'visual', true, true);
        } else {
            $logged_user = $smarty->get_template_vars('logged_user');
            if (instance_of($logged_user, 'User')) {
                $visual = UserConfigOptions::getValue('visual_editor', $logged_user);
                // if user is loaded $visual is set dependable of user config option
            } else {
                $visual = true;
            }
            // if
        }
    }
    // if
    $id = array_var($params, 'id');
    if (empty($id)) {
        $counter = 1;
        do {
            $id = "visual_editor_{$counter}";
            $counter++;
        } while (in_array($id, $ids));
    }
    // if
    $ids[] = $id;
    $params['id'] = $id;
    //$hide_editor = array_var($params, 'hide_editor');
    //if (empty($hide_editor)){
    $params['mce_editable'] = true;
    //}
    if (!isset($params['auto_expand'])) {
        $params['auto_expand'] = 'yes';
    }
    // if
    if ($visual && !$files_included) {
        $page =& PageConstruction::instance();
        $disable_image_upload = array_var($params, 'disable_image_upload', false);
        $page->addScript(get_asset_url('javascript/tinymce/tiny_mce.js'), false);
        $page->addScript(get_asset_url('javascript/tinymce/tiny_mce_init.js'), false);
        echo "<script type='text/javascript'>";
        if ($disable_image_upload) {
            echo "App.widgets.EditorImagePicker.disable_image_upload = true;";
        }
        // if
        echo "</script>";
        $files_included = true;
    }
    // if
    if (isset($params['class'])) {
        $classes = explode(' ', $params['class']);
        $classes[] = 'editor';
        if (in_array('tiny_value_present', $classes) && !$visual) {
            $classes[] = 'required';
        }
        // if
        $params['class'] = implode(' ', $classes);
    } else {
        $params['class'] = 'editor';
    }
    // if
    $return_string = '';
    if (!($variable_name = array_var($params, 'variable_name'))) {
        $name_parameter = array_var($params, 'name');
        $variable_name = substr($name_parameter, 0, strrpos($name_parameter, '['));
    }
    // if
    $variable_name .= '[inline_attachments][]';
    $inline_attachments = array_var($params, 'inline_attachments');
    if (is_foreachable($inline_attachments)) {
        foreach ($inline_attachments as $inline_attachment) {
            $return_string .= '<input type=hidden name="' . $variable_name . '" value="' . $inline_attachment . '" />' . "\n";
        }
        // foreach
    }
    // if
    unset($params['inline_attachments']);
    unset($params['variable_name']);
    $params['inline_attachments_name'] = $variable_name;
    $return_string .= open_html_tag('textarea', $params) . clean($content) . '</textarea>';
    return $return_string;
}
/**
 * Render head tags collected form PageConstruction
 * 
 * No parameters expected
 *
 * @param array $params
 * @param Smarty $smarty
 * @return string
 */
function smarty_function_page_head_tags($params, &$smarty)
{
    $instance =& PageConstruction::instance();
    $tags = $instance->getAllHeadTags();
    return is_foreachable($tags) ? implode("\n", $tags) : '';
}
 /**
  * Return all scripts
  *
  * @param void
  * @return array
  */
 function getScripts()
 {
     if (isset($this) && instance_of($this, 'PageConstruction')) {
         return $this->scripts;
     } else {
         $instance =& PageConstruction::instance();
         return $instance->getScripts();
     }
     // if
 }