<?php
/**
 * @package sfModalBoxPlugin
 *
 * @author Dinh Hai Minh <minhdh@bachkim.vn>
 * @since  Jul 2014
 *
 */
function mm_urlencode($str) {
  return str_replace('.', '%2E', urlencode($str));
}

function mm_form_tag($submit_url = '', $options = array()) {
  $tag = form_tag($submit_url, $options);
  $ret = sfContext::getInstance()->getRequest()->getParameter('ret');
  if ($ret) $tag .= input_hidden_tag('ret', $ret);
  return $tag;
}

function mm_form_tag_end() {
  return '</form>';
}

function mm_url_for($url, $absolute = true) {
  if (!preg_match('/^javascript/', $url)) {
    $firstSign = preg_match('/\?/', $url)? '&': '?';
    return url_for($url.$firstSign.'modalbox=1', $absolute);
  }
  return $url;
}

function mm_link_to($name, $url, $html_options = array()) {
  $ret = sfContext::getInstance()->getRequest()->getParameter('ret');
  $firstSign = preg_match('/\?/', $url)? '&': '?';
  return m_link_to($name, $url.$firstSign.'ret='.$ret, $html_options);
}

function mm_link_need_login($name, $url, $ajax = false, $html_options = array()) {
  if (sfContext::getInstance()->getUser()->isAuthenticated()) {
    if ($ajax) return m_link_to($name, $url, $html_options);
    else return link_to($name, $url, $html_options);
  } else {
    $ret = $ajax? "javascript:Modalbox.show('".mm_url_for($url)."');" : $url;
    $login_url = 'user/login?ret='.mm_urlencode($ret);
    $modal_options = array('title' => 'Bạn phải đăng nhập để thực hiện công việc này');
    return m_link_to($name, $login_url, $html_options, $modal_options);
  }
}

function m_link_to($name, $url, $html_options = array(), $modal_options = array(), $extra_params = array())
{
  if (!array_key_exists('title', $modal_options)) {
    $modal_options['title'] = @$html_options['title']? $html_options['title']: $name;
  }
  $confirm = '';
  if (array_key_exists('confirm', $html_options)) {
    $confirm = "if (!confirm('".$html_options['confirm']."')) return false; ";
    unset($html_options['confirm']);
  }
  $pre_click = array_key_exists('onclick', $html_options)? $html_options['onclick']: '';
  $link_extra_params = ''; 
  foreach ($extra_params as $key => $value) $link_extra_params .= "+'/$key/'+$value";

  $link = mm_url_for($url, true);
  $modal_script = m_link_to_element("'$link'$link_extra_params", $modal_options);
  $html_options['onclick'] = $confirm . $pre_click . $modal_script;
  return link_to($name, '#', $html_options);
}

function m_submit_tag($name, $html_options = array(), $modal_options = array()) {
  $modal_options = array_merge(array('title' => $name), $modal_options);
  $pre_click = array_key_exists('onclick', $html_options)? $html_options['onclick']: '';
  $html_options['onclick'] = $pre_click . m_submit_element($modal_options);
  return submit_tag($name, $html_options);
}

function m_submit_element($modal_options = array()) {
  $modal_options['params'] = 'Form.serialize(this.form.id)';
  return m_link_to_element('this.form.action', $modal_options);
}

function m_link_to_element($url, $modal_options = array()) {
  //escape strings for js
  $params_to_escape = sfConfig::get('app_params_to_escape_list');
  foreach ($modal_options as $option => $value) {
    if (in_array($option, $params_to_escape)) $modal_options[$option] = "'$value'";
  }
  use_helper('Javascript');
  $js_options = _options_for_javascript($modal_options);
  return "Modalbox.show($url, $js_options); return false;";
}

function m_finish($name, $url = null, $options = array()) {
  $status = $url? 'true': 'false';
  $options['onclick'] = "Modalbox.hide(); return $status;";
  return link_to($name, $url, $options);
}

function loadRessources() {
  // Prototype & scriptaculous 
  $response = sfContext::getInstance()->getResponse();
  $response->addJavascript(sfConfig::get('app_scriptaculous_web_dir'). '/js/scriptaculous');
  $response->addJavascript(sfConfig::get('app_scriptaculous_web_dir'). '/js/effects');

  $response->addJavascript(sfConfig::get('app_modalbox_web_dir'). '/js/modalbox', 'last');
  $response->addStylesheet(sfConfig::get('app_modalbox_web_dir'). '/css/modalbox'); 
}

loadRessources();
