Move file type internationalization out of data model
Move file type internationalization out of data model

--- a/include/git/FileDiff.class.php
+++ b/include/git/FileDiff.class.php
@@ -390,29 +390,27 @@
 	/**
 	 * Gets the from file type
 	 *
-	 * @param boolean $local true if caller wants localized type
-	 * @return string from file type
-	 */
-	public function GetFromFileType($local = false)
-	{
-		if (!$this->diffInfoRead)
-			$this->ReadDiffInfo();
-
-		return GitPHP_Blob::FileType($this->fromMode, $local);
+	 * @return int from file type
+	 */
+	public function GetFromFileType()
+	{
+		if (!$this->diffInfoRead)
+			$this->ReadDiffInfo();
+
+		return GitPHP_FilesystemObject::ObjectType($this->fromMode);
 	}
 
 	/**
 	 * Gets the to file type
 	 *
-	 * @param boolean $local true if caller wants localized type
-	 * @return string to file type
-	 */
-	public function GetToFileType($local = false)
-	{
-		if (!$this->diffInfoRead)
-			$this->ReadDiffInfo();
-
-		return GitPHP_Blob::FileType($this->toMode, $local);
+	 * @return int to file type
+	 */
+	public function GetToFileType()
+	{
+		if (!$this->diffInfoRead)
+			$this->ReadDiffInfo();
+
+		return GitPHP_FilesystemObject::ObjectType($this->toMode);
 	}
 
 	/**

--- a/include/git/FilesystemObject.class.php
+++ b/include/git/FilesystemObject.class.php
@@ -9,6 +9,33 @@
  */
 abstract class GitPHP_FilesystemObject extends GitPHP_GitObject
 {
+	/**
+	 * Unknown type
+	 *
+	 * @const
+	 */
+	const UnknownType = 0;
+
+	/**
+	 * Directory type
+	 *
+	 * @const
+	 */
+	const DirectoryType = 1;
+
+	/**
+	 * Symlink type
+	 *
+	 * @const
+	 */
+	const SymlinkType = 2;
+
+	/**
+	 * File type
+	 *
+	 * @const
+	 */
+	const FileType = 3;
 
 	/**
 	 * The object path
@@ -241,6 +268,27 @@
 	}
 
 	/**
+	 * Gets a filesystem object type from its octal mode
+	 *
+	 * @param string $octMode octal mode
+	 * @return int file type
+	 */
+	public static function ObjectType($octMode)
+	{
+		$mode = octdec($octMode);
+
+		if (($mode & 0x4000) == 0x4000) {
+			return GitPHP_FilesystemObject::DirectoryType;
+		} else if (($mode & 0xA000) == 0xA000) {
+			return GitPHP_FilesystemObject::SymlinkType;
+		} else if (($mode & 0x8000) == 0x8000) {
+			return GitPHP_FilesystemObject::FileType;
+		}
+
+		return GitPHP_FilesystemObject::UnknownType;
+	}
+
+	/**
 	 * Compares two objects by path
 	 *
 	 * @param GitPHP_FilesystemObject $a first object

--- a/include/git/blob/Blob.class.php
+++ b/include/git/blob/Blob.class.php
@@ -119,43 +119,6 @@
 	}
 
 	/**
-	 * Gets a file type from its octal mode
-	 *
-	 * @param string $octMode octal mode
-	 * @param boolean $local true if caller wants localized type
-	 * @return string file type
-	 */
-	public static function FileType($octMode, $local = false)
-	{
-		$mode = octdec($octMode);
-		if (($mode & 0x4000) == 0x4000) {
-			if ($local) {
-				return __('directory');
-			} else {
-				return 'directory';
-			}
-		} else if (($mode & 0xA000) == 0xA000) {
-			if ($local) {
-				return __('symlink');
-			} else {
-				return 'symlink';
-			}
-		} else if (($mode & 0x8000) == 0x8000) {
-			if ($local) {
-				return __('file');
-			} else {
-				return 'file';
-			}
-		}
-
-		if ($local) {
-			return __('unknown');
-		} else {
-			return 'unknown';
-		}
-	}
-
-	/**
 	 * Gets the blob size
 	 *
 	 * @return integer size

--- /dev/null
+++ b/include/smartyplugins/function.localfiletype.php
@@ -1,1 +1,44 @@
+<?php
+/**
+ * Smarty function to get a localized file type
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2012 Christopher Han
+ * @package GitPHP
+ * @subpackage Smarty
+ *
+ * @param array $params param array
+ * @param Smarty_Internal_Template $template smarty template
+ */
+function smarty_function_localfiletype($params, Smarty_Internal_Template $template)
+{
+	if (empty($params['type'])) {
+		trigger_error("localfiletype: missing 'type' parameter");
+		return;
+	}
 
+	$type = $params['type'];
+
+	$output = null;
+
+	switch ($type) {
+		case GitPHP_FilesystemObject::FileType:
+			$output = __('file');
+			break;
+		case GitPHP_FilesystemObject::SymlinkType:
+			$output = __('symlink');
+			break;
+		case GitPHP_FilesystemObject::DirectoryType:
+			$output = __('directory');
+			break;
+		default:
+			$output = __('unknown');
+			break;
+	}
+
+	if (!empty($params['assign']))
+		$template->assign($params['assign'], $output);
+	else
+		return $output;
+}
+

--- a/templates/commit.tpl
+++ b/templates/commit.tpl
@@ -100,7 +100,7 @@
 	 </td>
          <td>
 	   <span class="newfile">
-	     {assign var=localtotype value=$diffline->GetToFileType(1)}
+	     {localfiletype type=$diffline->GetToFileType() assign=localtotype}
 	     [
 	     {if $diffline->ToFileIsRegular()}
 	       {assign var=tomode value=$diffline->GetToModeShort()}
@@ -125,7 +125,7 @@
 	 </td>
          <td>
 	   <span class="deletedfile">
-	     {assign var=localfromtype value=$diffline->GetFromFileType(1)}
+	     {localfiletype type=$diffline->GetFromFileType() assign=localfromtype}
 	     [ {t 1=$localfromtype}deleted %1{/t} ]
 	   </span>
 	 </td>
@@ -153,8 +153,8 @@
 	     <span class="changedfile">
 	       [
 	       {if $diffline->FileTypeChanged()}
-	     	 {assign var=localfromtype value=$diffline->GetFromFileType(1)}
-	     	 {assign var=localtotype value=$diffline->GetToFileType(1)}
+	         {localfiletype type=$diffline->GetFromFileType() assign=localfromtype}
+	         {localfiletype type=$diffline->GetToFileType() assign=localtotype}
 	         {if $diffline->FileModeChanged()}
 		   {if $diffline->FromFileIsRegular() && $diffline->ToFileIsRegular()}
 		     {assign var=frommode value=$diffline->GetFromModeShort()}

--- a/templates/commitdiff.tpl
+++ b/templates/commitdiff.tpl
@@ -89,8 +89,7 @@
      <div class="diffBlob" id="{$filediff->GetFromHash()}_{$filediff->GetToHash()}">
      <div class="diff_info">
      {if ($filediff->GetStatus() == 'D') || ($filediff->GetStatus() == 'M')}
-       {assign var=localfromtype value=$filediff->GetFromFileType(1)}
-       {$localfromtype}:<a href="{$scripturl}?p={$project->GetProject()|rawurlencode}&amp;a=blob&amp;h={$filediff->GetFromHash()}&amp;hb={$commit->GetHash()}{if $filediff->GetFromFile()}&amp;f={$filediff->GetFromFile()}{/if}">{if $filediff->GetFromFile()}a/{$filediff->GetFromFile()}{else}{$filediff->GetFromHash()}{/if}</a>
+       {localfiletype type=$filediff->GetFromFileType()}:<a href="{$scripturl}?p={$project->GetProject()|rawurlencode}&amp;a=blob&amp;h={$filediff->GetFromHash()}&amp;hb={$commit->GetHash()}{if $filediff->GetFromFile()}&amp;f={$filediff->GetFromFile()}{/if}">{if $filediff->GetFromFile()}a/{$filediff->GetFromFile()}{else}{$filediff->GetFromHash()}{/if}</a>
        {if $filediff->GetStatus() == 'D'}
          {t}(deleted){/t}
        {/if}
@@ -101,8 +100,7 @@
      {/if}
 
      {if ($filediff->GetStatus() == 'A') || ($filediff->GetStatus() == 'M')}
-       {assign var=localtotype value=$filediff->GetToFileType(1)}
-       {$localtotype}:<a href="{$scripturl}?p={$project->GetProject()|rawurlencode}&amp;a=blob&amp;h={$filediff->GetToHash()}&amp;hb={$commit->GetHash()}{if $filediff->GetToFile()}&amp;f={$filediff->GetToFile()}{/if}">{if $filediff->GetToFile()}b/{$filediff->GetToFile()}{else}{$filediff->GetToHash()}{/if}</a>
+       {localfiletype type=$filediff->GetToFileType()}:<a href="{$scripturl}?p={$project->GetProject()|rawurlencode}&amp;a=blob&amp;h={$filediff->GetToHash()}&amp;hb={$commit->GetHash()}{if $filediff->GetToFile()}&amp;f={$filediff->GetToFile()}{/if}">{if $filediff->GetToFile()}b/{$filediff->GetToFile()}{else}{$filediff->GetToHash()}{/if}</a>
 
        {if $filediff->GetStatus() == 'A'}
          {t}(new){/t}

comments