<?php

class export_templates
{
  static function get_users_templates($entities_id)
  {
    global $app_user;
    
    $templates_list = array();
    
    $templates_query = db_query("select ep.* from app_ext_export_templates ep, app_entities e where e.id=ep.entities_id and ep.entities_id='" . db_input($entities_id) . "' and (find_in_set(" . $app_user['group_id'] . ",users_groups) or find_in_set(" . $app_user['id'] . ",assigned_to)) order by ep.sort_order, ep.name");
    while($templates = db_fetch_array($templates_query))
    {
      $templates_list[] = array('id'=>$templates['id'], 'name'=>$templates['name']);
    }
    
    return $templates_list;
  }
  
  static function has_users_access($entities_id, $templates_id)
  {
    global $app_user;
    
    $templates_query = db_query("select ep.* from app_ext_export_templates ep, app_entities e where e.id=ep.entities_id and ep.entities_id='" . db_input($entities_id) . "' and (find_in_set(" . db_input($app_user['group_id']) . ",users_groups) or find_in_set(" . db_input($app_user['id']) . ",assigned_to)) and ep.id='" . db_input($templates_id) . "' order by ep.sort_order, ep.name");
    if($templates = db_fetch_array($templates_query))
    {
      return true;
    }
    else
    {
      return false;
    }
  }
  
  static function get_html($entities_id, $items_id, $templates_id)
  {
    global $app_user, $current_path;
    
    $fields_access_schema = users::get_fields_access_schema($entities_id,$app_user['group_id']);
                                      
    //prepare forumulas query
    $listing_sql_query_select = fieldtype_formula::prepare_query_select($entities_id, '');
    
    $item_query = db_query("select e.*  " . $listing_sql_query_select . " from app_entity_" . $entities_id . " e where e.id='" . $items_id . "'");
    if($item = db_fetch_array($item_query))
    {
      $templates_query = db_query("select * from app_ext_export_templates where id='" . db_input($templates_id) . "'");
      if($templates = db_fetch_array($templates_query))
      {
        $export_template = $templates['description'];
                                  
        if(preg_match_all('/{#(\d+):[^}]*}/',$export_template,$matches))
        {
          foreach($matches[1] as $matches_key=>$fields_id)
          {
            $field_query = db_query("select f.* from app_fields f where f.type not in ('fieldtype_action') and f.id ='" . db_input($fields_id) . "' and  f.entities_id='" . db_input($entities_id) . "'");
            if($field = db_fetch_array($field_query))
            {            
              //check field access
              if(isset($fields_access_schema[$field['id']]))
              {
                if($fields_access_schema[$field['id']]=='hide') continue;
              }
                                          
              //prepare field value
              $value = items::prepare_field_value_by_type($field, $item);
              
              $output_options = array('class'=>$field['type'],
                                  'value'=>$value,
                                  'field'=>$field,
                                  'item'=>$item,
                                  'is_export'=>true,                              
                                  'path'=>$current_path);
                                  
              if(in_array($field['type'],array('fieldtype_textarea_wysiwyg')))
              {
                $output = trim(fields_types::output($output_options));
              }
              else
              {
                $output = trim(strip_tags(fields_types::output($output_options)));
              }   
              
              //echo '<br>' . $fields_id . ' ' . $output . ' ' . $matches[0][$matches_key];  
              
              $export_template = str_replace($matches[0][$matches_key],$output,$export_template);   
                                         
            }
          }
        }
        
        return $export_template;
      }
      else
      {
        return '';
      }
    }
    else
    {
      return '';
    }  
  }
}