From 3827f4922dc0f97329155f3faddba23ebfef86c0 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Wed, 10 Dec 2014 17:27:50 -0600
Subject: [PATCH] i18n: Make roles translatable

---
 include/class.role.php      | 37 ++++++++++++++++++++++++++-----------
 include/class.topic.php     |  9 ---------
 include/staff/role.inc.php  |  3 ++-
 include/staff/roles.inc.php |  2 +-
 4 files changed, 29 insertions(+), 22 deletions(-)

diff --git a/include/class.role.php b/include/class.role.php
index 36ea3f9a5..1a9aec2e5 100644
--- a/include/class.role.php
+++ b/include/class.role.php
@@ -89,6 +89,15 @@ class Role extends RoleModel {
         return $this->getPermission()->getInfo();
     }
 
+    function getTranslateTag($subtag) {
+        return _H(sprintf('role.%s.%s', $subtag, $this->getId()));
+    }
+    function getLocal($subtag) {
+        $tag = $this->getTranslateTag($subtag);
+        $T = CustomDataTranslation::translate($tag);
+        return $T != $tag ? $T : $this->ht[$subtag];
+    }
+
     function to_json() {
 
         $info = array(
@@ -191,20 +200,19 @@ class Role extends RoleModel {
         return $role;
     }
 
-    static function getRoles($criteria=null) {
+    static function getRoles($criteria=null, $localize=true) {
         static $roles = null;
 
         if (!isset($roles) || $criteria) {
 
             $filters = array();
             if (isset($criteria['enabled'])) {
-                if ($criteria['enabled'])
-                    $filters += array(
-                            'flags__hasbit' => self::FLAG_ENABLED);
-                else
-                    $filters [] = Q::not(array(
-                                'flags__hasbit' => self::FLAG_ENABLED));
+                $q = new Q(array('flags__hasbit' => self::FLAG_ENABLED));
+                if (!$criteria['enabled'])
+                    $q->negate();
+                $filters[] = $q;
             }
+
             $query = self::objects()
                 ->order_by('name')
                 ->values_flat('id', 'name');
@@ -212,13 +220,20 @@ class Role extends RoleModel {
             if ($filters)
                 $query->filter($filters);
 
+            $localize_this = function($id, $default) use ($localize) {
+                if (!$localize)
+                    return $default;
+                $tag = _H("role.name.{$id}");
+                $T = CustomDataTranslation::translate($tag);
+                return $T != $tag ? $T : $default;
+            };
+
             $names = array();
             foreach ($query as $row)
-                $names[$row[0]] = $row[1];
-
-            // TODO: Localize
+                $names[$row[0]] = $localize_this($row[0], $row[1]);
 
-            if ($criteria) return $names;
+            if ($criteria || !$localize)
+                return $names;
 
             $roles = $names;
         }
diff --git a/include/class.topic.php b/include/class.topic.php
index 375b6f2d1..5adc57563 100644
--- a/include/class.topic.php
+++ b/include/class.topic.php
@@ -296,15 +296,6 @@ class Topic extends VerySimpleModel {
                 return $T != $tag ? $T : $default;
             };
 
-            $localize_this = function($id, $default) use ($localize) {
-                if (!$localize)
-                    return $default;
-
-                $tag = _H("topic.name.{$id}");
-                $T = CustomDataTranslation::translate($tag);
-                return $T != $tag ? $T : $default;
-            };
-
             // Resolve parent names
             foreach ($topics as $id=>$info) {
                 $name = $localize_this($id, $info['topic']);
diff --git a/include/staff/role.inc.php b/include/staff/role.inc.php
index 49e1fff6a..e36b87073 100644
--- a/include/staff/role.inc.php
+++ b/include/staff/role.inc.php
@@ -6,6 +6,7 @@ if ($role) {
     $action = 'update';
     $submit_text = __('Save Changes');
     $info = $role->getInfo();
+    $trans['name'] = $role->getTranslateTag('name');
     $newcount=2;
 } else {
     $title = __('Add New Role');
@@ -48,7 +49,7 @@ $info = Format::htmlchars(($errors && $_POST) ? array_merge($info, $_POST) : $in
             <td width="180" class="required"><?php echo __('Name'); ?>:</td>
             <td>
                 <input size="50" type="text" name="name" value="<?php echo
-                $info['name']; ?>"/>
+                $info['name']; ?>" data-translate-tag="<?php echo $trans['name']; ?>"/>
                 <span class="error">*&nbsp;<?php echo $errors['name']; ?></span>
             </td>
         </tr>
diff --git a/include/staff/roles.inc.php b/include/staff/roles.inc.php
index 6ce76f7dc..49ba6d87c 100644
--- a/include/staff/roles.inc.php
+++ b/include/staff/roles.inc.php
@@ -51,7 +51,7 @@ $showing=$pageNav->showing().' '._N('role', 'roles', $count);
                 ?>
             </td>
             <td><a href="?id=<?php echo $id; ?>"><?php echo
-            $role->getName(); ?></a></td>
+            $role->getLocal('name'); ?></a></td>
             <td>&nbsp;<?php echo $role->isEnabled() ? __('Active') :
             '<b>'.__('Disabled').'</b>'; ?></td>
             <td><?php echo Format::date($role->getCreateDate()); ?></td>
-- 
GitLab