<?php

function embed_with_layout($content, $layout, $params = array()) {
    $params['content'] = $content;
    include_component($layout[0], $layout[1], $params);
}

function catalyz_start_custom_fields() {
    echo '<table border="0" cellpadding="2" cellspacing="0">';
}

function catalyz_end_custom_fields() {
    echo '</table>';
}

function catalyz_add_custom_fields($caption, $field, $icon = '', $error = '') {
    if ($icon && is_file(sfConfig::get('sf_web_dir') . '/images/page-icons/' . $icon)) {
        $iconCaption = __('Layout:', null, 'catalyz');
        $iconContent = image_tag('/images/page-icons/' . $icon);
    } else {
        $iconCaption = '';
        $iconContent = '&nbsp;';
    }
    if (empty($caption)) {
        printf('<tr valign="top"><td>%s</td><td width="100">%s<br />%s</td></tr>' . "\n", $field, $iconCaption, $iconContent);
    } else {
        printf('<tr valign="top"><td>%s:</td><td width="100">%s</td></tr>' . "\n", $caption, $iconCaption);
        printf('<tr valign="top"><td>%s</td><td width="100">%s</td></tr>' . "\n", $field, $iconContent);
    }
    if (!empty($error)) {
        printf('<tr><td colspan="2"><div class="error">%s</div></td></tr>' . "\n", $error);
    }
}

function catalyz_start_group($caption, $id = false, $checkbox_name = null, $icon = false) {
    if (false != $id) {
        $fieldset_id = sprintf(' id="%s"', $id);
    } else {
        $fieldset_id = $id;
    }
    if ($checkbox_name) {
        $caption = '<input type="checkbox" onclick="$(\'#' . $id . 'content\').toggle(); $(\'#' . $id . 'alert\').toggle()" name="' . $checkbox_name . '" checked="true" /> ' . $caption;
    }
    if ($icon) {
        echo '<tr valign="top"><td>';
    } else {
        echo '<tr valign="top"><td colspan="2">';
    }
    echo '<fieldset' . $fieldset_id . ' width="100%" class="ui-corner-all"><legend>' . $caption . '</legend>' . "\n";
    if ($checkbox_name) {
        echo '<p id="' . $id . 'alert" style="display: none;">' . __('This item will be deleted when you save this page.', null, 'catalyz') . '</p>' . "\n";
    }
    echo '<table border="0" cellpadding="2" cellspacing="0" class="none" id="' . $id . 'content">' . "\n";
}

function catalyz_add_content($content) {
    printf('<tr valign="top"><td colspan="2">%s</td></tr>' . "\n", $content);
}

function catalyz_end_group($icon = '') {
    echo '</table></fieldset></td>';
    if ($icon && is_file(sfConfig::get('sf_web_dir') . '/images/page-icons/' . $icon)) {
        echo '<td>' . __('Layout:', null, 'catalyz') . '<br />' . image_tag('/images/page-icons/' . $icon) . '</td>';
    }
    echo '</tr>';
}

function catalyz_add_picture_field($name, $page, $icon = '', $error = '') {
    $method = 'get' . $name;
    $value = $page->$method();
    catalyz_add_custom_fields($name,
        '<img src="' . ($value?$value:'/images/blank.jpg') . '" width="100" id="thumbnail' . $name . '" style="float: right" />'
         . object_input_tag($page, $method, array('size' => 80, 'id' => $name))
         . '<a href="javascript:mcImageManager.open(\'catalyzEdit\', \'' . $name . '\', \'\', function (url, data){	$(\'#thumbnail' . $name . '\').attr(\'src\', url);$(\'#' . $name . '\').attr(\'value\', url);}, {remove_script_host: true} );">[' . __('Browse', null, 'catalyz') . ']</a>' , $icon, $error);
}

function catalyz_add_picture_field2($id, $caption, $fieldName, $value, $icon = '', $error = '') {
    catalyz_add_custom_fields($caption,
        input_tag($fieldName, $value, array('size' => 70, 'id' => $id)) . "\n"
         . '<a href="javascript:mcImageManager.open(\'catalyzEdit\', \'' . $id . '\', \'\', function (url, data){	$(\'#thumbnail' . $id . '\').attr(\'src\', url);$(\'#' . $id . '\').attr(\'value\', url);}, {remove_script_host: true} );">[' . __('Browse', null, 'catalyz') . ']</a>'
         . '<br /><img src="' . ($value?$value:'/images/blank.jpg') . '" width="100" id="thumbnail' . $id . '" style="margin: 2px;" />' . "\n" , $icon, $error);
}

function catalyz_add_file_field2($id, $caption, $fieldName, $value, $icon = '', $error = '') {
	catalyz_add_custom_fields($caption,
	    input_tag($fieldName, $value, array('size' => 70, 'id' => $id)) . "\n"
	     . '<a href="javascript:mcFileManager.open(\'catalyzEdit\', \'' . $id . '\', \'\', function (url, data){	$(\'#file' . $id . '\').attr(\'src\', url);$(\'#' . $id . '\').attr(\'value\', url);}, {remove_script_host: true} );">[' . __('Browse', null, 'catalyz') . ']</a>' . "\n" , $icon, $error);
}

function catalyz_add_wysiwyg_field2($id, $caption, $fieldName, $value, $icon = '', $error = '', $options = array()) {
    catalyz_add_custom_fields($caption, textarea_tag($fieldName, $value, array_merge(array(
		'rich' => true, 'size' => '65x10',
		'style' => 'width: 545px;', 'tinymce_options' => sfConfig::get('app_tiny_mce_options'), 'id' => $id), $options)), $icon, $error);
}

function catalyz_add_input_field($caption, $fieldName, $value, $icon = '', $error = '') {
    catalyz_add_custom_fields($caption, input_tag($fieldName, $value, array('size' => 80, 'maxlength' => 255)), $icon, $error);
}

function catalyz_add_password_field($caption, $fieldName, $value, $icon = '', $error = '') {
    catalyz_add_custom_fields($caption, input_password_tag($fieldName, $value, array('size' => 80, 'maxlength' => 255)), $icon, $error);
}

function catalyz_add_hidden_field($fieldName, $value) {
    echo input_hidden_tag($fieldName, $value);
}

function catalyz_add_date_field($caption, $fieldName, $value, $icon = '', $error = '') {
    catalyz_add_custom_fields($caption, input_date_tag($fieldName, $value, array('rich' => true, 'calendar_button_img' => '/images/catalyz/calendar.png')), $icon, $error);
}

function catalyz_add_wysiwyg_field($name, $page, $icon = '', $options = array()) {
    $method = 'get' . $name;
    catalyz_add_custom_fields($name, object_textarea_tag($page, $method, array_merge(array(
                    'rich' => true, 'size' => '70x15',
                    'style' => 'width: 545px;',
                    'tinymce_options' => /*'language:"' . sfConfig::get('app_catalyz_language') . '"' .*/ sfConfig::get('app_tiny_mce_options')), $options)), $icon);
}

function catalyz_add_checkbox_field($caption, $fieldName, $checked = false, $value = '1', $icon = '', $error = '') {
	// catalyz_add_custom_fields($caption, checkbox_tag($fieldName, $value, $checked), $icon, $error);
	printf('<tr><td colspan="2">%s <label for="%s">%s</label></td></tr>' . "\n", checkbox_tag($fieldName, $value, $checked), $fieldName, $caption);
	if (!empty($error)) {
		printf('<tr><td colspan="2"><div class="error">%s</div></td></tr>' . "\n", $error);
	}
}

function catalyz_textarea_tag($id, $caption, $fieldName, $value, $icon = '', $error = '', $options = array()) {
	catalyz_add_custom_fields($caption, textarea_tag($fieldName, $value, array_merge(array('size' => '65x10', 'id' => $id), $options)), $icon, $error);
}

function catalyz_select_tag($id, $caption, $fieldName, $values = array(), $selected, $icon = '', $error = '', $option_select = array(), $options = array()) {
	catalyz_add_custom_fields($caption, select_tag($fieldName, options_for_select($values, $selected, $option_select), array_merge(array('id' => $id), $options)), $icon, $error);
}

function catalyz_add_page_selector_field($caption, $fieldName, $classType = null, $value = 0, $icon = '', $error = '') {
	$pages = ContentTree::instance()->getAll();
	$items = array();
	foreach($pages as $page){
		if((null ==$classType) || $page->getNodeClass() == $classType){
			$items[$page->getId()] = $page->getBreadcrum();
		}
	}

	catalyz_add_custom_fields($caption, select_tag($fieldName, options_for_select($items, $value, array('include_custom' => __('-- No page selected --', null, 'catalyz')))), $icon, $error);
}

function catalyz_show_error($message, $format = '') {
    if ($format) {
        $message = sprintf($format, $message);
    }
    printf('<div class="error">%s</div>', $message);
}

function catalyz_nonify($value) {
    if (empty($value)) {
        return htmlentities('<none>');
    }
    return $value;
}

function catalyz_toolbar($page) {
    if (!defined('RENDER_MODE') || (RENDER_MODE != 'preview' && RENDER_MODE != 'catalyz')) {
        include_partial('catalyz/websitetoolbar', array('page' => $page));
        return true;
    }
    return false;
}

function catalyz_start_tabs()
{
	printf('<div id="catalyz-tabs"><ul>');
	$tabIndex = 1;
	foreach(func_get_args() as $tabName) {
		printf('<li><a href="#catalyz-tab%d">%s</a></li>', $tabIndex++, $tabName);
	}

	printf('</ul>');
	echo javascript_tag('$(function() {
		$("#catalyz-tabs").tabs();
		});');
	printf('<div id="catalyz-tab1" style="min-height: 300px; padding: 0px">');
	catalyz_start_custom_fields();
}

function catalyz_next_tab()
{
	catalyz_end_custom_fields();
	static $nextTabIndex = 2;
	printf('</div><div id="catalyz-tab%d" style="min-height: 300px; padding: 0px">', $nextTabIndex++);
	catalyz_start_custom_fields();
}

function catalyz_end_tabs()
{
	catalyz_end_custom_fields();
	printf('</div></div>');
}

function catalyz_link_to_image_tag($link_to_url = '@homepage', $image_tag_url = '', $link_to_options = array(), $image_tag_options = array()) {
    return link_to(image_tag($image_tag_url, $image_tag_options), $link_to_url, $link_to_options);
}

function catalyz_sort_links($label, $sf_request, $field_name, $url, $parameters = '') {
    $result = '';
    $result1 = '';
    $result2 = '';
    if ($field_name == $sf_request->getParameter('sort')) {
        if ('desc' == $sf_request->getParameter('mode')) {
            $result1 = catalyz_link_to_image_tag($url . '&sort=' . $field_name . '&mode=asc' . $parameters, 'catalyz/caret/asc.gif', array('title' => 'Tri ascendant'), array('alt' => 'Tri ascendant', 'onmouseover' => 'this.src = "' . image_path('catalyz/caret/asc-active.gif') . '";', 'onmouseout' => 'this.src = "' . image_path('catalyz/caret/asc.gif') . '";'));
            $result2 = image_tag('catalyz/caret/desc-active.gif', array('alt' => 'Tri descendant'));
        } else {
            $result1 = image_tag('catalyz/caret/asc-active.gif', array('alt' => 'Tri ascendant'));
            $result2 = catalyz_link_to_image_tag($url . '&sort=' . $field_name . '&mode=desc' . $parameters, 'catalyz/caret/desc.gif', array('title' => 'Tri descendant'), array('alt' => 'Tri descendant', 'onmouseover' => 'this.src = "' . image_path('catalyz/caret/desc-active.gif') . '";', 'onmouseout' => 'this.src = "' . image_path('catalyz/caret/desc.gif') . '";'));
        }
    } else {
        $result1 = catalyz_link_to_image_tag($url . '&sort=' . $field_name . '&mode=asc' . $parameters, 'catalyz/caret/asc.gif', array('title' => 'Tri ascendant'), array('alt' => 'Tri ascendant', 'onmouseover' => 'this.src = "' . image_path('catalyz/caret/asc-active.gif') . '";', 'onmouseout' => 'this.src = "' . image_path('catalyz/caret/asc.gif') . '";'));
        $result2 = catalyz_link_to_image_tag($url . '&sort=' . $field_name . '&mode=desc' . $parameters, 'catalyz/caret/desc.gif', array('title' => 'Tri descendant'), array('alt' => 'Tri descendant', 'onmouseover' => 'this.src = "' . image_path('catalyz/caret/desc-active.gif') . '";', 'onmouseout' => 'this.src = "' . image_path('catalyz/caret/desc.gif') . '";'));
    }
    return sprintf('<table cellpadding="0" cellspacing="0" border="0"><tr><th rowspan="2">%s</th><th>%s</th></tr><tr><th>%s</th></tr></table>', $label, $result1, $result2);
}

function catalyz_pager($pager, $link, $parameters = '') {
    $pages = array();
    for($i = 1; $i <= $pager->getLastPage(); $i++) {
        $pages[$i] = $i;
    }

    $result = link_to('&laquo;', $link . '&page=1' . $parameters, array('class' => 'button-pager'));
    $result .= link_to('&lsaquo;', $link . '&page=' . $pager->getPreviousPage() . $parameters, array('class' => 'button-pager'));
    $result .= '&nbsp;Page&nbsp;';
    $result .= select_tag('page', options_for_select($pages, $pager->getPage()), array('id' => 'page-select', 'onchange' => 'return this.form.submit();'));
    $result .= link_to('&rsaquo;', $link . '&page=' . $pager->getNextPage() . $parameters, array('class' => 'button-pager'));
    $result .= link_to('&raquo;', $link . '&page=' . $pager->getLastPage() . $parameters, array('class' => 'button-pager'));

    return $result;
}

function catalyz_display_pager($pager, $url, $link, $parameters = '') {
    $result = '';
    if (1 < $pager->getLastPage()) {
        $result = form_tag($link);
        $result .= '<div class="pager">';
        $result .= '<table width="100%" border="0" cellspacing="0" cellpadding="0">';
        $result .= '<tr valign="top">';
        $result .= '<td width="145" align="left" nowrap="nowrap">';
        $result .= catalyz_pager($pager, $link, $parameters);
        $result .= '</td>';
        $result .= '</tr>';
        $result .= '</table>';
        $result .= '</div>';
        $result .= '
				</form>';
    } else {
        $result .= '<br />';
    }

    return $result;
}

?>