<?php

if (!empty($_GET['category_id'])) {
 $category_id = filter_var($_GET['category_id'], FILTER_SANITIZE_NUMBER_INT);
}
if (!empty($_GET['content_type'])) {
 $content_type_name = is_array($_GET['content_type']) ? $_GET['content_type'][0] : $_GET['content_type'];
} elseif (!empty($$class->content_type_id)) {
 $content_type_id = $$class->content_type_id;
 $content_type_i = content_type::find_by_id($content_type_id);
 $content_type_name = !empty($content_type_i->content_type) ? $content_type_i->content_type : 'content';
} elseif (!empty($_GET['content_type_id']) && !is_array($_GET['content_type_id'])) {
 $content_type_id = filter_var($_GET['content_type_id'], FILTER_SANITIZE_NUMBER_INT);
 $content_type_i = content_type::find_by_id($content_type_id);
 $content_type_name = $content_type_i->content_type;
} else {
 $content_type_name = 'content';
}

//if(!empty($content_type_name) && empty($category_id)){
// $category_id = 1;
//}

if (!empty($_GET['parent_id'])) {
 $parent_id = $_GET['parent_id'];
}


//first include the other inlcude files
if (!empty($content_type_name) && !empty($_GET['content_type'])) {
 $con_type = is_array($_GET['content_type']) ? $_GET['content_type'][0] : $_GET['content_type'];
 if (class_exists($con_type)) {
  $reflector = new ReflectionClass($con_type);
  $fnname = $reflector->getFileName();
  $fdir = dirname($fnname);
  $include_file_names = glob($fdir . DS . "*.inc");
  if (!empty($include_file_names)) {
   foreach ($include_file_names as $key => $pathname) {
    include_once "$pathname";
   }
  }
 }
}

if(empty($$class->content_id)){
 $$class->published_cb = 1;
 $$class->comment_status = 3;
}

/*Extra Content*/
$class_second = 'extra_content';
$$class_second = &$extra_content;


if (empty($content->parent_id) && !(empty($parent_id))) {
 $content->parent_id = $parent_id;
}

if (!empty($content_type_id)) {
 $content_type = content_type::find_by_id($content_type_id);
 $content_type_name = $content_type->content_type;
} elseif (!empty($content_type_name)) {
 $content_type = content_type::find_by_content_type($content_type_name);
}

/* For content type that is not defined through content type. Ex - Contact Form */

if (empty($content_type)) {
 $non_db_content_type = $content_type_name;
 if ($non_db_content_type && class_exists($non_db_content_type)) {
  $content_type = new $non_db_content_type;
  $content_type->read_role = property_exists($content_type, 'read_role') ? $content_type::$read_role : 'ANONYMOUS';
  $content_type->write_role = property_exists($content_type, 'write_role') ? $content_type::$write_role : 'ANONYMOUS';
  $content_type->update_role = property_exists($content_type, 'update_role') ? $content_type::$update_role : 'ADMIN';
  $content_type->comment_read_role = property_exists($content_type, 'comment_read_role') ? $content_type::$comment_read_role : 'ANONYMOUS';
  $content_type->comment_write_role = property_exists($content_type, 'comment_write_role') ? $content_type::$comment_write_role : 'ANONYMOUS';
  $content_type->comment_update_role = property_exists($content_type, 'comment_update_role') ? $content_type::$comment_update_role : 'ADMIN';
 }
 return;
}

content::$content_table_name = 'c_' . $content_type_name;
//
$extra_fields = new_content_type::find_fields_by_id($content_type->content_type_id);
$$class->content_type_id = $content_type->content_type_id;
//echo '<br> $$extension->content_type_id is '. $content_type->content_type_id ;

$content_field_array = content::$content_field_array;
foreach ($extra_fields as $records) {
 $content_field_array[] = $records->field_name;
}

//save contend id in a variable. get value for extra content save after content save
$contentId = $content->content_id;
foreach ($content_field_array as $key => $value) {
 if (!empty($_POST[$value][0])) {
  $$class->$value = trim(mysql_prep($_POST[$value][0]));
 } else {
  $$class->$value = "";
 }
}

$content->content_id = $contentId;

//get the extra content value of existing conetnt - used for update.
if (!empty($content->content_id)) {
 $extra_content = content::content_find_by_id($content_type_name, $content->content_id);
}

function cmp($a, $b) {
 return $a->field_position - $b->field_position;
}

if (!empty($extra_fields)) {
 usort($extra_fields, "cmp");
}

//form element to show on form
//echo '<br><br><br><br><br><br><br><br>';
$form_element = "";

foreach ($extra_fields as $records) {
 $field_label = !empty($records->field_label) ? ucfirst($records->field_label) : ucfirst($records->field_name);
 $requiredClass = !empty($records->required_cb) ? ' required ' : '';

 $field_type = $records->field_type;
 $field = strtolower($records->field_name);

 switch (DB_TYPE) {
  case 'ORACLE' :
   $field_num = $records->data_precision;
   break;

  default:
   $field_num = $records->field_num;
   break;
 }


 if (!empty($extra_content->$field)) {
  if ($field_type == 'CLOB') {
   $value = htmlentities(stream_get_contents($extra_content->$field));
  } else {
   $value = htmlentities($extra_content->$field);
  }
 } else {
  $value = "";
 }

 if (!in_array($field, ['content_id', 'CONTENT_ID'])) {
  switch ($field_type) {

   case 'int' :
   case 'NUMBER' :
    $form_element .= '<div class="' . $requiredClass . ' new_element int"><label>' . ($field_label) . '</label>
        <input type="number" name="' . $field . '[]" class="' . $field . '"
         value="' . $value . '">
       </div>';
    break;

   case 'varchar' :
   case 'VARCHAR2' :
    if (!empty($records->option_type)) {
     $form_element .= '<div class="' . $requiredClass . 'new_element option"><label>' . ($field_label) . '</label>';
     $form_element .= form::select_field_from_object($field, option_line::find_by_option_id($records->option_type), 'option_line_code', 'option_line_value', $value, '', '', 'option', '', '');
     $form_element .= '</div>';
    } else {
     $form_element .= '<div class="' . $requiredClass . 'new_element varchar"><label>' . $field_label . ' </label>
        <input type="text" name="' . $field . '[]" class="' . $field . '"
         value="' . $value . '">
       </div>';
    }
    break;

   case 'text' :
   case 'CLOB':
    $form_element .= '<div class="' . $requiredClass . 'new_element text"><label>' . ($field_label) .
     '&nbsp; <i class="fa fa-language clickable enable-editor" title="Enable Text Editor"></i></label>
        <textarea rows="10" name="' . $field . '[]" class="ed-bigtext  div-with-preview ' . $field . '">' . html_entity_decode($value) . '</textarea>
         <div class="ino-text-preview"></div>
       </div>';
    break;

   case 'mediumtext' :
    $form_element .= '<div class="' . $requiredClass . 'new_element mediumtext"><label>' . ($field_label) . '</label>
        <textarea name="' . $field . '[]" class="noformat ' . $field . '">' . $value . '</textarea>
       </div>';
    break;

   case 'date' :
    $form_element .= '<div class="' . $requiredClass . 'new_element varchar"><label>' . ($field_label) . '</label>
        <input type="date" name="' . $field . '[]" class="' . $field . '"
         value="' . $value . '">
       </div>';
    break;

   case 'tinyint' :
    $form_element .= '<div class="' . $requiredClass . 'new_element tinyint"><label>' . ($field_label) . '</label>
        <input type="checkbox" name="' . $field . '[]" class="checkbox' . $field . '"
         value="' . $value . '">
       </div>';
    break;

   case 'enum' :
    $form_element .= '<div class="' . $requiredClass . 'new_element enum"><label>' . ($field_label) . '</label>
        <select name="' . $field . '[]" class="checkbox" ' . $field . '>';
    $form_element .='<option value=" "></option>';
    $value_list = explode(",", $field_num);
//            echo '<pre>';
//            print_r($value_list);
//            echo '<pre>';
    foreach ($value_list as $key => $option_value) {

     $option_value = trim($option_value, "'");
     if ($option_value == $value) {
      $selected = ' selected ';
     } else {
      $selected = ' ';
     }
//             echo $option_value;
     $form_element .= "<option value=\"$option_value\"  $selected > $option_value </option>";
    }
    $form_element .= '</select></div>';
    break;

   default :
    $form_element .= '<div class="new_element varchar"><label>' . $field . '</label>
        <input type="text" name="' . $field . '[] " class="' . $field . '"
         value="' . $value . '">
       </div>';
  }
 }
}

//category for new content
$cat = new category();
$major_category_of_content_type = $cat->findBy_reference_table_and_id('content_type', $content_type->content_type_id);
if (!empty($major_category_of_content_type)) {
 $categories_of_content_type_select_option_array = array();
 foreach ($major_category_of_content_type as $objects) {
  $categories_of_content_type_select_option = $cat->all_child_category_select_option('category_id', $objects->category_id, '', '', false);
  $categories_of_content_type_select_option_array[] = $categories_of_content_type_select_option;
 }
}

//start of categories for existing content
/* if $category is missing from main function bur $category_id present in GET then insert $category_id to
 * $category array */
if ((empty($category)) && !empty($category_id)) {
 $category_obj = category::find_by_id($category_id);
 $category = [];
 array_push($category, $category_obj);
}

if ((!empty($category)) && (!empty($major_category_of_content_type))) {
 $category_select_statement_array = array();
// $major_category_obj = category::find_by_id($major_category_id);
 foreach ($category as $content_category) {
  if (empty($content_category->category_id)) {
   continue;
  }
  $category_id = $content_category->category_id;
  $major_category_id = category::find_major_category_id($category_id);
  $div_id = 'category_id_' . $major_category_id;
  $category_select_statement = $cat->all_child_category_select_option('category_id', $major_category_id, $category_id, $div_id, false);
  $category_select_statement_array[] = $category_select_statement;
  unset($category_select_statement);
 }
}

$breadCrum = "<div class='container ajax_content' id='top-bc'>";
if ((!empty($category)) && (!empty($content_type_name))) {
 foreach ($category as $object) {
  if (empty($object->category_id)) {
   continue;
  }
  $breadCrum .= category::breadCrum_by_category_contentType($object->category_id, $content_type_name);
  $breadCrum_l = HOME_URL . 'content.php?mode=9&amp;content_type=' . $content_type_name .
   '&amp;category_id=' . $object->category_id;
  $breadCrum .='>> <a class="new_post btn btn-info active" href="' . ($breadCrum_l) . '"> New Post</a>';
 }
} elseif ((!empty($category_id)) && (!empty($content_type_name))) {
 $breadCrum .= category::breadCrum_by_category_contentType($category_id, $content_type_name);
 $breadCrum_l = HOME_URL . 'content.php?mode=9&amp;content_type=' . $content_type_name .
  '&amp;category_id=' . $category_id;
 $breadCrum .='>> <a class="new_post btn btn-info active " href="' . ($breadCrum_l) . '">New Post</a>';
}
$breadCrum .='</div>';
?>
