Development

/plugins/mgI18nPlugin/trunk/modules/mgI18nAdmin/lib/basemgI18nAdminActions.class.php

You must first sign up to be able to contribute.

root/plugins/mgI18nPlugin/trunk/modules/mgI18nAdmin/lib/basemgI18nAdminActions.class.php

Revision 26028, 5.1 kB (checked in by rande, 3 years ago)

[mgI18nPlugin] change the finder base search folder

Line 
1 <?php
2 /*
3  * This file is part of the mgWidgetsPlugin package.
4  * (c) 2008 MenuGourmet
5  * (c) 2009 Thomas Rabaix <thomas.rabaix@soleoweb.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  *
13  * @package    mgI18nPlugin
14  * @author     Thomas Rabaix <thomas.rabaix@soleoweb.com>
15  * @version    SVN: $Id$
16  */
17 class basemgI18nAdminActions extends sfActions
18 {
19   public function executeGetTargets(sfWebRequest $request)
20   {
21     $this->forward404If(!sfConfig::get('mg_i18n_enabled'));
22     
23     $catalogue = $request->getParameter('catalogue');
24     $source    = $request->getParameter('source');
25
26     $catalogues = array();
27     
28     $cultures = sfConfig::get('app_mgI18nPlugin_cultures_available');
29     
30     foreach( $cultures as $code => $name)
31     {
32       $catalogues[] = $catalogue.'.'.$code;
33     }
34     
35     $trans_units = Doctrine::getTable('mgI18nTransUnit')
36       ->createQuery('tu')
37       ->leftJoin('tu.mgI18nCatalogue tc')
38       ->select('*')
39       ->whereIn('tc.name', $catalogues)
40       ->addWhere('tu.source = ?', $source)
41       ->execute();
42
43     $json = array();
44     
45     
46     foreach($trans_units as $trans_unit)
47     {
48       $name_catalogue = $trans_unit->mgI18nCatalogue->name;
49       $culture = $trans_unit->mgI18nCatalogue->getLanguage();
50       
51       $json['mg-i18n-target-'.$culture] = $trans_unit->target;
52       
53       
54       unset($cultures[$culture]);
55     }
56     
57     foreach($cultures as $code => $name)
58     {
59        $json['mg-i18n-target-'.$code] = '';
60     }
61     
62     return $this->renderText(json_encode($json));
63   }
64  
65   public function executeUpdateTargets(sfWebRequest $request)
66   {
67
68     $this->forward404If(!sfConfig::get('mg_i18n_enabled'));
69     
70     $catalogue = $request->getParameter('catalogue');
71     $source    = $request->getParameter('source');
72     $targets   = $request->getParameter('targets');
73     
74     $params = array(
75       'catalogue' => $catalogue,
76       'source'    => $source,
77       'targets'   => $targets
78     );
79     
80     $form = new mgI18nTargetsForm;
81
82
83     $form->bind($params);
84     if($form->isValid())
85     {
86       $form->save();
87
88       // allow to tweak the clear_cache method
89       $event = $this->getContext()->getEventDispatcher()->notifyUntil(new sfEvent($params, 'mgI18n.clear_cache'));
90
91       if(!$event->isProcessed())
92       {
93         chdir(sfConfig::get('sf_root_dir'));
94         $cc = new sfCacheClearTask($this->getContext()->getEventDispatcher(), new sfFormatter);
95         $cc->run(array(), array('--type=i18n'));
96       }
97     }
98     
99     return sfView::NONE;
100     
101   }
102
103   public function executeGetMessagesByType(sfWebRequest $request)
104   {
105     // TODO : set this value in a settings
106     $this->forward404If(!sfConfig::get('mg_i18n_enabled'));
107     
108     $valid_types = array('lib', 'application', 'ajax');
109
110     $type = $request->getParameter('type');
111     
112     $this->forward404If(!in_array($type, $valid_types));
113   
114     $finder = sfFinder::type('file')
115       ->ignore_version_control();
116
117     if($type == 'lib')
118     {
119       $in = sfConfig::get('sf_root_dir');
120       
121       $finder
122         ->discard('*actions.class.php')
123         ->name('*.class.php')
124       ;
125     }
126     else if($type == 'application')
127     {
128       $in = sfConfig::get('sf_root_dir');
129
130       $finder
131         ->name('*actions.class.php')
132       ;
133     }
134     // untranslatable sentence, hard to find
135     else if($type == 'ajax')
136     {
137       $event    = new sfEvent($this, 'mgI18nPlugin.assign_ajax_values');
138
139       $this->getContext()->getEventDispatcher()->filter($event, array());
140
141       $messages = $event->getReturnValue();
142       $finder   = false;
143     }
144     else
145     {
146       $this->forward404();
147     }
148
149     if($finder)
150     {
151       $files = $finder->in($in);
152
153       $php_extractor = new mgI18nPhpExtractor;
154
155       $messages = array();
156       foreach($files as $file)
157       {
158         $content  = file_get_contents($file);
159         $messages = array_merge($messages, $php_extractor->extract($content));
160       }
161     }
162
163     // now transform the output to be valid
164     $valid_messages = array();
165     foreach($messages as $message)
166     {
167       if(!isset($message['message']))
168       {
169         // no message, .... error in parsing
170         continue;
171       }
172       $original_catalogue = ($message['catalogue'] ? $message['catalogue'] : 'messages');
173   
174       $catalogue = $this->getContext()->getConfiguration()->getApplication().'.'.$original_catalogue;
175       
176       if(!array_key_exists($catalogue, $valid_messages))
177       {
178         $valid_messages[$catalogue] = array();
179       }
180
181       $this->getContext()->getConfiguration()->loadHelpers(array('Text'));
182
183       $hash = md5($message['message']);
184       $valid_messages[$catalogue][$hash] = array(
185         'source' => $message['message'],
186         'target' => htmlentities(truncate_text(__($message['message'], null, $original_catalogue), 70)),
187         'params' => isset($message['params']) ? $message['params'] : array(), // not fully implemented yet,
188         'is_translated' => $message['message'] != __($message['message'], null, $original_catalogue)
189       );
190     }
191
192     return $this->renderText(json_encode(array(
193       'type' => $type,
194       'messages' => $valid_messages
195     )));
196
197   }
198 }
Note: See TracBrowser for help on using the browser.