FINAL suepr merge step : added all modules to this super repos

This commit is contained in:
Bachir Soussi Chiadmi
2015-04-19 16:46:59 +02:00
7585 changed files with 1723356 additions and 18 deletions

View File

@@ -0,0 +1,339 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
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 2 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.

View File

@@ -0,0 +1,62 @@
About the International Phone Number format
===========================================
These rules have been formulated off of the E.123 (for display) and
E.164 (for input) specifications published by the ITU. In order to
prevent ambiguity, we have chosen to restrict some of the stipulations
these specifications give.
In summary, country calling codes are assigned by the ITU Telecommunication
Standardization Sector in E.164. We take an international phone number to
have the form +XX YYYYYYY where XX is the country code, and YYYYYYY to be
the subscriber's number, possibly with intervening spaces. The maximum
length for these phone numbers is 15.
Reference materials can be found here:
- http://www.itu.int/rec/T-REC-E.123/en
- http://www.itu.int/rec/T-REC-E.164/en
Modifications to E.123
----------------------
7.1: The international prefix symbol "+" MUST prefix international
phone numbers. All numbers missing this symbol will be assumed to be in
the default country code.
When reformatting numbers to a standard format, the following conventions
will be taken:
7.2: Parentheses will be normalized to spaces.
We do not support the multiple phone numbers as described by (7.4); users
can always specify that multiple values are allowed if this is desired.
The functionality specified by 7.5, 7.6 and 8 IS NOT implemented.
9.2 specifies that spacing SHALL OCCUR between the country code, the trunk
code and the subscriber number. As trunk codes are omitted by convention,
this means the only guaranteed separation will be between the country code
and subscriber number. Our implementation MAY treat hyphens, spaces and
parentheses as advisory indicators as to where spaces should be placed.
However, +1 7329060489 will stay as it was specified, while +1 (732) 906-0489
will be normalized to +1 732 906 0489. As a future feature, rules may
be implemented for country codes specifying these conventions, however,
I have deemed such functionality out of scope for now.
The Drupal task specifies that we should validate country codes, however,
due to the highly volatile nature of these codes, the author does not
believe that it is a good idea to maintain a list of valid country codes.
Thus, we only validate that the country code is three or less digits.
Modifications to E.164
----------------------
Our processing for NDD's will be similarly constrained. As per
7.3.2, we will treat 0 as a valid trunk code for all countries.
Other digits may be specified if the fall in the form of (X), where X is
a single digit that is 7 or 8.
Postscript
----------
Modifications to our implementation will occur as necessary by user bug
reports.

View File

@@ -0,0 +1,66 @@
Description
-----------
Another CCK module to provide a phone number field type.
Main differences cck_phone from Phone (CCK) phone.module are:
- Single CCK widget instead of ever growing CCK widget per country
- Supports all/multiple countries in one CCK field
- Covers almost all country codes (239 on last count) out of the box, with generic phone number validation
- Includes all countries codes or select your own list of countries
- Custom country specific phone numbers validation & formatting
- Custom country validation can be turned off, configurable per field
- Better tel link support (RFC3966) or mobile support - iPhone, Android, iPad, Opera Mobile/Mini, etc.
Prerequisites
-------------
The cck_phone.module requires the content.module to be installed.
Installation
------------
To install, copy the cck_phone directory and all its contents to your modules directory.
To enable this module, go to Administer > Modules, and enable Phone Number.
Themable
--------
theme_phone_number($element)
- FAPI theme for an individual phone number elements.
theme_phone_number_extension($extension = '')
- Theme function for phone extension
theme_cck_phone_formatter_default($element)
- Theme function for 'default' or global phone number field formatter.
theme_cck_phone_formatter_local($element)
- Theme function for 'local' phone number field formatter.
theme_cck_phone_mobile_tel($element, $phone = '')
- Theme function for mobile tel.
Todo Tasks: (Contribution is welcome!)
-----------
- D7 port using Field API
- migration option from phone.module
- phone types (home, work, mobile, fax, etc) support
- simpletest
Ongoing Tasks:
--------------
- add validation support to all country phone numbers
- translation of the module
Bugs/Features/Patches:
----------------------
If you want to report bugs, feature requests, or submit a patch, please do so at the project page on the Drupal web site.
http://drupal.org/project/cck_phone
Credits
-------
Initially sponsored by forDrupal.com

View File

@@ -0,0 +1,20 @@
/* input field */
.form-phone-number .form-item,
.form-phone-number .form-item label {
display: inline;
}
.form-phone-number .form-item label {
margin-left: 1em;
margin-right: .5em;
}
/* admin settings */
.cck-phone-settings .form-checkboxes .form-item {
float: left;
width: 33%;
}
.cck-phone-settings .cck-phone-default-country {
background: #eee;
font-weight: bold;
}

View File

@@ -0,0 +1,23 @@
name = Phone Number
description = "The phone module allows administrators to define a field type for phone numbers."
package = Fields
dependencies[] = field
core = 7.x
files[] = cck_phone.module
files[] = includes/cck_phone_countrycodes.inc
files[] = includes/cck_phone.token.inc
files[] = includes/phone.au.inc
files[] = includes/phone.ca.inc
files[] = includes/phone.gb.inc
files[] = includes/phone.hu.inc
files[] = includes/phone.my.inc
files[] = includes/phone.pl.inc
files[] = includes/phone.us.inc
; Information added by drupal.org packaging script on 2012-03-02
version = "7.x-1.x-dev"
core = "7.x"
project = "cck_phone"
datestamp = "1330689918"

View File

@@ -0,0 +1,62 @@
<?php
/**
* @file
* Defines phone number fields for CCK.
* Installation file
*/
/**
* Implements hook_field_schema().
*/
function cck_phone_field_schema($field) {
return array(
'columns' => array(
'number' => array(
'type' => 'varchar',
'length' => 15,
'not null' => FALSE,
),
'country_codes' => array(
'type' => 'varchar',
'length' => 2,
'not null' => FALSE,
),
'extension' => array(
'type' => 'varchar',
'length' => 6,
'not null' => FALSE,
),
),
);
}
/**
* Implements hook_install().
*/
function cck_phone_install() {
drupal_set_message(st('Phone number module installed successfully.'));
}
/**
* Implements hook_uninstall().
*/
function cck_phone_uninstall() {
}
/**
* Implements hook_enable().
*
* Notify content module when this module is enabled.
*/
function cck_phone_enable() {
// TODO: Migration path for phone.module to cck_phone
}
/**
* Implements hook_disable().
*
* Notify content module when this module is disabled.
*/
function cck_phone_disable() {
}

View File

@@ -0,0 +1,100 @@
(function ($) {
Drupal.PhoneNumber = Drupal.PhoneNumber || {};
/**
* Filters checkboxes based on their label.
* This code is shamelessly taken from checkbox_filter
*/
Drupal.PhoneNumber.filter = function() {
var field = $(this);
var checkboxes = field.parent().parent().find('.form-checkboxes .form-item');
var found = false;
var label = "";
var option = null;
for (var i = 0; i < checkboxes.length; i++) {
option = checkboxes.eq(i);
label = Drupal.PhoneNumber.trim(option.text());
if (label.toUpperCase().indexOf(field.val().toUpperCase()) < 0) {
option.hide();
} else {
option.show();
}
}
};
/**
* Trims whitespace from strings
*/
Drupal.PhoneNumber.trim = function(str) {
var str = str.replace(/^\s\s*/, ''),
ws = /\s/,
i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
};
/**
* Check/Uncheck all checkboxes
*/
Drupal.PhoneNumber.checkall = function(e) {
var field = $(this);
var checkboxes = $('.form-checkboxes .form-item:visible .form-checkbox', field.parent().parent());
var checked = (field.text() == Drupal.t('Select all'));
if (checked) {
checkboxes.attr('checked', true);
field.text(Drupal.t('Deselect all'));
}
else {
checkboxes.attr('checked', false);
Drupal.PhoneNumber.checkDefault();
field.text(Drupal.t('Select all'));
}
};
/**
* Country selection should include default country code by default.
*/
Drupal.PhoneNumber.checkDefault = function(e) {
var defaultCC = $('#edit-instance-settings-default-country').val();
var span = $('<span class="default-cc"></span>').append(Drupal.t('Default'));
$('.cck-phone-settings .form-checkboxes').find('.form-checkbox').each(function() {
if ($(this).val() == defaultCC) {
$('.cck-phone-default-country')
.removeClass('cck-phone-default-country')
.find('span.default-cc').remove();
// TODO: check for "Enable default country code" only set the checkbox
$(this)
// .attr('checked', 'checked')
.parents('.form-item:first')
.addClass('cck-phone-default-country')
.append(span);
}
});
};
/**
* Attach a filtering textfield to checkboxes.
*/
Drupal.behaviors.PhoneNumber = {
attach: function(context) {
// Ensure the new default country is checked
$('#edit-instance-settings-default-country, .cck-phone-settings .form-checkboxes').bind('change', Drupal.PhoneNumber.checkDefault);
$('#edit-instance-settings-default-country').trigger('change');
$('form#field-ui-field-edit-form').submit(Drupal.PhoneNumber.checkDefault);
// Filter for countries
var form = '<div class="form-item container-inline">'
+ ' <label>' + Drupal.t('Filter') + ':</label> '
+ ' <input class="cck-phone-filter form-text" type="text" size="30" />'
+ ' <a class="cck-phone-check" style="margin-left: 1em;" href="javascript://">' + Drupal.t('Select all') + '</a>'
+ '</div>';
$('.cck-phone-settings .form-checkboxes', context).before(form);
$('input.cck-phone-filter').bind('keyup', Drupal.PhoneNumber.filter);
$('a.cck-phone-check').bind('click', Drupal.PhoneNumber.checkall);
}
};
})(jQuery);

View File

@@ -0,0 +1,869 @@
<?php
/**
* @file
* Defines phone number fields for CCK.
* Provide some verifications on the phone numbers
*/
define('CCK_PHONE_PHONE_MIN_LENGTH', 4); // Is there a phone number less than 4 digits?
define('CCK_PHONE_PHONE_MAX_LENGTH', 15); // International standard 15 digits
define('CCK_PHONE_EXTENSION_MAX_LENGTH', 6);
define('CCK_PHONE_CC_MAX_LENGTH', 2);
// load country codes
require_once dirname(__FILE__) . '/cck_phone_countrycodes.inc';
/**
* Implements hook_init().
* This hook is called on module initialization.
*/
function cck_phone_init() {
// Token module support.
if (module_exists('token')) {
module_load_include('inc', 'cck_phone', 'cck_phone.token');
}
}
/**
* Implements hook_theme().
*/
function cck_phone_theme() {
return array(
'cck_phone_phone_number' => array(
'render element' => 'element',
),
'phone_number_extension' => array(
'render element' => 'element',
),
'cck_phone_formatter_global_phone_number' => array(
'variables' => array('element' => NULL),
),
'cck_phone_formatter_local_phone_number' => array(
'variables' => array('element' => NULL),
),
);
}
/**
* Implements hook_field_info().
*/
function cck_phone_field_info() {
return array(
'phone_number' => array(
'label' => t('Phone number'),
'description' => t('Store a number and country code in the database to assemble a phone number.'),
'settings' => array('size' => CCK_PHONE_PHONE_MAX_LENGTH),
'instance_settings' => array(
'enable_default_country' => TRUE,
'default_country' => NULL,
'all_country_codes' => TRUE,
'country_codes' => array('hide_single_cc' => FALSE, 'country_selection' => array()),
'country_code_position' => 'after',
'enable_country_level_validation' => TRUE,
'enable_extension' => FALSE,
),
'default_widget' => 'phone_number',
'default_formatter' => 'global_phone_number',
),
);
}
/**
* Implements hook_field_instance_settings_form().
*/
function cck_phone_field_instance_settings_form($field, $instance) {
drupal_add_css(drupal_get_path('module', 'cck_phone') . '/cck_phone.css');
drupal_add_js(drupal_get_path('module', 'cck_phone') . '/cck_phone.js');
$defaults = field_info_instance_settings($field['type']);
$settings = array_merge($defaults, $instance['settings']);
$form = array();
$form['enable_default_country'] = array(
'#type' => 'checkbox',
'#title' => t('Enable default country code'),
'#default_value' => $settings['enable_default_country'],
'#description' => t('Check this to enable default country code below.'),
);
$form['default_country'] = array(
'#type' => 'select',
'#title' => t('Default country code'),
'#default_value' => $settings['default_country'],
'#options' => _cck_phone_cc_options(TRUE),
'#description' => t('Item marked with * comes with country level phone number validation.'),
'#states' => array(
'invisible' => array(
':input[name="instance[settings][enable_default_country]"]' => array('checked' => FALSE),
),
),
);
$form['all_country_codes'] = array(
'#type' => 'checkbox',
'#title' => t('Show all country codes.'),
'#default_value' => $settings['all_country_codes'],
'#description' => t('Uncheck this to select the country to be displayed.'),
);
// Country codes settings
$form['country_codes'] = array(
'#title' => 'Country selection',
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#attributes' => array('class' => array('cck-phone-settings')),
'#states' => array(
'visible' => array(
':input[name="instance[settings][all_country_codes]"]' => array('checked' => FALSE),
),
),
);
$form['country_codes']['hide_single_cc'] = array(
'#type' => 'checkbox',
'#title' => t('Hide when only one country code'),
'#default_value' => $settings['country_codes']['hide_single_cc'],
'#description' => t('By default when there is only one country code, it will show as a display-only form element. Check this to hide the country code.'),
);
$form['country_codes']['country_selection'] = array(
'#type' => 'checkboxes',
'#title' => t('Select country codes to be included'),
'#default_value' => isset($settings['country_codes']['country_selection']) && !empty($settings['country_codes']['country_selection']) ? $settings['country_codes']['country_selection'] : array($settings['default_country'] => $settings['default_country']),
'#options' => _cck_phone_cc_options(TRUE),
'#description' => t('Country marks with <em>*</em> has custom country code settings and/or validation.'),
);
if (isset($settings['country_codes']['country_selection']) && !empty($settings['country_codes']['country_selection'])) {
$form['country_codes']['country_selection']['#default_value'] = $settings['country_codes']['country_selection'];
}
elseif ($settings['enable_default_country']) {
$form['country_codes']['country_selection']['#default_value'] = array($settings['default_country'] => $settings['default_country']);
}
$form['country_code_position'] = array(
'#type' => 'radios',
'#title' => t('Country code position'),
'#options' => array(
'before' => t('Before phone number'),
'after' => t('After phone number'),
),
'#default_value' => $settings['country_code_position'],
'#description' => t('Select the position of the country code selection field relative to the phone number text field.'),
);
$form['enable_country_level_validation'] = array(
'#type' => 'checkbox',
'#title' => t('Enable country level validation'),
'#default_value' => $settings['enable_country_level_validation'],
'#description' => t('Uncheck this to disable stringent country phone number validation.'),
);
$form['enable_extension'] = array(
'#type' => 'checkbox',
'#title' => t('Enable phone extension support'),
'#default_value' => $settings['enable_extension'],
'#description' => t('Check this to enable phone number extension field.'),
);
// Display country specific settings
foreach (_cck_phone_custom_cc() as $cc) {
$function = $cc . '_phone_field_settings';
if (function_exists($function)) {
$country_settings = $function($op, $field);
if (isset($country_settings) && !empty($country_settings)) {
$country_codes = cck_phone_countrycodes($cc);
// Wrap with fieldset
$wrapper = array(
'#title' => t('%country specific settings', array('%country' => $country_codes['country'])),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#attributes' => array('class' => 'cck-phone-settings cck-phone-settings-' . $cc),
);
$wrapper[] = $country_settings;
array_push($form, $wrapper);
}
}
}
return $form;
}
/**
* Implements hook_field_validate().
*/
function cck_phone_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
foreach ($items as $delta => $value) {
_cck_phone_validate($items[$delta], $delta, $field, $instance, $langcode, $errors);
}
}
/**
* Implements hook_field_presave().
*/
function cck_phone_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
foreach ($items as $delta => $value) {
_cck_phone_sanitize($items[$delta], $delta, $field, $instance, $langcode);
_cck_phone_process($items[$delta], $delta, $field, $instance, $langcode);
}
}
/**
* Implements hook_field_formatter_info().
*/
function cck_phone_field_formatter_info() {
return array(
'global_phone_number' => array(
'label' => t('Global phone number (default)'),
'field types' => array('phone_number'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
'local_phone_number' => array(
'label' => t('Local phone number'),
'field types' => array('phone_number'),
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
),
);
}
/**
* Implements hook_field_formatter_view().
*/
function cck_phone_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$element = array();
$settings = $display['settings'];
$formatter = $display['type'];
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#markup' => theme('cck_phone_formatter_' . $formatter, $item),
);
}
return $element;
}
/**
* Implements hook_field_is_empty().
*/
function cck_phone_field_is_empty($item, $field) {
return empty($item['number']);
}
/**
* Theme function for phone extension.
*/
function theme_phone_number_extension($element = '') {
return t('<em> ext.</em> %extension', array('%extension' => $element['element']));
}
/**
* Theme function for 'default' or global phone number field formatter.
*/
function theme_cck_phone_formatter_global_phone_number($element) {
$phone = '';
// Display a global phone number with country code.
if (!empty($element['number']) && !empty($element['country_codes'])) {
// Call country default formatter if exist
$custom_cc = _cck_phone_custom_cc();
if (isset($custom_cc[$element['country_codes']])) {
$function = $element['country_codes'] . '_formatter_default';
if (function_exists($function)) {
$phone = $function($element);
}
}
// Output a raw value if no custom formatter or formatter return empty
if (empty($phone)) {
$cc = cck_phone_countrycodes($element['country_codes']);
$phone = $cc['code'] . '-' . $element['number'];
}
// Extension
if (!empty($element['extension'])) {
$phone = $phone . theme('phone_number_extension', $element['extension']);
}
}
return $phone;
}
/**
* Theme function for 'local' phone number field formatter.
*/
function theme_cck_phone_formatter_local_phone_number($element) {
$phone = '';
// Display a local phone number without country code.
if (!empty($element['number'])) {
// Call country local formatter if exist
$custom_cc = _cck_phone_custom_cc();
if (isset($custom_cc[$element['country_codes']])) {
$function = $element['country_codes'] . '_formatter_local';
if (function_exists($function)) {
$phone = $function($element);
}
}
// Output a raw value if no custom formatter or formatter return empty
if (empty($phone)) {
$phone = $element['number'];
}
// Extension
if (!empty($element['extension'])) {
$phone = $phone . theme('phone_number_extension', $element['extension']);
}
}
return $phone;
}
/**
* Generate an array of country codes, for use in select or checkboxes form.
*
* @param boolean $show_custom
* Mark item with '*' to indicate the country code has include file.
* @param array $country_selection
* Limit the list to the countries listed in this array.
* @return string
*/
function _cck_phone_cc_options($show_custom = FALSE, $country_selection = array()) {
$options = array();
if ($show_custom) {
$custom_cc = _cck_phone_custom_cc();
}
foreach (cck_phone_countrycodes() as $cc => $value) {
$cc_name = $value['country'] . ' (' . $value['code'] . ')';
// faster using array key instead of in_array
if ($show_custom && isset($custom_cc[$cc])) {
$cc_name .= ' *';
}
if (!empty($country_selection) && $country_selection[$cc] === 0) {
continue;
}
$options[$cc] = check_plain($cc_name);
}
return $options;
}
/**
* Get list of country codes that has custom includes.
*
* @return
* Array of country codes abbreviation or empty array if none exist.
*/
function _cck_phone_custom_cc() {
static $countrycodes;
if (!isset($countrycodes)) {
// load custom country codes phone number includes
$path = drupal_get_path('module', 'cck_phone') . '/includes';
// scan include phone numbers directory
$files = file_scan_directory($path, '/^phone\..*\.inc$/');
$countrycodes = array();
foreach ($files as $file) {
module_load_include('inc', 'cck_phone', '/includes/' . $file->name);
list ($dummy, $countrycode) = explode('.', $file->name);
// faster using array key
$countrycodes[$countrycode] = $countrycode;
}
}
return $countrycodes;
}
function _cck_phone_valid_input($input) {
// lenient checking, as long as don't have invalid phone number character
$regex = '/^
[\s.()-]* # optional separator
(?: # }
\d # } 4-15 digits number
[\s.()-]* # } each followed by optional separator
){' . CCK_PHONE_PHONE_MIN_LENGTH . ',' . CCK_PHONE_PHONE_MAX_LENGTH . '} # }
$/x';
return preg_match($regex, $input);
}
function _cck_phone_valid_cc_input($list, $cc) {
if (isset($list[$cc]) && $list[$cc] == $cc) {
return TRUE;
}
return FALSE;
}
function _cck_phone_validate(&$item, $delta, $field, $instance, $langcode, &$errors) {
if (isset($item['number'])) {
$phone_input = trim($item['number']);
}
if (isset($item['country_codes'])) {
$countrycode = trim($item['country_codes']);
}
$ext_input = '';
$settings = $instance['settings'];
if ($settings['enable_extension']) {
$ext_input = trim($item['extension']);
}
if (isset($phone_input) && !empty($phone_input)) {
$error_params = array(
'%phone_input' => check_plain($phone_input), // original phone input
'%countrycode' => check_plain($countrycode),
'%min_length' => CCK_PHONE_PHONE_MIN_LENGTH,
'%max_length' => CCK_PHONE_PHONE_MAX_LENGTH,
'%ext_input' => check_plain($ext_input),
'%ext_max_length' => CCK_PHONE_EXTENSION_MAX_LENGTH,
);
// Only allow digit, dash, space and bracket
if (!_cck_phone_valid_input($phone_input, $ext_input)) {
$error = t('The phone number must be between %min_length and %max_length digits in length.', $error_params);
if ($settings['enable_extension'] && $ext_input != '') {
$error .= '<br />' . t('The phone extension must be less than %ext_max_length digits in length.', $error_params);
}
form_set_error($field['field_name'], $error);
}
else {
if (!$settings['all_country_codes']) {
if (!_cck_phone_valid_cc_input($settings['country_codes']['country_selection'], $countrycode)) {
$error = t('Invalid country code "%countrycode" submitted.', $error_params);
form_set_error($field['field_name'], $error);
}
}
// Generic number validation
if (!cck_phone_validate_number($countrycode, $phone_input, $ext_input)) {
$error = t('The phone number must be between %min_length and %max_length digits in length.', $error_params);
if ($field['enable_extension'] && $ext_input != '') {
$error .= '<br />' . t('The phone extension must be less than %ext_max_length digits in length.', $error_params);
}
form_set_error($field['field_name'], $error);
}
// Country level validation if enabled
elseif ($settings['enable_country_level_validation']) {
$custom_cc = _cck_phone_custom_cc();
if (isset($custom_cc[$countrycode])) {
$validate_function = $countrycode . '_validate_number';
if (function_exists($validate_function)) {
$error = '';
if (!$validate_function($phone_input, $ext_input, $error)) {
form_set_error($field['field_name'], t($error, $error_params));
}
}
}
}
}
}
}
function _cck_phone_process(&$item, $delta = 0, $field, $instance, $langcode) {
$settings = $instance['settings'];
// Clean up the phone number.
$item['number'] = cck_phone_clean_number($item['number']);
if (isset($item['extension'])) {
$item['extension'] = cck_phone_clean_number($item['extension']);
}
// Don't save an invalid default value.
if ((isset($instance['default_value']) && $item['number'] == $instance['default_value']) && (isset($settings['default_country']) && $item['country_codes'] == $settings['default_country'])) {
if (!cck_phone_validate_number($item['country_codes'], $item['number'], $item['extension'])) {
unset($item['number']);
unset($item['country_codes']);
unset($item['extension']);
}
}
}
/**
* Cleanup user-entered values for a phone number field according to field settings.
*
* @param $item
* A single phone number item, usually containing number and country code.
* @param $delta
* The delta value if this field is one of multiple fields.
* @param $field
* The CCK field definition.
* @param $node
* The node containing this phone number.
*/
function _cck_phone_sanitize(&$item, $delta, $field, $instance, $langcode) {
if (empty($item)) return;
$settings = $instance['settings'];
if (!empty($item['number'])) {
$cc = $item['country_codes'];
$item['number'] = cck_phone_clean_number($item['number']);
$custom_cc = _cck_phone_custom_cc();
if (isset($custom_cc[$cc])) {
$function = $cc . '_sanitize_number';
if (function_exists($function)) {
$function($item['number']);
}
}
}
if ($settings['enable_extension']) {
$item['extension'] = cck_phone_clean_number($item['extension']);
}
else {
unset($item['extension']);
}
}
/**
* Implements hook_field_widget_info().
*/
function cck_phone_field_widget_info() {
return array(
'phone_number' => array(
'label' => t('Phone number'),
'field types' => array('phone_number'),
'behaviors' => array(
'multiple values' => FIELD_BEHAVIOR_DEFAULT,
'default value' => FIELD_BEHAVIOR_NONE,
),
'settings' => array(
'size' => CCK_PHONE_PHONE_MAX_LENGTH,
),
),
);
}
/**
* Implements hook_field_settings_form().
*/
function cck_phone_field_settings_form($field, $instance, $has_data) {
$settings = $field['settings'];
$form = array();
$form['size'] = array(
'#type' => 'textfield',
'#title' => t('Size of phone number textfield'),
'#default_value' => $settings['size'],
'#element_validate' => array('_element_validate_integer_positive'),
'#required' => TRUE,
'#description' => t('International number is maximum 15 digits with additional country code, default is %length.', array('%length' => CCK_PHONE_PHONE_MAX_LENGTH)),
);
return $form;
}
/**
* Implements hook_field_widget_form().
*/
function cck_phone_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
// Retrieve any values set in $form_state, as will be the case during AJAX
// rebuilds of this form.
if (isset($form_state['values'][$field['field_name']][$langcode])) {
$items = $form_state['values'][$field['field_name']][$langcode];
unset($form_state['values'][$field['field_name']][$langcode]);
}
foreach ($items as $delta => $item) {
// Remove any items from being displayed that are not needed.
if (!isset($item['number']) || $item['number'] == '') {
unset($items[$delta]);
}
}
// Re-index deltas after removing empty items.
$items = array_values($items);
// Update order according to weight.
$items = _field_sort_items($field, $items);
// Essentially we use the phone_number type, extended with some enhancements.
$element_info = element_info('phone_number');
$element += array(
'#type' => 'phone_number',
'#default_value' => isset($items[$element['#delta']]) ? $items[$element['#delta']] : array(),
'#process' => array_merge($element_info['#process'], array('cck_phone_field_widget_process')),
);
return $element;
}
/*
* Implements hook_field_widget_error().
*/
function cck_phone_field_widget_error($element, $error, $form, &$form_state) {
form_error($element, $error['message'], $form, $form_state);
}
/**
* An element #process callback for the phone_number field type.
*
* Expands the phone_number type to include the extension and country codes.
*/
function cck_phone_field_widget_process($element, &$form_state, $form) {
$item = $element['#value'];
$field = field_widget_field($element, $form_state);
$instance = field_widget_instance($element, $form_state);
$settings = $instance['settings'];
if ($settings['enable_extension']) {
$element['extension'] = array(
'#type' => 'textfield',
'#maxlength' => CCK_PHONE_EXTENSION_MAX_LENGTH,
'#size' => CCK_PHONE_EXTENSION_MAX_LENGTH,
'#title' => t('ext'),
'#required' => FALSE,
'#default_value' => isset($item['extension']) ? $item['extension'] : NULL,
'#weight' => 2,
);
}
if ($settings['all_country_codes']) {
$element['country_codes']['#options'] = _cck_phone_cc_options();
}
else {
$element['country_codes']['#options'] = _cck_phone_cc_options(FALSE, $settings['country_codes']['country_selection']);
}
return $element;
}
/**
* Implements hook_element_info().
*/
function cck_phone_element_info() {
$path = drupal_get_path('module', 'cck_phone');
$types['phone_number'] = array(
'#input' => TRUE,
'#process' => array('cck_phone_phone_number_process'),
'#element_validate' => array('cck_phone_phone_number_validate'),
'#theme' => 'cck_phone_phone_number',
'#theme_wrappers' => array('form_element'),
'#attached' => array(
'css' => array($path . '/cck_phone.css'),
// 'js' => array($path . '/cck_phone.js'),
),
);
return $types;
}
/**
* Process an individual element.
*/
function cck_phone_phone_number_process($element, &$form_state, $form) {
$item = $element['#value'];
$field = field_widget_field($element, $form_state);
$instance = field_widget_instance($element, $form_state);
$settings = $instance['settings'];
$element['number'] = array(
'#type' => 'textfield',
'#maxlength' => $field['settings']['size'],
'#size' => $field['settings']['size'],
// '#title' => t('Number'),
'#required' => ($element['#delta'] == 0 && $element['#required']) ? $element['#required'] : FALSE,
'#default_value' => isset($item['number']) ? $item['number'] : NULL,
'#attached' => array(
'css' => array(
drupal_get_path('module', 'cck_phone') . '/cck_phone.css',
)
),
'#weight' => 0,
);
// If only one country code, make it as hidden form item
$country_list = array_count_values($settings['country_codes']['country_selection']);
if ($country_list['0'] == count($settings['country_codes']['country_selection']) - 1) {
foreach (array_keys($country_list) as $k => $v) {
if ($v != '0') { // only one value other than '0'
$cc = cck_phone_countrycodes($v);
$element['country_codes'] = array(
'#type' => 'hidden',
'#value' => $v,
);
if (!$settings['country_codes']['hide_single_cc']) {
$element['country_codes_markup'] = array(
'#type' => 'item',
'#markup' => $value = $cc['country'] . ' (' . $cc['code'] . ')',
'#weight' => ($settings['country_code_position'] == 'after' ? 1 : -1),
);
}
}
}
}
else {
$element['country_codes'] = array(
'#type' => 'select',
// '#title' => 'Country code',
'#options' => _cck_phone_cc_options(),
'#weight' => ($settings['country_code_position'] == 'after' ? 1 : -1),
);
}
if (!$element['#required']) {
$element['country_codes']['#empty_option'] = t('- Select -');
}
if (isset($item['country_codes'])) {
$element['country_codes']['#default_value'] = $item['country_codes'];
}
elseif ($settings['enable_default_country']) {
$element['country_codes']['#default_value'] = $settings['default_country'];
}
return $element;
}
/**
* An #element_validate callback for the phone_number element.
*/
function cck_phone_phone_number_validate(&$element, &$form_state) {
$item = $element['#value'];
$field = field_widget_field($element, $form_state);
$instance = field_widget_instance($element, $form_state);
$settings = $instance['settings'];
if (isset($item['number'])) {
$phone_input = trim($item['number']);
}
if (isset($item['country_codes'])) {
$countrycode = trim($item['country_codes']);
}
$ext_input = '';
if ($settings['enable_extension'] && isset($item['extension'])) {
$ext_input = trim($item['extension']);
}
if (isset($phone_input) && !empty($phone_input)) {
if (empty($countrycode)) {
form_set_error($field['field_name'], t('The phone number must be accompanied by a country code.'));
}
else {
$error_params = array(
'%phone_input' => check_plain($phone_input), // original phone input
'%countrycode' => check_plain($countrycode),
'%min_length' => CCK_PHONE_PHONE_MIN_LENGTH,
'%max_length' => CCK_PHONE_PHONE_MAX_LENGTH,
'%ext_input' => check_plain($ext_input),
'%ext_max_length' => CCK_PHONE_EXTENSION_MAX_LENGTH,
);
// Only allow digit, dash, space and bracket
if (!_cck_phone_valid_input($phone_input, $ext_input)) {
$error = t('The phone number must be between %min_length and %max_length digits in length.', $error_params);
if ($settings['enable_extension'] && $ext_input != '') {
$error .= '<br />' . t('The phone extension must be less than %ext_max_length digits in length.', $error_params);
}
form_set_error($field['field_name'], $error);
}
else {
if (!$settings['all_country_codes']) {
if (!_cck_phone_valid_cc_input($settings['country_codes']['country_selection'], $countrycode)) {
$error = t('Invalid country code "%countrycode" submitted.', $error_params);
form_set_error($field['field_name'], $error);
}
}
// Generic number validation
if (!cck_phone_validate_number($countrycode, $phone_input, $ext_input)) {
$error = t('The phone number must be between %min_length and %max_length digits in length.', $error_params);
if ($settings['enable_extension'] && $ext_input != '') {
$error .= '<br />' . t('The phone extension must be less than %ext_max_length digits in length.', $error_params);
}
form_set_error($field['field_name'], $error);
}
// Country level validation if enabled
elseif ($settings['enable_country_level_validation']) {
$custom_cc = _cck_phone_custom_cc();
if (isset($custom_cc[$countrycode])) {
$validate_function = $countrycode . '_validate_number';
if (function_exists($validate_function)) {
$error = '';
if (!$validate_function($phone_input, $ext_input, $error)) {
form_set_error($field['field_name'], t($error, $error_params));
}
}
}
}
}
}
}
}
/**
* Returns HTML for a phone number element.
*
* @param $variables
* An associative array containing:
* - element: A render element representing the file.
*
* @ingroup themeable
*/
function theme_cck_phone_phone_number($variables) {
$element = $variables['element'];
// This wrapper is required to apply JS behaviors and CSS styling.
$output = '';
$output .= '<div class="form-phone-number">';
$output .= drupal_render_children($element);
$output .= '</div>';
return $output;
}
/**
* Strip number of space, hash, dash, bracket, etc leaving digit only.
*
* @param string $number
* @return string Returns digit only phone number.
*/
function cck_phone_clean_number($number) {
// Remove none numeric characters
$number = preg_replace('/[^0-9]/', '', $number);
return $number;
}
/**
* Generic validation for Phone Number.
*
* @param string $countrycode
* @param string $number
* @return boolean Returns boolean FALSE if the phone number is not valid.
*/
function cck_phone_validate_number($countrycode, $number, $ext = '') {
// We don't want to worry about separators
$number = cck_phone_clean_number($number);
if ($number !== '' && drupal_strlen($number) > CCK_PHONE_PHONE_MAX_LENGTH) {
return FALSE;
}
$ext = cck_phone_clean_number($ext);
if ($ext !== '' && drupal_strlen($ext) > CCK_PHONE_EXTENSION_MAX_LENGTH) {
return FALSE;
}
return TRUE;
}

View File

@@ -0,0 +1,40 @@
<?php
/**
* @file
* Token hook implementations. Included if token.module is installed.
*/
/**
* Implementation of hook_token_list().
*
* Provide a user readable list of Phone Number tokens.
*/
function cck_phone_token_list($type = 'all') {
if ($type == 'field' || $type == 'all') {
$tokens = array();
$tokens['cck_phone']['number'] = t('Phone number');
$tokens['cck_phone']['country_codes'] = t('Country code');
$tokens['cck_phone']['extension'] = t('Extension');
return $tokens;
}
}
/**
* Implementation of hook_token_values().
*/
function cck_phone_token_values($type, $object = NULL, $options = array()) {
$tokens = array();
if ($type == 'field' && isset($object[0]['number'])) {
$item = $object[0];
$tokens['number'] = $item['number'];
$tokens['country_codes'] = $item['country_codes'];
$tokens['extension'] = isset($item['extension']) ? $item['extension'] : '';
}
return $tokens;
}

View File

@@ -0,0 +1,269 @@
<?php
/**
* @file
* Defines country codes for Phone Number.
* Provide country name and international codes per country codes.
*/
/**
* Get all the country codes for supported countries.
*
* @param $cc
* Optional, two character country code. If this is ommitted all country codes
* will be returned.
* @return
* If no country code is provided an array keyed by country code, values are
* arrays with a 'country' and 'code' values. If an invalid $cc is provided,
* FALSE will be returned. If the country code is valid the country code for
* that country will be returned.
*/
function cck_phone_countrycodes($cc = NULL) {
// Alpha-2 codes from ISO 3166-1 http://en.wikipedia.org/wiki/ISO_3166-1
// Country codes from http://en.wikipedia.org/wiki/Country_calling_code
static $country_code;
if (!isset($country_code)) {
$country_code = array(
'af' => array('country' => t('Afghanistan'), 'code' => '+93'),
'al' => array('country' => t('Albania'), 'code' => '+355'),
'dz' => array('country' => t('Algeria'), 'code' => '+213'),
'as' => array('country' => t('American Samoa'), 'code' => '+1'),
'ad' => array('country' => t('Andorra'), 'code' => '+376'),
'ao' => array('country' => t('Angola'), 'code' => '+244'),
'ai' => array('country' => t('Anguilla'), 'code' => '+1'),
'ag' => array('country' => t('Antigua and Barbuda'), 'code' => '+1'),
'ar' => array('country' => t('Argentina'), 'code' => '+54'),
'am' => array('country' => t('Armenia'), 'code' => '+374'),
'aw' => array('country' => t('Aruba'), 'code' => '+297'),
'au' => array('country' => t('Australia'), 'code' => '+61'),
'at' => array('country' => t('Austria'), 'code' => '+43'),
'az' => array('country' => t('Azerbaijan'), 'code' => '+994'),
'bs' => array('country' => t('Bahamas, The'), 'code' => '+1'),
'bh' => array('country' => t('Bahrain'), 'code' => '+973'),
'bd' => array('country' => t('Bangladesh'), 'code' => '+880'),
'bb' => array('country' => t('Barbados'), 'code' => '+1'),
'by' => array('country' => t('Belarus'), 'code' => '+375'),
'be' => array('country' => t('Belgium'), 'code' => '+32'),
'bz' => array('country' => t('Belize'), 'code' => '+501'),
'bj' => array('country' => t('Benin'), 'code' => '+229'),
'bm' => array('country' => t('Bermuda'), 'code' => '+1'),
'bt' => array('country' => t('Bhutan'), 'code' => '+975'),
'bo' => array('country' => t('Bolivia'), 'code' => '+591'),
'ba' => array('country' => t('Bosnia and Herzegovina'), 'code' => '+387'),
'bw' => array('country' => t('Botswana'), 'code' => '+267'),
'br' => array('country' => t('Brazil'), 'code' => '+55'),
'io' => array('country' => t('British Indian Ocean Territory'), 'code' => '+246'),
'vg' => array('country' => t('British Virgin Islands'), 'code' => '+1'),
'bn' => array('country' => t('Brunei'), 'code' => '+673'),
'bg' => array('country' => t('Bulgaria'), 'code' => '+359'),
'bf' => array('country' => t('Burkina Faso'), 'code' => '+226'),
'bi' => array('country' => t('Burundi'), 'code' => '+257'),
'kh' => array('country' => t('Cambodia'), 'code' => '+855'),
'cm' => array('country' => t('Cameroon'), 'code' => '+237'),
'ca' => array('country' => t('Canada'), 'code' => '+1'),
'cv' => array('country' => t('Cape Verde'), 'code' => '+238'),
'ky' => array('country' => t('Cayman Islands'), 'code' => '+1'),
'cf' => array('country' => t('Central African Republic'), 'code' => '+236'),
'td' => array('country' => t('Chad'), 'code' => '+235'),
'cl' => array('country' => t('Chile'), 'code' => '+56'),
'cn' => array('country' => t('China'), 'code' => '+86'),
'cx' => array('country' => t('Christmas Island'), 'code' => '+61'),
'cc' => array('country' => t('Cocos-Keeling Islands'), 'code' => '+61'),
'co' => array('country' => t('Colombia'), 'code' => '+57'),
'km' => array('country' => t('Comoros'), 'code' => '+269'),
'cg' => array('country' => t('Congo, Republic of the'), 'code' => '+242'),
'cd' => array('country' => t('Congo, Democratic Republic of'), 'code' => '+243'),
'ck' => array('country' => t('Cook Islands'), 'code' => '+682'),
'cr' => array('country' => t('Costa Rica'), 'code' => '+506'),
'hr' => array('country' => t('Croatia'), 'code' => '+385'),
'cu' => array('country' => t('Cuba'), 'code' => '+53'),
'cy' => array('country' => t('Cyprus'), 'code' => '+357'),
'cz' => array('country' => t('Czech Republic'), 'code' => '+420'),
'dk' => array('country' => t('Denmark'), 'code' => '+45'),
'dj' => array('country' => t('Djibouti'), 'code' => '+253'),
'dm' => array('country' => t('Dominica'), 'code' => '+1'),
'do' => array('country' => t('Dominican Republic'), 'code' => '+1'),
'tp' => array('country' => t('East Timor'), 'code' => '+670'),
'ec' => array('country' => t('Ecuador'), 'code' => '+593'),
'eg' => array('country' => t('Egypt'), 'code' => '+20'),
'sv' => array('country' => t('El Salvador'), 'code' => '+503'),
'gq' => array('country' => t('Equatorial Guinea'), 'code' => '+240'),
'er' => array('country' => t('Eritrea'), 'code' => '+291'),
'ee' => array('country' => t('Estonia'), 'code' => '+372'),
'et' => array('country' => t('Ethiopia'), 'code' => '+251'),
'fk' => array('country' => t('Falkland Islands'), 'code' => '+500'),
'fo' => array('country' => t('Faroe Islands'), 'code' => '+298'),
'fj' => array('country' => t('Fiji'), 'code' => '+679'),
'fi' => array('country' => t('Finland'), 'code' => '+358'),
'fr' => array('country' => t('France'), 'code' => '+33'),
'gf' => array('country' => t('French Guiana'), 'code' => '+594'),
'pf' => array('country' => t('French Polynesia'), 'code' => '+689'),
'ga' => array('country' => t('Gabon'), 'code' => '+241'),
'gm' => array('country' => t('Gambia, The'), 'code' => '+220'),
'ge' => array('country' => t('Georgia'), 'code' => '+995'),
'de' => array('country' => t('Germany'), 'code' => '+49'),
'gh' => array('country' => t('Ghana'), 'code' => '+233'),
'gi' => array('country' => t('Gibraltar'), 'code' => '+350'),
'gr' => array('country' => t('Greece'), 'code' => '+30'),
'gl' => array('country' => t('Greenland'), 'code' => '+299'),
'gd' => array('country' => t('Grenada'), 'code' => '+1'),
'gp' => array('country' => t('Guadeloupe'), 'code' => '+590'),
'gu' => array('country' => t('Guam'), 'code' => '+1'),
'gt' => array('country' => t('Guatemala'), 'code' => '+502'),
'gn' => array('country' => t('Guinea'), 'code' => '+224'),
'gw' => array('country' => t('Guinea-Bissau'), 'code' => '+245'),
'gy' => array('country' => t('Guyana'), 'code' => '+592'),
'ht' => array('country' => t('Haiti'), 'code' => '+509'),
'hn' => array('country' => t('Honduras'), 'code' => '+504'),
'hk' => array('country' => t('Hong Kong'), 'code' => '+852'),
'hu' => array('country' => t('Hungary'), 'code' => '+36'),
'is' => array('country' => t('Iceland'), 'code' => '+354'),
'in' => array('country' => t('India'), 'code' => '+91'),
'id' => array('country' => t('Indonesia'), 'code' => '+62'),
'ir' => array('country' => t('Iran'), 'code' => '+98'),
'iq' => array('country' => t('Iraq'), 'code' => '+964'),
'ie' => array('country' => t('Ireland'), 'code' => '+353'),
'il' => array('country' => t('Israel'), 'code' => '+972'),
'it' => array('country' => t('Italy'), 'code' => '+39'),
'ci' => array('country' => t('Ivory Coast'), 'code' => '+225'),
'jm' => array('country' => t('Jamaica'), 'code' => '+1'),
'jp' => array('country' => t('Japan'), 'code' => '+81'),
'jo' => array('country' => t('Jordan'), 'code' => '+962'),
'kz' => array('country' => t('Kazakhstan'), 'code' => '+7'),
'ke' => array('country' => t('Kenya'), 'code' => '+254'),
'ki' => array('country' => t('Kiribati'), 'code' => '+686'),
'kw' => array('country' => t('Kuwait'), 'code' => '+965'),
'kg' => array('country' => t('Kyrgyzstan'), 'code' => '+996'),
'la' => array('country' => t('Laos'), 'code' => '+856'),
'lv' => array('country' => t('Latvia'), 'code' => '+371'),
'lb' => array('country' => t('Lebanon'), 'code' => '+961'),
'ls' => array('country' => t('Lesotho'), 'code' => '+266'),
'lr' => array('country' => t('Liberia'), 'code' => '+231'),
'ly' => array('country' => t('Libya'), 'code' => '+218'),
'li' => array('country' => t('Liechtenstein'), 'code' => '+423'),
'lt' => array('country' => t('Lithuania'), 'code' => '+370'),
'lu' => array('country' => t('Luxembourg'), 'code' => '+352'),
'mo' => array('country' => t('Macau'), 'code' => '+853'),
'mk' => array('country' => t('Macedonia'), 'code' => '+389'),
'mg' => array('country' => t('Madagascar'), 'code' => '+261'),
'mw' => array('country' => t('Malawi'), 'code' => '+265'),
'my' => array('country' => t('Malaysia'), 'code' => '+60'),
'mv' => array('country' => t('Maldives'), 'code' => '+960'),
'ml' => array('country' => t('Mali'), 'code' => '+223'),
'mt' => array('country' => t('Malta'), 'code' => '+356'),
'mh' => array('country' => t('Marshall Islands'), 'code' => '+692'),
'mq' => array('country' => t('Martinique'), 'code' => '+596'),
'mr' => array('country' => t('Mauritania'), 'code' => '+222'),
'mu' => array('country' => t('Mauritius'), 'code' => '+230'),
'yt' => array('country' => t('Mayotte'), 'code' => '+269'),
'mx' => array('country' => t('Mexico'), 'code' => '+52'),
'fm' => array('country' => t('Micronesia, Federated States of'), 'code' => '+691'),
'md' => array('country' => t('Moldova'), 'code' => '+373'),
'mc' => array('country' => t('Monaco'), 'code' => '+377'),
'mn' => array('country' => t('Mongolia'), 'code' => '+976'),
'me' => array('country' => t('Montenegro'), 'code' => '+382'),
'ms' => array('country' => t('Montserrat'), 'code' => '+1'),
'ma' => array('country' => t('Morocco'), 'code' => '+212'),
'mz' => array('country' => t('Mozambique'), 'code' => '+258'),
'mm' => array('country' => t('Myanmar'), 'code' => '+95'),
'na' => array('country' => t('Namibia'), 'code' => '+264'),
'nr' => array('country' => t('Nauru'), 'code' => '+674'),
'np' => array('country' => t('Nepal'), 'code' => '+977'),
'nl' => array('country' => t('Netherlands'), 'code' => '+31'),
'an' => array('country' => t('Netherlands Antilles'), 'code' => '+599'),
'nc' => array('country' => t('New Caledonia'), 'code' => '+687'),
'nz' => array('country' => t('New Zealand'), 'code' => '+64'),
'ni' => array('country' => t('Nicaragua'), 'code' => '+505'),
'ne' => array('country' => t('Niger'), 'code' => '+227'),
'ng' => array('country' => t('Nigeria'), 'code' => '+234'),
'nu' => array('country' => t('Niue'), 'code' => '+683'),
'nf' => array('country' => t('Norfolk Island'), 'code' => '+672'),
'kp' => array('country' => t('North Korea'), 'code' => '+850'),
'mp' => array('country' => t('Northern Mariana Islands'), 'code' => '+1'),
'no' => array('country' => t('Norway'), 'code' => '+47'),
'om' => array('country' => t('Oman'), 'code' => '+968'),
'pk' => array('country' => t('Pakistan'), 'code' => '+92'),
'pw' => array('country' => t('Palau'), 'code' => '+680'),
'ps' => array('country' => t('Palestine'), 'code' => '+970'),
'pa' => array('country' => t('Panama'), 'code' => '+507'),
'pg' => array('country' => t('Papua New Guinea'), 'code' => '+675'),
'py' => array('country' => t('Paraguay'), 'code' => '+595'),
'pe' => array('country' => t('Peru'), 'code' => '+51'),
'ph' => array('country' => t('Philippines'), 'code' => '+63'),
'pl' => array('country' => t('Poland'), 'code' => '+48'),
'pt' => array('country' => t('Portugal'), 'code' => '+351'),
'pr' => array('country' => t('Puerto Rico'), 'code' => '+1'),
'qa' => array('country' => t('Qatar'), 'code' => '+974'),
'ro' => array('country' => t('Romania'), 'code' => '+40'),
'ru' => array('country' => t('Russia'), 'code' => '+7'),
'rw' => array('country' => t('Rwanda'), 'code' => '+250'),
'sh' => array('country' => t('Saint Helena'), 'code' => '+290'),
'kn' => array('country' => t('Saint Kitts and Nevis'), 'code' => '+1'),
'lc' => array('country' => t('Saint Lucia'), 'code' => '+1'),
'pm' => array('country' => t('Saint Pierre and Miquelon'), 'code' => '+508'),
'vc' => array('country' => t('Saint Vincent and the Grenadines'), 'code' => '+1'),
'ws' => array('country' => t('Samoa'), 'code' => '+1'),
'sm' => array('country' => t('San Marino'), 'code' => '+378'),
'st' => array('country' => t('Sao Tome and Principe'), 'code' => '+239'),
'sa' => array('country' => t('Saudi Arabia'), 'code' => '+966'),
'sn' => array('country' => t('Senegal'), 'code' => '+221'),
'rs' => array('country' => t('Serbia'), 'code' => '+381'),
'sc' => array('country' => t('Seychelles'), 'code' => '+248'),
'sl' => array('country' => t('Sierra Leone'), 'code' => '+232'),
'sg' => array('country' => t('Singapore'), 'code' => '+65'),
'sk' => array('country' => t('Slovakia'), 'code' => '+421'),
'si' => array('country' => t('Slovenia'), 'code' => '+386'),
'sb' => array('country' => t('Solomon Islands'), 'code' => '+677'),
'so' => array('country' => t('Somalia'), 'code' => '+252'),
'za' => array('country' => t('South Africa'), 'code' => '+27'),
'kr' => array('country' => t('South Korea'), 'code' => '+82'),
'ss' => array('country' => t('South Sudan'), 'code' => '+211'),
'es' => array('country' => t('Spain'), 'code' => '+34'),
'lk' => array('country' => t('Sri Lanka'), 'code' => '+94'),
'sd' => array('country' => t('Sudan'), 'code' => '+249'),
'sr' => array('country' => t('Suriname'), 'code' => '+597'),
'sz' => array('country' => t('Swaziland'), 'code' => '+268'),
'se' => array('country' => t('Sweden'), 'code' => '+46'),
'ch' => array('country' => t('Switzerland'), 'code' => '+41'),
'sy' => array('country' => t('Syria'), 'code' => '+963'),
'tw' => array('country' => t('Taiwan'), 'code' => '+886'),
'tj' => array('country' => t('Tajikistan'), 'code' => '+992'),
'tz' => array('country' => t('Tanzania'), 'code' => '+255'),
'th' => array('country' => t('Thailand'), 'code' => '+66'),
'tg' => array('country' => t('Togo'), 'code' => '+228'),
'tk' => array('country' => t('Tokelau'), 'code' => '+690'),
'to' => array('country' => t('Tonga'), 'code' => '+676'),
'tt' => array('country' => t('Trinidad and Tobago'), 'code' => '+1'),
'tn' => array('country' => t('Tunisia'), 'code' => '+216'),
'tr' => array('country' => t('Turkey'), 'code' => '+90'),
'tm' => array('country' => t('Turkmenistan'), 'code' => '+993'),
'tc' => array('country' => t('Turks and Caicos Islands'), 'code' => '+1'),
'tv' => array('country' => t('Tuvalu'), 'code' => '+688'),
'ug' => array('country' => t('Uganda'), 'code' => '+256'),
'ua' => array('country' => t('Ukraine'), 'code' => '+380'),
'ae' => array('country' => t('United Arab Emirates'), 'code' => '+971'),
'gb' => array('country' => t('United Kingdom'), 'code' => '+44'),
'us' => array('country' => t('United States'), 'code' => '+1'),
'uy' => array('country' => t('Uruguay'), 'code' => '+598'),
'vi' => array('country' => t('US Virgin Islands'), 'code' => '+1'),
'uz' => array('country' => t('Uzbekistan'), 'code' => '+998'),
'vu' => array('country' => t('Vanuatu'), 'code' => '+678'),
'va' => array('country' => t('Vatican City'), 'code' => '+39'),
've' => array('country' => t('Venezuela'), 'code' => '+58'),
'vn' => array('country' => t('Vietnam'), 'code' => '+84'),
'wf' => array('country' => t('Wallis and Futuna'), 'code' => '+681'),
'ye' => array('country' => t('Yemen'), 'code' => '+967'),
'zm' => array('country' => t('Zambia'), 'code' => '+260'),
'zw' => array('country' => t('Zimbabwe'), 'code' => '+263'),
);
}
if (is_null($cc)) {
return $country_code;
}
elseif (isset($country_code[$cc])) {
return $country_code[$cc];
}
return FALSE;
}

View File

@@ -0,0 +1,165 @@
<?php
/**
* @file
* Phone Number custom country API
*
* 'CC' will be used throughout this document to indicate country code
* abbreviation. You should replace it with the correct country code
* in the following functions name. For full list of country code
* abbreviation, refer to the 2 alphabet list in the countries.txt.
*/
/**
* Country field settings.
* Country can provide additional field settings for the phone number
* field as needed.
*
* @param $op
* The operation to be performed. Possible values:
* - "form": Display the field settings form.
* - "validate": Check the field settings form for errors.
* - "save": Declare which fields to save back to the database.
* - "database columns": Declare the columns that content.module should create
* and manage on behalf of the field. If the field module wishes to handle
* its own database storage, this should be omitted.
* - "filters": Declare the Views filters available for the field.
* (this is used in CCK's default Views tables definition)
* They always apply to the first column listed in the "database columns"
* array.
* @param $field
* The field on which the operation is to be performed.
* @return
* This varies depending on the operation.
* - "form": an array of form elements to add to
* the settings page.
* - "validate": no return value. Use form_set_error().
* - "save": an array of names of form elements to
* be saved in the database.
* - "database columns": an array keyed by column name, with arrays of column
* information as values. This column information must include "type", the
* MySQL data type of the column, and may also include a "sortable" parameter
* to indicate to views.module that the column contains ordered information.
* TODO: Details of other information that can be passed to the database layer can
* be found in the API for the Schema API.
* - "filters": an array of 'filters' definitions as expected by views.module
* (see Views Documentation).
* When providing several filters, it is recommended to use the 'name'
* attribute in order to let the user distinguish between them. If no 'name'
* is specified for a filter, the key of the filter will be used instead.
*/
function CC_phone_field_settings($op, $field) {
switch ($op) {
// Country specific field settings
case 'form':
// ...
return $form;
// Country specific field validation
case 'validate':
// ...
break;
// Country specific field save
case 'save':
// ...
return $settings;
}
}
/**
* Validate country level phone number.
*
* @param $number
* Digits only value.
* @param $ext
* Digits only value.
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC (check_plain'ed)
* - "%phone_input": the original number input by user (check_plain'ed)
* - "%min_length": allowed minimum length of the phone number
* - "%max_length": allowed maximum length of the phone number
* - "%ext_input": the original extension input by user (check_plain'ed)
* - "%ext_max_length": allowed maximum length of the phone extension
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function CC_validate_number($number, $ext = '', &$error) {
// Don't need to check for extension because it has been checked by generic validation as all digits, unless has special format/requirements
// your validation
if (FALSE) {
// t() is no needed
$error = '"%phone_input" is not a valid North American phone number, it should be a 10 digit number like "999 999 9999"';
return FALSE;
}
return TRUE;
}
/**
* Cleanup user-entered values for a phone number field for saving to DB.
*
* @param $number
* A single phone number item.
*/
function CC_sanitize_number(&$number) {
// your cleanup like removing trunk prefix
}
/**
* Default formatter for international phone number.
*
* @param $element
* $element['#item']['country_codes']: alpha-2 country code
* $element['#item']['number']: phone number
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC (check_plain'ed)
* - "%phone_input": the original number input by user (check_plain'ed)
* - "%min_length": allowed minimum length of the phone number
* - "%max_length": allowed maximum length of the phone number
* - "%ext_input": the original extension input by user (check_plain'ed)
* - "%ext_max_length": allowed maximum length of the phone extension
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function CC_formatter_default($element) {
$item = $element['#item'];
// Display a global phone number with country code.
$cc = cck_phone_countrycodes($item['country_codes']);
// Format the phone number however you like, this is the default
$phone = $cc['code'] . '-' . $item['number'];
return $phone;
}
/**
* Local formatter for local phone number.
*
* @param $element
* $element['#item']['country_codes']: alpha-2 country code
* $element['#item']['number']: phone number
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function CC_formatter_local($element) {
// Display a local phone number without country code.
// Format the phone number however you like, this is the default
$phone = $element['#item']['number'];
return $phone;
}

View File

@@ -0,0 +1,241 @@
File an issue if we miss any country codes.
Use the CC in the custom country code includes
e.g.
- file name: phone.CC.inc
- function names: cck_phone_CC_settings_validate, cck_phone_CC_settings_save
CC Country name (+Code)
--------------------------
af - Afghanistan (+93)
al - Albania (+355)
dz - Algeria (+213)
as - American Samoa (+1)
ad - Andorra (+376)
ao - Angola (+244)
ai - Anguilla (+1)
ag - Antigua and Barbuda (+1)
ar - Argentina (+54)
am - Armenia (+374)
aw - Aruba (+297)
au - Australia (+61)
at - Austria (+43)
az - Azerbaijan (+994)
bs - Bahamas, The (+1)
bh - Bahrain (+973)
bd - Bangladesh (+880)
bb - Barbados (+1)
by - Belarus (+375)
be - Belgium (+32)
bz - Belize (+501)
bj - Benin (+229)
bm - Bermuda (+1)
bt - Bhutan (+975)
bo - Bolivia (+591)
ba - Bosnia and Herzegovina (+387)
bw - Botswana (+267)
br - Brazil (+55)
io - British Indian Ocean Territory (+246)
vg - British Virgin Islands (+1)
bn - Brunei (+673)
bg - Bulgaria (+359)
bf - Burkina Faso (+226)
bi - Burundi (+257)
kh - Cambodia (+855)
cm - Cameroon (+237)
ca - Canada (+1)
cv - Cape Verde (+238)
ky - Cayman Islands (+1)
cf - Central African Republic (+236)
td - Chad (+235)
cl - Chile (+56)
cn - China (+86)
cx - Christmas Island (+61)
cc - Cocos-Keeling Islands (+61)
co - Colombia (+57)
km - Comoros (+269)
cg - Congo, Republic of the (+242)
cd - Congo, Democratic Republic of (+243)
ck - Cook Islands (+682)
cr - Costa Rica (+506)
hr - Croatia (+385)
cu - Cuba (+53)
cy - Cyprus (+357)
cz - Czech Republic (+420)
dk - Denmark (+45)
dj - Djibouti (+253)
dm - Dominica (+1)
do - Dominican Republic (+1)
tp - East Timor (+670)
ec - Ecuador (+593)
eg - Egypt (+20)
sv - El Salvador (+503)
gq - Equatorial Guinea (+240)
er - Eritrea (+291)
ee - Estonia (+372)
et - Ethiopia (+251)
fk - Falkland Islands (+500)
fo - Faroe Islands (+298)
fj - Fiji (+679)
fi - Finland (+358)
fr - France (+33)
gf - French Guiana (+594)
pf - French Polynesia (+689)
ga - Gabon (+241)
gm - Gambia, The (+220)
ge - Georgia (+995)
de - Germany (+49)
gh - Ghana (+233)
gi - Gibraltar (+350)
gr - Greece (+30)
gl - Greenland (+299)
gd - Grenada (+1)
gp - Guadeloupe (+590)
gu - Guam (+1)
gt - Guatemala (+502)
gn - Guinea (+224)
gw - Guinea-Bissau (+245)
gy - Guyana (+592)
ht - Haiti (+509)
hn - Honduras (+504)
hk - Hong Kong (+852)
hu - Hungary (+36)
is - Iceland (+354)
in - India (+91)
id - Indonesia (+62)
ir - Iran (+98)
iq - Iraq (+964)
ie - Ireland (+353)
il - Israel (+972)
it - Italy (+39)
ci - Ivory Coast (+225)
jm - Jamaica (+1)
jp - Japan (+81)
jo - Jordan (+962)
kz - Kazakhstan (+7)
ke - Kenya (+254)
ki - Kiribati (+686)
kw - Kuwait (+965)
kg - Kyrgyzstan (+996)
la - Laos (+856)
lv - Latvia (+371)
lb - Lebanon (+961)
ls - Lesotho (+266)
lr - Liberia (+231)
ly - Libya (+218)
li - Liechtenstein (+423)
lt - Lithuania (+370)
lu - Luxembourg (+352)
mo - Macau (+853)
mk - Macedonia (+389)
mg - Madagascar (+261)
mw - Malawi (+265)
my - Malaysia (+60)
mv - Maldives (+960)
ml - Mali (+223)
mt - Malta (+356)
mh - Marshall Islands (+692)
mq - Martinique (+596)
mr - Mauritania (+222)
mu - Mauritius (+230)
yt - Mayotte (+269)
mx - Mexico (+52)
fm - Micronesia, Federated States of (+691)
md - Moldova (+373)
mc - Monaco (+377)
mn - Mongolia (+976)
me - Montenegro (+382)
ms - Montserrat (+1)
ma - Morocco (+212)
mz - Mozambique (+258)
mm - Myanmar (+95)
na - Namibia (+264)
nr - Nauru (+674)
np - Nepal (+977)
nl - Netherlands (+31)
an - Netherlands Antilles (+599)
nc - New Caledonia (+687)
nz - New Zealand (+64)
ni - Nicaragua (+505)
ne - Niger (+227)
ng - Nigeria (+234)
nu - Niue (+683)
nf - Norfolk Island (+672)
kp - North Korea (+850)
mp - Northern Mariana Islands (+1)
no - Norway (+47)
om - Oman (+968)
pk - Pakistan (+92)
pw - Palau (+680)
ps - Palestine (+970)
pa - Panama (+507)
pg - Papua New Guinea (+675)
py - Paraguay (+595)
pe - Peru (+51)
ph - Philippines (+63)
pl - Poland (+48)
pt - Portugal (+351)
pr - Puerto Rico (+1)
qa - Qatar (+974)
ro - Romania (+40)
ru - Russia (+7)
rw - Rwanda (+250)
sh - Saint Helena (+290)
kn - Saint Kitts and Nevis (+1)
lc - Saint Lucia (+1)
pm - Saint Pierre and Miquelon (+508)
vc - Saint Vincent and the Grenadines (+1)
ws - Samoa (+1)
sm - San Marino (+378)
st - Sao Tome and Principe (+239)
sa - Saudi Arabia (+966)
sn - Senegal (+221)
rs - Serbia (+381)
sc - Seychelles (+248)
sl - Sierra Leone (+232)
sg - Singapore (+65)
sk - Slovakia (+421)
si - Slovenia (+386)
sb - Solomon Islands (+677)
so - Somalia (+252)
za - South Africa (+27)
kr - South Korea (+82)
ss - South Sudan (+211)
es - Spain (+34)
lk - Sri Lanka (+94)
sd - Sudan (+249)
sr - Suriname (+597)
sz - Swaziland (+268)
se - Sweden (+46)
ch - Switzerland (+41)
sy - Syria (+963)
tw - Taiwan (+886)
tj - Tajikistan (+992)
tz - Tanzania (+255)
th - Thailand (+66)
tg - Togo (+228)
tk - Tokelau (+690)
to - Tonga (+676)
tt - Trinidad and Tobago (+1)
tn - Tunisia (+216)
tr - Turkey (+90)
tm - Turkmenistan (+993)
tc - Turks and Caicos Islands (+1)
tv - Tuvalu (+688)
ug - Uganda (+256)
ua - Ukraine (+380)
ae - United Arab Emirates (+971)
gb - United Kingdom (+44)
us - United States (+1)
uy - Uruguay (+598)
vi - US Virgin Islands (+1)
uz - Uzbekistan (+998)
vu - Vanuatu (+678)
va - Vatican City (+39)
ve - Venezuela (+58)
vn - Vietnam (+84)
wf - Wallis and Futuna (+681)
ye - Yemen (+967)
zm - Zambia (+260)
zw - Zimbabwe (+263)

View File

@@ -0,0 +1,173 @@
<?php
/**
* @file
* CCK Field for Australia phone numbers.
*/
/**
* Patterns generated from Telecommunications Numbering Plan 1997
* http://www.comlaw.gov.au/comlaw/management.nsf/lookupindexpagesbyid/IP200506356?OpenDocument
*/
function _cck_phone_au_number_patterns() {
$valid_patterns = array(
// Geographic numbers (10 digits) - Central East Region (NSW, ACT, Northern VIC).
'/^02[4,6-9][0-9]{7}$/',
'/^023[3,8][0-9]{6}$/',
'/^025[0-3,5-9][0-9]{6}$/',
// Geographic numbers (10 digits) - South East Region (TAS, Southern NSW, VIC).
'/^03[5,7-9][0-9]{7}$/',
'/^034[0-5,7-9][0-9]{6}$/',
'/^036[1-5,7][0-9]{6}$/',
// Geographic numbers (10 digits) - North East Region (QLD).
'/^07[2-4][0-9]{7}$/',
'/^075[2-7][0-9]{6}$/',
'/^0776[0-9]{6}$/',
// Geographic numbers (10 digits) - Central and West Region (WA, SA, NT and Western NSW).
'/^08[7-9][0-9]{7}$/',
'/^085[1-4][0-9]{6}$/',
'/^086[0-8][0-9]{6}$/',
// Mobile numbers (10 digits).
'/^04[0-9]{8}$/',
// Local rate special numbers (10 digits).
'/^130[0-9]{7}$/',
// Local rate special numbers (6 digits).
'/^13[1-3,5-9][0-9]{3}$/',
'/^134[0-4,6-9][0-9]{2}$/',
// Free phone numbers (10 digits).
'/^180[0-1][0-9]{6}$/',
// Satelite telephone numbers (10 digits).
'/^014[1-3,5,7][0-9]{6}$/',
// Satelite telephone numbers (9 digits).
'/^014[0,4,6,8-9][0-9]{5}$/',
'/^015[0-9]{6}$/',
'/^017[1,2,8,9][0-9]{5}$/',
'/^018[0-9]{6}$/',
);
$invalid_patterns = array(
// Invalid geographic phone numbers.
'/^0[2,3,7]5550[0-9]{4}$/',
);
$format_search_patterns = array(
// Geographic numbers (10 digits).
'/^(0)([2,3,7,8])([0-9]{4})([0-9]{4})$/',
// Mobile numbers (10 digits).
'/^(0)(4[0-9]{2})([0-9]{3})([0-9]{3})$/',
// 1300 numbers, 1800 numbers (10 digits).
'/^(1[3,8][0-9]{2})([0-9]{3})([0-9]{3})$/',
// 13 numbers (6 digits).
'/^(13)([0-9]{2})([0-9]{2})$/',
// Satelite telephone numbers (10 digits).
'/^(0)(14)([0-9])([0-9]{3})([0-9]{3})$/',
// Satelite telephone numbers (9 digits).
'/^(0)(1[4,5,7,8])([0-9]{3})([0-9]{3})$/',
);
$format_replace_patterns = array(
// Geographic numbers (10 digits).
'($1$2) $3 $4',
// Mobile numbers (10 digits).
'$1$2 $3 $4',
// 1300 numbers, 1800 numbers (10 digits).
'$1 $2 $3',
// 13 numbers (6 digits).
'$1 $2 $3',
// Satelite telephone numbers (10 digits).
'$1$2 $3 $4 $5',
// Satelite telephone numbers (9 digits).
'$1$2 $3 $4',
);
$format_replace_patterns_international = array(
// Geographic numbers (10 digits).
'$2 $3 $4',
// Mobile numbers (10 digits).
'$2 $3 $4',
// 1300 numbers, 1800 numbers (10 digits).
'$1 $2 $3',
// 13 numbers (6 digits).
'$1 $2 $3',
// Satelite telephone numbers (10 digits).
'$2 $3 $4 $5',
// Satelite telephone numbers (9 digits).
'$2 $3 $4',
);
return array($valid_patterns, $invalid_patterns, $format_search_patterns, $format_replace_patterns, $format_replace_patterns_international);
}
/**
* Verifies that $number is a valid Australian phone number.
*/
function au_validate_number($number, $ext = '', &$error) {
// Don't need to check for extension because it has been checked by generic validation as all digits, unless has special format/requirements
// We don't want to worry about separators
$number = cck_phone_clean_number($number);
if (empty($number)) {
return TRUE;
}
list($valid_patterns, $invalid_patterns, $format_search_patterns, $format_replace_patterns, $format_replace_patterns_international) = _cck_phone_au_number_patterns();
$invalid_number = preg_replace($invalid_patterns, '', $number);
$correct_number = preg_replace($valid_patterns, '', $number);
if (empty($invalid_number)) {
$error = 'The phone number you have entered is classified as unusable by the Australian telecommunications authority.';
return FALSE;
}
elseif (!empty($correct_number)) {
$error = 'You have not entered a valid australian phone number. Please enter a 10 digit phone number including the area code, but not including the 61 international prefix. Valid 1800, 1300, and 13 numbers are accepted, as are satilite and AMPS numbers.';
return FALSE;
}
return TRUE;
}
/**
* Default formatter for Australian phone number.
*/
function au_formatter_default($element) {
// Display a global phone number with country code.
$cc = cck_phone_countrycodes($element['country_codes']);
list($valid_patterns, $invalid_patterns, $format_search_patterns, $format_replace_patterns, $format_replace_patterns_international) = _cck_phone_au_number_patterns();
return $cc['code'] .' '. preg_replace($format_search_patterns, $format_replace_patterns_international, $element['number']);
}
/**
* Local formatter for local Australian phone number.
*/
function au_formatter_local($element) {
list($valid_patterns, $invalid_patterns, $format_search_patterns, $format_replace_patterns, $format_replace_patterns_international) = _cck_phone_au_number_patterns();
return preg_replace($format_search_patterns, $format_replace_patterns, $element['number']);
}

View File

@@ -0,0 +1,74 @@
<?php
/**
* @file
* CCK Field for Canada phone numbers.
*/
/**
* Verifies that $number is a valid ten-digit North American phone number.
*
* @param $number
* Digits only value.
* @param $ext
* Digits only value.
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function ca_validate_number($number, $ext = '', &$error) {
return us_validate_number($number, $ext = '', $error);
}
/**
* Cleanup user-entered values for a phone number field for saving to DB.
*
* @param $number
* A single phone number item.
*/
function ca_sanitize_number(&$number) {
us_sanitize_number($number);
}
/**
* Default formatter for international phone number.
*
* @param $element
* $element['#item']['country_codes']: alpha-2 country code
* $element['#item']['number']: phone number
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function ca_formatter_default($element) {
return us_formatter_default($element);
}
/**
* Local formatter for local phone number.
*
* @param $element
* $element['#item']['country_codes']: alpha-2 country code
* $element['#item']['number']: phone number
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function ca_formatter_local($element) {
return us_formatter_local($element);
}

View File

@@ -0,0 +1,127 @@
<?php
/**
* @file
* CCK Field for United Kingdom phone numbers.
*/
function _uk_phone_rules() {
// TODO: more detailed check by area codes
return '/^
0*(2[03489])(\d{4})(\d{4}) # 02x [eight-digit local number]
|
0*(11[3-8])(\d{3})(\d{4}) # 011x [seven-digit local number]
|
0*(1[2-9]1)(\d{3})(\d{4}) # 01x1 [seven-digit local number]
|
0*(1[2-9][0|2-9]\d)(\d{5,6}) # 01xxx [mostly six-digit local numbers] (but not 01x1 codes)
$/x';
}
/**
* Validate country level phone number.
*
* @param $number
* Digits only value.
* @param $ext
* Digits only value.
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function gb_validate_number($number, $ext = '', &$error) {
// We don't want to worry about separators
$number = cck_phone_clean_number($number);
if (preg_match(_uk_phone_rules(), $number)) {
return TRUE;
}
// t() is not needed
$error = '"%phone_input" is not a valid United Kingdom phone number, it should be a 10 digit number like "99 9999 9999", with optional leading "0"';
return FALSE;
}
/**
* Cleanup user-entered values for a phone number field for saving to DB.
*
* @param $number
* A single phone number item.
*/
function gb_sanitize_number(&$number) {
// Remove trunk prefix '0'
$number = preg_replace('/^([0]*)/', '', $number);
}
/**
* Default formatter for international phone number.
*
* @param $element
* $element['#item']['country_codes']: alpha-2 country code
* $element['#item']['number']: phone number
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function gb_formatter_default($element) {
// Display a global phone number with country code.
$phone = '';
$number = $element['number'];
if ($number) {
$cc = cck_phone_countrycodes($element['country_codes']);
if (preg_match(_uk_phone_rules(), $number, $matches)) {
// output as +44 AA BBBB CCCC, +44 AAA BBB CCCC or +44 AAAA BBB CCC
array_shift($matches);
$phone = $cc['code'] . ' ' . implode(' ', $matches);
}
else {
$phone = "$cc[code] $number";
}
}
return $phone;
}
/**
* Local formatter for local phone number.
*
* @param $element
* $element['#item']['country_codes']: alpha-2 country code
* $element['#item']['number']: phone number
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function gb_formatter_local($element) {
// Display a local phone number without country code.
$phone = '';
$number = $element['number'];
if ($number) {
if (preg_match(_uk_phone_rules(), $number, $matches)) {
// output as 0AA BBBB CCCC, 0AAA BBB CCCC or 0AAAA BBB CCC
array_shift($matches);
$phone = '0' . implode(' ', $matches);
}
else {
$phone = "0$number";
}
}
return $phone;
}

View File

@@ -0,0 +1,165 @@
<?php
/**
* @file
* CCK Field for Hungarian phone numbers.
*/
/**
* Verifies and mage parts from $number
*
* @param $number
* Digits only value.
* @return boolean
* Array
* if array['error'] is present, the validation fails
* else ['area'] for area number
* ['first'] ['second'] for the AA/BBB-CCC numbers
* ['first'] ['second'] ['third'] for the A(A)/BBB-CC-DD numbers
*/
function hu_parts($number) {
/**
* Because Hungary an telephone numbers are both in open and closed number region, the area code length may be vary. If the area code is 1, 7 more digits follow it.
* If the area code is 20/30/70, also 7 more digits.
* If any other 2 digits are code is appear, we want 6 more digits.
*
* So complecated, I know :)
*
*/
$result = array();
// Last 9 digits
$n = drupal_substr($number, -9);
if ($n === FALSE || (drupal_substr($n, 0, 1) != 2 && drupal_substr($n, 0, 1) != 3 && drupal_substr($n, 0, 1) != 7)) {
// No 9 digit, check for 8 digit
if (drupal_substr($number, -8) === FALSE) {
// No 8 digit, too short for valid telephone number
$result['error'] = '"%phone_input" is not a valid Hungarian phone number, it should be a 9 digit number like "20/123-45-67" or a 8 digit number like "23/123-456"';
}
else {
$n = drupal_substr($number, -8);
// It is 8 digits
if (drupal_substr($n, 0, 1) == 1) {
// A number from Budapest
$result['area'] = drupal_substr($n, 0, 1);
$result['first'] = drupal_substr($n, 1, 3);
$result['second'] = drupal_substr($n, 4, 2);
$result['third'] = drupal_substr($n, 6, 2);
}
else {
// A number from countryside
$result['area'] = drupal_substr($n, 0, 2);
$result['first'] = drupal_substr($n, 2, 3);
$result['second'] = drupal_substr($n, 5, 3);
}
}
}
else {
// Cell phone number
$result['area'] = drupal_substr($n, 0, 2);
$result['first'] = drupal_substr($n, 2, 3);
$result['second'] = drupal_substr($n, 5, 2);
$result['third'] = drupal_substr($n, 7, 2);
}
return $result;
}
/**
* Verifies that $number is a valid nine digit Hungarian phone number.
*
* @param $number
* Digits only value.
* @param $ext
* Digits only value.
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function hu_validate_number($number, $ext = '', &$error) {
// Don't need to check for extension because it has been checked by generic validation as all digits, unless has special format/requirements
// We don't want to worry about separators
$number = cck_phone_clean_number($number);
$result = hu_parts($number);
if (isset($result['error'])) {
// t() is no needed
$error = $result['error'];
return FALSE;
}
return true;
}
/**
* Cleanup user-entered values for a phone number field for saving to DB.
*
* @param $number
* A single phone number item.
*/
function hu_sanitize_number(&$number) {
// Remove prefix '36' or '+36'
$number = preg_replace('/^(?:\+?36|06*)/', '', $number);
}
/**
* Default formatter for international phone number.
*
* @param $element
* $element['country_codes']: alpha-2 country code
* $element['number']: phone number
* $element['extension']: ext
* @return boolean
* STRING The formatted number
*/
function hu_formatter_default($element) {
$number = $element['number'];
$ext = $element['extension'];
$result = hu_parts($number);
if (isset($result['error'])) {
return $number . ($ext ? '/' . $ext : '');
}
// output as +36 (AA) BBB CCDD/ext or +36 (AA) BBB CCC/ext
$phone = '+36 (' . $result['area'] . ') ' . $result['first'] . ' ' . $result['second'] . $result['third'];
return $phone . ($ext ? '/' . $ext : '');
}
/**
* Local formatter for local phone number.
*
* @param $element
* $element['country_codes']: alpha-2 country code
* $element['number']: phone number
* $element['extension']: ext
* @return boolean
* STRING The formatted number
*/
function hu_formatter_local($element) {
$number = $element['number'];
$ext = $element['extension'];
$result = hu_parts($number);
if (isset($result['error'])) {
return $number . ($ext ? '/' . $ext : '');
}
// output as AA/BBB-CC-DD or AA/BBB-CCC
$phone = $result['area'] . '/' . $result['first'] . '-' . $result['second'] . (isset($result['third']) ? '-' . $result['third'] : '');
return $phone . ($ext ? '/' . $ext : '');
}

View File

@@ -0,0 +1,178 @@
<?php
/**
* @file
* CCK Field for Malaysia phone numbers.
*/
function _my_phone_rules() {
$rules = array();
// rule: 'area code, min length, max length'
// Geographic land line number
$rules[] = array("2", 7); // Domestic access code to Singapore
$rules[] = array("3", 8); // Selangor & Federal Territories of Kuala Lumpur & Putrajaya
$rules[] = array("4", 7); // Kedah, Penang & Perlis
$rules[] = array("5", 7); // Perak & Cameron Highlands (Pahang)
$rules[] = array("6", 7); // Melaka, Negeri Sembilan & Muar (Johor)
$rules[] = array("7", 7); // Johor (except Muar)
$rules[] = array("80", 6); // Domestic access code to Brunei (East Malaysia only)
// $rules[] = array("81", 6); // reserved
$rules[] = array("82", 6); // Kuching (Sarawak)
$rules[] = array("83", 6); // Sri Aman (Sarawak)
$rules[] = array("84", 6); // Sarikei, Bintangor, Sibu, Kanowit, Song, & Kapit (Sarawak)
$rules[] = array("85", 6); // Lawas, Limbang, Miri (Sarawak)
$rules[] = array("86", 6); // Bintulu, Belaga (Sarawak)
$rules[] = array("87", 6); // Inner District (Sabah) & Federal Territory of Labuan
$rules[] = array("88", 6); // Kota Kinabalu, Kudat (Sabah)
$rules[] = array("89", 6); // Lahad Datu, Sandakan, Tawau (Sabah)
$rules[] = array("9", 7); // Kelantan, Pahang (except Cameron Highlands) & Terengganu
// Mobile number structure
$rules[] = array("10", 7);
$rules[] = array("11", 7);
$rules[] = array("12", 7);
$rules[] = array("13", 7);
$rules[] = array("14", 7);
$rules[] = array("15", 7);
$rules[] = array("16", 7);
$rules[] = array("17", 7);
$rules[] = array("18", 7);
$rules[] = array("19", 7);
return $rules;
}
/**
* Verifies that $number is a valid Malaysia phone number.
*
* @param $number
* Digits only value.
* @param $ext
* Digits only value.
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function my_validate_number($number, $ext = '', &$error) {
// We don't want to worry about separators
$number = cck_phone_clean_number($number);
foreach (_my_phone_rules() as $rule) {
// define regular expression
$regex = '/^
([0]*) # an optional 0
(' . $rule[0] . ') # area code
\d{' . $rule[1] . '} # local number within length $rule[1] & $rule[2]
$/x';
$result = preg_match($regex, $number, $matches);
if ($result) {
return TRUE;
}
}
// t() is no needed
$error = '"%phone_input" is not a valid Malaysia phone number, it should be a 9-10 digit number like "03-2222 2222", "0" is optional and will be removed.';
return FALSE;
}
/**
* Cleanup user-entered values for a phone number field for saving to DB.
*
* @param $number
* A single phone number item.
*/
function my_sanitize_number(&$number) {
// Remove trunk prefix '0'
$number = preg_replace('/^([0]*)/', '', $number);
}
/**
* Default formatter for international phone number.
*
* @param $element
* $element['#item']['country_codes']: alpha-2 country code
* $element['#item']['number']: phone number
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function my_formatter_default($element) {
$phone = '';
// Display a global phone number with country code.
$cc = cck_phone_countrycodes($element['country_codes']);
// Format the phone number however you like, this is the default
foreach (_my_phone_rules() as $rule) {
// define regular expression
$regex = '/^
(' . $rule[0] . ') # area code
(\d{3,4})
(\d{4})
$/x';
$result = preg_match($regex, $element['number'], $matches);
if ($result) {
// output as +60A-BBB CCCC or +60A-BBBB CCCC
$phone = $cc['code'] . $matches[1] . '-' . $matches[2] . ' ' . $matches[3];
continue;
}
}
return $phone;
}
/**
* Local formatter for local phone number.
*
* @param $element
* $element['#item']['country_codes']: alpha-2 country code
* $element['#item']['number']: phone number
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function my_formatter_local($element) {
// Display a local phone number without country code.
$phone = $element['number'];
foreach (_my_phone_rules() as $rule) {
// define regular expression
$regex = '/^
(' . $rule[0] . ') # area code
(\d{3,4})
(\d{4})
$/x';
$result = preg_match($regex, $phone, $matches);
if ($result) {
// output as 0A-BBB CCCC or 0A-BBBB CCCC
$phone = '0' . $matches[1] . '-' . $matches[2] . ' ' . $matches[3];
continue;
}
}
return $phone;
}

View File

@@ -0,0 +1,69 @@
<?php
/**
* @file
* CCK Field for Polish phone numbers.
*/
/**
* Polish phone number regex helper.
*/
function _pl_regex() {
return '/^
([1-9]\d) # 2-digit area code
([1-9]\d{2}) # 3-digit phone prefix (cannot start with 0)
(\d{4})
$/x';
}
/**
* Verifies that $number is a valid 9-digit Polish phone number.
*/
function pl_validate_number($number, $ext = '', &$error) {
$number = cck_phone_clean_number($number);
$regex = _pl_regex();
$result = preg_match($regex, $number, $matches);
if ($result) {
return TRUE;
}
else {
$error = '"%phone_input" is not a valid Polish phone number, it should be a 9-digit
number like "99 999 99 99".';
return FALSE;
}
}
/**
* Helper for formatting output.
* Output as +48 (AA) BBB CCCC
*/
function _pl_formatter($element) {
$phone = '';
$regex = _pl_regex();
$result = preg_match($regex, $element['number'], $matches);
if ($result) {
$phone = '(' . $matches[1] . ') ' . $matches[2] . ' ' . $matches[3];
}
return $phone;
}
/**
* Default formatter for international phone number.
*/
function pl_formatter_default($element) {
$cc = cck_phone_countrycodes($element['country_codes']);
$number = _pl_formatter($element);
return $cc['code'] . ' ' . $number;
}
/**
* Local formatter for local phone number.
*/
function pl_formatter_local($element) {
return _pl_formatter($element);
}

View File

@@ -0,0 +1,139 @@
<?php
/**
* @file
* CCK Field for North American phone numbers.
*/
/**
* Verifies that $number is a valid ten-digit North American phone number.
*
* @param $number
* Digits only value.
* @param $ext
* Digits only value.
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function us_validate_number($number, $ext = '', &$error) {
// Don't need to check for extension because it has been checked by generic validation as all digits, unless has special format/requirements
// We don't want to worry about separators
$number = cck_phone_clean_number($number);
// define regular expression
$regex = '/^
([1]*) # an optional 1
[2-9][0-8]\d # area code (Allowed range of [2-9] for the first digit, [0-8] for the second, and [0-9] for the third digit)
[2-9]\d{2} # 3-digit prefix (cannot start with 0 or 1)
\d{4} # 4-digit line number
$/x';
$result = preg_match($regex, $number, $matches);
if ($result && $matches[1] == '') {
return TRUE;
}
elseif ($result && $matches[1] == '1') {
// t() is no needed
$error = 'Please enter a 10 digit North American phone number like "999 999 9999", without the country code "1" or "+1"';
return FALSE;
}
else {
// t() is no needed
$error = '"%phone_input" is not a valid North American phone number, it should be a 10 digit number like "999 999 9999"';
return FALSE;
}
}
/**
* Cleanup user-entered values for a phone number field for saving to DB.
*
* @param $number
* A single phone number item.
*/
function us_sanitize_number(&$number) {
// Remove prefix '1'
$number = preg_replace('/^([1]*)/', '', $number);
}
/**
* Default formatter for international phone number.
*
* @param $element
* $element['country_codes']: alpha-2 country code
* $element['number']: phone number
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function us_formatter_default($element) {
$phone = '';
// Display a global phone number with country code.
$cc = cck_phone_countrycodes($element['country_codes']);
// Format the phone number however you like, this is the default
// define regular expression
$regex = '/^
([2-9][0-8]\d) # area code (Allowed range of [2-9] for the first digit, [0-8] for the second, and [0-9] for the third digit)
([2-9]\d{2}) # 3-digit prefix (cannot start with 0 or 1)
(\d{4}) # 4-digit line number
/x';
$result = preg_match($regex, $element['number'], $matches);
if ($result) {
// output as +1 (AAA) BBB-CCCC
$phone = $cc['code'] . ' (' . $matches[1] . ') ' . $matches[2] . '-' . $matches[3];
}
return $phone;
}
/**
* Local formatter for local phone number.
*
* @param $element
* $element['country_codes']: alpha-2 country code
* $element['number']: phone number
* @param $error
* The error message to shown to user.
* Available parameters to use in the error message are
* - "%countrycode": the alpha-2 CC
* - "%phone_input": the original number input by user (could be invalid)
* - "%max_length": allowed maximum length of the phone number
* @return boolean
* TRUE if it is a valid phone number for that country, FALSE otherwise.
*/
function us_formatter_local($element) {
$phone = '';
// Display a local phone number without country code.
$regex = '/^
([2-9][0-8]\d) # area code (Allowed range of [2-9] for the first digit, [0-8] for the second, and [0-9] for the third digit)
([2-9]\d{2}) # 3-digit prefix (cannot start with 0 or 1)
(\d{4}) # 4-digit line number
/x';
$result = preg_match($regex, $element['number'], $matches);
if ($result) {
// output as (AAA) BBB CCCC
$phone = '(' . $matches[1] . ') ' . $matches[2] . '-' . $matches[3];
}
return $phone;
}