Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2.  
  3. class AdminLocalesController extends \BaseController {
  4.  
  5.     public function index()
  6.     {
  7.         $locales = Config::get('language::available_locales');
  8.         return View::make('admin.locales.index', compact('locales'));
  9.     }
  10.  
  11.     public function edit($id)
  12.     {
  13.         $id = $id; #dont touch it
  14.  
  15.         if (!in_array($id, array_keys(Config::get('language::available_locales'))))
  16.           die('HAL');
  17.  
  18.         $default_locale = require_once app_path()."/lang/en/main.php";
  19.         $main_locale = require_once app_path()."/lang/{$id}/auto.php";
  20.  
  21.         $static_locale_file = app_path()."/lang/{$id}/static.php";
  22.         if (is_file($static_locale_file))
  23.             $static_locale = require_once $static_locale_file;
  24.  
  25.         return View::make('admin.locales.edit', compact('id', 'default_locale', 'main_locale','static_locale'));
  26.     }
  27.  
  28.     public function update($id)
  29.     {
  30.         $data = Input::all();
  31.         $data = $data['data'];
  32.  
  33.         #delete elements with empty string values (including nested)
  34.        foreach ($data as $key => $value)
  35.         {
  36.             if (is_string($value) && trim($value) == '')
  37.             {
  38.                 unset($data[$key]);
  39.             }
  40.             elseif (is_array($value))
  41.             {
  42.                 foreach ($value as $skey => $svalue)
  43.                 {
  44.                     if (trim($svalue) == '')
  45.                         unset($data[$key][$skey]);
  46.                 }
  47.             }
  48.  
  49.         }
  50.  
  51.         //remove empty sub-arrays if present
  52.         foreach ($data as $key => $value)
  53.         {
  54.             if (is_array($value) && (count($value) == 0))
  55.                 unset($data[$key]);
  56.         }
  57.  
  58.         $file_contents = "<?php\n return ".var_export($data, TRUE)."?>";
  59.  
  60.         $static_locale_file = app_path()."/lang/{$id}/static.php";
  61.         if (is_file($static_locale_file))
  62.             copy(app_path()."/lang/{$id}/static.php", app_path()."/lang/{$id}/backup_static.php");
  63.  
  64.         if(file_put_contents(app_path()."/lang/{$id}/static.php", $file_contents))
  65.           Messagely::flash('success', "Locales seem to be updated");
  66.         else
  67.             Messagely::flash('error', 'File write error');
  68.  
  69.         return Redirect::to("/admin/locales/");
  70.     }
  71.  
  72.     public function revert($locale)
  73.     {
  74.         if (copy(app_path()."/lang/{$locale}/backup_static.php", app_path()."/lang/{$locale}/static.php"))
  75.             Messagely::flash('success', "File replaced from backup lang/{$locale}/static.php");
  76.         else
  77.             Messagely::flash('error', 'Copy from backup failed');
  78.  
  79.         return Redirect::to("/admin/locales/");
  80.     }
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement