-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #569 from nishizoe/t-4361
(refs #4361, BP from #4183) url_for, app_url_for for javascript.
- Loading branch information
Showing
7 changed files
with
250 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
apps/pc_frontend/modules/default/actions/urlForAction.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the OpenPNE package. | ||
* (c) OpenPNE Project (http://www.openpne.jp/) | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file and the NOTICE file that were distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* default actions. | ||
* | ||
* @package OpenPNE | ||
* @subpackage default | ||
* @author Shinichi Urabe <urabe@tejimaya.com> | ||
*/ | ||
class urlForAction extends sfAction | ||
{ | ||
/** | ||
* Executes urlFor action | ||
* | ||
* @param opWebRequest $request A request object | ||
*/ | ||
public function execute($request) | ||
{ | ||
// for javascript. | ||
$this->forward404Unless($request->isXmlHttpRequest()); | ||
$this->getResponse()->setContentType('text/plain'); | ||
$this->getResponse()->setHttpHeader('X-Content-Type-Options', 'nosniff', true); | ||
|
||
$this->getContext()->getConfiguration()->loadHelpers(array('Url', 'opUtil')); | ||
|
||
$application = $request['application']; | ||
$params = (array)$request['params']; | ||
$function = 'url_for'; | ||
|
||
if ($application) | ||
{ | ||
$params = array_merge(array($application), $params); | ||
$function = 'app_url_for'; | ||
} | ||
|
||
try | ||
{ | ||
return $this->renderText(call_user_func_array($function, $params)); | ||
} | ||
catch (Exception $e) | ||
{ | ||
$this->logMessage($e->getMessage(), 'err'); | ||
} | ||
|
||
return $this->set422Status('Can not find routing.'); | ||
} | ||
|
||
protected function set422Status($message) | ||
{ | ||
$this->getResponse()->setStatusCode(422, 'Unprocessable Entity'); | ||
|
||
return $this->renderText($message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* This file is part of the OpenPNE package. | ||
* (c) OpenPNE Project (http://www.openpne.jp/) | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file and the NOTICE file that were distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* OpenPNE url JavaScript helper library | ||
* | ||
* @author Shinichi Urabe <urabe@tejimaya.com> | ||
*/ | ||
|
||
/** | ||
* opUrl class | ||
*/ | ||
var opUrl = { | ||
|
||
/** | ||
* method app_url_for(). | ||
* | ||
* @param {strina} application ex: api ... | ||
* @param {string} internalUri 'module/action' or '@rule' of the action | ||
* @param {boolean} absolute return absolute path? | ||
* @returns {Deferred} Return a Deferred's Promise object. opUrl.url_for(... snip ...).done(function(url) {... snip ...}).fail(function(xhr, textStatus, errorThrown) {... snip ...}) | ||
*/ | ||
app_url_for: function(application, internalUri, absolute) | ||
{ | ||
return this.call(application, internalUri, absolute); | ||
}, | ||
|
||
/** | ||
* method url_for(). | ||
* | ||
* @param {string} internalUri 'module/action' or '@rule' of the action | ||
* @param {boolean} absolute return absolute path? | ||
* @returns {Deferred} Return a Deferred's Promise object. opUrl.url_for(... snip ...).done(function(url) {... snip ...}).fail(function(xhr, textStatus, errorThrown) {... snip ...}) | ||
*/ | ||
url_for: function(internalUri, absolute) | ||
{ | ||
return this.call('', internalUri, absolute); | ||
}, | ||
|
||
call: function(application, internalUri, absolute) | ||
{ | ||
var key = this.generate_cache_key(application, internalUri, absolute); | ||
var result = opLocalStorage.get(key); | ||
if (typeof result === 'string') | ||
{ | ||
var deferred = $.Deferred(); | ||
deferred.resolve(result); | ||
|
||
return deferred.promise(); | ||
} | ||
|
||
return this.request(application, internalUri, absolute).done(function(data) { | ||
opLocalStorage.set(key, data); | ||
}); | ||
}, | ||
|
||
request: function(application, internalUri, absolute) | ||
{ | ||
var deferred = $.Deferred(); | ||
|
||
$.ajax({ | ||
type: 'POST', | ||
url: openpne.urlForUrl, | ||
data: { application: application, params: [internalUri, Number(absolute)] }, | ||
dataType: 'text', | ||
success: deferred.resolve, | ||
error: function(xhr, textStatus, errorThrown) { | ||
deferred.reject(xhr, textStatus, errorThrown); | ||
} | ||
}); | ||
|
||
return deferred.promise(); | ||
}, | ||
|
||
generate_cache_key: function(application, internalUri, absolute) | ||
{ | ||
return 'opUrlKey:' + encodeURIComponent(application) + '&' + encodeURIComponent(internalUri) + '&' + encodeURIComponent(String(absolute)); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters