Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dolibarr
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Software_Artifact_Infrastructure_Repository
dolibarr
Commits
d5ee73a1
Commit
d5ee73a1
authored
8 years ago
by
Xebax
Browse files
Options
Downloads
Patches
Plain Diff
Add Countries to the REST API.
parent
02a978bc
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
htdocs/api/class/api_countries.class.php
+167
-0
167 additions, 0 deletions
htdocs/api/class/api_countries.class.php
htdocs/core/class/ccountry.class.php
+7
-4
7 additions, 4 deletions
htdocs/core/class/ccountry.class.php
with
174 additions
and
4 deletions
htdocs/api/class/api_countries.class.php
0 → 100644
+
167
−
0
View file @
d5ee73a1
<?php
/*
* Copyright (C) 2016 Xebax Christy <xebax@wanadoo.fr>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use
Luracast\Restler\RestException
;
require_once
DOL_DOCUMENT_ROOT
.
'/main.inc.php'
;
require_once
DOL_DOCUMENT_ROOT
.
'/core/class/ccountry.class.php'
;
/**
* API class for countries
*
* @access protected
* @class DolibarrApiAccess {@requires user,external}
*/
class
Countries
extends
DolibarrApi
{
private
$translations
=
null
;
/**
* Constructor
*/
function
__construct
()
{
global
$db
;
$this
->
db
=
$db
;
}
/**
* Get the list of countries.
*
* The names of the countries will be translated to the given language if
* the $lang parameter is provided. The value of $lang must be a language
* code supported by Dolibarr, for example 'en_US' or 'fr_FR'.
* The returned list is sorted by country ID.
*
* @param string $filter To filter the countries by name
* @param int $limit Number of items per page
* @param int $page Page number (starting from zero)
* @param string $lang Code of the language the names of the
* countries must be translated to
* @return List of countries
*
* @throws RestException
*/
function
index
(
$filter
=
''
,
$limit
=
0
,
$page
=
0
,
$lang
=
''
)
{
$list
=
array
();
// Note: The filter is not applied in the SQL request because it must
// be applied to the translated names, not to the names in database.
$sql
=
"SELECT rowid FROM "
.
MAIN_DB_PREFIX
.
"c_country ORDER BY rowid"
;
if
(
$limit
)
{
if
(
$page
<
0
)
{
$page
=
0
;
}
$offset
=
$limit
*
$page
;
$sql
.
=
$this
->
db
->
plimit
(
$limit
,
$offset
);
}
$result
=
$this
->
db
->
query
(
$sql
);
if
(
$result
)
{
$num
=
$this
->
db
->
num_rows
(
$result
);
for
(
$i
=
0
;
$i
<
$num
;
$i
++
)
{
$obj
=
$this
->
db
->
fetch_object
(
$result
);
$country
=
new
Ccountry
(
$this
->
db
);
if
(
$country
->
fetch
(
$obj
->
rowid
)
>
0
)
{
// Translate the name of the country if needed
// and then apply the filter if there is one.
$this
->
translateLabel
(
$country
,
$lang
);
if
(
empty
(
$filter
)
||
stripos
(
$country
->
label
,
$filter
)
!==
FALSE
)
{
$list
[]
=
$this
->
_cleanObjectDatas
(
$country
);
}
}
}
}
else
{
throw
new
RestException
(
503
,
'Error when retrieving list of countries : '
.
$country
->
error
);
}
return
$list
;
}
/**
* Get country by ID.
*
* @param int $id ID of country
* @param string $lang Code of the language the name of the
* country must be translated to
*
* @throws RestException
*/
function
get
(
$id
,
$lang
=
''
)
{
$country
=
new
Ccountry
(
$this
->
db
);
if
(
$country
->
fetch
(
$id
)
<
0
)
{
throw
new
RestException
(
503
,
'Error when retrieving country : '
.
$country
->
error
);
}
else
if
(
$country
->
fetch
(
$id
)
==
0
)
{
throw
new
RestException
(
404
,
'country not found'
);
}
$this
->
translateLabel
(
$country
,
$lang
);
return
$this
->
_cleanObjectDatas
(
$country
);
}
/**
* Clean sensible object datas
*
* @param object $object Object to clean
* @return array Array of cleaned object properties
*/
function
_cleanObjectDatas
(
$object
)
{
$object
=
parent
::
_cleanObjectDatas
(
$object
);
unset
(
$object
->
error
);
unset
(
$object
->
errors
);
return
$object
;
}
/**
* Translate the name of the country to the given language.
*
* @param Ccountry $country
* @param string $lang
*/
private
function
translateLabel
(
$country
,
$lang
)
{
if
(
!
empty
(
$lang
))
{
// Load the translations if this is a new language.
if
(
$this
->
translations
==
null
||
$this
->
translations
->
getDefaultLang
()
!==
$lang
)
{
global
$conf
;
$this
->
translations
=
new
Translate
(
''
,
$conf
);
$this
->
translations
->
setDefaultLang
(
$lang
);
$this
->
translations
->
load
(
'dict'
);
}
if
(
$country
->
code
)
{
$key
=
'Country'
.
$country
->
code
;
$translation
=
$this
->
translations
->
trans
(
$key
);
if
(
$translation
!=
$key
)
{
$country
->
label
=
html_entity_decode
(
$translation
);
}
}
}
}
}
This diff is collapsed.
Click to expand it.
htdocs/core/class/ccountry.class.php
+
7
−
4
View file @
d5ee73a1
...
...
@@ -143,7 +143,7 @@ class Ccountry // extends CommonObject
*
* @param int $id Id object
* @param string $code Code
* @return int
<
0 if
K
O,
>
0 if O
K
* @return int
>
0 if O
K
, 0 if
not found, <0 if K
O
*/
function
fetch
(
$id
,
$code
=
''
)
{
...
...
@@ -171,10 +171,13 @@ class Ccountry // extends CommonObject
$this
->
code_iso
=
$obj
->
code_iso
;
$this
->
label
=
$obj
->
label
;
$this
->
active
=
$obj
->
active
;
}
$this
->
db
->
free
(
$resql
);
return
1
;
$this
->
db
->
free
(
$resql
);
return
1
;
}
else
{
return
0
;
}
}
else
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment