<?php 

function _tf_get_date($item, $key, $table_obj) {
	return (isValidTimeStamp($item[$key])) ? date(_DATE_FORMAT_, $item[$key]) : $item[$key];
}

function _tf_get_time_date($item, $key, $table_obj) {
	return (isValidTimeStamp($item[$key])) ? date(_TIME_FORMAT_." "._DATE_FORMAT_, $item[$key]) : $item[$key];
}

function _tf_get_cron_time_date($item, $key, $table_obj) {
	if($item[$key] == 0) return "-";
	return (isValidTimeStamp($item[$key])) ? date("g:i A \o\\n D, jS M, Y", $item[$key]) : $item[$key];
}

function _tf_view_array_data($item, $key, $table_obj) {
	$data = _unserialize($item[$key]);
	if(!_count($data)) return "";
	ob_start();
	?>
	<pre style="max-width: 253px;"><?php print_r($data); ?></pre>
	<?php
	$res = ob_get_contents();
	ob_get_clean();
	return $res;
}

function _tf_get_link($item, $key, $table_obj) {
	return "<a href=\"".$item[$key]."\" target=\"_blank\">".$item[$key]."</a>";
}

function _tf_get_serial_number($item, $key, $table_obj) {
	if(isset($table_obj->temp['sn'])) {
		$table_obj->i = $sn = $table_obj->temp['sn'];
	} else {
		$table_obj->temp['sn'] = $sn = $table_obj->i;
	}
	$table_obj->i++;
	$table_obj->temp['sn']++;
	return $sn;
}

function _tf_get_fullname($item, $key, $table_obj) {
	return get_fullname($item, true);
}

function _tf_get_role($item, $key, $table_obj) {
	return get_role_name($item[$key]);
}

function _tf_get_fullname_id($item, $key, $table_obj) {
	
	if(isset($table_obj->fullname[$item[$key]])) {
		return $table_obj->fullname[$item[$key]];
	} else {
		$table_obj->fullname[$item[$key]] = get_fullname($item[$key], true);
		return $table_obj->fullname[$item[$key]];
	}
}

function _tf_get_profile_picture($item, $key, $table_obj) {
	return '<img class="img img-72" src="'.get_user_image($item).'">';
}

function _tf_get_gender($item, $key, $table_obj) {
	return display_gender($item['gender']);
}

function _tf_to_uppercase($item, $key, $table_obj) {
	return strtoupper($item[$key]);
}

function _tf_to_lowercase($item, $key, $table_obj) {
	return strtolower($item[$key]);
}

function _tf_ucwords($item, $key, $table_obj) {
	return ucwords($item[$key]);
}

function _tf_to_money($item, $key, $table_obj) {
	return toMoney($item[$key]);
}

function _tf_select_row($item, $key, $table_obj) {
	if(!isset($table_obj->action_argsPreset)) {
		$args = array();
		if(_count($table_obj->action_args)) {
			foreach ($table_obj->action_args as $action_args) {
				$args = array_merge($args, $action_args);
			}
		}
		$table_obj->action_argsPreset = $args;
	} else {
		$args = $table_obj->action_argsPreset;
	}
	$btn = "<input type=\"checkbox\" name=\"".$table_obj->table_id."-selectrow[]\" value=\"\" ".$table_obj->apply_action_args($args, $item, false)." data-table-id=\"".$table_obj->table_id."\" data-rowid=\"".$item['rowid']."\" class=\"".$table_obj->table_id."-selectrow selectrowbtn\"";
	if(_count($table_obj->action_links)) {
		$conditions = "";
		$hasCondition = false;
		foreach ($table_obj->action_links as $action_name => $action_link) {
			if(!$table_obj->test_action_condition($action_name, $item)) {
				$hasCondition = true;
				$conditions .= sha1($action_name) .",";
			}
		}
		if($hasCondition) {
			$conditions = substr($conditions, 0, -1);
			$btn .= " data-action-condition='".$conditions."'";
		}
	}
	
	$btn .= ">";
	return $btn;
}

function _tf_display_position($items, $key, $table_obj) {
    try {
        $locale = 'en_US';
	    $nf = new NumberFormatter($locale, NumberFormatter::ORDINAL);
	    return $nf->format($items[$key]);
    }
    catch (Exception $e) {

    }
    finally {
        return ordinal_suffix_of($items[$key]);
    }
}

function _tf_serialized_array_count($items, $key, $table_obj) {
	return (is_serialized($items[$key])) ? _count(unserialize($items[$key])) : 0;
}


function _tf_get_image_thumbnail($items, $key, $table_obj) {
	return "<a  href=\"".$items[$key]."\" data-magnify=true><img src=\"".$items[$key]."\" class=\"img img-responsive thumbnail\" style=\"height: 80px\"></a>";
}

function _tf_get_file_preview($items, $key, $table_obj) {
	if(!file_exists(ABSPATH.$items[$key])) {
		return "File is no longer available";
	}
	switch($items['file_type']) {
		case 'application/pdf':
			$url = "app/assets/js/dropzone/img/pdf-icon.png";
			break;

		case 'application/x-xls':
		case 'application/msexcel':
		case 'application/x-msexcel':
		case 'application/x-ms-excel':
		case 'application/x-excel':
		case 'application/x-dos_ms_excel':
		case 'application/xls':
		case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
			$url = "app/assets/js/dropzone/img/excel-icon.png";
			break;

		default:
			$url = $items[$key];
			break;
	}
	$html = "<div style=\"border: 1px solid #adadad; height: 100px; width:100px; background: center center no-repeat url('".$url."'); background-size: contain;\"></div>";
	$html .= "<div id=\"img-".$table_obj->i."\" style=\"display: none;\">".get_option("home_url").$items[$key]."</div>";
	$html .= ctc_btn("img-".$table_obj->i, "Copy File Link", false);
	return $html;
}

function _tf_get_file_size($items, $key, $table_obj) {
	return FileSizeConvert($items[$key]);
}

function _tf_get_db_section($items, $key, $table_obj) {
	if($items[$key] === null or $items[$key] === "") {
		return "-";
	}
	if(isset($table_obj->db_section)) {
		return $table_obj->db_section[$items[$key]]['section_name'];
	} else {
		$post = new post;
		$section_field = get_option("section_field");
		$table_obj->db_section = $post->get_post_by_meta_data(['type' => $section_field], "LIKE", true);
		return $table_obj->db_section[$items[$key]]['section_name'];
	}
}


function _tf_implode($item, $key, $table_obj) {
	$array = _unserialize($item[$key]);
	return is_array($array) ? implode(", ", $array) : $array;
}

function _tf_pm_get_attachments($item, $key, $table_obj) {
	$files = get_library_files($item[$key]);
	$html = "<ul style=\"padding: 0px;\">";
	if(isset($files['files']) && _count($files['files']) && is_array($files['files'])) {
		foreach ($files['files'] as $url => $file_meta) {
			$html .= "<li style=\"display: block;\"><a class=\"popmeup\" data-width=700 data-height=500 href=\"".$url."\" target=\"_blank\">".$file_meta['title']."</a></li>";
		}
	}
	$html .= "</ul>";
	return $html;
}