diff --git a/sites/all/modules/contrib/backup_migrate/LICENSE.txt b/sites/all/modules/contrib/backup_migrate/LICENSE.txt new file mode 100644 index 0000000..d159169 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/LICENSE.txt @@ -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. + + + Copyright (C) + + 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. + + , 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. diff --git a/sites/all/modules/contrib/backup_migrate/README.txt b/sites/all/modules/contrib/backup_migrate/README.txt new file mode 100644 index 0000000..9fc089b --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/README.txt @@ -0,0 +1,89 @@ + +------------------------------------------------------------------------------- +Backup and Migrate 3 for Drupal 7.x + by Ronan Dowling, Gorton Studios - ronan (at) gortonstudios (dot) com +------------------------------------------------------------------------------- + +DESCRIPTION: +This module makes the task of backing up your Drupal database, code and uploaded +files and of migrating data from one Drupal install to another easier. + +Database backup files are a list of SQL statements which can be executed with a +tool such as phpMyAdmin or the command-line mysql client. File and code +backup files are tarballs which can be restored by extracting them to the +desired directory. + +------------------------------------------------------------------------------- + +INSTALLATION: +* Put the module in your Drupal modules directory and enable it in + admin/modules. +* Go to admin/people/permissions and grant permission to any roles that need to be + able to backup or restore the database. +* Configure and use the module at admin/config/system/backup_migrate + +OPTIONAL: +* Enable token.module to allow token replacement in backup file names. +* To Backup to Amazon S3: + - Download the S3 library from http://undesigned.org.za/2007/10/22/amazon-s3-php-class + and place the file 'S3.php' in the includes directory in this module. + The stable version (0.4.0 – 20th Jul 2009) works best with Backup and Migrate. + +LIGHTTPD USERS: +Add the following code to your lighttp.conf to secure your backup directories: + $HTTP["url"] =~ "^/sites/default/files/backup_migrate/" { + url.access-deny = ( "" ) + } +You may need to adjust the path to reflect the actual path to the files. + +IIS 7 USERS: +Add the following code to your web.config code to secure your backup directories: + + + + +You may need to adjust the path to reflect the actual path to the files. + +------------------------------------------------------------------------------- + +VERY IMPORTANT SECURITY NOTE: +Backup files may contain sensitive data and by default, are saved to your web +server in a directory normally accessible by the public. This could lead to a +very serious security vulnerability. Backup and Migrate attempts to protect +backup files using a .htaccess file, but this is not guaranteed to work on all +environments (and is guaranteed to fail on web servers that are not apache). You +should test to see if your backup files are publicly accessible, and if in doubt +do not save backups to the server, or use the destinations feature to save to a +folder outside of your webroot. + +OTHER WARNINGS: +A failed restore can destroy your database and therefore your entire Drupal +installation. ALWAYS TEST BACKUP FILES ON A TEST ENVIRONMENT FIRST. If in doubt +do not use this module. + +This module has only been tested with MySQL and does not work with any other dbms. +If you have experiences with Postgres or any other dbms and are willing to help +test and modify the module to work with it, please contact the developer at +ronan (at) gortonstudios (dot) com. + +Make sure your php timeout is set high enough to complete a backup or restore +operation. Larger databases require more time. Also, while the module attempts +to keep memory needs to a minimum, a backup or restore will require +significantly more memory then most Drupal operations. + +If your backup file contains the 'sessions' table all other users will be logged +out after you run a restore. To avoid this, exclude the sessions table when +creating your backups. Be aware though that you will need to recreate the +sessions table if you use this backup on an empty database. + +Do not change the file extension of backup files or the restore function will be +unable to determine the compression type the file and will not function +correctly. + +IF A RESTORE FAILS: +Don't panic, the restore file should work with phpMyAdmin's import function, or +with the mysql command line tool. If it does not, then it is likely corrupt; you +may panic now. MAKE SURE THAT THIS MODULE IS NOT YOUR ONLY FORM OF BACKUP. + +------------------------------------------------------------------------------- + diff --git a/sites/all/modules/contrib/backup_migrate/backup_migrate.css b/sites/all/modules/contrib/backup_migrate/backup_migrate.css new file mode 100644 index 0000000..59554e7 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/backup_migrate.css @@ -0,0 +1,67 @@ +.schedule-list-disabled +{ + filter:alpha(opacity=50); + -moz-opacity: .50; + opacity: .50; +} + +.backup-migrate-cache-time { + font-size: 0.85em; +} + +.backup-migrate-tables-checkboxes .form-item { + width: 15em; + float: left; + overflow: hidden; + white-space: nowrap; + height: 1.75em; + margin: .25em .25em 0 0; +} +.backup-migrate-tables-checkboxes .form-item label input { + margin-right: .5em; +} + +div.backup-migrate-tables-checkboxes { + height: 20em; + overflow: auto; +} +div.backup-migrate-tags, +div.backup-migrate-description, +div.backup-migrate-date +{ + font-size: smaller; +} +span.backup-migrate-label { + font-weight: bold; +} +table.backup-migrate-listing td { + vertical-align: top; +} + +div.row-error { + color: #FF0000; + font-weight: bold; +} + +.backup-migrate-listing { + margin-bottom: 2em; +} +.backup-migrate-actions { + text-align: right; +} + +html.js #backup-migrate-ui-manual-quick-backup-form fieldset.collapsed, +.backup-migrate-inline { + margin-bottom: 1em; +} + +.backup-migrate-form-dependent { + padding-left: 2em; + padding-bottom: 1em; +} +.backup-migrate-form-dependent .form-item { + margin-top: 0; +} +.backup-migrate-footer-message { + margin-bottom: 1em; +} diff --git a/sites/all/modules/contrib/backup_migrate/backup_migrate.info b/sites/all/modules/contrib/backup_migrate/backup_migrate.info new file mode 100644 index 0000000..eb9258a --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/backup_migrate.info @@ -0,0 +1,16 @@ +name = Backup and Migrate +description = "Backup the Drupal database and files or migrate them to another environment." +core = 7.x + +files[] = includes/destinations.inc +files[] = includes/profiles.inc +files[] = includes/schedules.inc + +configure = admin/config/system/backup_migrate + +; Information added by Drupal.org packaging script on 2015-05-11 +version = "7.x-3.1" +core = "7.x" +project = "backup_migrate" +datestamp = "1431350582" + diff --git a/sites/all/modules/contrib/backup_migrate/backup_migrate.install b/sites/all/modules/contrib/backup_migrate/backup_migrate.install new file mode 100644 index 0000000..97820cb --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/backup_migrate.install @@ -0,0 +1,644 @@ + array( + 'key' => 'machine_name', + 'key name' => 'Profile ID', + 'admin_title' => 'name', + 'primary key' => 'profile_id', + 'identifier' => 'item', // Exports will be defined as $preset + 'default hook' => 'exportables_backup_migrate_profiles', // Function hook name. + 'api' => array( + 'owner' => 'backup_migrate', + 'api' => 'backup_migrate_exportables', // Base name for api include files. + 'minimum_version' => 1, + 'current_version' => 1, + ), + ), + 'fields' => array( + 'profile_id' => array( + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'description' => 'Primary ID field for the table. Not used for anything except internal lookups.', + 'no export' => TRUE, // Do not export database-only keys. + ), + 'machine_name' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '0', + 'description' => 'The primary identifier for a profile.', + ), + 'name' => array( + 'description' => 'The name of the profile.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE + ), + 'filename' => array( + 'description' => 'The name of the profile.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE + ), + 'append_timestamp' => array( + 'description' => 'Append a timestamp to the filename.', + 'type' => 'int', + 'size' => 'tiny', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0 + ), + 'timestamp_format' => array( + 'description' => 'The format of the timestamp.', + 'type' => 'varchar', + 'length' => 14, + 'not null' => TRUE + ), + 'filters' => array( + 'description' => 'The filter settings for the profile.', + 'type' => 'text', + 'not null' => TRUE, + 'serialize' => TRUE, + 'serialized default' => 'a:0:{}', + ), + ), + 'primary key' => array('profile_id'), + ); + $schema['backup_migrate_destinations'] = array( + 'export' => array( + 'key' => 'machine_name', + 'key name' => 'Destination ID', + 'admin_title' => 'name', + 'primary key' => 'destination_id', + 'identifier' => 'item', // Exports will be defined as $preset + 'default hook' => 'exportables_backup_migrate_destinations', // Function hook name. + 'api' => array( + 'owner' => 'backup_migrate', + 'api' => 'backup_migrate_exportables', // Base name for api include files. + 'minimum_version' => 1, + 'current_version' => 1, + ), + ), + 'fields' => array( + 'destination_id' => array( + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'description' => 'Primary ID field for the table. Not used for anything except internal lookups.', + 'no export' => TRUE, // Do not export database-only keys. + ), + 'machine_name' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '0', + 'description' => 'The primary identifier for a destination.', + ), + 'name' => array( + 'description' => 'The name of the destination.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE + ), + 'subtype' => array( + 'description' => 'The type of the destination.', + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE + ), + 'location' => array( + 'description' => 'The the location string of the destination.', + 'type' => 'text', + 'not null' => TRUE + ), + 'settings' => array( + 'description' => 'Other settings for the destination.', + 'type' => 'text', + 'not null' => TRUE, + 'serialize' => TRUE, + 'serialized default' => 'a:0:{}', + ), + ), + 'primary key' => array('destination_id'), + ); + $schema['backup_migrate_sources'] = array( + 'export' => array( + 'key' => 'machine_name', + 'key name' => 'Source ID', + 'admin_title' => 'name', + 'primary key' => 'source_id', + 'identifier' => 'item', // Exports will be defined as $preset + 'default hook' => 'exportables_backup_migrate_sources', // Function hook name. + 'api' => array( + 'owner' => 'backup_migrate', + 'api' => 'backup_migrate_exportables', // Base name for api include files. + 'minimum_version' => 1, + 'current_version' => 1, + ), + ), + 'fields' => array( + 'source_id' => array( + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'description' => 'Primary ID field for the table. Not used for anything except internal lookups.', + 'no export' => TRUE, // Do not export database-only keys. + ), + 'machine_name' => array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '0', + 'description' => 'The primary identifier for a source.', + ), + 'name' => array( + 'description' => 'The name of the source.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE + ), + 'subtype' => array( + 'description' => 'The type of the source.', + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE + ), + 'location' => array( + 'description' => 'The the location string of the source.', + 'type' => 'text', + 'not null' => TRUE + ), + 'settings' => array( + 'description' => 'Other settings for the source.', + 'type' => 'text', + 'not null' => TRUE, + 'serialize' => TRUE, + 'serialized default' => 'a:0:{}', + ), + ), + 'primary key' => array('source_id'), + ); + + $schema['backup_migrate_schedules'] = array( + 'export' => array( + 'key' => 'machine_name', + 'key name' => 'Source ID', + 'admin_title' => 'name', + 'primary key' => 'schedule_id', + 'identifier' => 'item', // Exports will be defined as $preset + 'default hook' => 'exportables_backup_migrate_schedules', // Function hook name. + 'api' => array( + 'owner' => 'backup_migrate', + 'api' => 'backup_migrate_exportables', // Base name for api include files. + 'minimum_version' => 1, + 'current_version' => 1, + ), + ), + 'fields' => array( + 'schedule_id' => array( + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'description' => 'Primary ID field for the table. Not used for anything except internal lookups.', + 'no export' => TRUE, // Do not export database-only keys. + ), + 'machine_name' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '0', + 'description' => 'The primary identifier for a profile.', + ), + 'name' => array( + 'description' => 'The name of the profile.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE + ), + 'source_id' => array( + 'description' => 'The {backup_migrate_destination}.destination_id of the source to backup from.', + 'type' => 'varchar', + 'length' => 255, + 'default' => 'db', + 'not null' => TRUE + ), + 'destination_id' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '0', + 'description' => 'The {backup_migrate_destination}.destination_id of the destination to back up to.', + ), + 'copy_destination_id' => array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '0', + 'description' => 'A second {backup_migrate_destination}.destination_id of the destination to copy the backup to.', + ), + 'profile_id' => array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '0', + 'description' => 'The primary identifier for a profile.', + ), + 'keep' => array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'The number of backups to keep.', + ), + 'period' => array( + 'type' => 'int', + 'not null' => TRUE, + 'default' => 0, + 'description' => 'The number of seconds between backups.', + ), + 'enabled' => array( + 'description' => 'Whether the schedule is enabled.', + 'type' => 'int', + 'size' => 'tiny', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0 + ), + 'cron' => array( + 'description' => 'Whether the schedule should be run during cron.', + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => 'builtin', + ), + 'cron_schedule' => array( + 'description' => 'The cron schedule to run on.', + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '0 4 * * *', + ), + ), + 'primary key' => array('schedule_id'), + ); + + return $schema; +} + +/** + * Implementation of hook_install(). + */ +function backup_migrate_install() { +} + +/** + * Remove variables on uninstall. + */ +function backup_migrate_uninstall() { + db_query("DELETE FROM {variable} WHERE name LIKE 'backup_migrate_%'"); + db_query("DELETE FROM {variable} WHERE name LIKE 'nodesquirrel_%'"); + cache_clear_all('variables', 'cache'); +} + +/** + * Update from 1.x to 2.x. + */ +function backup_migrate_update_2000() { + _backup_migrate_setup_database_defaults(); + return array(); +} + +/** + * Adding filter field for dev release of 2009-01-28 + */ +function backup_migrate_update_2001() { + $ret = array(); + $schema = drupal_get_schema_unprocessed('backup_migrate', 'backup_migrate_profiles'); + + // Add the filters field to the db. + if (!db_field_exists('backup_migrate_profiles', 'filters')) { + db_add_field('backup_migrate_profiles', 'filters', array('description' => t('The filter settings for the profile.'),'type' => 'text', 'not null' => TRUE)); + } + // Add the source field + if (!db_field_exists('backup_migrate_profiles', 'source_id')) { + db_add_field('backup_migrate_profiles', 'source_id', array('description' => t('The {backup_migrate_destination}.destination_id of the source to backup from.'), 'type' => 'varchar', 'length' => 255, 'default' => 'db', 'not null' => TRUE)); + } + // Remove the compression field. + if (db_field_exists('backup_migrate_profiles', 'compression')) { + db_drop_field('backup_migrate_profiles', 'compression'); + } + return $ret; +} + +/** + * Clearing the cache because there was a menu structure change in the dev of 2009-05-31 + */ +function backup_migrate_update_2002() { + // Cache should clear automatically. Nothing to do here. + return array(); +} + +/** + * Allowing non-int profile ids in schedules 2009-05-31 + */ +function backup_migrate_update_2003() { + $ret = array(); + $spec = array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '0', + 'description' => 'The primary identifier for a profile.', + ); + + db_change_field('backup_migrate_schedules', 'profile_id', 'profile_id', $spec); + return $ret; +} + +/** + * Allowing non-int profile ids 2009-07-01 + */ +function backup_migrate_update_2004() { + $ret = array(); + $spec = array( + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE, + 'default' => '0', + ); + + $spec['description'] = 'The primary identifier for a destination.'; + db_change_field('backup_migrate_destinations', 'destination_id', 'destination_id', $spec); + + $spec['description'] = 'The primary identifier for a profile.'; + db_change_field('backup_migrate_profiles', 'profile_id', 'profile_id', $spec); + + $spec['description'] = 'The primary identifier for a schedule.'; + db_change_field('backup_migrate_schedules', 'schedule_id', 'schedule_id', $spec); + + // Drop the user/pass fields as they weren't being used. + if (db_field_exists('backup_migrate_destinations', 'username')) { + db_drop_field('backup_migrate_destinations', 'username'); + } + if (db_field_exists('backup_migrate_destinations', 'password')) { + db_drop_field('backup_migrate_destinations', 'password'); + } + + return $ret; +} + +/** + * Change the default database id to something friendlier 2009-08-08 + */ +function backup_migrate_update_2005() { + require_once './'. drupal_get_path('module', 'backup_migrate') .'/includes/crud.inc'; + require_once './'. drupal_get_path('module', 'backup_migrate') .'/includes/profiles.inc'; + + $ret = array(); + // Change the destination ids of the defined database sources mostly to make using them with drush friendlier. + + // Change the db_url:default id to simply 'db' + $ret[] = db_query("UPDATE {backup_migrate_profiles} SET source_id = 'db' WHERE source_id = 'db_url:default'"); + $ret[] = db_query("UPDATE {backup_migrate_schedules} SET destination_id = 'db' WHERE destination_id = 'db_url:default'"); + + // Change the defined db keys from db_url:key to db:key. + $ret[] = db_query("UPDATE {backup_migrate_profiles} SET source_id = REPLACE(source_id, 'db_url:', 'db:')"); + $ret[] = db_query("UPDATE {backup_migrate_schedules} SET destination_id = REPLACE(destination_id, 'db_url:', 'db:')"); + + // Add the source field to the schedule + if (!db_field_exists('backup_migrate_schedules', 'source_id')) { + db_add_field('backup_migrate_schedules', 'source_id', array('description' => t('The db source to backup from.'), 'type' => 'varchar', 'length' => 255, 'default' => 'db', 'not null' => TRUE)); + } + + // Copy source data from profiles to schedules. + $result = db_query('SELECT p.source_id, s.schedule_id FROM {backup_migrate_schedules} s LEFT JOIN {backup_migrate_profiles} p ON s.profile_id = p.profile_id', array(), array('fetch' => PDO::FETCH_ASSOC)); + foreach ($result as $schedule) { + if (!$schedule['source_id']) { + $schedule['source_id'] = 'db'; + } + $ret[] = db_query("UPDATE {backup_migrate_schedules} SET source_id = '". $schedule['source_id'] ."' WHERE schedule_id = '". $schedule['profile_id'] ."'"); + } + + if (db_field_exists('backup_migrate_profiles', 'source_id')) { + db_drop_field('backup_migrate_profiles', 'source_id'); + } + + // Copy the no-data and exclude tables settings into the 'filter' field. + $result = db_query('SELECT * FROM {backup_migrate_profiles}', array(), array('fetch' => PDO::FETCH_ASSOC)); + foreach ($result as $item) { + if (isset($item['nodata_tables']) && isset($item['exclude_tables'])) { + $profile = backup_migrate_get_profile($item['profile_id']); + $profile->filters['nodata_tables'] = unserialize($item['nodata_tables']); + $profile->filters['exclude_tables'] = unserialize($item['exclude_tables']); + $profile->save(); + } + } + if (db_field_exists('backup_migrate_profiles', 'nodata_tables')) { + db_drop_field('backup_migrate_profiles', 'nodata_tables'); + } + if (db_field_exists('backup_migrate_profiles', 'exclude_tables')) { + db_drop_field('backup_migrate_profiles', 'exclude_tables'); + } + + return $ret; +} + +/** + * Move the backup and migrate directory to the private directory. + */ +function backup_migrate_update_7200() { + $from = 'public://backup_migrate'; + $to = 'private://backup_migrate'; + if (drupal_realpath($from) && !drupal_realpath($to)) { + if (!rename($from, $to)) { + drupal_set_message(t('Unable to move the backups directory to your private folder, please check file permissions and move the directory %from to %to', array('%from' => drupal_realpath($from), '%to' => drupal_realpath($to))), 'warning'); + } + } +} + + +/** + * Change the filename field to support 255 characters. + */ +function backup_migrate_update_7202() { + $ret = array(); + db_change_field('backup_migrate_profiles', 'filename', 'filename', array('type' => 'varchar', 'length' => 255, 'not null' => TRUE)); + return $ret; +} + + +/** + * Update the schedule last run times to use variables instead of saving with the schedule. + */ +function backup_migrate_update_7203() { + $result = db_query('SELECT * FROM {backup_migrate_schedules}', array(), array('fetch' => PDO::FETCH_ASSOC)); + foreach ($result as $item) { + if (isset($item['last_run'])) { + variable_set('backup_migrate_schedule_last_run_' . $item['schedule_id'], $item['last_run']); + } + } + if (db_field_exists('backup_migrate_schedules', 'last_run')) { + db_drop_field('backup_migrate_schedules', 'last_run'); + } +} + +/** + * Uninstall backup migrate files if it's installed + */ +function backup_migrate_update_7300() { + if (module_exists('backup_migrate_files')) { + module_disable(array('backup_migrate_files')); + $ret[] = array( + 'success' => true, + 'query' => 'Disabled the Backup and Migrate Files module', + ); + } + if (module_exists('nodesquirrel')) { + module_disable(array('nodesquirrel')); + $ret[] = array( + 'success' => true, + 'query' => 'Disabled the NodeSquirrel module', + ); + } + + // Add sources to the schema. + $schema['backup_migrate_sources'] = array( + 'fields' => array( + 'source_id' => array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '0', + 'description' => t('The primary identifier for a source.'), + ), + 'name' => array( + 'description' => t('The name of the source.'), + 'type' => 'varchar', + 'length' => 255, + 'not null' => TRUE + ), + 'type' => array( + 'description' => t('The type of the source.'), + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE + ), + 'location' => array( + 'description' => t('The the location string of the source.'), + 'type' => 'text', + 'not null' => TRUE + ), + 'settings' => array( + 'description' => t('Other settings for the source.'), + 'type' => 'text', + 'not null' => TRUE, + 'serialize' => TRUE, + 'serialized default' => 'a:0:{}', + ), + ), + 'primary key' => array('source_id'), + ); + + if (!db_table_exists('backup_migrate_sources')) { + db_create_table('backup_migrate_sources', $schema['backup_migrate_sources']); + } + + // Move custom destinations to sources. + $result = db_query("SELECT * FROM {backup_migrate_destinations} WHERE type in ('filesource', 'db')", array(), array('fetch' => PDO::FETCH_ASSOC)); + foreach ($result as $item) { + $item['source_id'] = $item['destination_id']; + drupal_write_record('backup_migrate_source', $item); + } + + // Change 'destination' settings to 'source' settings + $result = db_query('SELECT * FROM {backup_migrate_profiles}', array(), array('fetch' => PDO::FETCH_ASSOC)); + foreach ($result as $item) { + $item['filters'] = unserialize($item['filters']); + $item['filters']['sources'] = $item['filters']['destinations']; + unset($item['filters']['destinations']); + drupal_write_record('backup_migrate_profiles', $item, array('profile_id')); + } +} + +/** + * Switch the cron switch to text. + */ +function backup_migrate_update_7301() { + db_change_field('backup_migrate_schedules', 'cron', 'cron', array('type' => 'varchar', 'length' => 32, 'not null' => TRUE, 'default' => 'builtin')); + db_add_field('backup_migrate_schedules', 'cron_schedule', array('description' => 'The cron schedule to run on.', 'type' => 'varchar', 'length' => 255, 'default' => '0 4 * * *', 'not null' => TRUE)); +} + +/** + * Add a second destination to schedules. + */ +function backup_migrate_update_7302() { + db_add_field('backup_migrate_schedules', 'copy_destination_id', + array( + 'type' => 'varchar', + 'length' => 32, + 'not null' => TRUE, + 'default' => '0', + 'description' => 'A second {backup_migrate_destination}.destination_id of the destination to copy the backup to.', + ) + ); +} + +/** + * Add a serial id field to all tables to allow them to be ctools exportable. + */ +function backup_migrate_update_7303() { + foreach (array('backup_migrate_sources' => 'source_id', 'backup_migrate_destinations' => 'destination_id', 'backup_migrate_schedules' => 'schedule_id', 'backup_migrate_profiles' => 'profile_id') as $table => $id) { + // Take the primary key status from the machine name so it can be renamed + // A serial field must be defined as a key, so make a temporary index. + // See: https://www.drupal.org/node/190027 + db_add_index($table, 'temp', array($id)); + db_drop_primary_key($table); + // Drop our temporary index. + db_drop_index($table, 'temp'); + + // Switch the name of the id to 'machine_name' to be more ctools standard + db_change_field($table, $id, 'machine_name', array('type' => 'varchar', 'length' => 32, 'not null' => TRUE)); + + // Add a serial ID + db_add_field($table, $id, + array( + 'type' => 'serial', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'description' => 'Primary ID field for the table. Not used for anything except internal lookups.', + 'no export' => TRUE, // Do not export database-only keys. + ), + array('primary key' => array($id)) + ); + } + foreach (array('backup_migrate_sources', 'backup_migrate_destinations') as $table) { + db_change_field($table, 'type', 'subtype', array('type' => 'varchar', 'length' => 32, 'not null' => TRUE)); + } +} + +/** + * Update all schedules to use the built in cron if none is specified. + */ +function backup_migrate_update_7304() { + db_query("UPDATE {backup_migrate_schedules} SET cron = 'builtin' WHERE cron = '0'"); +} diff --git a/sites/all/modules/contrib/backup_migrate/backup_migrate.js b/sites/all/modules/contrib/backup_migrate/backup_migrate.js new file mode 100644 index 0000000..6105b92 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/backup_migrate.js @@ -0,0 +1,114 @@ +(function($) { + Drupal.behaviors.backupMigrate = { + attach: function(context) { + if (Drupal.settings.backup_migrate !== undefined) { + if (Drupal.settings.backup_migrate.dependents !== undefined) { + for (key in Drupal.settings.backup_migrate.dependents) { + info = Drupal.settings.backup_migrate.dependents[key]; + dependent = $('#edit-' + info['dependent']); + for (key in info['dependencies']) { + $('[name="' + key + '"]').each(function() { + var dependentval = info['dependencies'][key]; + var dependency = $(this); + (function(dependent, dependency) { + var checkval = function(inval) { + if (dependency.attr('type') == 'radio') { + var val = $('[name="' + dependency.attr('name') + '"]:checked').val(); + return val == inval; + } + else if (dependency.attr('type') == 'checkbox') { + return dependency.is(':checked') && inval == dependency.val(); + } + else { + return dependency.val() == inval; + } + return false; + }; + if (!checkval(dependentval)) { + // Hide doesn't work inside collapsed fieldsets. + dependent.css('display', 'none'); + } + dependency.bind('load change click keypress focus', function() { + if (checkval(dependentval)) { + dependent.slideDown(); + } + else { + dependent.slideUp(); + } + }).load(); + })(dependent, dependency); + }); + } + } + for (key in Drupal.settings.backup_migrate.destination_selectors) { + var info = Drupal.settings.backup_migrate.destination_selectors[key]; + (function(info) { + var selector = $('#' + info['destination_selector']); + var copy = $('#' + info['copy']) + var copy_selector = $('#' + info['copy_destination_selector']); + var copy_selector_options = {}; + + // Store a copy of the secondary selector options. + copy_selector.find('optgroup').each(function() { + var label = $(this).attr('label'); + copy_selector_options[label] = []; + $(this).find('option').each(function() { + copy_selector_options[label].push(this); + }); + $(this).remove(); + }) + + // Assign an action to the main selector to modify the secondary selector + selector.each(function() { + $(this).bind('load change click keypress focus', function() { + var group = $(this).find('option[value=' + $(this).val() + ']').parents('optgroup').attr('label'); + if (group) { + copy.parent().find('.backup-migrate-destination-copy-label').text(info['labels'][group]); + copy_selector.empty(); + for (var key in copy_selector_options) { + if (key != group) { + copy_selector.append(copy_selector_options[key]); + } + } + } + }).load(); + }); + })(info); + } + // Add the convert to checkboxes functionality to all multiselects. + $('#backup-migrate-ui-manual-backup-form select[multiple], #backup-migrate-crud-edit-form select[multiple]').each(function() { + var self = this; + $(self).after( + $('').append( + $(''+ Drupal.settings.backup_migrate.checkboxLinkText +'').click(function() { + var $select = $(self); + var $checkboxes = $('
').addClass('backup-migrate-tables-checkboxes'); + $('option', $select).each(function(i) { + $checkboxes.append( + $('
').append( + $('').prepend( + $('') + .bind('click change load', function() { + if (this.checked) { + $(this).parent().addClass('checked'); + } + else { + $(this).parent().removeClass('checked'); + } + }).load() + ) + ) + ); + }); + $select.parent().find('.backup-migrate-checkbox-link').remove(); + $select.before($checkboxes); + $select.hide(); + }) + ) + ); + }); + } + } + } + } +})(jQuery); diff --git a/sites/all/modules/contrib/backup_migrate/backup_migrate.module b/sites/all/modules/contrib/backup_migrate/backup_migrate.module new file mode 100644 index 0000000..60ab30b --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/backup_migrate.module @@ -0,0 +1,1871 @@ + + t('Backup and Migrate makes the task of backing up your Drupal database and migrating data from one Drupal install to another easier. It provides a function to backup the entire database to file or download, and to restore from a previous backup. You can also schedule the backup operation. Compression of backup files is also supported. The database backup files created with this module can be imported into this or any other Drupal installation with the !restorelink, or you can use a database tool such as phpMyAdmin or the mysql command line command.', + array( + '!restorelink' => user_access('restore from backup') ? l(t('restore feature'), BACKUP_MIGRATE_MENU_PATH . '/restore') : t('restore feature'), + '!phpmyadminurl' => 'http://www.phpmyadmin.net' + ) + ) + ), + BACKUP_MIGRATE_MENU_PATH => array( + 'title' => t('Quick Backup Tab'), + 'body' => t('Use this form to run simple manual backups of your database and files.'), + 'access arguments' => array('perform backup'), + ), + BACKUP_MIGRATE_MENU_PATH . '/export/advanced' => array( + 'title' => t('Advanced Backup Tab'), + 'body' => t('Use this form to run manual backups of your database and files with more advanced options. If you have any !profilelink saved you can load those settings. You can save any of the changes you make to these settings as a new settings profile.', + array("!profilelink" => user_access('administer backup and migrate') ? l(t('settings profiles'), BACKUP_MIGRATE_MENU_PATH . '/profile') : t('settings profiles'), '!restorelink' => user_access('restore from backup') ? l(t('restore feature'), BACKUP_MIGRATE_MENU_PATH . '/restore') : t('restore feature'), '!phpmyadminurl' => 'http://www.phpmyadmin.net')), + 'access arguments' => array('perform backup'), + ), + BACKUP_MIGRATE_MENU_PATH . '/restore' => array( + 'title' => t('Restore Tab'), + 'body' => t('Upload a backup and migrate backup file. The restore function will not work with database dumps from other sources such as phpMyAdmin.'), + 'access arguments' => array('restore from backup'), + ), + BACKUP_MIGRATE_MENU_PATH . '/settings/destination' => array( + 'title' => t('Destinations'), + 'body' => t('Destinations are the places you can save your backup files to or them load from.'), + 'more' => t('Files can be saved to a directory on your web server, downloaded to your desktop or emailed to a specified email account. From the Destinations tab you can create, delete and edit destinations or list the files which have already been backed up to the available destinations.'), + 'access arguments' => array('administer backup and migrate'), + ), + BACKUP_MIGRATE_MENU_PATH . '/settings/source' => array( + 'title' => t('Sources'), + 'body' => t('Sources are the things that can be backed up such as database or file directories.'), + 'access arguments' => array('administer backup and migrate'), + ), + BACKUP_MIGRATE_MENU_PATH . '/settings/profile' => array( + 'title' => t('Profiles'), + 'body' => t('Profiles are saved backup settings. Profiles store your table exclusion settings as well as your backup file name, compression and timestamp settings. You can use profiles in !schedulelink and for !manuallink.', + array('!schedulelink' => user_access('administer backup and migrate') ? l(t('schedules'), BACKUP_MIGRATE_MENU_PATH . '/schedule') : t('settings profiles'), '!manuallink' => user_access('perform backups') ? l(t('manual backups'), BACKUP_MIGRATE_MENU_PATH) : t('manual backups'))), + 'more' => t('You can create new profiles using the add profiles tab or by checking the "Save these settings" button on the advanced backup page.'), + 'access arguments' => array('administer backup and migrate'), + ), + BACKUP_MIGRATE_MENU_PATH . '/schedule' => array( + 'title' => t('Scheduling'), + 'body' => t('Automatically backup up your database and files on a regular schedule using cron.', + array('!cronurl' => 'http://drupal.org/cron')), + 'more' => t('Each schedule will run a maximum of once per cron run, so they will not run more frequently than your cron is configured to run. If you specify a number of backups to keep for a schedule, old backups will be deleted as new ones created. If specifiy a number of files to keep other backup files in that schedule\'s destination will get deleted.'), + 'access arguments' => array('administer backup and migrate'), + ), + BACKUP_MIGRATE_MENU_PATH . '/settings/import' => array( + 'title' => t('Importing Settings'), + 'body' => t('Import a settings profile, backup schedule, source or destination by pasting the export code into the textarea.', + array('!cronurl' => 'http://drupal.org/cron')), + ), + ); + if (module_exists('help')) { + $help[BACKUP_MIGRATE_MENU_PATH]['body'] .= ' ' . t('Visit the !helppage for more help using this module.', array('!helppage' => l(t('help page'), 'admin/help/backup_migrate'))); + } + + if (isset($help[$section])) { + return $help[$section]['body']; + } + + if ($section == 'admin/help#backup_migrate') { + $out = ""; + foreach ($help as $key => $section) { + if (isset($section['access arguments'])) { + foreach ($section['access arguments'] as $access) { + if (!user_access($access)) { + continue 2; + } + } + } + if (@$section['title']) { + if (!is_numeric($key)) { + $section['title'] = l($section['title'], $key); + } + $out .= "

". $section['title'] ."

"; + } + $out .= "

". $section['body'] ."

"; + if (!empty($section['more'])) { + $out .= "

". $section['more'] ."

"; + } + } + return $out; + } +} + +/** + * Implements hook_menu(). + */ +function backup_migrate_menu() { + $items = array(); + + $items[BACKUP_MIGRATE_MENU_PATH] = array( + 'title' => 'Backup and Migrate', + 'description' => 'Backup/restore your database and files or migrate data to or from another Drupal site.', + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('', 'backup_migrate_ui_manual_backup_quick', TRUE), + 'access arguments' => array('access backup and migrate'), + 'type' => MENU_NORMAL_ITEM, + ); + $items[BACKUP_MIGRATE_MENU_PATH . '/export'] = array( + 'title' => 'Backup', + 'description' => 'Backup the database.', + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('', 'backup_migrate_ui_manual_backup_quick', TRUE), + 'access arguments' => array('access backup and migrate'), + 'weight' => 0, + 'type' => MENU_DEFAULT_LOCAL_TASK, + ); + $items[BACKUP_MIGRATE_MENU_PATH . '/export/quick'] = array( + 'title' => 'Quick Backup', + 'description' => 'Backup the database.', + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('', 'backup_migrate_ui_manual_backup_quick', TRUE), + 'access arguments' => array('perform backup'), + 'weight' => 0, + 'type' => MENU_DEFAULT_LOCAL_TASK, + ); + $items[BACKUP_MIGRATE_MENU_PATH . '/export/advanced'] = array( + 'title' => 'Advanced Backup', + 'description' => 'Backup the database.', + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('', 'backup_migrate_ui_manual_backup_advanced', TRUE), + 'access arguments' => array('perform backup'), + 'weight' => 1, + 'type' => MENU_LOCAL_TASK, + ); + $items[BACKUP_MIGRATE_MENU_PATH . '/restore'] = array( + 'title' => 'Restore', + 'description' => 'Restore the database from a previous backup', + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('', 'backup_migrate_ui_manual_restore', TRUE), + 'access arguments' => array('restore from backup'), + 'weight' => 1, + 'type' => MENU_LOCAL_TASK, + ); + $items[BACKUP_MIGRATE_MENU_PATH . '/backups'] = array( + 'title' => 'Saved Backups', + 'description' => 'Previously created backups', + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('', 'backup_migrate_ui_saved_backups', TRUE), + 'access arguments' => array('access backup files'), + 'weight' => 2, + 'type' => MENU_LOCAL_TASK, + ); + // @TODO: This was removed because it was getting far to complicated for an abstraction. Exposing the actual schedules is more direct. + // $items[BACKUP_MIGRATE_MENU_PATH . '/schedule'] = array( + // 'title' => 'Schedule', + // 'description' => 'Schedule automatic backups', + // 'page callback' => 'backup_migrate_menu_callback', + // 'page arguments' => array('', 'backup_migrate_ui_schedule', TRUE), + // 'access arguments' => array('administer backup and migrate'), + // 'weight' => 3, + // 'type' => MENU_LOCAL_TASK, + // ); + $items[BACKUP_MIGRATE_MENU_PATH . '/settings'] = array( + 'title' => 'Settings', + 'description' => 'Module settings.', + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('crud', 'backup_migrate_crud_ui_list_all', TRUE), + 'access arguments' => array('administer backup and migrate'), + 'weight' => 4, + 'type' => MENU_LOCAL_TASK, + ); + $items[BACKUP_MIGRATE_MENU_PATH . '/settings/import'] = array( + 'title' => 'Import', + 'description' => 'Import backup and migrate settings.', + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('crud', 'backup_migrate_crud_ui_import', TRUE), + 'access arguments' => array('administer backup and migrate'), + 'weight' => 10, + 'type' => MENU_LOCAL_TASK, + ); + + backup_migrate_include('crud'); + $items += backup_migrate_crud_menu(); + return $items; +} + +/** + * Implements hook_element_info(). + */ +function backup_migrate_element_info() { + $types['backup_migrate_dependent'] = array( + '#dependencies' => array(), + '#theme' => 'backup_migrate_dependent', + ); + $types['backup_migrate_file_list'] = array( + '#theme' => 'backup_migrate_file_list', + '#process' => array('form_process_backup_migrate_file_list'), + '#multiple' => FALSE, + '#files' => array(), + '#display_options' => array(), + '#default_value' => 0, + ); + return $types; +} + +/** + * Callback function for backup_migrate_file_list '#process' callback. + */ +function form_process_backup_migrate_file_list($element) { + if (count($element['#files']) > 0) { + $weight = 0; + $options = array(); + $max = $count = $element['#display_options']['limit']; + + $files = $element['#files']; + foreach ($files as $id => $file) { + $options[$id] = $file->info('filename'); + } + $element['files'] = array( + '#type' => 'radios', + '#options' => $options, + '#default_value' => $element['#default_value'], + '#parents' => $element['#parents'], + ); + } + + return $element; +} + +/** + * Implements hook_form_update_script_selection_form_alter(). + */ +function backup_migrate_form_update_script_selection_form_alter(&$form, $form_state) { + $msg = t('Before you run any database updates, make sure you have a backup of your database. Use !backup_migrate to create that backup now.', array('!backup_migrate' => l(t('Backup and Migrate'), BACKUP_MIGRATE_MENU_PATH))); + + drupal_set_message($msg, 'warning'); + +} + +/** + * Implements hook_cron(). + * + * Takes care of scheduled backups and deletes abandoned temp files. + */ +function backup_migrate_cron() { + // Set the message mode to logging. + _backup_migrate_message_callback('_backup_migrate_message_log'); + + // Clean up any previous abandoned tmp files before we attempt to back up. + backup_migrate_include('files'); + _backup_migrate_temp_files_delete(); + backup_migrate_include('schedules'); + backup_migrate_schedules_cron(); + + // Clean up any tmp files from this run. + _backup_migrate_temp_files_delete(); +} + + +/** + * Implements hook_cronapi(). + */ +function backup_migrate_cronapi($op, $job = NULL) { + $items = array(); + backup_migrate_include('schedules'); + foreach (backup_migrate_get_schedules() as $schedule) { + if ($schedule->get('cron') == BACKUP_MIGRATE_CRON_ELYSIA) { + $id = $schedule->get('id'); + $items['backup_migrate_' . $id] = array( + 'description' => t('Run the Backup and Migrate \'!name\' schedule', array('!name' => $schedule->get('name'))), + 'rule' => $schedule->get('cron_schedule'), + 'callback' => 'backup_migrate_schedule_run', + 'arguments' => array($id), + 'file' => 'includes/schedules.inc', + ); + } + } + return $items; +} + +/** + * Implements hook_permission(). + */ +function backup_migrate_permission() { + return array( + 'access backup and migrate' => array( + 'title' => t('Access Backup and Migrate'), + 'description' => t('Access the Backup and Migrate admin section.'), + ), + 'perform backup' => array( + 'title' => t('Perform a backup'), + 'description' => t('Back up any of the available databases.'), + ), + 'access backup files' => array( + 'title' => t('Access backup files'), + 'description' => t('Access and download the previously created backup files.'), + ), + 'delete backup files' => array( + 'title' => t('Delete backup files'), + 'description' => t('Delete the previously created backup files.'), + ), + 'restore from backup' => array( + 'title' => t('Restore the site'), + 'description' => t('Restore the site\'s database from a backup file.'), + ), + 'administer backup and migrate' => array( + 'title' => t('Administer Backup and Migrate'), + 'description' => t('Edit Backup and Migrate profiles, schedules and destinations.'), + ), + ); +} + +/** + * Implements hook_simpletest(). + */ +function backup_migrate_simpletest() { + $dir = drupal_get_path('module', 'backup_migrate') .'/tests'; + $tests = file_scan_directory($dir, '\.test$'); + return array_keys($tests); +} + +/** + * Implements hook_theme(). + */ +function backup_migrate_theme() { + $themes = array( + 'backup_migrate_ui_manual_quick_backup_form_inline' => array( + 'arguments' => array('form'), + 'render element' => 'form', + ), + 'backup_migrate_ui_quick_schedule_form_inline' => array( + 'arguments' => array('form'), + 'render element' => 'form', + ), + 'backup_migrate_dependent' => array( + 'arguments' => array('element'), + 'render element' => 'element', + ), + 'backup_migrate_file_list' => array( + 'arguments' => array('element'), + 'render element' => 'element', + ), + 'backup_migrate_group' => array( + 'variables' => array('title' => '', 'body' => '', 'description' => ''), + ), + ); + return $themes; +} + +/* Blocks */ + +/** + * Implements hook_block_info(). + */ +function backup_migrate_block_info() { + $blocks = array(); + $blocks['quick_backup'] = array( + 'info' => t('Backup and Migrate Quick Backup'), + 'admin' => TRUE + ); + return $blocks; +} + +/** + * Implements hook_block_view(). + */ +function backup_migrate_block_view($delta) { + $function = '_backup_migrate_block_view_' . $delta; + if (function_exists('_backup_migrate_block_view_' . $delta)) { + return $function(); + } +} + +/** + * Quick Backup block. + */ +function _backup_migrate_block_view_quick_backup() { + if (user_access('access backup and migrate') && user_access('perform backup') && $_GET['q'] != BACKUP_MIGRATE_MENU_PATH) { + drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css'); + + // Messages should be sent to the browser. + _backup_migrate_message_callback('_backup_migrate_message_browser'); + return array( + 'subject' => t('Quick Backup'), + 'content' => drupal_get_form('backup_migrate_ui_manual_quick_backup_form', FALSE), + ); + } + return array(); +} + +/** + * Implements hook_backup_migrate_destinations(). + */ +function backup_migrate_backup_migrate_schedules() { + backup_migrate_include('sources'); + + $schedules = array(); + + // Declare a default backup for each source that can be enabled. + // @TODO: This makes setting up scheduled easier, but only when combined with the now disabled quick-schedule feature. + // $sources = backup_migrate_get_sources(); + // foreach ($sources as $id => $source) { + // $schedule = array( + // 'schedule_id' => $id, + // 'name' => $source->get('name'), + // 'source_id' => $id, + // 'destination_id' => 'scheduled', + // 'profile_id' => 'default', + // 'period' => variable_get('backup_migrate_default_schedule', 60*60), + // 'keep' => BACKUP_MIGRATE_SMART_DELETE, + // 'enabled' => FALSE, + // ); + // $schedules[$id] = backup_migrate_crud_create_item('schedule', $schedule); + // } + + if (variable_get('nodesquirrel_secret_key', FALSE) != FALSE) { + $schedule = array( + 'machine_name' => 'nodesquirrel', + 'name' => 'NodeSquirrel', + 'source_id' => variable_get('nodesquirrel_schedule_source_id', 'db'), + 'destination_id' => 'nodesquirrel', + 'profile_id' => 'default', + 'period' => variable_get('nodesquirrel_schedule', 60*60*24), + 'enabled' => variable_get('nodesquirrel_schedule_enabled', TRUE) && variable_get('nodesquirrel_secret_key', FALSE) != FALSE, + 'keep' => variable_get('nodesquirrel_schedule_keep', BACKUP_MIGRATE_SMART_DELETE) + ); + $schedules['nodesquirrel'] = backup_migrate_crud_create_item('schedule', $schedule); + } + + return $schedules; +} + + +/** + * Implementation of hook_ctools_plugin_api(). + * + * Tell CTools that we support the default_mymodule_presets API. + */ +function backup_migrate_ctools_plugin_api($owner, $api) { + if ($owner == 'backup_migrate') { + return array('version' => 1); + } +} + + + +/* Menu Callbacks */ + +/** + * A menu callback helper. Handles file includes and interactivity setting. + */ +function backup_migrate_menu_callback($include, $function, $interactive = TRUE) { + if ($include) { + backup_migrate_include($include); + } + // Set the message handler based on interactivity setting. + _backup_migrate_message_callback($interactive ? '_backup_migrate_message_browser' : '_backup_migrate_message_log'); + // Get the arguments with the first 3 removed. + $args = array_slice(func_get_args(), 3); + return call_user_func_array($function, $args); +} + +/** + * Include views .inc files as necessary. + */ +function backup_migrate_include() { + static $used = array(); + foreach (func_get_args() as $file) { + if (!isset($used[$file])) { + require_once backup_migrate_include_path($file); + } + + $used[$file] = TRUE; + } +} + +/** + * Include views .inc files as necessary. + */ +function backup_migrate_include_path($file) { + return DRUPAL_ROOT . '/' . drupal_get_path('module', 'backup_migrate') . "/includes/$file.inc"; +} + + +/** + * The menu callback for easy manual backups. + */ +function backup_migrate_ui_manual_backup_quick() { + $out = array(); + if (user_access('perform backup')) { + return drupal_get_form('backup_migrate_ui_manual_quick_backup_form'); + } + else { + return t('You do not have permission to back up this site.'); + } + return $out; +} + +/** + * The menu callback for advanced manual backups. + */ +function backup_migrate_ui_manual_backup_advanced() { + backup_migrate_include('profiles'); + $out = array(); + $profile_id = arg(BACKUP_MIGRATE_MENU_DEPTH + 2); + $profile = _backup_migrate_profile_saved_default_profile($profile_id); + + $out[] = drupal_get_form('backup_migrate_ui_manual_backup_load_profile_form', $profile); + $out[] = drupal_get_form('backup_migrate_ui_manual_backup_form', $profile); + return $out; +} + +/** + * The backup/export load profile form. + */ +function backup_migrate_ui_manual_backup_load_profile_form($form, &$form_state, $profile = NULL) { + $form = array(); + $profile_options = _backup_migrate_get_profile_form_item_options(); + if (count($profile_options) > 0) { + $profile_options = array(0 => t('-- Select a Settings Profile --')) + $profile_options; + $form['profile'] = array( + "#title" => t("Settings Profile"), + "#collapsible" => TRUE, + "#collapsed" => FALSE, + "#prefix" => '
', + "#suffix" => '
', + "#tree" => FALSE, + "#description" => t("You can load a profile. Any changes you made below will be lost."), + ); + $form['profile']['profile_id'] = array( + "#type" => "select", + "#title" => t("Load Settings"), + '#default_value' => is_object($profile) ? $profile->get_id() : 0, + "#options" => $profile_options, + ); + $form['profile']['load_profile'] = array( + '#type' => 'submit', + '#value' => t('Load Profile'), + ); + } + return $form; +} + +/** + * Submit the profile load form. + */ +function backup_migrate_ui_manual_backup_load_profile_form_submit($form, &$form_state) { + if ($profile = backup_migrate_get_profile($form_state['values']['profile_id'])) { + variable_set("backup_migrate_profile_id", $profile->get_id()); + $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . '/export/advanced'; + } + else { + variable_set("backup_migrate_profile_id", NULL); + } +} + +/** + * The menu callback for quick schedules. + */ +function backup_migrate_ui_schedule() { + backup_migrate_include('sources', 'schedules', 'profiles'); + $out = ''; + + $sources = backup_migrate_get_sources(); + $schedules = backup_migrate_get_schedules(); + + $out = drupal_get_form('backup_migrate_ui_schedule_form', $sources, $schedules); + return $out; +} + +/** + * The quick schedule form. + */ +function backup_migrate_ui_schedule_form($form, &$form_state, $sources, $schedules) { + $form['quickschedule'] = array('#tree' => TRUE); + + // Add a quick schedule item for each source. + foreach ($sources as $id => $source) { + if (isset($schedules[$id])) { + $schedule = $schedules[$id]; + $key = preg_replace('/[^A-Za-z0-9\-_]/', '-', $id); + $form['quickschedule'][$key] = array(); + $form['quickschedule'][$key]['#theme'] = 'backup_migrate_ui_quick_schedule_form_inline'; + + $form['quickschedule'][$key]['id'] = array( + '#type' => 'value', + '#value' => $id, + ); + + $form['quickschedule'][$key]['enabled'] = array( + '#type' => 'checkbox', + '#title' => t('Automatically backup my @source', array('@source' => $source->get('name'))), + '#default_value' => $schedule->get('enabled'), + ); + /* + $form['quickschedule'][$id]['source_id'] = array( + '#type' => 'item', + '#title' => t('Backup Source'), + '#value' => $source->get('name'), + ); + */ + $form['quickschedule'][$key]['settings'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'quickschedule[' . $key . '][enabled]' => TRUE, + ), + ); + $options = array( + (60*60) => t('Once an hour'), + (60*60*24) => t('Once a day'), + (60*60*24*7) => t('Once a week'), + ); + $period = $schedule->get('period'); + if (!isset($options[$period])) { + $options[$period] = $schedule->get('frequency_description'); + } + $form['quickschedule'][$key]['settings']['period'] = array( + '#type' => 'select', + '#title' => t('Schedule Frequency'), + '#options' => $options, + '#default_value' => $period, + ); + $form['quickschedule'][$key]['settings']['destination'] = _backup_migrate_get_destination_pulldown('scheduled backup', $schedule->get('destination_id'), $schedule->get('copy_destination_id')); + + $form['quickschedule'][$key]['settings']['destination']['destination_id']['#parents'] = array('quickschedule', $key, 'destination_id'); + $form['quickschedule'][$key]['settings']['destination']['copy']['#parents'] = array('quickschedule', $key, 'copy'); + $form['quickschedule'][$key]['settings']['destination']['copy_destination']['copy_destination_id']['#parents'] = array('quickschedule', $key, 'copy_destination_id'); + + + array( + '#type' => 'select', + '#title' => t('Backup Destination'), + '#options' => _backup_migrate_get_destination_form_item_options('scheduled backup'), + '#default_value' => $schedule->get('period'), + ); + $form['quickschedule'][$key]['settings']['profile_id'] = array( + '#type' => 'select', + '#title' => t('Settings Profile'), + '#default_value' => $schedule->get('profile_id'), + '#options' => _backup_migrate_get_profile_form_item_options(), + ); + + $keep = $schedule->get('keep'); + $option_keys = array_unique(array(BACKUP_MIGRATE_SMART_DELETE, 30, 100, 500, BACKUP_MIGRATE_KEEP_ALL, $keep)); + $options = array(); + foreach ($option_keys as $i) { + $options[$i] = $schedule->generate_keep_description($i); + } + $form['quickschedule'][$key]['settings']['keep'] = array( + '#type' => 'select', + '#title' => t('Backup retention'), + '#options' => $options, + '#default_value' => $keep, + ); + $form['quickschedule'][$key]['settings']['advanced'] = array( + '#type' => 'markup', + '#markup' => l(t('Advanced Settings.'), BACKUP_MIGRATE_MENU_PATH . '/settings/schedule/edit/' . $id, array('query' => drupal_get_destination())), + ); + // Set the parent for the setttings up a level. + foreach (element_children($form['quickschedule'][$key]['settings']) as $child) { + $form['quickschedule'][$key]['settings'][$child]['#parents'] = array('quickschedule', $key, $child); + } + } + } + if (element_children($form['quickschedule'])) { + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Save Schedules'), + ); + } + $form['advanced'] = array( + '#type' => 'markup', + '#markup' => '', + ); + return $form; +} + +/** + * Submit the quick schedule form. + */ +function backup_migrate_ui_schedule_form_submit($form, &$form_state) { + backup_migrate_include('schedules'); + if (user_access('adminsiter backup and migrate')) { + // Override the backups. + foreach ($form_state['values']['quickschedule'] as $key => $values) { + $id = $values['id']; + if ($schedule = backup_migrate_get_schedule($id)) { + $schedule->from_array($values); + $schedule->save(); + } + } + drupal_set_message(t('Your schedules have been saved')); + } +} + + + +/** + * The quick backup form. + */ +function backup_migrate_ui_manual_quick_backup_form($form, &$form_state, $inline = TRUE) { + backup_migrate_include('profiles', 'destinations', 'sources'); + drupal_add_js(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.js'); + + $form = array(); + + // Theme the form if we want it inline. + if ($inline) { + $form['#theme'] = 'backup_migrate_ui_manual_quick_backup_form_inline'; + } + + $form['quickbackup'] = array( + '#type' => 'fieldset', + "#title" => t("Quick Backup"), + "#collapsible" => FALSE, + "#collapsed" => FALSE, + "#tree" => FALSE, + ); + + $form['quickbackup']['source_id'] = _backup_migrate_get_source_pulldown(variable_get('backup_migrate_source_id', NULL)); + + $form['quickbackup']['destination'] = _backup_migrate_get_destination_pulldown('manual backup', variable_get('backup_migrate_destination_id', 'download'), variable_get('backup_migrate_copy_destination_id', '')); + + $profile_options = _backup_migrate_get_profile_form_item_options(); + $form['quickbackup']['profile_id'] = array( + "#type" => "select", + "#title" => t("Settings Profile"), + '#default_value' => variable_get('backup_migrate_profile_id', NULL), + "#options" => $profile_options, + ); + + $form['quickbackup']['description_enabled'] = array( + '#type' => 'checkbox', + "#title" => t("Add a note to the backup"), + ); + $form['quickbackup']['description'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'description_enabled' => TRUE, + ), + ); + + $form['quickbackup']['description']['description'] = array( + '#type' => 'textarea', + "#title" => t("Note"), + '#description' => t('This note will be saved with the backup file and shown on the listing page.'), + ); + + $form['advanced'] = array( + '#type' => 'markup', + '#markup' => '', + ); + + $form['quickbackup']['submit'] = array( + '#type' => 'submit', + '#value' => t('Backup now'), + '#weight' => 1, + ); + + $form['#validate'] = array('backup_migrate_ui_manual_quick_backup_form_validate'); + $form['#submit'] = array('backup_migrate_ui_manual_quick_backup_form_submit'); + + return _backup_migrate_ui_action_form($form, $form_state, 'backup'); +} + +/** + * Validate the quick backup form. + */ +function backup_migrate_ui_manual_quick_backup_form_validate($form, &$form_state) { + backup_migrate_include('profiles', 'destinations'); + if ($form_state['values']['source_id'] == $form_state['values']['destination_id']) { + form_set_error('destination_id', t('A source cannot be backed up to itself. Please pick a different destination for this backup.')); + } + // Wrap profile_id for prevent Undefined index notice. + $profile_id = isset($form_state['values']['profile_id']) ? $form_state['values']['profile_id'] : NULL; + + // For a quick backup use the default settings. + $settings = _backup_migrate_profile_saved_default_profile($profile_id); + + // Set the destination to the one chosen in the pulldown. + $settings->destination_id = array($form_state['values']['destination_id']); + $settings->source_id = $form_state['values']['source_id']; + + // Add the second destination. + if (!empty($form_state['values']['copy_destination_id'])) { + $settings->destination_id[] = $form_state['values']['copy_destination_id']; + } + + // Add the note + if (!empty($form_state['values']['description_enabled'])) { + $settings->filters['utils_description'] = $form_state['values']['description']; + } + $form_state['values']['settings'] = $settings; +} + +/** + * Submit the quick backup form. + */ +function backup_migrate_ui_manual_quick_backup_form_submit($form, &$form_state) { + backup_migrate_include('profiles', 'destinations'); + if (user_access('perform backup') && !empty($form_state['values']['settings'])) { + // Save the settings for next time. + variable_set("backup_migrate_source_id", $form_state['values']['source_id']); + variable_set("backup_migrate_destination_id", $form_state['values']['destination_id']); + variable_set("backup_migrate_copy_destination_id", $form_state['values']['copy_destination_id']); + variable_set("backup_migrate_profile_id", $form_state['values']['profile_id']); + + // Do the backup. + backup_migrate_ui_manual_backup_perform($form_state['values']['settings']); + } + $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH; +} + +/** + * Theme the quick backup form. + */ +function theme_backup_migrate_ui_manual_quick_backup_form_inline($form) { + drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css'); + + $form = $form['form']; + + // Remove the titles so that the pulldowns can be displayed inline. + unset($form['quickbackup']['source_id']['#title']); + unset($form['quickbackup']['destination']['destination_id']['#title']); + unset($form['quickbackup']['destination']['destination_id']['#description']); + unset($form['quickbackup']['profile_id']['#title']); + unset($form['quickbackup']['destination']['copy_destination']['copy_destination_id']['#title']); + + $replacements = array( + '!from' => drupal_render($form['quickbackup']['source_id']), + '!to' => drupal_render($form['quickbackup']['destination']['destination_id']), + '!profile' => drupal_render($form['quickbackup']['profile_id']), + //'!submit' => drupal_render($form['quickbackup']['submit']), + ); + $form['quickbackup']['markup'] = array( + '#type' => 'markup', + "#prefix" => '
', + "#suffix" => '
', + '#markup' => t('Backup my !from to !to using !profile', $replacements), + ); + + $replacements = array( + '!to' => drupal_render($form['quickbackup']['destination']['copy_destination']['copy_destination_id']), + ); + $form['quickbackup']['destination']['copy']["#prefix"] = '
'; + $form['quickbackup']['destination']['copy']["#suffix"] = $replacements['!to'] . '
'; + // This is not good translation practice as it relies on the structure of english + // If I add the pulldown to the label, howerver, the box toggles when the pulldown is clicked. + //$form['quickbackup']['destination']['copy']['#title'] = t('Save a copy to'); + + unset($form['quickbackup']['source_id']); + unset($form['quickbackup']['destination']['destination_id']); + unset($form['quickbackup']['destination']['copy_destination']); + unset($form['quickbackup']['profile_id']); + //unset($form['quickbackup']['submit']); + return drupal_render_children($form); +} + +/** + * Theme the quick schedule form. + */ +function theme_backup_migrate_ui_quick_schedule_form_inline($form) { + drupal_add_js(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.js'); + drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css'); + + $form = $form['form']; + + // Remove the titles so that the pulldowns can be displayed inline. + unset($form['settings']['source_id']['#title']); + unset($form['settings']['period']['#title']); + unset($form['settings']['destination']['destination_id']['#title']); + unset($form['settings']['destination']['destination_id']['#description']); + unset($form['settings']['profile_id']['#title']); + unset($form['settings']['destination']['copy_destination']['copy_destination_id']['#title']); + unset($form['settings']['profile_id']['#title']); + unset($form['settings']['keep']['#title']); + unset($form['settings']['keep']['#field_prefix']); + unset($form['settings']['keep']['#field_suffix']); + + $replacements = array( + '!from' => drupal_render($form['settings']['source_id']), + '!to' => drupal_render($form['settings']['destination']['destination_id']), + '!frequency' => drupal_render($form['settings']['period']), + '!profile' => drupal_render($form['settings']['profile_id']), + '!keep' => drupal_render($form['settings']['keep']), + ); + $form['settings'] = array('inline' => array( + '#type' => 'markup', + "#prefix" => '
', + "#suffix" => '
', + '#markup' => t('Backup !frequency to !to using !profile keeping !keep', $replacements), + )) + $form['settings']; + + $replacements = array( + '!to' => drupal_render($form['settings']['destination']['copy_destination']['copy_destination_id']), + ); + $form['settings']['destination']['copy']["#prefix"] = '
'; + $form['settings']['destination']['copy']["#suffix"] = $replacements['!to'] . '
'; + + + unset($form['settings']['period']); + unset($form['settings']['destination']['destination_id']); + unset($form['settings']['destination']['copy_destination']); + unset($form['settings']['profile_id']); + unset($form['settings']['keep']); + + return drupal_render_children($form); +} + + +/** + * Theme the dependent form section. + */ +function theme_backup_migrate_dependent($vars) { + $element = $vars['element']; + drupal_add_js(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.js'); + drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css'); + + $id = drupal_html_id(implode('-', $element['#array_parents'])); + + $settings = array( + 'backup_migrate' => array( + 'dependents' => array($id => array('dependent' => $id, 'dependencies' => $element['#dependencies'])) + ) + ); + drupal_add_js($settings, 'setting'); + + return '
' . drupal_render_children($element) . '
'; +} + + +/** + * Theme file list form widget. + */ +function theme_backup_migrate_file_list($vars) { + $element = $vars['element']; + + // Render the radios so they can be placed in the file list rendering. + $vars['element']['#display_options']['form_elements'] = array(); + if (isset($vars['element']['files']) && count($vars['element']['files'])) { + foreach (element_children($vars['element']['files']) as $id) { + $vars['element']['files'][$id]['#title'] = ''; + $vars['element']['#display_options']['form_elements'][$id] = render($vars['element']['files'][$id]); + } + } + + $out = _backup_migrate_ui_destination_display_file_list($vars['element']['#files'], $vars['element']['#display_options']); + return $out; +} + +/** + * Theme the dependent form section. + */ +function theme_backup_migrate_group($vars) { + $output = ''; + if (!empty($vars['title'])) { + $output .= '

' . $vars['title'] . '

'; + } + if (!empty($vars['description'])) { + $output .= '
' . $vars['description'] . '
'; + } + $output .= '
' . $vars['body'] . '
'; + $output = '
' . $output . '
'; + return $output; +} + + +/** + * The backup/export form. + */ +function backup_migrate_ui_manual_backup_form($form, &$form_state, $profile) { + drupal_add_js(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.js', array('type' => 'module', 'scope' => 'footer')); + + $form = array(); + + $form += _backup_migrate_get_source_form('db'); + + $form_state['profile'] = $profile; + + $form['machine_name'] = array( + "#type" => "value", + '#default_value' => $profile->get_id(), + ); + + $form['storage'] = array( + "#type" => "value", + '#default_value' => $profile->storage, + ); + $form['destination'] = array( + "#type" => "fieldset", + "#title" => t("Backup Destination"), + "#collapsible" => TRUE, + "#collapsed" => FALSE, + "#tree" => FALSE, + "#description" => t("Choose where the backup file will be saved. Backup files contain sensitive data, so be careful where you save them. Select 'Download' to download the file to your desktop."), + // '#weight' => 70, + ); + $form['destination']['destination'] = _backup_migrate_get_destination_pulldown('manual backup', variable_get('backup_migrate_destination_id', 'download')); + + $form += _backup_migrate_ui_backup_settings_form($profile); + + // Make the source settings dependent on the source selected. + // Except don't because it looks super weird. + // foreach (element_children($form['sources']) as $source) { + // $form['sources'][$source] = array( + // '#type' => 'backup_migrate_dependent', + // '#dependencies' => array( + // 'source_id' => $source, + // ), + // 'settings' => $form['sources'][$source] + // ); + // } + + if (user_access('administer backup and migrate')) { + $form['save_settings'] = array( + "#type" => "checkbox", + "#title" => t('Save these settings.'), + "#default_value" => FALSE, + '#weight' => 80, + ); + $form['save_options'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'save_settings' => TRUE, + ), + '#weight' => 90, + ); + $name = array( + '#default_value' => $profile->get('name'), + '#type' => 'textfield', + '#title' => t('Save the settings as'), + ); + + if ($profile->get_id()) { + $form['save_options']['create_new'] = array( + '#default_value' => $profile->get('name'), + '#type' => 'radios', + '#default_value' => 0, + '#options' => array( + 0 => t("Replace the '%profile' profile", array('%profile' => $profile->get('name'))), + 1 => t('Create new profile'), + ), + ); + + $name["#title"] = t('Profile name'); + $name["#description"] = t("This will be the name of your new profile if you select 'Create new profile' otherwise it will become the name of the '%profile' profile.", array('%profile' => $profile->get('name'))); + } + else { + $name["#title"] = t('Save the settings as'); + $name["#description"] = t('Pick a name for the settings. Your settings will be saved as a profile and will appear in the Profiles Tab.', array('!url' => url(BACKUP_MIGRATE_MENU_PATH . '/profile'))); + $name["#default_value"] = t('Untitled Profile'); + } + $form['save_options']['name'] = $name; + $form['save_options'][] = array( + '#type' => 'submit', + '#value' => t('Save Without Backing Up'), + ); + } + $form['#validate'][] = 'backup_migrate_ui_manual_quick_backup_form_validate'; + $form['#submit'][] = 'backup_migrate_ui_manual_backup_form_submit'; + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Backup now'), + '#weight' => 100, + ); + return _backup_migrate_ui_action_form($form, $form_state, 'backup'); +} + + +/** + * Submit the form. Save the values as defaults if desired and output the backup file. + */ +function backup_migrate_ui_manual_backup_form_submit($form, &$form_state) { + // Create a profile with the given settings. + $profile = $form_state['profile']; + $profile->from_array($form_state['values']); + + // Save the settings profile if the save box is checked. + if ($form_state['values']['save_settings'] && user_access('administer backup and migrate')) { + if (@$form_state['values']['create_new']) { + // Reset the id and storage so a new item will be saved. + $profile->set_id(NULL); + $profile->storage = BACKUP_MIGRATE_STORAGE_NONE; + } + $profile->save(); + variable_set("backup_migrate_profile_id", $profile->get_id()); + variable_set("backup_migrate_destination_id", $form_state['values']['destination_id']); + } + + // Perform the actual backup if that is what was selected. + if ($form_state['values']['op'] !== t('Save Without Backing Up') && user_access('perform backup')) { + backup_migrate_ui_manual_backup_perform($profile); + } + $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . "/export/advanced"; +} + +/** + * Alter a B&M action (backup/restore) form to allow for filters to add additional form steps. + **/ +function _backup_migrate_ui_action_form($form, &$form_state, $op = 'backup') { + $form['operation'] = array('#type' => 'value', '#value' => $op); + + // If the form has been submitted at least once then see if there are additional form elements to add. + if (!empty($form_state['storage']['values']) && !empty($form_state['values']['settings'])) { + $page = _backup_migrate_filter_before_action_form($form_state['values']['settings'], $op); + if (!empty($page)) { + $page['#validate'] = $form['#validate']; + $page['#submit'] = $form['#submit']; + $page['submit'] = array( + '#type' => 'submit', + '#value' => isset($form_state['values']['op']) ? check_plain($form_state['values']['op']) : t('Continue'), + '#weight' => 1, + ); + $form = $page; + } + } + + // Add some pre and post processing functions to validate and submit. + array_unshift($form['#validate'], 'backup_migrate_ui_action_form_pre_validate'); + array_push($form['#validate'], 'backup_migrate_ui_action_form_post_validate'); + + array_unshift($form['#submit'], 'backup_migrate_ui_action_form_pre_submit'); + + return $form; +} + +/** + * Allow filters, sources and destinations to present additional form elements + * to the end user before an action is submitted. + **/ +function backup_migrate_ui_action_form_pre_validate($form, &$form_state) { + if (!empty($form_state['storage']['values'])) { + $form_state['values'] += $form_state['storage']['values']; + } +} + +/** + * Allow filters, sources and destinations to present additional form elements + * to the end user before an action is submitted. + **/ +function backup_migrate_ui_action_form_post_validate($form, &$form_state) { + backup_migrate_include('filters'); + + if (!empty($form_state['values']['settings'])) { + // See if there are extra form items to be processed. + $form = _backup_migrate_filter_before_action_form($form_state['values']['settings'], $form_state['values']['operation']); + if ($form) { + // First time through the form simply rebuild the form to show the additional fields. + if (empty($form_state['storage']['values'])) { + $form_state['storage']['values'] = $form_state['values']; + $form_state['rebuild'] = TRUE; + } + else { + backup_migrate_filters_before_action_form_validate($form_state['values']['settings'], $form_state['values']['operation'], $form, $form_state); + } + } + } +} + +/** + * Allow filters, sources and destinations to present additional form elements + * to the end user before an action is submitted. + **/ +function backup_migrate_ui_action_form_pre_submit($form, &$form_state) { + if (!empty($form_state['storage']['values']) && !empty($form_state['values']['settings'])) { + // Check if there are any form elements to submit. + backup_migrate_filters_before_action_form_submit($form_state['values']['settings'], $form_state['values']['operation'], $form, $form_state); + // Clear the storage so the form can continue as normal + unset($form_state['storage']); + } +} + +/** + * Retrieve an array of form items to be filled out before an action can continue. + */ +function _backup_migrate_filter_before_action_form($settings, $op) { + $form = array(); + $form = backup_migrate_filters_before_action_form($settings, $op); + return $form; +} + +/** + * Perform an actual manual backup and tell the user of the progress. + */ +function backup_migrate_ui_manual_backup_perform($settings) { + // Peform the actual backup. + backup_migrate_perform_backup($settings); +} + +/** + * The restore/import upload page. + */ +function backup_migrate_ui_manual_restore() { + return drupal_get_form('backup_migrate_ui_manual_restore_form'); +} + +/** + * The restore/import upload form. + */ +function backup_migrate_ui_manual_restore_form() { + backup_migrate_include('filters', 'destinations', 'sources'); + + drupal_set_message(t('Restoring will delete some or all of your data and cannot be undone. Always test your backups on a non-production server!'), 'warning', FALSE); + + $form = array(); + + $from_default = (isset($_GET['page']) || isset($_GET['sort'])) ? 'saved' : 'upload'; + $form['backup_migrate_restore_from_upload'] = array( + "#type" => "radio", + "#title" => t('Restore from an uploaded file'), + '#return_value' => 'upload', + '#parents' => array('from'), + '#default_value' => $from_default, + ); + + $form['backup_migrate_restore_upload'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'from' => 'upload', + ), + ); + + $form['backup_migrate_restore_upload']['backup_migrate_restore_upload'] = array( + '#title' => t('Upload a Backup File'), + '#type' => 'file', + '#description' => t("Upload a backup file created by Backup and Migrate. For other database or file backups please use another tool for import. Max file size: %size", array("%size" => format_size(file_upload_max_size()))), + ); + + $form['backup_migrate_restore_from_saved'] = array( + "#type" => "radio", + "#title" => t('Restore from a saved backup'), + '#return_value' => 'saved', + '#parents' => array('from'), + '#default_value' => $from_default, + ); + + $form['backup_migrate_restore_saved'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'from' => 'saved', + ), + ); + + list($files, $fetch_time, $remote) = backup_migrate_get_saved_backups(); + $keys = array_keys($files); + $default = reset($keys); + $form['backup_migrate_restore_saved']['file'] = array( + '#type' => 'backup_migrate_file_list', + '#display_options' => array( + 'limit' => 5, + 'pager' => FALSE, + 'more' => BACKUP_MIGRATE_MENU_PATH . '/backups', + 'operations' => FALSE, + ), + '#default_value' => $default, + '#files' => $files, + ); + + $sources = _backup_migrate_get_source_form_item_options(); + if (count($sources) > 1) { + $form['source_id'] = array( + "#type" => "select", + "#title" => t("Restore to"), + "#options" => $sources, + "#description" => t("Choose where to restore to. Any database destinations you have created and any databases specified in your settings.php can be restored to."), + "#default_value" => 'db', + ); + } + else { + $form['source_id'] = array( + "#type" => "value", + "#value" => 'db', + ); + } + + $form = array_merge_recursive($form, backup_migrate_filters_settings_form(backup_migrate_filters_settings_default('restore'), 'restore')); + // Add the advanced fieldset if there are any fields in it. + if (@$form['advanced']) { + $form['advanced']['#type'] = 'fieldset'; + $form['advanced']['#title'] = t('Advanced Options'); + $form['advanced']['#collapsed'] = TRUE; + $form['advanced']['#collapsible'] = TRUE; + } + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Restore now'), + ); + if (user_access('access backup files')) { + $form[] = array( + '#type' => 'markup', + '#markup' => t('

Or you can restore one of the files in your saved backup destinations.

', array("!url" => url(BACKUP_MIGRATE_MENU_PATH . '/backups'))), + ); + } + $form['#attributes'] = array('enctype' => 'multipart/form-data'); + return $form; +} + +/** + * The restore submit. Do the restore. + */ +function backup_migrate_ui_manual_restore_form_submit($form, &$form_state) { + backup_migrate_include('destinations'); + $validators = array('file_validate_extensions' => array('gz zip sql mysql bz bz2')); + + $values = $form_state['values']; + if ($values['from'] == 'upload' && $file = file_save_upload('backup_migrate_restore_upload', $validators)) { + backup_migrate_perform_restore('upload', $file->uri, $values); + } + else if ($values['from'] == 'saved' && $file = $values['file']) { + list($destination, $file_id) = explode('/', $file, 2); + $file = + backup_migrate_perform_restore($destination, $file_id, $form_state['values']); + } + + $form_state['redirect'] = BACKUP_MIGRATE_MENU_PATH . '/restore'; +} + +/** + * Convert an item to an 'exportable'. + */ +function backup_migrate_ui_export_form($form, &$form_state, $item) { + if ($item && function_exists('ctools_var_export')) { + $code = ctools_var_export($item); + $form = ctools_export_form($form_state, $code); + return $form; + } + return array(); +} + +/** + * List the previously created backups from accross multiple destinations. + */ +function backup_migrate_ui_saved_backups() { + backup_migrate_include('profiles', 'destinations', 'sources'); + drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css'); + + $remote = FALSE; + $now = $fetch_time = time(); + $refresh = isset($_GET['refresh']); + $out = ''; + list($files, $fetch_time, $remote) = backup_migrate_get_saved_backups($refresh); + + // Redirect if refreshing instead of displaying. + if ($refresh) { + drupal_goto($_GET['q']); + } + + $fetch = $out = ''; + + // Get the fetch link. + if ($fetch_time < $now) { + $fetch = '
'. t('This listing was fetched !time ago. !refresh', array('!time' => format_interval($now - $fetch_time, 1), '!refresh' => l(t('fetch now'), $_GET['q'], array('query' => array('refresh' =>'true'))))) .'
'; + } + + // Render the list. + $out .= $fetch; + $out .= _backup_migrate_ui_destination_display_file_list($files, array('limit' => 20, 'pager' => TRUE)); + $out .= $fetch; + + // Add a remote backups warning if there are no remote backups. + if (!$remote && user_access('administer backup and migrate')) { + drupal_set_message( + t('You do not have any offsite backups. For the highest level of protection, create an offsite destination or set up NodeSquirrel to save your backups to.', + array('!addurl' => url(BACKUP_MIGRATE_MENU_PATH . '/settings/destination/add'), + '!nsurl' => url(BACKUP_MIGRATE_MENU_PATH . '/nodesquirrel') + )), 'warning', FALSE); + } + + return $out; +} + +/** + * List the previously created backups from accross multiple destinations. + */ +function backup_migrate_get_saved_backups($refresh = FALSE) { + backup_migrate_include('profiles', 'destinations', 'sources'); + drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css'); + + $remote = FALSE; + $now = $fetch_time = time(); + $out = ''; + $files = $sort = array(); + foreach (backup_migrate_get_destinations('list files') as $destination) { + $dest_id = $destination->get_id(); + $dest_files = $destination->list_files(); + if ($dest_files) { + $location = l($destination->get_name(), BACKUP_MIGRATE_MENU_PATH . '/settings/destination/list/files/' . $dest_id); + if ($destination->get('remote')) { + $location .= $destination->get('remote') ? ' (' . t('offsite') . ')' : ''; + $remote = TRUE; + } + + // Refresh the destination + if ($refresh) { + $destination->file_cache_clear(); + } + + // Get the file fetch time. + if ($destination->cache_files && $destination->fetch_time) { + $fetch_time = min($destination->fetch_time, $fetch_time); + } + + // Add the files from this destination to the full list. + foreach ($dest_files as $id => $file) { + $dest_files[$id]->file_info['bam_other_safe'][t('Location:')] = $location; + $sort[$dest_id . '/' . $id] = $file->info('filetime'); + $files[$dest_id . '/' . $id] = $file; + } + } + } + array_multisort($sort, SORT_DESC, $files); + + return array($files, $fetch_time, $remote); +} + +/** + * Perform a backup with the given settings. + */ +function backup_migrate_perform_backup(&$settings) { + backup_migrate_include('destinations', 'files', 'filters'); + timer_start('backup_migrate_backup'); + + // If not in 'safe mode', increase the maximum execution time: + if (!ini_get('safe_mode') && strpos(ini_get('disable_functions'), 'set_time_limit') === FALSE && ini_get('max_execution_time') < 1200) { + set_time_limit(variable_get('backup_migrate_backup_max_time', 1200)); + } + + // Confirm the destinations are valid + $valid = backup_migrate_destination_confirm_destination($settings); + if (!$valid) { + backup_migrate_backup_fail("Could not run backup because a there was no valid destination to save to.", array(), $settings); + return FALSE; + } + + // Create the temporary file object to be backed up into. + $filename = _backup_migrate_construct_filename($settings); + $file = new backup_file(array('filename' => $filename)); + if (!$file) { + backup_migrate_backup_fail("Could not run backup because a temporary file could not be created.", array(), $settings); + return FALSE; + } + + // Register shutdown callback to deal with timeouts. + register_shutdown_function('backup_migrate_shutdown', $settings); + + $file = backup_migrate_filters_backup($file, $settings); + if (!$file) { + if (_backup_migrate_check_timeout()) { + backup_migrate_backup_fail('Could not complete the backup because the script timed out. Try increasing your PHP max_execution_time setting.', array('!url' => 'http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time'), $settings); + } + else { + backup_migrate_backup_fail("Could not complete the backup.", array(), $settings); + } + return FALSE; + } + + $file = backup_migrate_destination_save_file($file, $settings); + if (!$file) { + backup_migrate_backup_fail("Could not run backup because the file could not be saved to the destination.", array(), $settings); + return FALSE; + } + + // Backup succeeded, + $time = timer_stop('backup_migrate_backup'); + $message = '%source backed up successfully to %file (%size) in destination %dest in !time. !action'; + $params = array( + '%file' => $filename, + '%dest' => $settings->get_destination_name(), + '%source' => $settings->get_source_name(), + '%size' => format_size($file->filesize()), + '!time' => backup_migrate_format_elapsed_time($time['time']), + '!action' => !empty($settings->performed_action) ? $settings->performed_action : '', + ); + if (($destination = $settings->get_destination()) && ($links = $destination->get_file_links($file->file_id()))) { + $params['!links'] = implode(", ", $links); + } + + backup_migrate_backup_succeed($message, $params, $settings); + return $file; +} + +/** + * Format an elapsed time given in milleseconds. + */ + +function backup_migrate_format_elapsed_time($time) { + $ms = t('!time ms', array('!time' => $time)); + if ($time < 1000) { + return $ms; + } + return '' . format_interval($time / 1000) . ''; +} + +/** + * Restore from a file in the given destination. + */ +function backup_migrate_perform_restore($destination_id, $file, $settings = array()) { + backup_migrate_include('files', 'filters'); + timer_start('backup_migrate_restore'); + + // If not in 'safe mode', increase the maximum execution time: + if (!ini_get('safe_mode') && ini_get('max_execution_time') < variable_get('backup_migrate_backup_max_time', 1200)) { + set_time_limit(variable_get('backup_migrate_restore_max_time', 1200)); + } + + // Make the settings into a default profile. + if (!is_object($settings)) { + $settings = backup_migrate_crud_create_item('profile', $settings); + $settings->source_id = empty($settings->source_id) ? 'db' : $settings->source_id; + } + + // Register shutdown callback. + register_shutdown_function('backup_migrate_shutdown', $settings); + + if (!is_object($file)) { + // Load the file from the destination. + $file = backup_migrate_destination_get_file($destination_id, $file); + if (!$file) { + _backup_migrate_message("Could not restore because the file could not be loaded from the destination.", array(), 'error'); + backup_migrate_cleanup(); + return FALSE; + } + } + + // Filter the file and perform the restore. + $file = backup_migrate_filters_restore($file, $settings); + if (!$file) { + if (_backup_migrate_check_timeout()) { + backup_migrate_restore_fail('Could not perform the restore because the script timed out. Try increasing your PHP max_execution_time setting.', array('!url' => 'http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time'), 'error'); + } + else { + backup_migrate_restore_fail("Could not perform the restore.", array(), 'error'); + } + backup_migrate_cleanup(); + return FALSE; + } + + $time = timer_stop('backup_migrate_restore'); + if ($file) { + $destination = backup_migrate_get_destination($destination_id); + $message = '%source restored from %dest file %file in !time. !action'; + $params = array( + '%file' => $file->filename(), + '%source' => $settings->get_source_name(), + '%dest' => $destination->get_name(), + '!time' => backup_migrate_format_elapsed_time($time['time']), + '!action' => !empty($settings->performed_action) ? $settings->performed_action : '', + ); + if ($destination && $destination->op('list files')) { + $path = $destination->get_settings_path(); + $params['!links'] = t('Restore again', array('!restoreurl' => url($path . '/list/restorefile/' . $destination->get_id() .'/'. $file->file_id()))); + } + backup_migrate_restore_succeed($message, $params, $settings); + } + // Delete any temp files we've created. + backup_migrate_cleanup(); + + // No errors. Return the file. + return $file; +} + +/** + * Clean up when a backup operation fails. + */ +function backup_migrate_backup_fail($message, $params, $settings) { + backup_migrate_include('files', 'filters'); + + _backup_migrate_message($message, $params, 'error'); + backup_migrate_cleanup(); + backup_migrate_filters_invoke_all('backup_fail', $settings, $message, $params); + return FALSE; +} + +/** + * Clean up when a backup operation suceeds. + */ +function backup_migrate_backup_succeed($message, $params, $settings) { + backup_migrate_include('filters', 'files'); + _backup_migrate_message($message, $params, 'success'); + backup_migrate_cleanup(); + backup_migrate_filters_invoke_all('backup_succeed', $settings, $message, $params); + return FALSE; +} + +/** + * Clean up when a restore operation fails. + */ +function backup_migrate_restore_fail($message, $params, $settings) { + backup_migrate_include('files', 'filters'); + _backup_migrate_message($message, $params, 'error'); + backup_migrate_cleanup(); + backup_migrate_filters_invoke_all('restore_fail', $settings, $message, $params); + return FALSE; +} + +/** + * Clean up when a restore operation suceeds. + */ +function backup_migrate_restore_succeed($message, $params, $settings) { + backup_migrate_include('filters', 'files'); + _backup_migrate_message($message, $params, 'success'); + backup_migrate_cleanup(); + backup_migrate_filters_invoke_all('restore_succeed', $settings, $message, $params); + return FALSE; +} + + +/** + * Cleanup after a success or failure. + */ +function backup_migrate_cleanup() { + // Check that the cleanup function exists. If it doesn't then we probably didn't create any files to be cleaned up. + if (function_exists('_backup_migrate_temp_files_delete')) { + _backup_migrate_temp_files_delete(); + } +} + + +/** + * Shutdown callback. Called when the script terminates even if the script timed out. + */ +function backup_migrate_shutdown($settings) { + // If we ran out of time, set an error so the user knows what happened + if (_backup_migrate_check_timeout()) { + backup_migrate_cleanup(); + backup_migrate_backup_fail('The operation timed out. Try increasing your PHP max_execution_time setting.', array('!url' => 'http://www.php.net/manual/en/info.configuration.php#ini.max-execution-time'), $settings); + // The session will have already been written and closed, so we need to write any changes directly. + _drupal_session_write(session_id(), session_encode()); + // Add a redirect or we'll just get whitescreened. + drupal_goto(BACKUP_MIGRATE_MENU_PATH); + } +} + + + +/* Actions/Workflow integration */ + +/** + * Action to backup the drupal site. Requires actions.module. +function action_backup_migrate_backup($op, $edit = array()) { + switch ($op) { + case 'do': + _backup_migrate_backup_with_defaults(); + watchdog('action', 'Backed up database'); + break; + + case 'metadata': + return array( + 'description' => t('Backup the database with the default settings'), + 'type' => t('Backup and Migrate'), + 'batchable' => TRUE, + 'configurable' => FALSE, + ); + + // Return an HTML config form for the action. + + case 'form': + return ''; + + // Validate the HTML form. + + case 'validate': + return TRUE; + + // Process the HTML form to store configuration. + + case 'submit': + return ''; + } +} + */ + +/* + * Implements hook_action_info(). +function backup_migrate_action_info() { + return array( + 'backup_migrate_action_backup' => array( + 'label' => t('Backup the database'), + 'description' => t('Backup the database with the default settings.'), + ), + ); +} + */ + +/* + * Action callback. + */ +function backup_migrate_action_backup() { + _backup_migrate_backup_with_defaults(); +} + +/* Utilities */ + +/** + * Backup the database with the default settings. + */ +function _backup_migrate_backup_with_defaults($destination_id = "manual") { + backup_migrate_include('files', 'profiles'); + + $settings = _backup_migrate_profile_saved_default_profile(); + $settings->destination_id = $destination_id; + $settings->source_id = 'db'; + backup_migrate_perform_backup($settings); +} + +/** + * Helper function to set a drupal message and watchdog message depending on whether the module is being run interactively. + */ +function _backup_migrate_message($message, $replace = array(), $type = 'status') { + // Only set a message if there is a callback handler to handle the message. + if (($callback = _backup_migrate_message_callback()) && function_exists($callback)) { + $callback($message, $replace, $type); + } + + // Store the message in case it's needed (for the status notification filter for example). + _backup_migrate_messages($message, $replace, $type); +} + +/** + * Helper function to set a drupal message and watchdog message depending on whether the module is being run interactively. + */ +function _backup_migrate_messages($message = NULL, $replace = array(), $type = 'status') { + static $messages = array(); + if ($message) { + $messages[] = array('message' => $message, 'replace' => $replace, 'type' => $type); + } + return $messages; +} + +/** + * Send a message to the browser. The normal type of message handling for interactive use. + */ +function _backup_migrate_message_browser($message, $replace, $type) { + // Log the message as well for admins. + _backup_migrate_message_log($message, $replace, $type); + + // If there are links, we can display them in the browser. + if (!empty($replace['!links'])) { + $message .= " (!links)"; + } + // Use drupal_set_message to display to the user. + drupal_set_message(t($message, $replace), str_replace('success', 'status', $type), FALSE); +} + +/** + * Log message if we are in a non-interactive mode such as a cron run. + */ +function _backup_migrate_message_log($message, $replace, $type) { + // We only want to log the errors or successful completions. + if (in_array($type, array('error', 'success'))) { + watchdog('backup_migrate', $message, $replace, $type == 'error' ? WATCHDOG_ERROR : WATCHDOG_NOTICE); + } +} + +/** + * Set or retrieve a message handler. + */ +function _backup_migrate_message_callback($callback = NULL) { + static $current_callback = '_backup_migrate_message_log'; + if ($callback !== NULL) { + $current_callback = $callback; + } + return $current_callback; +} + +function _backup_migrate_check_timeout() { + static $timeout; + + // Max execution of 0 means unlimited. + if (ini_get('max_execution_time') == 0) { + return FALSE; + } + // Figure out when we should stop execution. + if (!$timeout) { + $timeout = (!empty($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : time()) + ini_get('max_execution_time') - variable_get('backup_migrate_timeout_buffer', 5); + } + return (time() > $timeout); +} + +/** + * Convert an associated array to an ini format string. + */ +function _backup_migrate_array_to_ini($data, $prefix = '') { + $content = ""; + foreach ($data as $key => $val) { + if ($prefix) { + $key = $prefix . '[' . $key .']'; + } + if (is_array($val)) { + $content .= _backup_migrate_array_to_ini($val, $key); + } + else { + $content .= $key . " = \"". $val ."\"\n"; + } + } + return $content; +} + +/** + * Execute a command line command. Returns false if the function failed. + */ +function backup_migrate_exec($command, $args = array()) { + if (!function_exists('exec') || ini_get('safe_mode')) { + return FALSE; + } + + // Escape the arguments + foreach ($args as $key => $arg) { + $args[$key] = escapeshellarg($arg); + } + $command = strtr($command, $args); + $output = $result = NULL; + + // Run the command. + exec($command, $output, $result); + + return $result == 0; +} + +/** + * A custom version of format size which treats 1GB as 1000 MB rather than 1024 MB + * This is a more standard and expected version for storage (as opposed to memory). + */ +function backup_migrate_format_size($size, $langcode = LANGUAGE_NONE) { + $precision = 2; + $multiply = pow(10, $precision); + + if ($size == 0) { + return t('0 bytes', array(), array('langcode' => $langcode)); + } + if ($size < 1024) { + return format_plural($size, '1 byte', '@count bytes', array(), array('langcode' => $langcode)); + } + else { + $size = ceil($size * $multiply / 1024); + $string = '@size KB'; + if ($size >= (1024 * $multiply)) { + $size = ceil($size / 1024); + $string = '@size MB'; + } + if ($size >= 1000 * $multiply) { + $size = ceil($size / 1000); + $string = '@size GB'; + } + if ($size >= 1000 * $multiply) { + $size = ceil($size / 1000); + $string = '@size TB'; + } + return t($string, array('@size' => round($size/$multiply, $precision)), array('langcode' => $langcode)); + } + +} diff --git a/sites/all/modules/contrib/backup_migrate/includes/backup_migrate.drush.inc b/sites/all/modules/contrib/backup_migrate/includes/backup_migrate.drush.inc new file mode 100644 index 0000000..022090d --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/backup_migrate.drush.inc @@ -0,0 +1,277 @@ + 'backup_migrate_drush_backup', + 'description' => dt('Backup the site\'s database with Backup and Migrate.'), + 'aliases' => array('bb'), + 'examples' => array( + 'drush bam-backup' => 'Backup the default databse to the manual backup directory using the default settings.', + 'drush bam-backup db scheduled mysettings' => 'Backup the database to the scheduled directory using a settings profile called "mysettings"', + 'drush bam-backup files' => 'Backup the files directory to the manual directory using the default settings. The Backup and Migrate Files module is required for files backups.', + ), + 'arguments' => array( + 'source' => "Optional. The id of the source (usually a database) to backup. Use 'drush bam-sources' to get a list of sources. Defaults to 'db'", + 'destination' => "Optional. The id of destination to send the backup file to. Use 'drush bam-destinations' to get a list of destinations. Defaults to 'manual'", + 'profile' => "Optional. The id of a settings profile to use. Use 'drush bam-profiles' to get a list of available profiles. Defaults to 'default'", + ), + ); + $items['bam-restore'] = array( + 'callback' => 'backup_migrate_drush_restore', + 'description' => dt('Restore the site\'s database with Backup and Migrate.'), + 'arguments' => array( + 'source' => "Required. The id of the source (usually a database) to restore the backup to. Use 'drush bam-sources' to get a list of sources. Defaults to 'db'", + 'destination' => "Required. The id of destination to send the backup file to. Use 'drush bam-destinations' to get a list of destinations. Defaults to 'manual'", + 'backup id' => "Required. The id of a backup file restore. Use 'drush bam-backups' to get a list of available backup files.", + ), + 'options' => array( + 'yes' => 'Skip confirmation', + ), + ); + $items['bam-destinations'] = array( + 'callback' => 'backup_migrate_drush_destinations', + 'description' => dt('Get a list of available destinations.'), + ); + + $items['bam-sources'] = array( + 'callback' => 'backup_migrate_drush_sources', + 'description' => dt('Get a list of available sources.'), + ); + $items['bam-profiles'] = array( + 'callback' => 'backup_migrate_drush_profiles', + 'description' => dt('Get a list of available settings profiles.'), + ); + $items['bam-backups'] = array( + 'callback' => 'backup_migrate_drush_destination_files', + 'description' => dt('Get a list of previously created backup files.'), + 'arguments' => array( + 'destination' => "Optional. The id of destination to list backups from. Use 'drush bam-destinations' to get a list of destinations.", + ), + ); + return $items; +} + +/** + * Implementation of hook_drush_help(). + */ +function backup_migrate_drush_help($section) { + switch ($section) { + case 'drush:bam-backup': + return dt("Backup the site's database using default settings."); + case 'drush:bam-restore': + return dt('Restore the site\'s database with Backup and Migrate.'); + case 'drush:bam-destinations': + return dt('Get a list of available destinations.'); + case 'drush:bam-profiles': + return dt('Get a list of available settings profiles.'); + case 'drush:bam-backups': + return dt('Get a list of previously created backup files.'); + } +} + +/** + * Backup the default database. + */ +function backup_migrate_drush_backup($source_id = 'db', $destination_id = 'manual', $profile_id = 'default') { + backup_migrate_include('profiles', 'destinations', 'sources'); + + // Set the message mode to logging. + _backup_migrate_message_callback('_backup_migrate_message_drush'); + + if (!backup_migrate_get_source($source_id)) { + _backup_migrate_message("Could not find the source '@source'. Try using 'drush bam-sources' to get a list of available sources or use 'db' to backup the Drupal database.", array('@source' => $source_id), 'error'); + return; + } + if (!backup_migrate_get_destination($destination_id)) { + _backup_migrate_message("Could not find the destination '@destination'. Try using 'drush bam-destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error'); + return; + } + $settings = backup_migrate_get_profile($profile_id); + if(!$settings) { + _backup_migrate_message("Could not find the profile '@profile'. Try using 'drush bam-profiles' to get a list of available profiles.", array('@profile' => $profile_id), 'error'); + return; + } + + _backup_migrate_message('Starting backup...'); + $settings->destination_id = $destination_id; + $settings->source_id = $source_id; + backup_migrate_perform_backup($settings); +} + +/** + * Restore to the default database. + */ +function backup_migrate_drush_restore($source_id = '', $destination_id = '', $file_id = '') { + + backup_migrate_include('profiles', 'destinations', 'sources'); + + // Set the message mode to drush output. + _backup_migrate_message_callback('_backup_migrate_message_drush'); + + if (!backup_migrate_get_source($source_id)) { + _backup_migrate_message("Could not find the source '@source'. Try using 'drush bam-sources' to get a list of available sources or use 'db' to backup the Drupal database.", array('@source' => $source_id), 'error'); + return; + } + if (!$destination = backup_migrate_get_destination($destination_id)) { + _backup_migrate_message("Could not find the destination '@destination'. Try using 'drush bam-destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error'); + return; + } + else if (!$file_id || !$file = backup_migrate_destination_get_file($destination_id, $file_id)) { + _backup_migrate_message("Could not find the file '@file'. Try using 'drush bam-backups @destination' to get a list of available backup files in this destination destinations.", array('@destination' => $destination_id, '@file' => $file_id), 'error'); + return; + } + + drush_print(dt('Restoring will delete some or all of your data and cannot be undone. ALWAYS TEST YOUR BACKUPS ON A NON-PRODUCTION SERVER!')); + if (!drush_confirm(dt('Are you sure you want to perform the restore?'))) { + return drush_user_abort(); + } + _backup_migrate_message('Starting restore...'); + $settings = array('source_id' => $source_id); + backup_migrate_perform_restore($destination_id, $file_id, $settings); +} + +/** + * Get a list of available destinations. + */ +function backup_migrate_drush_destinations() { + return _backup_migrate_drush_destinations('all'); +} + +/** + * Get a list of available sources. + */ +function backup_migrate_drush_sources() { + return _backup_migrate_drush_sources('source'); +} + + +/** + * Get a list of available destinations with the given op. + */ +function _backup_migrate_drush_destinations($op = NULL) { + backup_migrate_include('destinations'); + $rows = array(array(dt('ID'), dt('Name'), dt('Operations'))); + foreach (backup_migrate_get_destinations($op) as $destination) { + $rows[] = array( + $destination->get_id(), + $destination->get_name(), + implode (', ', $destination->ops()), + ); + } + drush_print_table($rows, TRUE, array(32, 32)); +} + + +/** + * Get a list of available destinations with the given op. + */ +function _backup_migrate_drush_sources($op = NULL) { + backup_migrate_include('sources'); + $rows = array(array(dt('ID'), dt('Name'), dt('Operations'))); + foreach (backup_migrate_get_sources($op) as $destination) { + $rows[] = array( + $destination->get_id(), + $destination->get_name(), + implode (', ', $destination->ops()), + ); + } + drush_print_table($rows, TRUE, array(32, 32)); +} + +/** + * Get a list of available profiles. + */ +function backup_migrate_drush_profiles() { + backup_migrate_include('profiles'); + $rows = array(array(dt('ID'), dt('Name'))); + foreach (backup_migrate_get_profiles() as $profile) { + $rows[] = array( + $profile->get_id(), + $profile->get_name(), + ); + } + drush_print_table($rows, TRUE, array(32, 32)); +} + +/** + * Get a list of files in a given destination + */ +function backup_migrate_drush_destination_files($destination_id = NULL) { + backup_migrate_include('destinations'); + $destinations = array(); + + // Set the message mode to drush output. + _backup_migrate_message_callback('_backup_migrate_message_drush'); + if ($destination_id && !$destination = backup_migrate_get_destination($destination_id)) { + _backup_migrate_message("Could not find the destination '@destination'. Try using 'drush bam-destinations' to get a list of available destinations.", array('@destination' => $destination_id), 'error'); + return; + } + + // Single destination required. + if ($destination) { + $destinations = array($destination); + } + // List all destinations + else { + $destinations = backup_migrate_get_destinations('list files'); + } + + // Load all the files. + $rows = $sort = array(); + foreach ($destinations as $destination) { + $destination->file_cache_clear(); + $dest_files = $destination->list_files(); + foreach ($dest_files as $id => $file) { + $info = $file->info(); + $rows[] = array( + check_plain($info['filename']), + $destination->get_id(), + format_date($info['filetime'], 'small'), + format_interval(time() - $info['filetime'], 1), + format_size($info['filesize']), + ); + $sort[] = $info['filetime']; + } + } + + $headers = array(array( + dt('Filename'), + dt('Destination'), + dt('Date'), + dt('Age'), + dt('Size'), + )); + + if (count($rows)) { + array_multisort($sort, SORT_DESC, $rows); + drush_print_table(array_merge($headers, $rows), TRUE); + } + else { + drush_print(dt('There are no backup files to display.')); + } +} + +/** + * Send a message to the drush log. + */ +function _backup_migrate_message_drush($message, $replace, $type) { + // If this is an error use drush_set_error to notify the end user and set the exit status + if ($type == 'error') { + drush_set_error(strip_tags(dt($message, $replace))); + } + else { + // Use drush_log to display to the user. + drush_log(strip_tags(dt($message, $replace)), str_replace('status', 'notice', $type)); + } + // Watchdog log the message as well for admins. + _backup_migrate_message_log($message, $replace, $type); +} diff --git a/sites/all/modules/contrib/backup_migrate/includes/crud.inc b/sites/all/modules/contrib/backup_migrate/includes/crud.inc new file mode 100644 index 0000000..c6b0dcf --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/crud.inc @@ -0,0 +1,989 @@ + array( + 'class' => 'backup_migrate_schedule', + 'include' => 'schedules', + ), + 'source' => array( + 'class' => 'backup_migrate_source', + 'include' => 'sources', + ), + 'destination' => array( + 'class' => 'backup_migrate_destination', + 'include' => 'destinations', + ), + 'profile' => array( + 'class' => 'backup_migrate_profile', + 'include' => 'profiles', + ), + ); + return $out; +} + +/** + * Get the info for a particular crud type. + */ +function backup_migrate_crud_type_info($type) { + $types = backup_migrate_crud_types(); + if (isset($types[$type])) { + return $types[$type]; + } + return NULL; +} + +/** + * Get a list of avaiable classes of each crud type. + */ +function backup_migrate_crud_subtypes($type) { + $out = array(); + if ($info = backup_migrate_crud_type_info($type)) { + // Include the function that contains the type base. + if (!empty($info['include'])) { + backup_migrate_include($info['include']); + } + + // Allow modules (including this one) to declare backup and migrate subtypes. + // We don't use module_invoke_all so we can avoid the side-effects of array_merge_recursive. + $out = array(); + foreach (module_implements('backup_migrate_' . $type . '_subtypes') as $module) { + $function = $module . '_backup_migrate_' . $type . '_subtypes'; + $result = $function(); + if (isset($result) && is_array($result)) { + foreach ($result as $key => $val) { + $out[$key] = $val; + } + } + } + } + + return $out; +} + +/** + * Get the info for a particular crud subtype. + */ +function backup_migrate_crud_subtype_info($type, $subtype) { + $types = backup_migrate_crud_subtypes($type); + if (isset($types[$subtype])) { + return $types[$subtype]; + } + return NULL; +} + +/** + * Get a generic object of the given type to be used for static-like functions. + * + * I'm not using actual static method calls since they don't work on variables prior to PHP 5.3.0 + */ +function backup_migrate_crud_type_load($type, $subtype = NULL) { + $out = $info = NULL; + + if ($subtype) { + $info = backup_migrate_crud_subtype_info($type, $subtype); + } + else { + $info = backup_migrate_crud_type_info($type); + } + + if (!empty($info)) { + if (!empty($info['include'])) { + backup_migrate_include($info['include']); + } + if (!empty($info['file'])) { + include_once './'. (isset($info['path']) ? $info['path'] : '') . $info['file']; + } + + if (class_exists($info['class'])) { + $out = new $info['class']; + $out = $out->create(array('subtype' => $subtype)); + } + } + return $out; +} + +/** + * Page callback to create a new item. + */ +function backup_migrate_crud_create($type, $subtype = NULL) { + if ($item = backup_migrate_crud_type_load($type, $subtype)) { + return $item; + } + return NULL; +} + +/** + * Get the menu items handled by the CRUD code. + */ +function backup_migrate_crud_menu() { + $items = array(); + foreach (backup_migrate_crud_types() as $type => $info) { + $item = backup_migrate_crud_type_load($type); + $items += (array)$item->get_menu_items(); + foreach (backup_migrate_crud_subtypes($type) as $subtype => $info) { + $subitem = backup_migrate_crud_type_load($type, $subtype); + $items += (array)$subitem->get_menu_items(); + } + } + return $items; +} + +/** + * Page callback to create a new item. + */ +function backup_migrate_crud_ui_create($type, $subtype = NULL) { + if ($item = backup_migrate_crud_create($type, $subtype)) { + return drupal_get_form('backup_migrate_crud_edit_form', $item); + } + return drupal_not_found(); +} + +/** + * Page callback to list all items. + */ +function backup_migrate_crud_ui_list($type) { + $out = ''; + if ($type = backup_migrate_crud_type_load($type)) { + $out = $type->get_list(); + } + return $out; +} + +/** + * Page callback to list all items. + */ +function backup_migrate_crud_ui_list_all() { + $out = ''; + foreach (backup_migrate_crud_types() as $type => $info) { + $type = backup_migrate_crud_type_load($type); + $out[] = theme('backup_migrate_group', array('title' => t($type->title_plural), 'body' => $type->get_list())); + } + return implode('', $out); +} + + +/** + * Page callback to edit an item. + */ +function backup_migrate_crud_ui_edit($type, $item_id = NULL) { + if ($type = backup_migrate_crud_type_load($type)) { + if ($item_id && $item = $type->item($item_id)) { + return drupal_get_form('backup_migrate_crud_edit_form', $item); + } + drupal_goto($type->get_settings_path()); + } +} + +/** + * Does a crud item with the given name exist. + * + * Callback for the 'machine_name' form type. + */ +function backup_migrate_crud_item_exists($machine_name, $element, $form_state) { + return $form_state['values']['item']->item_exists($machine_name); +} + +/** + * A form callback to edit an item. + */ +function backup_migrate_crud_edit_form($form, $form_state, $item) { + $form = $item->edit_form(); + + $form['item'] = array( + '#type' => 'value', + '#value' => $item, + ); + $form['#validate'][] = 'backup_migrate_crud_edit_form_validate'; + $form['#submit'][] = 'backup_migrate_crud_edit_form_submit'; + + return $form; +} + +/** + * Validate the item edit form. + */ +function backup_migrate_crud_edit_form_validate($form, &$form_state) { + $item = $form_state['values']['item']; + $item->edit_form_validate($form, $form_state); +} + +/** + * Submit the item edit form. + */ +function backup_migrate_crud_edit_form_submit($form, &$form_state) { + $item = $form_state['values']['item']; + + $item->edit_form_submit($form, $form_state); + if (empty($form_state['redirect'])) { + $form_state['redirect'] = $item->get_settings_path(); + } +} + + +/** + * Page callback to delete an item. + */ +function backup_migrate_crud_ui_delete($type, $item_id = NULL) { + if ($type = backup_migrate_crud_type_load($type)) { + if ($item_id && $item = $type->item($item_id)) { + return drupal_get_form('backup_migrate_crud_delete_confirm_form', $item); + } + drupal_goto($type->get_settings_path()); + } +} + +/** + * Ask confirmation for deletion of a item. + */ +function backup_migrate_crud_delete_confirm_form($form, &$form_state, $item) { + $form['item'] = array( + '#type' => 'value', + '#value' => $item, + ); + if ($item->storage == BACKUP_MIGRATE_STORAGE_OVERRIDEN) { + $message = $item->revert_confirm_message(); + return confirm_form($form, t('Are you sure?'), $item->get_settings_path(), $message, t('Revert'), t('Cancel')); + } else { + $message = $item->delete_confirm_message(); + return confirm_form($form, t('Are you sure?'), $item->get_settings_path(), $message, t('Delete'), t('Cancel')); + } +} + +/** + * Delete a item after confirmation. + */ +function backup_migrate_crud_delete_confirm_form_submit($form, &$form_state) { + if ($form_state['values']['confirm']) { + $item = $form_state['values']['item']; + $item->delete(); + } + $form_state['redirect'] = $item->get_settings_path(); +} + +/** + * Export an item. + */ +function backup_migrate_crud_ui_export($type, $item_id = NULL) { + if ($type = backup_migrate_crud_type_load($type)) { + if ($item_id && $item = $type->item($item_id)) { + return drupal_get_form('backup_migrate_crud_export_form', $item->export()); + } + drupal_goto($type->get_settings_path()); + } +} + +/** + * Ask confirmation for deletion of a destination. + */ +function backup_migrate_crud_export_form($form, &$form_state, $export) { + $form['export'] = array( + '#title' => t('Exported content'), + '#type' => 'textarea', + '#rows' => min(30, count(explode("\n", $export))), + '#value' => $export, + ); + return $form; +} + +/** + * Page callback to import an item. + */ +function backup_migrate_crud_ui_import() { + return drupal_get_form('backup_migrate_crud_import_form'); +} + +/** + * Ask confirmation for deletion of a item. + */ +function backup_migrate_crud_import_form($form, &$form_state) { + $form['code'] = array( + '#type' => 'textarea', + '#title' => t('Paste Exported Code Here'), + '#required' => TRUE, + ); + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Import'), + ); + return $form; +} + +/** + * Validate handler to import a view + */ +function backup_migrate_crud_import_form_validate($form, &$form_state) { + $item = backup_migrate_crud_create_from_import($form_state['values']['code']); + if ($item) { + $form_state['values']['item'] = $item; + } + else { + form_set_error('code', t('Unable to import item.')); + } +} + +/** + * import a item after confirmation. + */ +function backup_migrate_crud_import_form_submit($form, &$form_state) { + $item = $form_state['values']['item']; + $item->save(); + _backup_migrate_message('Your !type was imported', array('!type' => t($item->singular))); + $form_state['redirect'] = $item->get_settings_path(); +} + +/** + * Create an object from the exported object. + */ +function backup_migrate_crud_create_from_import($code) { + $item = NULL; + $code = 'return ' . $code . ';'; + ob_start(); + $values = eval($code); + ob_end_clean(); + + if ($values) { + if (!empty($values['type_name']) && $type = backup_migrate_crud_type_load($values['type_name'])) { + $item = $type->create($values); + // Make sure the item's ID doesn't already exist. + $item->unique_id(); + } + } + return $item; +} + +/** + * Get all items of the given type. + */ +function backup_migrate_crud_get_items($type) { + if ($type = backup_migrate_crud_type_load($type)) { + return $type->all_items(); + } +} + + +/** + * Get an item of the specified type. + */ +function backup_migrate_crud_get_item($type, $id) { + if ($type = backup_migrate_crud_type_load($type)) { + return $type->item($id); + } +} + +/** + * Create a new item of the given type. + */ +function backup_migrate_crud_create_item($type, $params) { + if ($type = backup_migrate_crud_type_load($type)) { + return $type->create($params); + } +} + +/** + * A base class for items which can be stored in the database, listed, edited, deleted etc. + */ +class backup_migrate_item { + var $show_in_list = TRUE; + var $settings_path = '/settings/'; + var $db_table = ''; + var $type_name = ''; + var $storage = FALSE; + var $default_values = array(); + var $singular = 'item'; + var $plural = 'items'; + var $title_plural = 'Items'; + var $title_singular = 'Item'; + + /** + * This function is not supposed to be called. It is just here to help the po extractor out. + */ + function strings() { + // Help the pot extractor find these strings. + t('item'); + t('items'); + t('Items'); + t('Item'); + + // Help the pot extractor find these strings. + t('List !type'); + t('Create !type'); + t('Delete !type'); + t('Edit !type'); + t('Export !type'); + } + + + /** + * Constructor, set the basic info pulled from the db or generated programatically. + */ + function __construct($params = array()) { + $this->from_array($this->_merge_defaults((array)$params, (array)$this->get_default_values())); + } + + /** + * Merge parameters with the given defaults. + * + * Works like array_merge_recursive, but it doesn't turn scalar values into arrays. + */ + function _merge_defaults($params, $defaults) { + foreach ($defaults as $key => $val) { + if (!isset($params[$key])) { + $params[$key] = $val; + } + else if (is_array($params[$key])) { + $params[$key] = $this->_merge_defaults($params[$key], $val); + } + } + return $params; + } + + /** + * Get the default values for standard parameters. + */ + function get_default_values() { + return $this->default_values; + } + + /** + * Save the item to the database. + */ + function save() { + if (!$this->get_id()) { + $this->unique_id(); + } + $record = $this->to_array(); + drupal_write_record($this->db_table, $record, !empty($this->storage) ? $this->get_primary_key() : array()); + } + + /** + * Delete the item from the database. + */ + function delete() { + $keys = (array)$this->get_machine_name_field(); + db_query('DELETE FROM {' . $this->db_table . '} WHERE ' . $keys[0] . ' = :id', array(':id' => $this->get_id())); + } + + + /** + * Load an existing item from an array. + */ + function from_array($params) { + foreach ($params as $key => $value) { + if (method_exists($this, 'set_'. $key)) { + $this->{'set_'. $key}($value); + } + else { + $this->{$key} = $value; + } + } + } + + /** + * Return as an array of values. + */ + function to_array() { + $out = array(); + // Return fields as specified in the schema. + $schema = $this->get_schema(); + if (!empty($schema['fields']) && is_array($schema['fields'])) { + foreach ($schema['fields'] as $field => $info) { + $out[$field] = $this->get($field); + } + } + return $out; + } + + /** + * Return as an exported array of values. + */ + function export() { + $out = $this->to_array(); + $out['type_name'] = $this->type_name; + + ob_start(); + var_export($out); + $out = ob_get_contents(); + ob_end_clean(); + return $out; + } + + /** + * Load an existing item from an database (serialized) array. + */ + function load_row($data) { + $params = array(); + $schema = $this->get_schema(); + // Load fields as specified in the schema. + foreach ($schema['fields'] as $field => $info) { + $params[$field] = empty($info['serialize']) ? $data[$field] : unserialize($data[$field]); + } + $this->from_array($params); + } + + + /** + * Decode a loaded db row (unserialize necessary fields). + */ + function decode_db_row($data) { + $params = array(); + $schema = $this->get_schema(); + // Load fields as specified in the schema. + foreach ($schema['fields'] as $field => $info) { + $params[$field] = empty($info['serialize']) ? $data[$field] : unserialize($data[$field]); + } + return $params; + } + + /** + * Return the fields which must be serialized before saving to the db. + */ + function get_serialized_fields() { + $out = array(); + $schema = $this->get_schema(); + foreach ($schema['fields'] as $field => $info) { + if (!empty($info['serialize'])) { + $out[] = $field; + } + } + return $out; + } + + /** + * Get the primary key field title from the schema. + */ + function get_primary_key() { + $schema = $this->get_schema(); + return @$schema['primary key']; + } + + /** + * Get the machine name field name from the schema. + */ + function get_machine_name_field() { + $schema = $this->get_schema(); + if (isset($schema['export']['key'])) { + return $schema['export']['key']; + } + return @$schema['primary key']; + } + + /** + * Get the schema for the item type. + */ + function get_schema() { + return drupal_get_schema($this->db_table); + } + + /** + * Get the primary id for this item (if any is set). + * + * We only handle single field keys since that's all we need. + */ + function get_id() { + $keys = (array)$this->get_machine_name_field(); + return !empty($keys[0]) && !empty($this->{$keys[0]}) ? (string)$this->{$keys[0]} : ''; + } + + /** + * Set the primary id for this item (if any is set). + */ + function set_id($id) { + $keys = (array)$this->get_machine_name_field(); + if (!empty($keys[0])) { + return $this->{$keys[0]} = $id; + } + return NULL; + } + + /** + * Return a random (very very likely unique) string id for a new item. + */ + function generate_id() { + $id = md5(uniqid(mt_rand(), true)); + + // Find the shortest possible unique id from (min 4 chars). + for ($i = 4; $i < 32; $i++) { + $new_id = substr($id, 0, $i); + if (!$this->item($new_id)) { + return $new_id; + } + } + // If we get here, then all 28 increasingly complex ids were already taken so we'll try again. + // this could theoretially lead to an infinite loop, but the odds are incredibly low. + return $this->generate_id(); + } + + /** + * Make sure this item has a unique id. Should only be called for new items or the item will collide with itself. + */ + function unique_id() { + $id = $this->get_id(); + + // Unset the autoincrement field so it can be regenerated. + foreach ((array)$this->get_primary_key() as $key) { + $this->{$key} = NULL; + } + + // If the item doesn't have an ID or if it's id is already taken, generate random one. + if (!$id || $this->item($id)) { + $this->set_id($this->generate_id()); + } + } + + /** + * Get the name of the item. + */ + function get_name() { + return @$this->name; + } + + /** + * Get the member with the given key. + */ + function get($key) { + if (method_exists($this, 'get_'. $key)) { + return $this->{'get_'. $key}(); + } + return @$this->{$key}; + } + + /* UI Stuff */ + + /** + * Get the action links for a destination. + */ + function get_action_links() { + $out = array(); + + $item_id = $this->get_id(); + + $path = $this->get_settings_path(); + + if (@$this->storage == BACKUP_MIGRATE_STORAGE_DB || @$this->storage == BACKUP_MIGRATE_STORAGE_OVERRIDEN) { + $out['edit'] = l(t("edit"), $path . "/edit/$item_id"); + } + else if (@$this->storage == BACKUP_MIGRATE_STORAGE_NONE) { + $out['edit'] = l(t("override"), $path . "/edit/$item_id"); + } + if (@$this->storage == BACKUP_MIGRATE_STORAGE_DB) { + $out['delete'] = l(t("delete"), $path . "/delete/$item_id"); + } + else if (@$this->storage == BACKUP_MIGRATE_STORAGE_OVERRIDEN) { + $out['delete'] = l(t("revert"), $path . "/delete/$item_id"); + } + $out['export'] = l(t("export"), $path . "/export/$item_id"); + + return $out; + } + + /** + * Get a table of all items of this type. + */ + function get_list() { + $items = $this->all_items(); + $rows = array(); + foreach ((array)$items as $item) { + if ($item->show_in_list()) { + if ($row = $item->get_list_row()) { + $rows[] = $row; + } + } + } + if (count($rows)) { + $out = theme('table', array('header' => $this->get_list_header(), 'rows' => $rows)); + } + else { + $out = t('There are no !items to display.', array('!items' => $this->plural)); + } + if (user_access('administer backup and migrate')) { + $out .= ' '. l(t('Create a new !item', array('!item' => $this->singular)), $this->get_settings_path() .'/add'); + } + return $out; + } + + /** + * Get the columns needed to list the type. + */ + function show_in_list() { + return $this->show_in_list; + } + + /** + * Get the columns needed to list the type. + */ + function get_settings_path() { + return BACKUP_MIGRATE_MENU_PATH . $this->settings_path . $this->type_name; + } + + + /** + * Get the columns needed to list the type. + */ + function get_list_column_info() { + return array( + 'actions' => array('title' => t('Operations'), 'html' => TRUE), + ); + } + + /** + * Get header for a lost of this type. + */ + function get_list_header() { + $out = array(); + foreach ($this->get_list_column_info() as $key => $col) { + $out[] = $col['title']; + } + return $out; + } + + /** + * Get a row of data to be used in a list of items of this type. + */ + function get_list_row() { + $out = array(); + foreach ($this->get_list_column_info() as $key => $col) { + $out[$key] = empty($col['html']) ? check_plain($this->get($key)) : $this->get($key); + if (isset($col['class'])) { + $out[$key] = array('data' => $out[$key], 'class' => $col['class']); + } + } + return $out; + } + + /** + * Get the rendered action links for a destination. + */ + function get_actions() { + $links = $this->get_action_links(); + return implode("   ", $links); + } + + /** + * Get the edit form for the item. + */ + function edit_form() { + $form = array(); + $form['item'] = array( + '#type' => 'value', + '#value' => $this, + ); + $name = $this->get('name'); + $form['name'] = array( + "#type" => "textfield", + "#title" => t("!type name", array('!type' => $this->title_singular)), + "#default_value" => empty($name) ? t('Untitled !type', array('!type' => $this->title_singular)) : $name, + "#required" => TRUE, + ); + $form['id'] = array( + '#type' => 'value', + '#value' => $this->get_id(), + ); + + $form['machine_name'] = array( + '#type' => 'machine_name', + '#default_value' => $this->get_id(), + '#maxlength' => 255, + '#machine_name' => array( + 'source' => array('name'), + 'exists' => 'backup_migrate_crud_item_exists', + ), + ); + + $form['actions'] = array('#prefix' => '
', '#suffix' => '
', '#weight' => 99); + $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Save !type', array('!type' => t($this->singular)))); + $form['actions']['cancel'] = array('#value' => l(t('Cancel'), $this->get_settings_path())); + return $form; + } + + /** + * Validate the edit form for the item. + */ + function edit_form_validate($form, &$form_state) { + } + + /** + * Submit the edit form for the item. + */ + function edit_form_submit($form, &$form_state) { + $this->from_array($form_state['values']); + $this->save(); + _backup_migrate_message('Your !type was saved', array('!type' => t($this->singular))); + } + + /** + * Get the message to send to the user when confirming the deletion of the item. + */ + function delete_confirm_message() { + return t('Are you sure you want to delete the !type %name?', array('!type' => t($this->singular), '%name' => $this->get('name'))); + } + + /** + * Get the message to send to the user when confirming the deletion of the item. + */ + function revert_confirm_message() { + return t('Are you sure you want to revert the !type %name back to the default settings?', array('!type' => t($this->singular), '%name' => $this->get('name'))); + } + + /* Static Functions */ + /** + * Get the menu items for manipulating this type. + */ + function get_menu_items() { + $path = $this->get_settings_path(); + + $type = $this->type_name; + $items[$path] = array( + 'title' => $this->title_plural, + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('crud', 'backup_migrate_crud_ui_list', TRUE, $this->type_name), + 'access arguments' => array('administer backup and migrate'), + 'weight' => 2, + 'type' => MENU_LOCAL_TASK, + ); + $items[$path .'/list'] = array( + 'title' => 'List !type', + 'title arguments' => array('!type' => t($this->title_plural)), + 'weight' => 1, + 'type' => MENU_DEFAULT_LOCAL_TASK, + ); + $items[$path .'/add'] = array( + 'title' => 'Add !type', + 'title arguments' => array('!type' => t($this->title_singular)), + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('crud', 'backup_migrate_crud_ui_create', TRUE, $this->type_name), + 'access arguments' => array('administer backup and migrate'), + 'weight' => 2, + 'type' => MENU_LOCAL_ACTION, + ); + $items[$path .'/delete'] = array( + 'title' => 'Delete !type', + 'title arguments' => array('!type' => t($this->title_singular)), + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('crud', 'backup_migrate_crud_ui_delete', TRUE, $this->type_name), + 'access arguments' => array('administer backup and migrate'), + 'type' => MENU_CALLBACK, + ); + $items[$path .'/edit'] = array( + 'title' => 'Edit !type', + 'title arguments' => array('!type' => t($this->title_singular)), + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('crud', 'backup_migrate_crud_ui_edit', TRUE, $this->type_name), + 'access arguments' => array('administer backup and migrate'), + 'type' => MENU_CALLBACK, + ); + $items[$path .'/export'] = array( + 'title' => 'Export !type', + 'title arguments' => array('!type' => t($this->title_singular)), + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('crud', 'backup_migrate_crud_ui_export', TRUE, $this->type_name), + 'access arguments' => array('administer backup and migrate'), + 'type' => MENU_CALLBACK, + ); + return $items; + } + + + /** + * Create a new items with the given input. Doesn't load the parameters, but could use them to determine what type to create. + */ + function create($params = array()) { + $type = get_class($this); + return new $type($params); + } + + /** + * Get all of the given items. + */ + function all_items() { + static $cache = array(); + $items = array(); + + // Get any items stored as a variable. This allows destinations to be defined in settings.php + $defaults = (array)variable_get($this->db_table . '_defaults', array()); + foreach ($defaults as $info) { + if (is_array($info) && $item = $this->create($info)) { + $items[$item->get_id()] = $item; + } + } + + // Get the items from the db. + $result = db_query("SELECT * FROM {{$this->db_table}}", array(), array('fetch' => PDO::FETCH_ASSOC)); + foreach ($result as $info) { + $info = $this->decode_db_row($info); + if ($item = $this->create($info)) { + $item->storage = empty($items[$item->get_id()]) ? BACKUP_MIGRATE_STORAGE_DB : BACKUP_MIGRATE_STORAGE_OVERRIDEN; + $items[$item->get_id()] = $item; + } + } + + // Allow other modules to declare destinations programatically. + $default_items = module_invoke_all($this->db_table); + + // Get CTools exported versions. + if (function_exists('ctools_include')) { + ctools_include('export'); + $defaults = ctools_export_load_object($this->db_table); + foreach ($defaults as $info) { + $info = (array)$info; + if (!empty($info) && $item = $this->create($info)) { + $default_items[$item->get_id()] = $item; + } + } + } + + // Get any items stored as a variable again to correctly mark overrides. + $defaults = (array)variable_get($this->db_table . '_defaults', array()); + foreach ($defaults as $info) { + if (is_array($info) && $item = $this->create($info)) { + $default_items[] = $item; + } + } + + // Add the default items to the array or set the storage flag if they've already been overridden. + foreach ($default_items as $item) { + if (isset($items[$item->get_id()])) { + $items[$item->get_id()]->storage = BACKUP_MIGRATE_STORAGE_OVERRIDEN; + } + else { + $item->storage = BACKUP_MIGRATE_STORAGE_NONE; + $items[$item->get_id()] = $item; + } + } + + // Allow other modules to alter the items. This should maybe be before the db override code above + // but then the filters are not able to set defaults for missing values. Other modules should just + // be careful not to overwrite the user's UI changes in an unexpected way. + drupal_alter($this->db_table, $items); + + return $items; + } + + /** + * A particular item. + */ + function item($item_id) { + $items = $this->all_items(); + return !empty($items[$item_id]) ? $items[$item_id] : NULL; + } + + /** + * A particular item. + */ + function item_exists($item_id) { + $items = $this->all_items(); + return !empty($items[$item_id]); + } +} diff --git a/sites/all/modules/contrib/backup_migrate/includes/destinations.browser.inc b/sites/all/modules/contrib/backup_migrate/includes/destinations.browser.inc new file mode 100644 index 0000000..af75649 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/destinations.browser.inc @@ -0,0 +1,76 @@ + $file->uri)); + backup_migrate_temp_files_add($file->uri); + return $out; + } + return NULL; + } +} + +/** + * A destination type for browser download. + * + * @ingroup backup_migrate_destinations + */ +class backup_migrate_destination_browser_download extends backup_migrate_destination_browser { + var $supported_ops = array('manual backup'); + // Browser downloads must always be the last destination as they must end the current process when they are done. + var $weight = 1000; + + function __construct() { + $params = array(); + $params['name'] = "Download"; + $params['machine_name'] = 'download'; + parent::__construct($params); + } + + /** + * File save destination callback. + */ + function save_file($file, $settings) { + backup_migrate_include('files'); + $file->transfer(); + } +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/destinations.db.inc b/sites/all/modules/contrib/backup_migrate/includes/destinations.db.inc new file mode 100644 index 0000000..9a2e23a --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/destinations.db.inc @@ -0,0 +1,311 @@ +set_source($this->get_id()); + + // Restore the file to the source database. + $file = backup_migrate_perform_restore($this->get_id(), $file, $settings); + + return $file; + } + + /** + * Destination configuration callback. + */ + function edit_form() { + $form = parent::edit_form(); + $form['scheme']['#title'] = t('Database type'); +// $form['scheme']['#options'] = array($GLOBALS['db_type'] => $GLOBALS['db_type']); + $form['scheme']['#description'] = t('The type of the database. Drupal only supports one database type at a time, so this must be the same as the current database type.'); + $form['path']['#title'] = t('Database name'); + $form['path']['#description'] = t('The name of the database. The database must exist, it will not be created for you.'); + $form['user']['#description'] = t('Enter the name of a user who has write access to the database.'); + return $form; + } + + /** + * Validate the configuration form. Make sure the db info is valid. + */ + function edit_form_validate($form, &$form_state) { + if (!preg_match('/[a-zA-Z0-9_\$]+/', $form_state['values']['path'])) { + form_set_error('path', t('The database name is not valid.')); + } + parent::edit_form_validate($form, $form_state); + } + + /** + * Get the form for the settings for this destination. + * + * Return the default tables whose data can be ignored. These tables mostly contain + * info which can be easily reproducted (such as cache or search index) + * but also tables which can become quite bloated but are not necessarily extremely + * important to back up or migrate during development (such ass access log and watchdog) + */ + function backup_settings_default() { + $core = array( + 'cache', + 'cache_admin_menu', + 'cache_browscap', + 'cache_content', + 'cache_filter', + 'cache_calendar_ical', + 'cache_location', + 'cache_menu', + 'cache_page', + 'cache_reptag', + 'cache_views', + 'cache_views_data', + 'cache_block', + 'cache_update', + 'cache_form', + 'cache_bootstrap', + 'cache_field', + 'cache_image', + 'cache_path', + 'sessions', + 'search_dataset', + 'search_index', + 'search_keywords_log', + 'search_total', + 'watchdog', + 'accesslog', + 'devel_queries', + 'devel_times', + ); + $nodata_tables = array_merge($core, module_invoke_all('devel_caches')); + return array( + 'nodata_tables' => $nodata_tables, + 'exclude_tables' => array(), + 'utils_lock_tables' => FALSE, + ); + } + + /** + * Get the form for the backup settings for this destination. + */ + function backup_settings_form($settings) { + $objects = $this->get_object_names(); + $form['#description'] = t("You may omit specific tables, or specific table data from the backup file. Only omit data that you know you will not need such as cache data, or tables from other applications. Excluding tables can break your Drupal install, so do not change these settings unless you know what you're doing."); + $form['exclude_tables'] = array( + "#type" => "select", + "#multiple" => TRUE, + "#title" => t("Exclude the following tables altogether"), + "#options" => $objects, + "#default_value" => $settings['exclude_tables'], + "#description" => t("The selected tables will not be added to the backup file."), + ); + $tables = $this->get_table_names(); + $form['nodata_tables'] = array( + "#type" => "select", + "#multiple" => TRUE, + "#title" => t("Exclude the data from the following tables"), + "#options" => $tables, + "#default_value" => $settings['nodata_tables'], + "#description" => t("The selected tables will have their structure backed up but not their contents. This is useful for excluding cache data to reduce file size."), + ); + $form['utils_lock_tables'] = array( + '#type' => 'checkbox', + '#title' => t('Lock tables during backup'), + '#default_value' => !empty($settings['utils_lock_tables']) ? $settings['utils_lock_tables'] : NULL, + '#description' => t('This can help reduce data corruption, but will make your site unresponsive.'), + ); + return $form; + } + + /** + * Backup from this source. + */ + function backup_to_file($file, $settings) { + $file->push_type($this->get_file_type_id()); + + backup_migrate_filters_invoke_all('pre_backup', $this, $file, $settings); + //$this->lock_tables($settings); + + // Switch to a different db if specified. + $success = $this->_backup_db_to_file($file, $settings); + + //$this->unlock_tables($settings); + backup_migrate_filters_invoke_all('post_backup', $this, $file, $settings, $success); + + return $success ? $file : FALSE; + } + + /** + * Restore to this source. + */ + function restore_from_file($file, &$settings) { + $num = 0; + $type = $this->get_file_type_id(); + // Open the file using the file wrapper. Check that the dump is of the right type (allow .sql for legacy reasons). + if ($file->type_id() !== $this->get_file_type_id() && $file->type_id() !== 'sql') { + _backup_migrate_message("Unable to restore from file %file because a %type file can't be restored to this database.", array("%file" => $file->filepath(), '%type' => $file->type_id()), 'error'); + } + else { + backup_migrate_filters_invoke_all('pre_restore', $file, $settings); + + // Restore the database. + $num = $this->_restore_db_from_file($file, $settings); + $settings->performed_action = $num ? t('%num SQL commands executed.', array('%num' => $num)) : ''; + + backup_migrate_filters_invoke_all('post_restore', $file, $settings, $num); + } + return $num; + } + + /** + * Get the db connection for the specified db. + */ + function _get_db_connection() { + if (!$this->connection) { + $target = $key = ''; + $parts = explode(':', $this->get_id()); + // One of the predefined databases (set in settings.php) + if ($parts[0] == 'db') { + $key = empty($parts[1]) ? 'default' : $parts[1]; + $target = empty($parts[2]) ? 'default' : $parts[2]; + } + // Another db url. + else { + // If the url is specified build it into a connection info array. + if (!empty($this->dest_url)) { + $info = array( + 'driver' => empty($this->dest_url['scheme']) ? NULL : $this->dest_url['scheme'], + 'host' => empty($this->dest_url['host']) ? NULL : $this->dest_url['host'], + 'port' => empty($this->dest_url['port']) ? NULL : $this->dest_url['port'], + 'username' => empty($this->dest_url['user']) ? NULL : $this->dest_url['user'], + 'password' => empty($this->dest_url['pass']) ? NULL : $this->dest_url['pass'], + 'database' => empty($this->dest_url['path']) ? NULL : $this->dest_url['path'], + ); + $key = uniqid('backup_migrate_tmp_'); + $target = 'default'; + Database::addConnectionInfo($key, $target, $info); + } + // No database selected. Assume the default. + else { + $key = $target = 'default'; + } + } + if ($target && $key) { + $this->connection = Database::getConnection($target, $key); + } + } + return $this->connection; + } + + /** + * Backup the databases to a file. + */ + function _backup_db_to_file($file, $settings) { + // Must be overridden. + } + + /** + * Backup the databases to a file. + */ + function _restore_db_from_file($file, $settings) { + // Must be overridden. + } + + /** + * Get a list of objects in the database. + */ + function get_object_names() { + // Must be overridden. + $out = $this->_get_table_names(); + if (method_exists($this, '_get_view_names')) { + $out += $this->_get_view_names(); + } + return $out; + } + + /** + * Get a list of tables in the database. + */ + function get_table_names() { + // Must be overridden. + $out = $this->_get_table_names(); + return $out; + } + + /** + * Get a list of tables in the database. + */ + function _get_table_names() { + // Must be overridden. + return array(); + } + + /** + * Lock the database in anticipation of a backup. + */ + function lock_tables($settings) { + if ($settings->filters['utils_lock_tables']) { + $tables = array(); + foreach ($this->get_table_names() as $table) { + // There's no need to lock excluded or structure only tables because it doesn't matter if they change. + if (empty($settings->filters['exclude_tables']) || !in_array($table, (array)$settings->filters['exclude_tables'])) { + $tables[] = $table; + } + } + $this->_lock_tables($tables); + } + } + + /** + * Lock the list of given tables in the database. + */ + function _lock_tables($tables) { + // Must be overridden. + } + + /** + * Unlock any tables that have been locked. + */ + function unlock_tables($settings) { + if ($settings->filters['utils_lock_tables']) { + $this->_unlock_tables(); + } + } + + /** + * Unlock the list of given tables in the database. + */ + function _unlock_tables($tables) { + // Must be overridden. + } + + /** + * Get the file type for to backup this destination to. + */ + function get_file_type_id() { + return 'sql'; + } +} diff --git a/sites/all/modules/contrib/backup_migrate/includes/destinations.db.mysql.inc b/sites/all/modules/contrib/backup_migrate/includes/destinations.db.mysql.inc new file mode 100644 index 0000000..77ccfa4 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/destinations.db.mysql.inc @@ -0,0 +1,477 @@ + array( + "extension" => "sql", + "filemime" => "text/x-sql", + "backup" => TRUE, + "restore" => TRUE, + ), + "mysql" => array( + "extension" => "mysql", + "filemime" => "text/x-sql", + "backup" => TRUE, + "restore" => TRUE, + ), + ); + } + + /** + * Declare any mysql databases defined in the settings.php file as a possible destination. + */ + function destinations() { + $out = array(); + global $databases; + foreach ((array)$databases as $db_key => $target) { + foreach ((array)$target as $tgt_key => $info) { + // Only mysql/mysqli supported by this destination. + $key = $db_key . ':' . $tgt_key; + if ($info['driver'] === 'mysql') { + $url = $info['driver'] . '://' . $info['username'] . ':' . $info['password'] . '@' . $info['host'] . (isset($info['port']) ? ':' . $info['port'] : '') . '/' . $info['database']; + if ($destination = backup_migrate_create_destination('mysql', array('url' => $url))) { + // Treat the default database differently because it is probably the only one available. + if ($key == 'default:default') { + $destination->set_id('db'); + $destination->set_name(t('Default Database')); + // Dissalow backing up to the default database because that's confusing and potentially dangerous. + $destination->remove_op('scheduled backup'); + $destination->remove_op('manual backup'); + } + else { + $destination->set_id('db:'. $key); + $destination->set_name($key .": ". $destination->get_display_location()); + } + $out[$destination->get_id()] = $destination; + } + } + } + } + return $out; + } + + /** + * Get the file type for to backup this destination to. + */ + function get_file_type_id() { + return 'mysql'; + } + + /** + * Get the form for the backup settings for this destination. + */ + function backup_settings_form($settings) { + $form = parent::backup_settings_form($settings); + + $form['use_mysqldump'] = array( + "#type" => "checkbox", + "#title" => t("Use mysqldump command"), + "#default_value" => !empty($settings['use_mysqldump']), + "#description" => t("Use the mysqldump command line tool if available. This can be faster for large databases but will not work on all servers. Also exporting SQL views is not really solid with this option. EXPERIMENTAL"), + ); + + return $form; + } + + + /** + * Backup the databases to a file. + * + * Returns a list of sql commands, one command per line. + * That makes it easier to import without loading the whole file into memory. + * The files are a little harder to read, but human-readability is not a priority + */ + function _backup_db_to_file($file, $settings) { + if (!empty($settings->filters['use_mysqldump']) && $this->_backup_db_to_file_mysqldump($file, $settings)) { + return TRUE; + } + + $lines = 0; + $exclude = !empty($settings->filters['exclude_tables']) ? $settings->filters['exclude_tables'] : array(); + $nodata = !empty($settings->filters['nodata_tables']) ? $settings->filters['nodata_tables'] : array(); + if ($file->open(TRUE)) { + $file->write($this->_get_sql_file_header()); + $alltables = $this->_get_tables(); + $allviews = $this->_get_views(); + + foreach ($alltables as $table) { + if (_backup_migrate_check_timeout()) { + return FALSE; + } + if ($table['name'] && !isset($exclude[$table['name']])) { + $file->write($this->_get_table_structure_sql($table)); + $lines++; + if (!in_array($table['name'], $nodata)) { + $lines += $this->_dump_table_data_sql_to_file($file, $table); + } + } + } + foreach ($allviews as $view) { + if (_backup_migrate_check_timeout()) { + return FALSE; + } + if ($view['name'] && !isset($exclude[$view['name']])) { + $file->write($this->_get_view_create_sql($view)); + } + + } + $file->write($this->_get_sql_file_footer()); + $file->close(); + return $lines; + } + else { + return FALSE; + } + } + + + /** + * Backup the databases to a file using the mysqldump command. + */ + function _backup_db_to_file_mysqldump($file, $settings) { + $success = FALSE; + $nodata_tables = array(); + $alltables = $this->_get_tables(); + + + $command = 'mysqldump --result-file=%file --opt -Q --host=%host --port=%port --user=%user --password=%pass %db'; + $args = array( + '%file' => $file->filepath(), + '%host' => $this->dest_url['host'], + '%port' => !empty($this->dest_url['port']) ? $this->dest_url['port'] : '3306', + '%user' => $this->dest_url['user'], + '%pass' => $this->dest_url['pass'], + '%db' => $this->dest_url['path'], + ); + + // Ignore the excluded and no-data tables. + $db = $this->dest_url['path']; + if (!empty($settings->filters['exclude_tables'])) { + foreach ((array)$settings->filters['exclude_tables'] as $table) { + if (isset($alltables[$table])) { + $command .= ' --ignore-table='. $db .'.'. $table; + } + } + } + if (!empty($settings->filters['nodata_tables'])) { + foreach ((array)$settings->filters['nodata_tables'] as $table) { + if (isset($alltables[$table])) { + $nodata_tables[] = $table; + $command .= ' --ignore-table='. $db .'.'. $table; + } + } + } + $success = backup_migrate_exec($command, $args); + + // Get the nodata tables. + if ($success && !empty($nodata_tables)) { + $tables = implode(' ', array_unique($nodata_tables)); + $command = "mysqldump --no-data --opt -Q --host=%host --port=%port --user=%user --password=%pass %db $tables >> %file"; + $success = backup_migrate_exec($command, $args); + } + return $success; + } + + /** + * Backup the databases to a file. + */ + function _restore_db_from_file($file, $settings) { + $num = 0; + + if ($file->open() && $conn = $this->_get_db_connection()) { + // Read one line at a time and run the query. + while ($line = $this->_read_sql_command_from_file($file)) { + if (_backup_migrate_check_timeout()) { + return FALSE; + } + if ($line) { + // Prepeare and exexute the statement instead of the api function to avoid substitution of '{' etc. + $stmt = $conn->prepare($line); + $stmt->execute(); + $num++; + } + } + // Close the file with fclose/gzclose. + $file->close(); + } + else { + drupal_set_message(t("Unable to open file %file to restore database", array("%file" => $file->filepath())), 'error'); + $num = FALSE; + } + return $num; + } + + + /** + * Read a multiline sql command from a file. + * + * Supports the formatting created by mysqldump, but won't handle multiline comments. + */ + function _read_sql_command_from_file($file) { + $out = ''; + while ($line = $file->read()) { + $first2 = substr($line, 0, 2); + $first3 = substr($line, 0, 2); + + // Ignore single line comments. This function doesn't support multiline comments or inline comments. + if ($first2 != '--' && ($first2 != '/*' || $first3 == '/*!')) { + $out .= ' ' . trim($line); + // If a line ends in ; or */ it is a sql command. + if (substr($out, strlen($out) - 1, 1) == ';') { + return trim($out); + } + } + } + return trim($out); + } + + /** + * Get a list of tables in the database. + */ + function _get_table_names() { + $out = array(); + foreach ($this->_get_tables() as $table) { + $out[$table['name']] = $table['name']; + } + return $out; + } + + /** + * Get a list of views in the database. + */ + function _get_view_names() { + $out = array(); + foreach ($this->_get_views() as $view) { + $out[$view['name']] = $view['name']; + } + return $out; + } + + /** + * Lock the list of given tables in the database. + */ + function _lock_tables($tables) { + if ($tables) { + $tables_escaped = array(); + foreach ($tables as $table) { + $tables_escaped[] = '`'. db_escape_table($table) .'` WRITE'; + } + $this->query('LOCK TABLES '. implode(', ', $tables_escaped)); + } + } + + /** + * Unlock all tables in the database. + */ + function _unlock_tables($settings) { + $this->query('UNLOCK TABLES'); + } + + /** + * Get a list of tables in the db. + */ + function _get_tables() { + $out = array(); + // get auto_increment values and names of all tables + $tables = $this->query("show table status", array(), array('fetch' => PDO::FETCH_ASSOC)); + foreach ($tables as $table) { + // Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed. + // See: http://drupal.org/node/1171866 + $table = array_change_key_case($table); + if (!empty($table['engine'])) { + $out[$table['name']] = $table; + } + } + return $out; + } + + /** + * Get a list of views in the db. + */ + function _get_views() { + $out = array(); + // get auto_increment values and names of all tables + $tables = $this->query("show table status", array(), array('fetch' => PDO::FETCH_ASSOC)); + foreach ($tables as $table) { + // Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed. + // See: http://drupal.org/node/1171866 + $table = array_change_key_case($table); + if (empty($table['engine'])) { + $out[$table['name']] = $table; + } + } + return $out; + } + + /** + * Get the sql for the structure of the given table. + */ + function _get_table_structure_sql($table) { + $out = ""; + $result = $this->query("SHOW CREATE TABLE `". $table['name'] ."`", array(), array('fetch' => PDO::FETCH_ASSOC)); + foreach ($result as $create) { + // Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed. + // See: http://drupal.org/node/1171866 + $create = array_change_key_case($create); + $out .= "DROP TABLE IF EXISTS `". $table['name'] ."`;\n"; + // Remove newlines and convert " to ` because PDO seems to convert those for some reason. + $out .= strtr($create['create table'], array("\n" => ' ', '"' => '`')); + if ($table['auto_increment']) { + $out .= " AUTO_INCREMENT=". $table['auto_increment']; + } + $out .= ";\n"; + } + return $out; + } + + /** + * Get the sql for the structure of the given table. + */ + function _get_view_create_sql($view) { + $out = ""; + // Switch SQL mode to get rid of "CREATE ALGORITHM..." what requires more permissions + troubles with the DEFINER user + $sql_mode = $this->query("SELECT @@SESSION.sql_mode")->fetchField(); + $this->query("SET sql_mode = 'ANSI'"); + $result = $this->query("SHOW CREATE VIEW `" . $view['name'] . "`", array(), array('fetch' => PDO::FETCH_ASSOC)); + $this->query("SET SQL_mode = :mode", array(':mode' => $sql_mode)); + foreach ($result as $create) { + $out .= "DROP VIEW IF EXISTS `". $view['name'] ."`;\n"; + $out .= "SET sql_mode = 'ANSI';\n"; + $out .= strtr($create['Create View'], "\n", " ") . ";\n"; + $out .= "SET sql_mode = '$sql_mode';\n"; + } + return $out; + } + + /** + * Get the sql to insert the data for a given table + */ + function _dump_table_data_sql_to_file($file, $table) { + $rows_per_line = variable_get('backup_migrate_data_rows_per_line', 30); + $bytes_per_line = variable_get('backup_migrate_data_bytes_per_line', 2000); + + $lines = 0; + $data = $this->query("SELECT * FROM `". $table['name'] ."`", array(), array('fetch' => PDO::FETCH_ASSOC)); + $rows = $bytes = 0; + + // Escape backslashes, PHP code, special chars + $search = array('\\', "'", "\x00", "\x0a", "\x0d", "\x1a"); + $replace = array('\\\\', "''", '\0', '\n', '\r', '\Z'); + + $line = array(); + foreach ($data as $row) { + // DB Escape the values. + $items = array(); + foreach ($row as $key => $value) { + $items[] = is_null($value) ? "null" : "'". str_replace($search, $replace, $value) ."'"; + } + + // If there is a row to be added. + if ($items) { + // Start a new line if we need to. + if ($rows == 0) { + $file->write("INSERT INTO `". $table['name'] ."` VALUES "); + $bytes = $rows = 0; + } + // Otherwise add a comma to end the previous entry. + else { + $file->write(","); + } + + // Write the data itself. + $sql = implode(',', $items); + $file->write('('. $sql .')'); + $bytes += strlen($sql); + $rows++; + + // Finish the last line if we've added enough items + if ($rows >= $rows_per_line || $bytes >= $bytes_per_line) { + $file->write(";\n"); + $lines++; + $bytes = $rows = 0; + } + } + } + // Finish any unfinished insert statements. + if ($rows > 0) { + $file->write(";\n"); + $lines++; + } + + return $lines; + } + + /** + * Get the db connection for the specified db. + */ + function _get_db_connection() { + if (!$this->connection) { + $this->connection = parent::_get_db_connection(); + // Set the sql mode because the default is ANSI,TRADITIONAL which is not aware of collation or storage engine. + $this->connection->exec("SET sql_mode=''"); + } + return $this->connection; + } + + /** + * Run a db query on this destination's db. + */ + function query($query, $args = array(), $options = array()) { + if ($conn = $this->_get_db_connection()) { + return $conn->query($query, $args, $options); + } + } + + /** + * The header for the top of the sql dump file. These commands set the connection + * character encoding to help prevent encoding conversion issues. + */ + function _get_sql_file_header() { + return "/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=NO_AUTO_VALUE_ON_ZERO */; + +SET NAMES utf8; + +"; + } + + /** + * The footer of the sql dump file. + */ + function _get_sql_file_footer() { + return " + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +"; + } +} diff --git a/sites/all/modules/contrib/backup_migrate/includes/destinations.email.inc b/sites/all/modules/contrib/backup_migrate/includes/destinations.email.inc new file mode 100644 index 0000000..c7d426d --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/destinations.email.inc @@ -0,0 +1,140 @@ +filepath()); + $max = variable_get('backup_migrate_max_email_size', 20971520); + if ($size > $max) { + _backup_migrate_message('Could not email the file @file because it is @size and Backup and Migrate only supports emailing files smaller than @max.', array('@file' => $file->filename(), '@size' => format_size($size), '@max' => format_size($max)), 'error'); + return FALSE; + } + $attachment = new stdClass(); + $attachment->filename = $file->filename(); + $attachment->path = $file->filepath(); + _backup_migrate_destination_email_mail_backup($attachment, $this->get_location()); + return $file; + } + + /** + * Get the form for the settings for this filter. + */ + function edit_form() { + $form = parent::edit_form(); + $form['location'] = array( + "#type" => "textfield", + "#title" => t("Email Address"), + "#default_value" => $this->get_location(), + "#required" => TRUE, + "#description" => t('Enter the email address to send the backup files to. Make sure the email sever can handle large file attachments'), + ); + return $form; + } + + /** + * Validate the configuration form. Make sure the email address is valid. + */ + function settings_form_validate($values) { + if (!valid_email_address($values['location'])) { + form_set_error('[location]', t('The e-mail address %mail is not valid.', array('%mail' => $form_state['values']['location']))); + } + } +} + +/** + * @function + * Temporary mail handler class. + * + * Defines a mail class to send a message with an attachment. Eventually Drupal + * core should provide this functionality, at which time this code will be + * removed. + * + * More info on sending email at . + * This function taken from dba.module. + * + * @param $attachment + * An object which contains two variables "path" the path to the file and + * filename and "filename" which is just the filename. + */ +function _backup_migrate_destination_email_mail_backup($attachment, $to) { + // Send mail + $attach = fread(fopen($attachment->path, "r"), filesize($attachment->path)); + $mail = new mime_mail(); + $mail->from = variable_get('site_mail', ini_get('sendmail_from')); + $mail->headers = 'Errors-To: [EMAIL='. $mail->from .']'. $mail->from .'[/EMAIL]'; + $mail->to = $to; + $mail->subject = t('Database backup from !site: !file', array('!site' => variable_get('site_name', 'drupal'), '!file' => $attachment->filename)); + $mail->body = t('Database backup attached.') ."\n\n"; + + $mail->add_attachment("$attach", $attachment->filename, "Content-Transfer-Encoding: base64 /9j/4AAQSkZJRgABAgEASABIAAD/7QT+UGhvdG9zaG", NULL, TRUE); + $mail->send(); +} + +class mime_mail { + var $parts; + var $to; + var $from; + var $headers; + var $subject; + var $body; + + function mime_mail() { + $this->parts = array(); + $this->to = ""; + $this->from = ""; + $this->headers = ""; + $this->subject = ""; + $this->body = ""; + } + + function add_attachment($message, $name = "", $ctype = "application/octet-stream", $encode = NULL, $attach = FALSE) { + $this->parts[] = array( + "ctype" => $ctype, + "message" => $message, + "encode" => $encode, + "name" => $name, + "attach" => $attach, + ); + } + + function build_message($part) { + $message = $part["message"]; + $message = chunk_split(base64_encode($message)); + $encoding = "base64"; + $disposition = $part['attach'] ? "Content-Disposition: attachment; filename=$part[name]\n" : ''; + return "Content-Type: ". $part["ctype"] . ($part["name"] ? "; name = \"". $part["name"] ."\"" : "") ."\nContent-Transfer-Encoding: $encoding\n$disposition\n$message\n"; + } + + function build_multipart() { + $boundary = "b". md5(uniqid(time())); + $multipart = "Content-Type: multipart/mixed; boundary = $boundary\n\nThis is a MIME encoded message.\n\n--$boundary"; + for ($i = sizeof($this->parts) - 1; $i >= 0; $i--) { + $multipart .= "\n". $this->build_message($this->parts[$i]) ."--$boundary"; + } + return $multipart .= "--\n"; + } + + function send() { + $mime = ""; + if (!empty($this->from)) $mime .= "From: ". $this->from ."\n"; + if (!empty($this->headers)) $mime .= $this->headers ."\n"; + if (!empty($this->body)) $this->add_attachment($this->body, "", "text/plain"); + $mime .= "MIME-Version: 1.0\n". $this->build_multipart(); + mail(trim($this->to), $this->subject, "", $mime); + } +} diff --git a/sites/all/modules/contrib/backup_migrate/includes/destinations.file.inc b/sites/all/modules/contrib/backup_migrate/includes/destinations.file.inc new file mode 100644 index 0000000..47fcbaa --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/destinations.file.inc @@ -0,0 +1,318 @@ +get_location()); + } + + /** + * File save destination callback. + */ + function _save_file($file, $settings) { + if ($this->confirm_destination() && $dir = $this->get_location()) { + $filepath = rtrim($dir, "/") ."/". $file->filename(); + // Copy the file if there are multiple destinations. + if (count($settings->get_destinations()) > 1) { + file_unmanaged_copy($file->filepath(), $filepath); + } + // Otherwise we can move it and save a delete. + else { + file_unmanaged_move($file->filepath(), $filepath); + } + + // chmod, chown and chgrp the file if needed. + if ($chmod = $this->settings('chmod')) { + if (!@drupal_chmod($filepath, octdec($chmod))) { + _backup_migrate_message('Unable to set the file mode for: @file', array('@file' => $filepath), 'error'); + } + } + if ($chgrp = $this->settings('chgrp')) { + if (!@chgrp($filepath, $chgrp)) { + _backup_migrate_message('Unable to set the file group for: @file', array('@file' => $filepath), 'error'); + } + } + return $file; + } + } + + /** + * Determine if we can read the given file. + */ + function can_read_file($file_id) { + return $this->op('restore') && is_readable($this->get_filepath($file_id)); + } + + /** + * File load destination callback. + */ + function load_file($file_id) { + $filepath = $this->get_filepath($file_id); + if (file_exists($filepath)) { + backup_migrate_include('files'); + return new backup_file(array('filepath' => $filepath)); + } + } + + /** + * Get the file object for the given file. + */ + function get_file($file_id) { + $files = $this->list_files(); + if (isset($files[$file_id])) { + isset($files[$file_id]); + } + return NULL; + } + + /** + * File list destination callback. + */ + function _list_files() { + $files = array(); + if ($dir = $this->get_realpath()) { + if ($handle = @opendir($dir)) { + backup_migrate_include('files'); + while (FALSE !== ($file = readdir($handle))) { + if (substr($file, 0, 1) !== '.') { + $filepath = $dir ."/". $file; + $files[$file] = new backup_file(array('filepath' => $filepath)); + } + } + } + } + return $files; + } + + /** + * File delete destination callback. + */ + function _delete_file($file_id) { + $filepath = $this->get_filepath($file_id); + file_unmanaged_delete($filepath); + } + + /** + * Get the filepath from the given file id. + */ + function get_filepath($file_id) { + if ($dir = $this->get_realpath()) { + $filepath = rtrim($dir, '/') .'/'. $file_id; + return $filepath; + } + return FALSE; + } + + /** + * Get the form for the settings for the files destination. + */ + function edit_form() { + $form = parent::edit_form(); + $form['location'] = array( + "#type" => "textfield", + "#title" => t("Directory path"), + "#default_value" => $this->get_location(), + "#required" => TRUE, + "#description" => t('Enter the path to the directory to save the backups to. Use a relative path to pick a path relative to your Drupal root directory. The web server must be able to write to this path.'), + ); + $form['settings'] = array( + '#type' => 'fieldset', + '#title' => t('Advanced Settings'), + '#tree' => TRUE, + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + if (function_exists('chmod')) { + $form['settings']['chmod'] = array( + '#type' => 'textfield', + '#title' => t('Change file mode (chmod)'), + '#size' => 5, + '#default_value' => $this->settings('chmod'), + '#description' => t('If you enter a value here, backup files will be chmoded with the mode you specify. Specify the mode in octal form (e.g. 644 or 0644) or leave blank to disable this feature.'), + ); + } + if (function_exists('chgrp')) { + $form['settings']['chgrp'] = array( + '#type' => 'textfield', + '#title' => t('Change file group (chgrp)'), + '#size' => 5, + '#default_value' => $this->settings('chgrp'), + '#description' => t('If you enter a value here, backup files will be chgrped to the group you specify. Leave blank to disable this feature.'), + ); + } + return $form; + } + + /** + * Validate the form for the settings for the files destination. + */ + function edit_form_validate($form, &$form_state) { + $values = $form_state['values']; + if (isset($values['settings']['chmod']) && !empty($values['settings']['chmod']) && !preg_match('/0?[0-7]{3}/', $values['settings']['chmod'])) { + form_set_error('chmod', t('You must enter a valid chmod octal value (e.g. 644 or 0644) in the change mode field, or leave it blank.')); + } + parent::edit_form_validate($form, $form_state); + } + + /** + * Submit the form for the settings for the files destination. + */ + function edit_form_submit($form, &$form_state) { + // Add a 0 to the start of a 3 digit file mode to make it proper PHP encoded octal. + if (strlen($form_state['values']['settings']['chmod']) == 3) { + $form_state['values']['settings']['chmod'] = '0' . $form_state['values']['settings']['chmod']; + } + parent::edit_form_submit($form, $form_state); + } + + /** + * Check that a destination is valid. + */ + function confirm_destination() { + if ($dir = $this->get_location()) { + return $this->check_dir($dir); + } + return FALSE; + } + + /** + * Prepare the destination directory for the backups. + */ + function check_dir($directory) { + if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) { + // Unable to create destination directory. + _backup_migrate_message("Unable to create or write to the save directory '%directory'. Please check the file permissions of that directory and try again.", array('%directory' => $directory), "error"); + return FALSE; + } + + // If the destination directory is within the webroot, then secure it as best we can. + if ($this->dir_in_webroot($directory)) { + $directory = $this->check_web_dir($directory); + } + + return $directory; + } + + /** + * Check that a web accessible directory has been properly secured, othewise attempt to secure it. + */ + function check_web_dir($directory) { + // Check if the file has already been tested. + if (is_file($directory .'/tested.txt')) { + return $directory; + } + else { + file_create_htaccess($directory, TRUE); + + // Check the user agent to make sure we're not responding to a request from drupal itself. + // That should prevent infinite loops which could be caused by poormanscron in some circumstances. + if (strpos($_SERVER['HTTP_USER_AGENT'], 'Drupal') !== FALSE) { + return FALSE; + } + + // Check to see if the destination is publicly accessible + $test_contents = "this file should not be publicly accessible"; + // Create the the text.txt file if it's not already there. + if (!is_file($directory .'/test.txt') || file_get_contents($directory .'/test.txt') != $test_contents) { + if ($fp = fopen($directory .'/test.txt', 'w')) { + @fputs($fp, $test_contents); + fclose($fp); + } + else { + $message = t("Security notice: Backup and Migrate was unable to write a test text file to the destination directory %directory, and is therefore unable to check the security of the backup destination. Backups to the server will be disabled until the destination becomes writable and secure.", array('%directory' => $directory)); + drupal_set_message($message, "error"); + return FALSE; + } + } + + // Attempt to read the test file via http. This may fail for other reasons, + // so it's not a bullet-proof check. + if ($this->test_file_readable_remotely($directory .'/test.txt', $test_contents)) { + $message = t("Security notice: Backup and Migrate will not save backup files to the server because the destination directory is publicly accessible. If you want to save files to the server, please secure the '%directory' directory", array('%directory' => $directory)); + drupal_set_message($message, "error"); + return FALSE; + } + // Directory tested OK, so we mark it as tested. + if ($fp = fopen($directory .'/tested.txt', 'w')) { + $contents = t('The presence of this file indicates that this directory has been tested as safe to use as a destination for Backup and Migrate. If you change the permissions of this directory or change your web server settings, please delete this file so that the directory can be checked again.'); + @fputs($fp, $contents); + fclose($fp); + } + return $directory; + } + } + + /** + * Check if the given directory is within the webroot and is therefore web accessible. + */ + function dir_in_webroot($directory) { + $real_dir = drupal_realpath($directory); + $real_root = drupal_realpath(DRUPAL_ROOT); + if ($real_dir == $real_root || strpos($real_dir, $real_root . '/') === 0) { + return TRUE; + } + return FALSE; + } + +/** + * Check if a file can be read remotely via http. + */ + function test_file_readable_remotely($directory, $contents) { + $real_dir = drupal_realpath($directory); + $real_root = drupal_realpath(DRUPAL_ROOT); + if ($real_dir && $real_root) { + // Get the root relative path. + $path = substr($real_dir, strlen($real_root)); + + $url = $GLOBALS['base_url'] . str_replace('\\', '/', $path); + $result = drupal_http_request($url); + if (!empty($result->data) && strpos($result->data, $contents) !== FALSE) { + return TRUE; + } + } + return FALSE; + } +} + +/** + * The manual files directory. + */ +class backup_migrate_destination_files_manual extends backup_migrate_destination_files { + var $supported_ops = array('manual backup', 'restore', 'list files', 'configure', 'delete'); + function __construct($params = array()) { + $dir = 'private://backup_migrate/manual'; + parent::__construct($params + array('location' => $dir, 'name' => t('Manual Backups Directory'))); + } +} + +/** + * The scheduled files directory. + */ +class backup_migrate_destination_files_scheduled extends backup_migrate_destination_files { + var $supported_ops = array('scheduled backup', 'restore', 'list files', 'configure', 'delete'); + function __construct($params = array()) { + $dir = 'private://backup_migrate/scheduled'; + parent::__construct($params + array('location' => $dir, 'name' => t('Scheduled Backups Directory'))); + } +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/destinations.ftp.inc b/sites/all/modules/contrib/backup_migrate/includes/destinations.ftp.inc new file mode 100644 index 0000000..34f24e0 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/destinations.ftp.inc @@ -0,0 +1,435 @@ +ftp_object(); + if (drupal_ftp_file_to_ftp($file->filepath(), $file->filename(), '.', $ftp)) { + return $file; + } + return FALSE; + } + + /** + * Load from the ftp destination. + */ + function load_file($file_id) { + backup_migrate_include('files'); + $file = new backup_file(array('filename' => $file_id)); + $this->ftp_object(); + if (drupal_ftp_ftp_to_file($file->filepath(), $file_id, '.', $this->ftp)) { + return $file; + } + return FALSE; + } + + /** + * Delete from the ftp destination. + */ + function _delete_file($file_id) { + $this->ftp_object(); + drupal_ftp_delete_file($file_id, $this->ftp); + } + + function _list_files() { + backup_migrate_include('files'); + $files = array(); + $this->ftp_object(); + $ftp_files = (array)drupal_ftp_file_list('.', $this->ftp); + foreach ($ftp_files as $file) { + $files[$file['filename']] = new backup_file($file); + } + return $files; + } + + /** + * Get the form for the settings for this filter. + */ + function edit_form() { + $form = parent::edit_form(); + $form['scheme']['#type'] = 'value'; + $form['scheme']['#value'] = 'ftp'; + $form['port'] = array( + "#type" => "textfield", + "#title" => t("Port"), + "#default_value" => @$this->dest_url['port'] ? $this->dest_url['port'] : '21', + "#weight" => 15, + ); + $form['pasv'] = array( + '#type' => 'checkbox', + '#title' => t('Use PASV transfers'), + '#default_value' => $this->get_pasv(), + '#weight' => 50, + ); + return $form; + } + + function set_pasv($value) { + $this->settings['pasv'] = (bool)$value; + } + + function get_pasv() { + return isset($this->settings['pasv']) ? $this->settings['pasv'] : FALSE; + } + + function ftp_object() { + if (!$this->ftp) { + $this->dest_url['port'] = empty($this->dest_url['port']) ? '21' : $this->dest_url['port']; + $this->dest_url['pasv'] = $this->get_pasv(); + $this->ftp = drupal_ftp_ftp_object($this->dest_url['host'], $this->dest_url['port'], $this->dest_url['user'], $this->dest_url['pass'], $this->dest_url['path'], $this->dest_url['pasv']); + } + return $this->ftp; + } +} + +// The FTP code below was taken from the ftp module by Aaron Winborn. + +// Inspired by http://www.devarticles.com/c/a/PHP/My-FTP-Wrapper-Class-for-PHP/ +// It's been drupalized, however, and most of the bugs from that example have been fixed. +// - winborn 2007-06-22 - 2007-06-28 + +define('DRUPAL_FTP_FT_DIRECTORY', 0); +define('DRUPAL_FTP_FT_FILE', 1); + +/** + * creates a new ftp object. if any elements of ftp_map are missing, they'll be filled with the server defaults. + */ +function drupal_ftp_ftp_object($server, $port, $user, $pass, $dir, $pasv) { + $ftp = new stdClass(); + + $ftp->__server = $server; + $ftp->__port = $port; + $ftp->__user = $user; + $ftp->__password = $pass; + $ftp->__directory = $dir; + $ftp->__pasv = $pasv; + + return $ftp; +} + +/** + * The drupal_ftp_connect function + * This function connects to an FTP server and attempts to change into the directory specified by + * the fourth parameter, $directory. + */ +function drupal_ftp_connect(&$ftp) { + if (is_NULL($ftp)) { + $ftp = drupal_ftp_ftp_object(); + } + + if (!$ftp->__conn && !drupal_ftp_connected($ftp)) { + // Attempt to connect to the remote server + $ftp->__conn = @ftp_connect($ftp->__server, $ftp->__port); + + if (!$ftp->__conn) { + _backup_migrate_message('FTP Error: Couldn\'t connect to server @server', array('@server' => $ftp->__server), 'error'); + return FALSE; + } + + // Attempt to login to the remote server + $ftp->__login = @ftp_login($ftp->__conn, $ftp->__user, $ftp->__password); + + if (!$ftp->__login) { + _backup_migrate_message('FTP Error: Couldn\'t login as user @ftp_user to @server', array('@ftp_user' => $ftp->__user, '@server' => $ftp->__server), 'error'); + return FALSE; + } + + // Attempt to change into the working directory + $chdir = @ftp_chdir($ftp->__conn, $ftp->__directory); + + if (!$chdir) { + _backup_migrate_message('FTP Error: Couldn\'t change into the @directory directory', array('@directory' => $ftp->__directory), 'error'); + return FALSE; + } + + // Set PASV - if needed + if ($ftp->__pasv) { + $pasv = @ftp_pasv($ftp->__conn, TRUE); + if (!$pasv) { + _backup_migrate_message('FTP Error: Couldn\'t set PASV mode', array(), 'error'); + return FALSE; + } + } + } + + // Everything worked OK, return TRUE + return TRUE; +} + +/** + * The drupal_ftp_connected function + * This function queries the FTP server with the ftp_systype command to check if the connection is still alive. + * It returns TRUE on success or FALSE on disconnection. + */ +function drupal_ftp_connected(&$ftp) { + // Attempt to call the ftp_systype to see if the connect + // to the FTP server is still alive and kicking + + if (is_NULL($ftp)) { + $ftp = drupal_ftp_ftp_object(); + return FALSE; + } + + if (!@ftp_systype($ftp->__conn)) { + // The connection is dead + return FALSE; + } + else { + // The connection is still alive + return TRUE; + } +} + +/** + * This function tries to retrieve the contents of a file from the FTP server. + * Firstly it changes into the $directory directory, and then attempts to download the file $filename. + * The file is saved locally and its contents are returned to the caller of the function. + */ +function drupal_ftp_ftp_to_file($file, $filename, $directory, &$ftp) { + // Change into the remote directory and retrieve the content + // of a file. Once retrieve, return this value to the caller + + if (!@drupal_ftp_connect($ftp)) { + return FALSE; + } + + // We are now connected, so let's retrieve the file contents. + // Firstly, we change into the directory + $chdir = @ftp_chdir($ftp->__conn, $directory); + + if (!$chdir) { + _backup_migrate_message('FTP Error: Couldn\'t change into directory: @directory', array('@directory' => $directory), 'error'); + return FALSE; + } + + // We have changed into the directory, let's attempt to get the file + $fp = @fopen($file, 'wb'); + $get_file = @ftp_fget($ftp->__conn, $fp, $filename, FTP_BINARY); + fclose($fp); + + $fp = NULL; + + if (!$get_file) { + _backup_migrate_message('FTP Error: Unable to download file: @filename from @directory', array('@filename' => $filename, '@directory' => $directory), 'error'); + return FALSE; + } + + return TRUE; +} + +/** + * Send a file to an FTP server. + */ +function drupal_ftp_file_to_ftp($file, $ftp_filename, $ftp_directory, &$ftp) { + if (!@drupal_ftp_connect($ftp)) { + return FALSE; + } + + if ($source = drupal_realpath($file)) { + // Now we can try to write to the remote file + $complete_filename = $ftp_directory .'/'. $ftp_filename; + $put_file = @ftp_put($ftp->__conn, $complete_filename, $source, FTP_BINARY); + if (!$put_file) { + _backup_migrate_message('FTP Error: Couldn\'t write to @complete_filename when trying to save file on the ftp server.', array('@complete_filename' => $complete_filename), 'error'); + return FALSE; + } + + // Everything worked OK + return TRUE; + } + else { + _backup_migrate_message('FTP Error: Couldn\'t find @file.', array('@file'), 'error'); + return FALSE; + } +} + +/** + * The drupal_ftp_change_directory Function + * This function simply changes into the $directory folder on the FTP server. + * If a connection or permission error occurs then _backup_migrate_message() will contain the error message. + */ +function drupal_ftp_change_directory($directory, &$ftp) { + // Switch to another directory on the web server. If we don't + // have permissions then an error will occur + + if (!@drupal_ftp_connect($ftp)) { + return FALSE; + } + + // Try and change into another directory + $chdir = ftp_chdir($ftp->__conn, $directory); + + if (!$chdir) { + _backup_migrate_message('FTP Error: Couldn\'t change into directory: @directory', array('@directory', $directory), 'error'); + return FALSE; + } + else { + // Changing directories worked OK + return TRUE; + } +} + +/** + * The drupal_ftp_file_list Function + * This function will change into the $directory folder and get a list of files and directories contained in that folder. + * This function still needs a lot of work, but should work in most cases. + */ +function drupal_ftp_file_list($directory, &$ftp) { + // This function will attempt to change into the specified + // directory and retrieve a list of files as an associative + // array. This list will include file name, size and date last modified + + $file_array = array(); + + // Can we switch to the desired directory? + if (!drupal_ftp_change_directory($directory, $ftp)) { + return FALSE; + } + + // We are in the directory, let's retrieve a list of files + // This is slower than parsing the raw return values, but it is faster. + $file_list = ftp_nlist($ftp->__conn, $directory); + + // Save the list of files + if (@is_array($file_list)) { + // Interate through the array + foreach ($file_list as $file) { + $file_array[] = array( + 'filename' => $file, + 'filesize' => ftp_size($ftp->__conn, $directory ."/". $file), + 'filetime' => ftp_mdtm($ftp->__conn, $directory ."/". $file), + ); + } + } + sort($file_array); + return $file_array; +} + +/** + * The drupal_ftp_create_directory Function + * This function tries to make a new directory called $folder_name on the FTP server. + * If it can create the folder, then the folder is given appropriate rights with the CHMOD command. + */ +function drupal_ftp_create_directory($folder_name, &$ftp) { + // Makes a new folder on the web server via FTP + + if (!@drupal_ftp_connect($ftp)) { + return FALSE; + } + + $create_result = @ftp_mkdir($ftp->__conn, $folder_name); + + if ($create_result == TRUE) { + // Can we change the files permissions? + $exec_result = @ftp_site($ftp->__conn, 'chmod 0777 '. $folder_name .'/'); + + if ($exec_result == TRUE) { + return TRUE; + } + else { + _backup_migrate_message('FTP Error: Couldn\'t set owner permissions on @folder.', array('@folder', $folder_name), 'error'); + return FALSE; + } + } + else { + _backup_migrate_message('FTP Error: Couldn\'t create new folder @folder.', array('@folder', $folder_name), 'error'); + return FALSE; + } +} + +/** + * The drupal_ftp_delete_file Function + * This function attempts to delete a file called $filename from the FTP server. + */ +function drupal_ftp_delete_file($filename, &$ftp) { + // Remove the specified file from the FTP server + if (!@drupal_ftp_connect($ftp)) { + return FALSE; + } + + $delete_result = @ftp_delete($ftp->__conn, $filename); + + if ($delete_result == TRUE) { + // The file/folder was renamed successfully + return TRUE; + } + else { + // Couldn't delete the selected file + _backup_migrate_message('FTP Error: Couldn\'t delete the selected file: @filename', array('@filename' => $filename), 'error'); + return FALSE; + } +} + +/** + * The drupal_ftp_delete_folder Function + * This function was one of the hardest to write. It recursively deletes all files and folders from a directory called $folder_name. + */ +function drupal_ftp_delete_folder($folder_name, &$ftp) { + if (!@drupal_ftp_connect($ftp)) { + return FALSE; + } + + @ftp_chdir($ftp->__conn, $folder_name); + $location = @ftp_pwd($ftp->__conn); + + $directories = array(); + $files = array(); + $dir_counter = 0; + $file_counter = 0; + $content = @ftp_nlist($ftp->__conn, "."); + + for ($i = 0; $i < sizeof($content); $i++) { + // If we can change into this then it's a directory. + // If not, it's a file + if ($content[$i] != "." && $content[$i] != "..") { + if (@ftp_chdir($ftp->__conn, $content[$i])) { + // We have a directory + $directories[] = $content[$i]; + $dir_counter++; + @ftp_cdup($ftp->__conn); + } + else { + // We have a file + $files[] = $content[$i]; + $file_counter++; + } + } + } + + for ($j = 0; $j < $file_counter; $j++) { + @ftp_delete($ftp->__conn, $files[$j]); + } + + for ($j = 0; $j < $dir_counter; $j++) { + if ($directories[$j] != "." OR $directories[$j] != "..") { + $location = ftp_pwd($ftp->__conn); + drupal_ftp_delete_folder($directories[$j], $ftp); + @ftp_cdup ($ftp->__conn); + @ftp_rmdir($ftp->__conn, $directories[$j]); + } + } + + // Lastly, we change into the directory that we want to delete and see + // if we can cdup. If we can, we delete it. + @ftp_chdir($ftp->__conn, $folder_name); + @ftp_cdup($ftp->__conn); + @ftp_rmdir($ftp->__conn, $folder_name); + + // Did the recursive folder/file deletion work? + return TRUE; +} diff --git a/sites/all/modules/contrib/backup_migrate/includes/destinations.inc b/sites/all/modules/contrib/backup_migrate/includes/destinations.inc new file mode 100644 index 0000000..871a866 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/destinations.inc @@ -0,0 +1,1280 @@ + array( + 'description' => t('Save the backup files to any directory on this server which the web-server can write to.'), + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.file.inc', + 'class' => 'backup_migrate_destination_files', + 'type_name' => t('Server Directory'), + 'local' => TRUE, + 'can_create' => TRUE, + ), + 'file_manual' => array( + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.file.inc', + 'type_name' => t('Server Directory'), + 'class' => 'backup_migrate_destination_files_manual', + ), + 'file_scheduled' => array( + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.file.inc', + 'type_name' => t('Server Directory'), + 'class' => 'backup_migrate_destination_files_scheduled', + ), + ); + } + $out += array( + 'browser_download' => array( + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.browser.inc', + 'class' => 'backup_migrate_destination_browser_download', + ), + 'browser_upload' => array( + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.browser.inc', + 'class' => 'backup_migrate_destination_browser_upload', + ), + 'nodesquirrel' => array( + 'description' => t('Save the backup files to the NodeSquirrel.com backup service.'), + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.nodesquirrel.inc', + 'class' => 'backup_migrate_destination_nodesquirrel', + 'type_name' => t('NodeSquirrel.com'), + 'can_create' => TRUE, + 'remote' => TRUE, + ), + 'ftp' => array( + 'description' => t('Save the backup files to any a directory on an FTP server.'), + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.ftp.inc', + 'class' => 'backup_migrate_destination_ftp', + 'type_name' => t('FTP Directory'), + 'can_create' => TRUE, + 'remote' => TRUE, + ), + 's3' => array( + 'description' => t('Save the backup files to a bucket on your !link.', array('!link' => l(t('Amazon S3 account'), 'http://aws.amazon.com/s3/'))), + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.s3.inc', + 'class' => 'backup_migrate_destination_s3', + 'type_name' => t('Amazon S3 Bucket'), + 'can_create' => TRUE, + 'remote' => TRUE, + ), + 'email' => array( + 'type_name' => t('Email'), + 'description' => t('Send the backup as an email attachment to the specified email address.'), + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/destinations.email.inc', + 'class' => 'backup_migrate_destination_email', + 'can_create' => TRUE, + 'remote' => TRUE, + ), + ); + + return $out; +} + +/** + * Implementation of hook_backup_migrate_destinations(). + * + * Get the built in backup destinations and those in the db. + */ +function backup_migrate_backup_migrate_destinations() { + $out = array(); + + // Add the default, out of the box destinations for new users. + if (variable_get('backup_migrate_allow_backup_to_file', TRUE)) { + if (variable_get('file_private_path', FALSE)) { + $out['manual'] = backup_migrate_create_destination('file_manual', array('machine_name' => 'manual')); + $out['scheduled'] = backup_migrate_create_destination('file_scheduled', array('machine_name' => 'scheduled')); + } + else { + _backup_migrate_message('You must specify a private file system path in the !settings to backup to the server.', array('!settings' => l(t('file system settings'), 'admin/config/media/file-system')), 'warning'); + } + } + // Add the browser destination for downloading to the desktop. + if (variable_get('backup_migrate_allow_backup_to_download', TRUE)) { + $out['download'] = backup_migrate_create_destination('browser_download'); + } + $out['upload'] = backup_migrate_create_destination('browser_upload'); + // Expose the configured databases as sources. + backup_migrate_include('filters'); + $out += backup_migrate_filters_invoke_all('destinations'); + + return $out; +} + +/** + * Get all the available backup destination. + * + * @param $op + * The operation which will be performed on the destination. Hooks can use this + * to return only those destinations appropriate for the given op. + * Options include: + * 'manual backup' - destinations available for manual backup + * 'scheduled backup' - destinations available for schedules backup + * 'list files' - destinations whose backup files can be listed + * 'restore' - destinations whose files can be restored from + * 'all' - all available destinations should be returned + */ +function backup_migrate_get_destinations($op = 'all') { + static $destinations = NULL; + + // Get the list of destinations and cache them locally. + if ($destinations === NULL) { + $destinations = backup_migrate_crud_get_items('destination'); + } + + // Return all if that's what was asked for. + if ($op == 'all') { + return $destinations; + } + + // Return only those destinations which support the given op. + $out = array(); + foreach ($destinations as $key => $destination) { + if ($destination->op($op)) { + $out[$key] = $destination; + } + } + return $out; +} + +/** + * Get the destination of the given id. + */ +function backup_migrate_get_destination($id) { + $destinations = backup_migrate_get_destinations('all'); + return empty($destinations[$id]) ? NULL : $destinations[$id]; +} + +/** + * Create a destination object of the given type with the given params. + */ +function backup_migrate_create_destination($destination_type, $params = array()) { + $params['subtype'] = $destination_type; + return backup_migrate_crud_create_item('destination', $params); +} + +/** + * Load a file from a destination and return the file info. + */ +function backup_migrate_destination_get_file($destination_id, $file_id) { + if ($destination = backup_migrate_get_destination($destination_id)) { + return $destination->load_file($file_id); + } + return NULL; +} + +/** + * Load a file from a destination and return the file info. + */ +function backup_migrate_destination_get_latest_file($destination_id) { + $out = NULL; + if ($destination = backup_migrate_get_destination($destination_id)) { + $files = $destination->list_files(); + $max = 0; + foreach ((array)$files as $file) { + $info = $file->info(); + + // If there's a datestamp, it should override the filetime as it's probably more reliable. + $time = !empty($info['datestamp']) ? $info['datestamp'] : $info['filetime']; + if ($time > $max) { + $max = $time; + $out = $file; + } + } + } + return $out; +} + + +/** + * Check if a file exists in the given destination. + */ +function backup_migrate_destination_file_exists($destination_id, $file_id) { + if ($destination = backup_migrate_get_destination($destination_id)) { + return $destination->file_exists($file_id); + } + return NULL; +} + +/** + * Send a file to the destination specified by the settings array. + */ +function backup_migrate_destination_confirm_destination(&$settings) { + if ($destinations = $settings->get_destinations()) { + foreach ($destinations as $key => $destination) { + // Check that the destination is ready to go. + if (!$destination->confirm_destination()) { + unset($destinations[$key]); + } + } + } + $settings->destinations = $destinations; + return count($destinations); +} + +/** + * Send a file to the destination specified by the settings array. + */ +function backup_migrate_destination_save_file($file, &$settings) { + $saved_to = array(); + if ($destinations = $settings->get_destinations()) { + foreach ($destinations as $destination) { + // Make sure the file only gets saved to each destination once. + $id = $destination->get('id'); + if (!in_array($id, $saved_to)) { + if ($destination->save_file($file, $settings)) { + $saved_to[] = $id; + } + } + } + } + return $saved_to ? $file : FALSE; +} + +/** + * Delete a file in the given destination. + */ +function backup_migrate_destination_delete_file($destination_id, $file_id) { + if ($destination = backup_migrate_get_destination($destination_id)) { + return $destination->delete_file($file_id); + } +} + +/** + * Get the action links for a file on a given destination. + */ +function _backup_migrate_destination_get_file_links($destination_id, $file_id) { + $out = array(); + if ($destination = backup_migrate_get_destination($destination_id)) { + $out = $destination->get_file_links($file_id); + } + return $out; +} + +/* UI Menu Callbacks */ + +/** + * List the backup files in the given destination. + */ +function backup_migrate_ui_destination_display_files($destination_id = NULL) { + drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css'); + + $rows = $sort = array(); + if ($destination = backup_migrate_get_destination($destination_id)) { + drupal_set_title(t('@title Files', array('@title' => $destination->get_name()))); + return _backup_migrate_ui_destination_display_files($destination, 20, TRUE); + } + drupal_goto(BACKUP_MIGRATE_MENU_PATH . "/destination"); +} + +/** + * List the backup files in the given destination. + */ +function _backup_migrate_ui_destination_display_files($destination = NULL, $limit = NULL, $show_pager = FALSE) { + if ($destination) { + // Refresh the file listing cache if requested. + if (isset($_GET['refresh'])) { + $destination->file_cache_clear(); + drupal_goto($_GET['q']); + } + $files = $destination->list_files(); + + $fetch = $out = ''; + + // Get the fetch link. + if ($destination->cache_files && $destination->fetch_time) { + $fetch = '
'. t('This listing was fetched !time ago. !refresh', array('!time' => format_interval(time() - $destination->fetch_time, 1), '!refresh' => l(t('fetch now'), $_GET['q'], array('query' => array('refresh' => 'true'))))) .'
'; + } + + $out .= $fetch; + $out .= _backup_migrate_ui_destination_display_file_list($files, array('limit' => 20, 'pager' => TRUE)); + $out .= $fetch; + + return $out; + } +} + +/** + * List the backup files in the given destination. + */ +function _backup_migrate_ui_destination_display_file_list($files, $options = array()) { + drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css'); + + // Set soem default options + $options += array( + 'pager' => TRUE, + 'more' => FALSE, + 'operations' => TRUE, + 'limit' => NULL, + 'form_elements' => NULL, + ); + + $rows = $sort = array(); + if ($files) { + $headers = array( + array('data' => 'Filename', 'field' => 'filename'), + array('data' => 'Created', 'field' => 'filetime', 'sort' => 'desc'), + array('data' => 'Size', 'field' => 'filesize'), + ); + if ($options['operations']) { + $headers[] = array('data' => 'Operations'); + } + if ($options['form_elements']) { + array_unshift($headers, ''); + } + + $sort_order = tablesort_get_order($headers); + $sort_key = $sort_order['sql'] ? $sort_order['sql'] : 'filetime'; + $sort_dir = tablesort_get_sort($headers) == 'desc' ? SORT_DESC : SORT_ASC; + + $i = 0; + foreach ((array)$files as $id => $file) { + $info = $file->info(); + + // If there's a datestamp, it should override the filetime as it's probably more reliable. + $info['filetime'] = !empty($info['datestamp']) ? $info['datestamp'] : $info['filetime']; + + $description = ''; + + // Add the description as a new row. + if (!empty($info['description'])) { + $description .= '
' . check_plain($info['description']) . '
'; + } + // Add the backup source. + if (!empty($info['bam_sourcename'])) { + $description .= '
'. t('Source:') .' ' . check_plain($info['bam_sourcename']) . '
'; + } + // Add the tags as a new row. + if (!empty($info['tags'])) { + $tags = check_plain(implode(', ', (array)$info['tags'])); + $description .= '
' . t('Tags:') . ' ' . $tags . '
'; + } + // Add the other info. + if (!empty($info['bam_other_safe'])) { + foreach ($info['bam_other_safe'] as $label => $data) { + $description .= '
' . $label . ' ' . $data . '
'; + } + } + + // Show only files that can be restored from. + $sort[] = $info[$sort_key]; + $row = array( + check_plain($info['filename']) . $description, + t('!time ago', array('!time' => format_interval(time() - $info['filetime'], 2))) . '
' . format_date($info['filetime'], 'small') . '
', + format_size($info['filesize']), + ); + if ($options['operations']) { + $row[] = array('data' => implode('   ', $file->destination->get_file_links($file->file_id())), 'class' => 'backup-migrate-actions'); + } + if (isset($options['form_elements'][$id])) { + array_unshift($row, $options['form_elements'][$id]); + } + + $rows[] = $row; + + } + + array_multisort($sort, $sort_dir, $rows); + + // Show only some of them if it's limited. + $showing = $pager = $more = ''; + if ($options['limit']) { + $limit = $options['limit']; + $total = count($rows); + $end = $limit; + $start = 0; + + if ($options['pager']) { + $page = isset($_GET['page']) ? $_GET['page'] : ''; + $start = $page * $limit; + + $element = 0; + $GLOBALS['pager_total'][$element] = ceil($total / $limit); + $GLOBALS['pager_page_array'][$element] = $page; + $tags = array( + t('« newest'), + t('« newer'), + '', + t('older »'), + t('oldest »'), + ); + $pager = theme('pager', $tags, $limit, $element, array(), ceil($total/$limit)); + + $end = min($total - 1, $start + $limit); + } + if ($total > $limit && $options['more']) { + $more = ' ' . l(t('view all'), $options['more']); + } + $showing = t('Showing @start to @end of @total files.', array('@start' => $start + 1, '@end' => $end + 1, '@total' => $total)); + + // Limit the number of rows shown. + $rows = array_slice($rows, $start, $limit, TRUE); + } + + $out = theme("table", array('header' => $headers, 'rows' => $rows, 'class' => 'backup-migrate-listing backup-migrate-listing-files')); + $out .= $showing; + $out .= $pager; + $out .= $more; + } + else { + $out = t('There are no backup files to display.'); + } + return $out; +} + +/** + * List the backup files in the given destination. + */ +function _backup_migrate_ui_destination_display_file_list_options($files, $limit = NULL) { + drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css'); + + $rows = $sort = array(); + if ($files) { + $headers = array( + '', + array('data' => 'Filename', 'field' => 'filename'), + array('data' => 'Created', 'field' => 'filetime', 'sort' => 'desc'), + array('data' => 'Size', 'field' => 'filesize'), + ); + + $sort_order = tablesort_get_order($headers); + $sort_key = $sort_order['sql'] ? $sort_order['sql'] : 'filetime'; + $sort_dir = tablesort_get_sort($headers) == 'desc' ? SORT_DESC : SORT_ASC; + + $i = 0; + foreach ((array)$files as $file) { + $info = $file->info(); + + // If there's a datestamp, it should override the filetime as it's probably more reliable. + $info['filetime'] = !empty($info['datestamp']) ? $info['datestamp'] : $info['filetime']; + + $description = ''; + + // Add the description as a new row. + if (!empty($info['description'])) { + $description .= '
' . check_plain($info['description']) . '
'; + } + // Add the backup source. + if (!empty($info['bam_sourcename'])) { + $description .= '
'. t('Source:') .' ' . check_plain($info['bam_sourcename']) . '
'; + } + // Add the tags as a new row. + if (!empty($info['tags'])) { + $tags = check_plain(implode(', ', (array)$info['tags'])); + $description .= '
' . t('Tags:') . ' ' . $tags . '
'; + } + // Add the other info. + if (!empty($info['bam_other_safe'])) { + foreach ($info['bam_other_safe'] as $label => $data) { + $description .= '
' . $label . ' ' . $data . '
'; + } + } + + // Show only files that can be restored from. + $sort[] = $info[$sort_key]; + $rows[] = array( + 'radio', + check_plain($info['filename']) . $description, + t('!time ago', array('!time' => format_interval(time() - $info['filetime'], 2))) . '
' . format_date($info['filetime'], 'small') . '
', + format_size($info['filesize']), + ); + } + + array_multisort($sort, $sort_dir, $rows); + + // Show only some of them if it's limited. + $showing = $pager = ''; + if ($limit) { + $total = count($rows); + $end = $limit; + $start = 0; + + + $showing = t('Showing @start to @end of @total files.', array('@start' => $start + 1, '@end' => $end + 1, '@total' => $total)); + + // Limit the number of rows shown. + $rows = array_slice($rows, $start, $limit, TRUE); + } + + $out = theme("table", array('header' => $headers, 'rows' => $rows, 'class' => 'backup-migrate-listing backup-migrate-listing-files')); + $out .= $showing; + } + else { + $out = t('There are no backup files to display.'); + } + return $out; +} + + +/** + * Download a file to the browser. + */ +function backup_migrate_ui_destination_download_file($destination_id = NULL, $file_id = NULL) { + if ($file = backup_migrate_destination_get_file($destination_id, $file_id)) { + backup_migrate_include('files'); + $file->transfer(); + } + drupal_goto(BACKUP_MIGRATE_MENU_PATH); +} + +/** + * Restore a backup file from a destination. + */ +function backup_migrate_ui_destination_restore_file($destination_id = NULL, $file_id = NULL) { + if (backup_migrate_destination_file_exists($destination_id, $file_id)) { + return drupal_get_form('backup_migrate_ui_destination_restore_file_confirm', $destination_id, $file_id); + } + _backup_migrate_message('Cannot restore from the the file: %file_id because it does not exist.', array('%file_id' => $file_id), 'error'); + if ($destination_id && user_access('access backup files')) { + drupal_goto(BACKUP_MIGRATE_MENU_PATH . '/destination/list/files/' . $destination_id); + } + drupal_goto(BACKUP_MIGRATE_MENU_PATH); +} + +/** + * Ask confirmation for file restore. + */ +function backup_migrate_ui_destination_restore_file_confirm($form, &$form_state, $destination_id, $file_id) { + $sources = _backup_migrate_get_source_form_item_options(); + if (count($sources) > 1) { + $form['source_id'] = array( + "#type" => "select", + "#title" => t("Restore to"), + "#options" => $sources, + "#description" => t("Choose the database to restore to. Any database destinations you have created and any databases specified in your settings.php can be restored to."), + "#default_value" => 'db', + ); + } + else { + $form['source_id'] = array( + "#type" => "value", + "#value" => 'db', + ); + } + + $form['destination_id'] = array('#type' => 'value', '#value' => $destination_id); + $form['file_id'] = array('#type' => 'value', '#value' => $file_id); + $form = confirm_form($form, t('Are you sure you want to restore the database?'), BACKUP_MIGRATE_MENU_PATH . "/destination/list/files/". $destination_id, t('Are you sure you want to restore the database from the backup file %file_id? This will delete some or all of your data and cannot be undone. Always test your backups on a non-production server!', array('%file_id' => $file_id)), t('Restore'), t('Cancel')); + drupal_set_message(t('Restoring will delete some or all of your data and cannot be undone. Always test your backups on a non-production server!'), 'warning', FALSE); + $form = array_merge_recursive($form, backup_migrate_filters_settings_form(backup_migrate_filters_settings_default('restore'), 'restore')); + $form['actions']['#weight'] = 100; + + // Add the advanced fieldset if there are any fields in it. + if (@$form['advanced']) { + $form['advanced']['#type'] = 'fieldset'; + $form['advanced']['#title'] = t('Advanced Options'); + $form['advanced']['#collapsed'] = true; + $form['advanced']['#collapsible'] = true; + } + + return $form; +} + +/** + * Do the file restore. + */ +function backup_migrate_ui_destination_restore_file_confirm_submit($form, &$form_state) { + $destination_id = $form_state['values']['destination_id']; + $file_id = $form_state['values']['file_id']; + if ($destination_id && $file_id) { + backup_migrate_perform_restore($destination_id, $file_id, $form_state['values']); + } + $redir = user_access('access backup files') ? BACKUP_MIGRATE_MENU_PATH . "/destination/list/files/". $destination_id : BACKUP_MIGRATE_MENU_PATH; + $form_state['redirect'] = $redir; +} + +/** + * Menu callback to delete a file from a destination. + */ +function backup_migrate_ui_destination_delete_file($destination_id = NULL, $file_id = NULL) { + if (backup_migrate_destination_file_exists($destination_id, $file_id)) { + return drupal_get_form('backup_migrate_ui_destination_delete_file_confirm', $destination_id, $file_id); + } + _backup_migrate_message('Cannot delete the file: %file_id because it does not exist.', array('%file_id' => $file_id), 'error'); + if ($destination_id && user_access('access backup files')) { + drupal_goto(BACKUP_MIGRATE_MENU_PATH .'/destination/list/files/' . $destination_id); + } + drupal_goto(BACKUP_MIGRATE_MENU_PATH); +} + +/** + * Ask confirmation for file deletion. + */ +function backup_migrate_ui_destination_delete_file_confirm($form, &$form_state, $destination_id, $file_id) { + $form['destination_id'] = array('#type' => 'value', '#value' => $destination_id); + $form['file_id'] = array('#type' => 'value', '#value' => $file_id); + return confirm_form($form, t('Are you sure you want to delete the backup file?'), BACKUP_MIGRATE_MENU_PATH . '/destination/list/files/'. $destination_id, t('Are you sure you want to delete the backup file %file_id? This action cannot be undone.', array('%file_id' => $file_id)), t('Delete'), t('Cancel')); +} + +/** + * Delete confirmed, perform the delete. + */ +function backup_migrate_ui_destination_delete_file_confirm_submit($form, &$form_state) { + if (user_access('delete backup files')) { + $destination_id = $form_state['values']['destination_id']; + $file_id = $form_state['values']['file_id']; + backup_migrate_destination_delete_file($destination_id, $file_id); + _backup_migrate_message('Database backup file deleted: %file_id', array('%file_id' => $file_id)); + } + $form_state['redirect'] = user_access('access backup files') ? BACKUP_MIGRATE_MENU_PATH . "/destination/list/files/". $destination_id : BACKUP_MIGRATE_MENU_PATH; +} + +/* Utilities */ + +/** + * Get pulldown to select existing source options. + */ +function _backup_migrate_get_destination_pulldown($op, $destination_id = NULL, $copy_destination_id = NULL) { + drupal_add_js(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.js'); + + $destinations = _backup_migrate_get_destination_form_item_options($op); + $form = array( + '#element_validate' => array('_backup_migrate_destination_pulldown_validate'), + '#after_build' => array('_backup_migrate_process_destination_pulldown'), + ); + $form['destination_id'] = array( + '#type' => 'select', + '#title' => t('Backup Destination'), + '#options' => $destinations, + '#default_value' => $destination_id, + //'#process' => array('_backup_migrate_process_destination_pulldown'), + ); + if (user_access('administer backup and migrate')) { + $form['destination_id']['#description'] = l(t('Create new destination'), BACKUP_MIGRATE_MENU_PATH . '/settings/destination/add'); + } + $form['copy'] = array( + '#type' => 'checkbox', + '#title' => '' . t('Save a copy to a second destination') . '', + '#default_value' => !empty($copy_destination_id), + ); + $form['copy_destination'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'copy' => TRUE, + ), + ); + $form['copy_destination']['copy_destination_id'] = array( + '#type' => 'select', + '#title' => t('Second Backup Destination'), + '#options' => $destinations, + '#default_value' => $copy_destination_id, + ); + + + + return $form; +} + +/** + * Return the destination pulldown with an option to save a second copy. + */ +function _backup_migrate_process_destination_pulldown($element) { + $id = $element['destination_id']['#id']; + $settings = array( + 'backup_migrate' => array( + 'destination_selectors' => array( + $id => array( + 'destination_selector' => $id, + 'copy_destination_selector' => $element['copy_destination']['copy_destination_id']['#id'], + 'copy' => $element['copy']['#id'], + 'labels' => array( + t('Local') => t('Save an offsite copy to'), + t('Offsite') => t('Save a local copy to'), + ) + ) + ) + ) + ); + drupal_add_js($settings, 'setting'); + + return $element; +} + +/** + * Validate the destination and second destination widget. + */ +function _backup_migrate_destination_pulldown_validate($element, &$form_state, $form) { + if (empty($element['copy']['#value']) || $element['copy_destination']['copy_destination_id']['#value'] == $element['destination_id']['#value']) { + _backup_migrate_destination_pulldown_set_value($form_state['values'], $element['copy_destination']['copy_destination_id']['#parents'], ''); + } +} + +/** + * Set the value of a form field given it's parent array. + */ +function _backup_migrate_destination_pulldown_set_value(&$values, $parents, $value) { + $key = array_shift($parents); + if (empty($parents)) { + $values[$key] = $value; + } + else { + _backup_migrate_destination_pulldown_set_value($values[$key], $parents, $value); + } +} + +/** + * Get the destination options as an options array for a form item. + */ +function _backup_migrate_get_destination_form_item_options($op) { + $remote = $local = array(); + foreach (backup_migrate_get_destinations($op) as $key => $destination) { + if ($destination->op('remote backup')) { + $remote[$key] = $destination->get_name(); + } + else { + $local[$key] = $destination->get_name(); + } + } + $out = array(); + if ($remote && $local) { + $out = array( + t('Local') => $local, + t('Offsite') => $remote, + ); + } + else { + $out = $remote + $local; + } + return $out; +} + +/** + * A base class for creating destinations. + */ +class backup_migrate_destination extends backup_migrate_location { + var $db_table = "backup_migrate_destinations"; + var $type_name = "destination"; + var $default_values = array('settings' => array()); + var $singular = 'destination'; + var $plural = 'destinations'; + var $title_plural = 'Destinations'; + var $title_singular = 'Destination'; + var $cache_files = FALSE; + var $fetch_time = NULL; + var $cache_expire = 86400; // 24 hours + var $weight = 0; + + var $destination_type = ""; + var $supported_ops = array(); + + /** + * This function is not supposed to be called. It is just here to help the po extractor out. + */ + function strings() { + // Help the pot extractor find these strings. + t('Destination'); + t('Destinations'); + t('destinations'); + t('destination'); + } + + /** + * Save the given file to the destination. + */ + function save_file($file, $settings) { + $this->file_cache_clear(); + + // Save the file metadata if the destination supports it. + $this->save_file_info($file, $settings); + return $this->_save_file($file, $settings); + } + + /** + * Save the given file to the destination. + */ + function _save_file($file, $settings) { + // This must be overriden. + return $file; + } + + /** + * Save the file metadata + */ + function save_file_info($file, $settings) { + $info = $this->create_info_file($file); + // Save the info file and the actual file. + return $this->_save_file($info, $settings); + } + + /** + * Load the file with the given destination specific id and return as a backup_file object. + */ + function load_file($file_id) { + // This must be overriden. + return NULL; + } + + /** + * Check if a file exists in the given destination. + */ + function file_exists($file_id) { + // Check if the file exists in the list of available files. Actual destination types may have more efficient ways of doing this. + $files = $this->list_files(); + return isset($files[$file_id]); + } + + /** + * List all the available files in the given destination with their destination specific id. + */ + function list_files() { + $files = NULL; + if ($this->cache_files) { + $files = $this->file_cache_get(); + } + if ($files === NULL) { + $files = $this->_list_files(); + $files = $this->load_files_info($files); + if ($this->cache_files) { + $this->file_cache_set($files); + } + } + + $out = array(); + if (is_array($files)) { + foreach ($files as $id => $file) { + if ($file->is_recognized_type()) { + $out[$id] = $file; + $out[$id]->destination = &$this; + } + } + } + return $out; + } + + /** + * List all the available files in the given destination with their destination specific id. + */ + function count_files() { + return count($this->list_files()); + } + + + /** + * List all the available files in the given destination with their destination specific id. + */ + function _list_files() { + return array(); + } + + /** + * Load up the file's metadata from the accompanying .info file if applicable. + */ + function load_files_info($files) { + foreach ($files as $key => $file) { + // See if there is an info file with the same name as the backup. + if (isset($files[$key . '.info'])) { + $info_file = $this->load_file($files[$key . '.info']->file_id()); + $info = drupal_parse_info_file($info_file->filepath()); + // Allow the stored metadata to override the detected metadata. + unset($info['filename']); + $file->file_info = $info + $file->file_info; + // Remove the metadata file from the list + unset($files[$key . '.info']); + } + + // Add destination specific info. + $file->info_set('destination_id', $this->get('id')); + $file->info_set('remote', $this->get('remote')); + } + + + return $files; + } + + /** + * Create an ini file and write the meta data. + */ + function create_info_file($file) { + $info = $this->_file_info_file($file); + $data = _backup_migrate_array_to_ini($file->file_info); + $info->put_contents($data); + return $info; + } + + /** + * Create the info file object. + */ + function _file_info_file($file) { + $info = new backup_file(array('filename' => $this->_file_info_filename($file->file_id()))); + return $info; + } + + /** + * Determine the file name of the info file for a file. + */ + function _file_info_filename($file_id) { + return $file_id . '.info'; + } + + + /** + * Cache the file list. + */ + function file_cache_set($files) { + cache_set('backup_migrate_file_list:'. $this->get_id(), $files, 'cache', time() + $this->cache_expire); + } + + /** + * Retrieve the file list. + */ + function file_cache_get() { + backup_migrate_include('files'); + $cache = cache_get('backup_migrate_file_list:'. $this->get_id()); + if (!empty($cache->data) && $cache->created > (time() - $this->cache_expire)) { + $this->fetch_time = $cache->created; + return $cache->data; + } + $this->fetch_time = 0; + return NULL; + } + + /** + * Retrieve the file list. + */ + function file_cache_clear() { + if ($this->cache_files) { + $this->file_cache_set(NULL); + } + } + + /** + * Delete the file with the given destination specific id. + */ + function delete_file($file_id) { + $this->file_cache_clear(); + $this->_delete_file($file_id); + $this->_delete_file($this->_file_info_filename($file_id)); + } + + /** + * Delete the file with the given destination specific id. + */ + function _delete_file($file_id) { + // This must be overriden. + } + + /** + * Get the edit form for the item. + */ + function edit_form() { + if (get_class($this) !== 'backup_migrate_destination') { + $form = parent::edit_form(); + $form['subtype'] = array( + "#type" => "value", + "#default_value" => $this->get('subtype'), + ); + } + else { + $types = backup_migrate_get_destination_subtypes(); + $items = array( + 'remote' => array( + 'title' => t('Offsite Destinations'), + 'description' => t('For the highest level of protection, set up an offsite backup destination in a location separate from your website.'), + 'items' => array(), + ), + 'local' => array( + 'title' => t('Local Destinations'), + 'description' => t('Local backups are quick and convenient but do not provide the additional safety of offsite backups.'), + 'items' => array(), + ), + 'other' => array( + 'title' => t('Other Destinations'), + 'description' => t('These destinations have not been classified because they were created for Backup and Migrate version 2. They may not work correctly with this version.'), + 'items' => array(), + ), + + ); + + // If no (valid) node type has been provided, display a node type overview. + $path = $this->get_settings_path(); + foreach ($types as $key => $type) { + if (@$type['can_create']) { + $type_url_str = str_replace('_', '-', $key); + $out = '
'. l($type['type_name'], $path . "/add/$type_url_str", array('attributes' => array('title' => t('Add a new @s destination.', array('@s' => $type['type_name']))))) .'
'; + $out .= '
'. filter_xss_admin($type['description']) .'
'; + + if (!empty($type['local'])) { + $items['local']['items'][] = $out; + } + if (!empty($type['remote'])) { + $items['remote']['items'][] = $out; + } + if (empty($type['local']) && empty($type['remote'])) { + $items['other']['items'][] = $out; + } + } + } + if (count($items['local']['items']) || count($items['remote']['items']) || count($items['other']['items'])) { + $output = '

' . t('Choose the type of destination you would like to create:') . '

'; + foreach ($items as $group) { + if (count($group['items'])) { + $group['body'] = '
'. implode('', $group['items']) .'
'; + $output .= theme('backup_migrate_group', $group); + } + } + } + else { + $output = t('No destination types available.'); + } + $form['select_type'] = array( + '#type' => 'markup', + '#markup' => $output, + ); + } + return $form; + } + + /** + * Get the message to send to the user when confirming the deletion of the item. + */ + function delete_confirm_message() { + return t('Are you sure you want to delete the destination %name? Backup files already saved to this destination will not be deleted.', array('%name' => $this->get_name())); + } + + /** + * Get a boolean representing if the destination is remote or local. + */ + function get_remote() { + return $this->op('remote backup'); + } + + /** + * Get the action links for a destination. + */ + function get_action_links() { + $out = parent::get_action_links(); + $item_id = $this->get_id(); + + // Don't display the download/delete/restore ops if they are not available for this destination. + if ($this->op('list files') && user_access("access backup files")) { + $out = array('list files' => l(t("list files"), $this->get_settings_path() . '/list/files/'. $item_id)) + $out; + } + if (!$this->op('configure') || !user_access('administer backup and migrate')) { + unset($out['edit']); + } + return $out; + } + + /** + * Get the action links for a file on a given destination. + */ + function get_file_links($file_id) { + $out = array(); + + // Don't display the download/delete/restore ops if they are not available for this destination. + $can_read = $this->can_read_file($file_id); + $can_delete = $this->can_delete_file($file_id); + + $path = $this->get_settings_path(); + + $destination_id = $this->get_id(); + if ($can_read && user_access("access backup files")) { + $out[] = l(t("download"), $path . '/downloadfile/'. $destination_id .'/'. $file_id); + } + if ($can_read && user_access("restore from backup")) { + $out[] = l(t("restore"), $path . '/list/restorefile/' . $destination_id .'/'. $file_id); + } + if ($can_delete && user_access("delete backup files")) { + $out[] = l(t("delete"), $path . '/list/deletefile/' . $destination_id .'/'. $file_id); + } + return $out; + } + + /** + * Determine if we can read the given file. + */ + function can_read_file($file_id) { + return $this->op('restore'); + } + + /** + * Determine if we can read the given file. + */ + function can_delete_file($file_id) { + return $this->op('delete'); + } + + /** + * Get the form for the settings for this destination type. + */ + function settings_default() { + return array(); + } + + /** + * Get the form for the settings for this destination. + */ + function settings_form($form) { + return $form; + } + + /** + * Validate the form for the settings for this destination. + */ + function settings_form_validate($form_values) { + } + + /** + * Submit the settings form. Any values returned will be saved. + */ + function settings_form_submit($form_values) { + return $form_values; + } + + /** + * Check that a destination is valid. + */ + function confirm_destination() { + return true; + } + + /** + * Add the menu items specific to the destination type. + */ + function get_menu_items() { + $items = parent::get_menu_items(); + + $path = $this->get_settings_path(); + + $items[$path . '/list/files'] = array( + 'title' => 'Destination Files', + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('destinations', 'backup_migrate_ui_destination_display_files', TRUE), + 'access arguments' => array('access backup files'), + 'type' => MENU_LOCAL_TASK, + ); + $items[$path . '/list/deletefile'] = array( + 'title' => 'Delete File', + 'description' => 'Delete a backup file', + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('destinations', 'backup_migrate_ui_destination_delete_file', TRUE), + 'access arguments' => array('delete backup files'), + 'type' => MENU_LOCAL_TASK, + ); + $items[$path . '/list/restorefile'] = array( + 'title' => 'Restore from backup', + 'description' => 'Restore database from a backup file on the server', + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('destinations', 'backup_migrate_ui_destination_restore_file', TRUE), + 'access arguments' => array('restore from backup'), + 'type' => MENU_LOCAL_TASK, + ); + $items[$path . '/downloadfile'] = array( + 'title' => 'Download File', + 'description' => 'Download a backup file', + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('destinations', 'backup_migrate_ui_destination_download_file', TRUE), + 'access arguments' => array('access backup files'), + 'type' => MENU_CALLBACK, + ); + return $items; + } + + +} + +/** + * A base class for creating destinations. + */ +class backup_migrate_destination_remote extends backup_migrate_destination { + /** + * The location is a URI so parse it and store the parts. + */ + function get_location() { + return $this->url(FALSE); + } + + /** + * The location to display is the url without the password. + */ + function get_display_location() { + return $this->url(TRUE); + } + + /** + * Return the location with the password. + */ + function set_location($location) { + $this->location = $location; + $this->set_url($location); + } + + /** + * Destination configuration callback. + */ + function edit_form() { + $form = parent::edit_form(); + $form['scheme'] = array( + "#type" => "textfield", + "#title" => t("Scheme"), + "#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : '', + "#required" => TRUE, + "#weight" => 0, + ); + $form['host'] = array( + "#type" => "textfield", + "#title" => t("Host"), + "#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost', + "#required" => TRUE, + "#weight" => 10, + ); + $form['path'] = array( + "#type" => "textfield", + "#title" => t("Path"), + "#default_value" => @$this->dest_url['path'], + "#required" => TRUE, + "#weight" => 20, + ); + $form['user'] = array( + "#type" => "textfield", + "#title" => t("Username"), + "#default_value" => @$this->dest_url['user'], + "#required" => TRUE, + "#weight" => 30, + ); + $form['pass'] = array( + "#type" => "password", + "#title" => t("Password"), + "#default_value" => @$this->dest_url['pass'], + '#description' => '', + "#weight" => 40, + ); + if (@$this->dest_url['pass']) { + $form['old_password'] = array( + "#type" => "value", + "#value" => @$this->dest_url['pass'], + ); + $form['pass']["#description"] .= t(' You do not need to enter a password unless you wish to change the currently saved password.'); + } + return $form; + } + + /** + * Submit the configuration form. Glue the url together and add the old password back if a new one was not specified. + */ + function edit_form_submit($form, &$form_state) { + $form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password']; + $form_state['values']['location'] = $this->glue_url($form_state['values'], FALSE); + parent::edit_form_submit($form, $form_state); + } +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/destinations.nodesquirrel.inc b/sites/all/modules/contrib/backup_migrate/includes/destinations.nodesquirrel.inc new file mode 100644 index 0000000..a929f07 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/destinations.nodesquirrel.inc @@ -0,0 +1,980 @@ +confirm_destination()) { + return $destination; + } + } + return FALSE; +} + +/** + * Get the NS destination for the given key. + */ +function backup_migrate_nodesquirrel_get_destination($secret_key) { + if ($secret_key) { + backup_migrate_include('destinations'); + $destination = backup_migrate_create_destination('nodesquirrel', array('machine_name' => 'nodesquirrel')); + $destination->settings['secret_key'] = $secret_key; + return $destination; + } + return NULL; +} + + +/** + * Get a helper link to activate a site and create a tree. + */ +function backup_migrate_nodesquirrel_get_activate_help_text() { + $activate_link = backup_migrate_nodesquirrel_get_activate_link(); + return array( + '#type' => 'item', + '#title' => t('Need a Secret Key?'), + '#markup' => t('Visit !nodesquirrel.', array('!nodesquirrel' => $activate_link)), + '#description' => t('Don\'t worry if you don\'t have an account yet. You can create one when you get there.'), + ); +} + +/** + * Get a helper link to activate a site and create a tree. + */ +function backup_migrate_nodesquirrel_get_activate_link() { + $activate_link = l('nodesquirrel.com/activate', variable_get('nodesquirrel_activate_url', 'http://manage.nodesquirrel.com/activate'), array('query' => array('url' => url('', array('absolute' => TRUE)), 'email' => variable_get('site_mail', ''), 'configure' => url($_GET['q'], array('absolute' => TRUE))))); + return $activate_link; +} + +/** + * Get a helper link to activate a site and create a tree. + */ +function backup_migrate_nodesquirrel_get_manage_link($destination) { + $url = variable_get('nodesquirrel_manage_url', 'https://manage.nodesquirrel.com') . '/backups/' . $destination->_get_destination(); + return l($url, $url); +} + +/** + * Get a helper link to activate a site and create a tree. + */ +function backup_migrate_nodesquirrel_get_plan_link() { + $url = variable_get('nodesquirrel_manage_url', 'https://manage.nodesquirrel.com') . '/plan'; + return l($url, $url); +} + +/** + * Get the path to the nodesquirrel settings tab. + */ +function backup_migrate_nodesquirrel_settings_path() { + $path = BACKUP_MIGRATE_MENU_PATH . '/settings/destinations/nodesquirrel/edit'; + return $path; +} + + +/** + * NodeSquirrel settings page callback. + */ +function backup_migrate_nodesquirrel_settings() { + return drupal_get_form('backup_migrate_nodesquirrel_settings_form'); +} + +/** + * NodeSquirrel settings form. + */ +function backup_migrate_nodesquirrel_credentials_settings_form($key = '', $status) { + $collapse = !empty($status); + $form['nodesquirrel_credentials'] = array( + '#type' => 'fieldset', + '#title' => t('NodeSquirrel Credentials'), + '#collapsible' => $collapse, + '#collapsed' => $collapse, + ); + + $form['nodesquirrel_credentials']['nodesquirrel_secret_key'] = array( + '#type' => 'textfield', + '#title' => t('Secret Key'), + '#size' => 80, + '#default_value' => $key, + ); + if (empty($key)) { + $form['nodesquirrel_credentials']['secret_key_help'] = backup_migrate_nodesquirrel_get_activate_help_text(); + } + + return $form; +} + +/** + * Return a form element with some help text describing NodeSquirrel. + */ +function backup_migrate_nodesquirrel_info_form() { + $form = array(); + + $form['nodesquirrel_info'] = array( + '#type' => 'fieldset', + '#title' => t('What is NodeSquirrel?'), + ); + + $form['nodesquirrel_info']['intro'] = array( + '#type' => 'markup', + '#markup' => t('

NodeSquirrel is the cloud backup service built by the maintainers of Backup and Migrate.

NodeSquirrel is now free for all Drupal sites, courtesy of Pantheon.

Create your free account at !signup

', array('!nodesquirrel' => l('nodesquirrel.com', 'http://www.nodesquirrel.com'), '!signup' => backup_migrate_nodesquirrel_get_activate_link())), + ); + return $form; +} + +/** + * Display the NodeSquirrel status on the configuration form. + */ +function backup_migrate_nodesquirrel_status_form($key, $destination, $status) { + $form = array(); + + $form['nodesquirrel_status'] = array( + '#type' => 'fieldset', + '#title' => t('NodeSquirrel Status'), + ); + $form['nodesquirrel_status']['status'] = array( + '#type' => 'item', + '#title' => t('NodeSquirrel Status'), + '#markup' => t('Not Configured. Enter your Secret Key below to get started.'), + ); + + // Warn the user if the key they entered is invalid. + if ($key && empty($status)) { + $form['nodesquirrel_status']['status']['#markup'] = t('Your secret key does not seem to be valid. Please check that you entered it correctly or visit !ns to generate a new key.', array('!ns' => backup_migrate_nodesquirrel_get_activate_link())); + } + else if (!empty($destination) && is_array($status)) { + if (!empty($status['lifetime_backups_used']) && !empty($status['lifetime_backups']) && $status['lifetime_backups_used'] >= $status['lifetime_backups']) { + $form['nodesquirrel_status']['status']['#markup'] = t('Your !num backup trial has expired. Visit !link to continue backing up.', array('!num' => $status['lifetime_backups'], '!link' => backup_migrate_nodesquirrel_get_plan_link())); + } + else { + $form['nodesquirrel_status']['status']['#markup'] = t('Ready to Backup'); + if (user_access('perform backup')) { + $form['nodesquirrel_status']['status']['#markup'] .= ' ' . l('(' . t('backup now') . ')', BACKUP_MIGRATE_MENU_PATH); + } + } + if (!empty($status['plan_name'])) { + $form['nodesquirrel_status']['plan_name'] = array( + '#type' => 'item', + '#title' => t('Current Plan'), + '#markup' => check_plain($status['plan_name']) + ); + + if (isset($status['plan_id']) && strpos($status['plan_id'], 'trial') !== FALSE) { + if (isset($status['lifetime_backups']) && isset($status['lifetime_backups_used'])) { + $remains = $status['lifetime_backups'] - $status['lifetime_backups_used']; + $remains = $remains > 0 ? $remains : t('none'); + $form['nodesquirrel_status']['plan_name']['#markup'] .= ' ' . t('(@remains remaining of @backups backup trial)', array('@backups' => $status['lifetime_backups'], '@remains' => $remains)); + } + + if (isset($status['lifespan']) && isset($status['age']) && $status['lifespan'] > 0) { + $remains = ceil(($status['lifespan'] - $status['age']) / 86400); + if ($remains <= 0) { + $form['nodesquirrel_status']['plan_name']['#markup'] .= ' ' . t('(Your !span day trial has expired.)', array('!span' => ceil($status['lifespan'] / 86400))); + } + else { + $form['nodesquirrel_status']['plan_name']['#markup'] .= ' ' . format_plural($remains, '(1 day remaining)', '(!span days remaining)', array('!span' => ceil($remains))); + } + } + } + + } + + if (isset($status['backups_used'])) { + $form['nodesquirrel_status']['backups_used'] = array( + '#type' => 'item', + '#title' => t('Number of Stored Backups'), + '#markup' => $status['backups_used'] == 0 ? t('None') : number_format($status['backups_used']) + ); + } + + if (isset($status['last_backup'])) { + $form['nodesquirrel_status']['last_backup'] = array( + '#type' => 'item', + '#title' => t('Last Backup'), + '#markup' => empty($status['last_backup']) ? t('Never') : t('!date (!ago ago)', array('!date' => format_date($status['last_backup'], 'small'), '!ago' => format_interval(time() - $status['last_backup'], 1))) + ); + } + if ($status['bytes_per_locker']) { + if (isset($status['bytes_used'])) { + $form['nodesquirrel_status']['space'] = array( + '#type' => 'item', + '#title' => t('Storage Space'), + '#markup' => t('!used used of !total (!remaining remaining)', array('!used' => backup_migrate_format_size($status['bytes_used']), '!total' => backup_migrate_format_size($status['bytes_per_locker']), '!remaining' => backup_migrate_format_size(max(0, $status['bytes_per_locker'] - $status['bytes_used'])))) + ); + } + else { + $form['nodesquirrel_status']['space'] = array( + '#type' => 'item', + '#title' => t('Total Storage Space'), + '#markup' => t('!total', array('!total' => backup_migrate_format_size($status['bytes_per_locker']))) + ); + } + } + $form['nodesquirrel_status']['manage'] = array( + '#type' => 'item', + '#title' => t('Management Console'), + '#markup' => backup_migrate_nodesquirrel_get_manage_link($destination), + '#description' => t('You can use the NodeSquirrel management console to add and edit your sites, reset your secret key, download and delete backups, and modify your NodeSquirrel account.'), + ); + + } + + return $form; +} + +function backup_migrate_nodesquirrel_schedule_settings_form($destination, $status) { + backup_migrate_include('sources', 'schedules', 'profiles'); + + // If the schedule has been overriden it must be edited in the schedule tab. + $schedule = backup_migrate_crud_get_item('schedule', 'nodesquirrel'); + + $default = 60*60*24; + + $form = array(); + $form['nodesquirrel_schedule'] = array( + '#type' => 'fieldset', + '#title' => t('Backup Schedule'), + '#description' => t('Set up a schedule to back up your site to NodeSquirrel. You can customize this schedule or add additional schedules in the !schedule.', array('!schedule' => l(t('Schedules tab'), BACKUP_MIGRATE_MENU_PATH . '/schedule'), '!cron' => l(t('cron'), 'http://drupal.org/cron'))), + ); + + $key = 'nodesquirrel_schedule'; + $form['nodesquirrel_schedule'][$key] = array(); + $defaults = array( + 'period' => empty($schedule) ? variable_get('nodesquirrel_schedule', 60*60*24) : $schedule->get('period'), + 'enabled' => empty($schedule) ? variable_get('nodesquirrel_schedule_enabled', TRUE) : $schedule->get('enabled'), + 'source_id' => empty($schedule) ? variable_get('nodesquirrel_schedule_source_id', 'db') : $schedule->get('source_id'), + 'keep' => empty($schedule) ? variable_get('nodesquirrel_schedule_keep', BACKUP_MIGRATE_SMART_DELETE) : $schedule->get('keep'), + ); + + $form['nodesquirrel_schedule'][$key]['nodesquirrel_schedule_enabled'] = array( + '#type' => 'checkbox', + '#title' => t('Automatically backup to NodeSquirrel'), + '#default_value' => $defaults['enabled'], + ); + $form['nodesquirrel_schedule'][$key]['settings'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'nodesquirrel_schedule_enabled' => TRUE, + ), + ); + $form['nodesquirrel_schedule'][$key]['settings']['nodesquirrel_schedule_source_id'] = _backup_migrate_get_source_pulldown($defaults['source_id']); + $options = array( + (60*60) => t('Once an hour'), + (60*60*24) => t('Once a day'), + (60*60*24*7) => t('Once a week'), + ); + $period = $defaults['period']; + if (!isset($options[$period])) { + $options[$period] = empty($schedule) ? t('Custom') : $schedule->get('frequency_description'); + } + $form['nodesquirrel_schedule'][$key]['settings']['nodesquirrel_schedule'] = array( + '#type' => 'select', + '#title' => t('Schedule Frequency'), + '#options' => $options, + '#default_value' => $period, + ); + $form['nodesquirrel_schedule'][$key]['settings']['nodesquirrel_schedule_keep'] = array( + '#type' => 'checkbox', + '#title' => t('Automatically delete old backups'), + '#return_value' => $defaults['keep'] == BACKUP_MIGRATE_KEEP_ALL ? BACKUP_MIGRATE_SMART_DELETE : $defaults['keep'], + '#default_value' => $defaults['keep'], + ); + return $form; +} + +/** + * NodeSquirrel settings form. + */ +function backup_migrate_nodesquirrel_settings_form($form_state) { + _backup_migrate_message_callback('_backup_migrate_message_browser'); + + $form = array(); + + $key = variable_get('nodesquirrel_secret_key', ''); + $status = array(); + if ($destination = backup_migrate_nodesquirrel_get_destination($key)) { + $status = $destination->check_limits(); + } + + $form += backup_migrate_nodesquirrel_info_form(); + $form += backup_migrate_nodesquirrel_status_form($key, $destination, $status); + $form += backup_migrate_nodesquirrel_credentials_settings_form($key, $status); + $form += backup_migrate_nodesquirrel_schedule_settings_form($destination, $status); + + return system_settings_form($form); +} + + +/** + * A destination for sending database backups to the NodeSquirel backup service. + * + * @ingroup backup_migrate_destinations + */ +class backup_migrate_destination_nodesquirrel extends backup_migrate_destination { + var $supported_ops = array('scheduled backup', 'manual backup', 'remote backup', 'restore', 'list files', 'configure', 'delete'); + var $cache_files = TRUE; + // Don't generate a metadata file as NodeSquirrel can save metadata natively. + var $save_metadata = FALSE; + + /** + * Get the destination name. Provide a default. + */ + function get_name() { + if (empty($this->name)) { + return t('NodeSquirrel'); + } + return $this->name; + } + + /** + * Get the menu items for manipulating this type. + */ + function get_menu_items() { + $items = array(); + $path = BACKUP_MIGRATE_MENU_PATH . '/nodesquirrel'; + $items[$path] = array( + 'title' => t('NodeSquirrel'), + 'page callback' => 'backup_migrate_menu_callback', + 'page arguments' => array('destinations.nodesquirrel', 'backup_migrate_nodesquirrel_settings'), + 'access arguments' => array('administer backup and migrate'), + 'weight' => 10, + 'type' => MENU_LOCAL_TASK, + ); + return $items; + } + + /** + * Declare any mysql databases defined in the settings.php file as a possible destination. + */ + function destinations() { + $out = array(); + $out['nodesquirrel'] = backup_migrate_create_destination('nodesquirrel', array('machine_name' => 'nodesquirrel')); + if ($secret_key = variable_get('nodesquirrel_secret_key', '')) { + $out['nodesquirrel']->settings['secret_key'] = $secret_key; + } + else { + $out['nodesquirrel']->supported_ops = array('manual backup', 'remote backup'); + } + return $out; + } + + /** + * Returns a form to be completed before saving to the destination can be complete. + */ + function before_backup_form($settings) { + $form = array(); + $key = $this->settings('secret_key'); + if (empty($key)) { + $form += backup_migrate_nodesquirrel_info_form(); + $form += backup_migrate_nodesquirrel_credentials_settings_form(); + } + return $form; + } + + /** + * Validate the before_backup_form form. + */ + function before_backup_form_validate($settings, $form, $form_state) { + if (isset($form_state['values']['nodesquirrel_secret_key'])) { + $key = trim($form_state['values']['nodesquirrel_secret_key']); + if ($error = $this->vaidate_key($key)) { + form_set_error('secret_key', $error); + } + } + } + + /** + * Submit the before_backup_form form. + */ + function before_backup_form_submit($settings, $form, $form_state) { + if (isset($form_state['values']['nodesquirrel_secret_key'])) { + $this->settings['secret_key'] = $form_state['values']['nodesquirrel_secret_key']; + variable_set('nodesquirrel_secret_key', $this->settings['secret_key']); + } + } + + + /** + * Save to the NodeSquirrel destination. + */ + function save_file($file, $settings) { + if ($destination = $this->_get_destination()) { + srand((double)microtime()*1000000); + + $filename = $file->filename(); + $filesize = filesize($file->filepath()); + $ticket = $this->_xmlrpc('backups.getUploadTicket', array($destination, $filename, $filesize, $file->file_info)); + + if ($ticket) { + + $url = $ticket['url']; + + // If the ticket requires authentication add our username/password to the url. + if (!empty($ticket['auth']) && $ticket['auth'] = 'basic') { + $parts = parse_url($ticket['url']); + list($parts['user'], $parts['pass']) = $this->get_user_pass(); + $url = $this->glue_url($parts, FALSE); + } + + $out = $this->_post_file($url, 'POST', $ticket['params'], $file); + + if ($out->code == 200) { + // Confirm the upload. + $confirm = $this->_xmlrpc('backups.confirmUpload', array($destination, $filename, $filesize)); + + if ($confirm['success']) { + // Set a message with a link to the manage console. + $url = variable_get('nodesquirrel_manage_url', 'http://manage.nodesquirrel.com') . '/backups/' . $this->_get_destination(); + _backup_migrate_message('Your backup has been saved to your NodeSquirrel account. View it at !account', array('!account' => l($url, $url))); + + return $file; + } + else { + _backup_migrate_message('The backup file never made it to the NodeSquirrel backup server. There may have been a network problem. Please try again later'); + } + } + else { + $error = !empty($out->headers['x-bams-error']) ? $out->headers['x-bams-error'] : $out->error; + _backup_migrate_message('The NodeSquirrel server returned the following error: %err', array('%err' => $error), 'error'); + } + } + else if ($err = xmlrpc_error()) { + // XMLRPC errors are already handled by the server function below. + } + else { + _backup_migrate_message('The NodeSquirrel server refused the backup but did not specify why. Maybe the server is down.'); + } + } + return NULL; + } + + /** + * Load from the NodeSquirrel destination. + */ + function load_file($file_id) { + if ($destination = $this->_get_destination()) { + backup_migrate_include('files'); + $file = new backup_file(array('filename' => $file_id)); + + $ticket = $this->_xmlrpc('backups.getDownloadTicket', array($destination, $file_id)); + + if ($ticket && $url = $ticket['url']) { + // If the ticket requires authentication add our username/password to the url. + if (!empty($ticket['auth']) && $ticket['auth'] = 'basic') { + $parts = parse_url($ticket['url']); + $parts['user'] = @$this->dest_url['user']; + $parts['pass'] = @$this->dest_url['pass']; + $url = $this->glue_url($parts, FALSE); + } + + $out = drupal_http_request($url); + + if ($out->code == 200) { + file_put_contents($file->filepath(), $out->data); + return $file; + } + else { + $error = !empty($out->headers['x-bams-error']) ? $out->headers['x-bams-error'] : $out->error; + _backup_migrate_message('The server returned the following error: %err', array('%err' => $error), 'error'); + } + } + } + return NULL; + } + + /** + * Delete from the NodeSquirrel destination. + */ + function delete_file($file_id) { + if ($destination = $this->_get_destination()) { + $result = $this->_xmlrpc('backups.deleteFile', array($destination, $file_id)); + } + } + + /** + * List the files in the remote destination. + */ + function _list_files() { + $files = array(); + backup_migrate_include('files'); + + if ($destination = $this->_get_destination()) { + $file_list = $this->_xmlrpc('backups.listFiles', array($destination)); + foreach ((array)$file_list as $file) { + $files[$file['filename']] = new backup_file($file); + } + } + + return $files; + } + + /** + * List the files in the remote destination. + */ + function check_limits() { + if (empty($this->limits)) { + $this->limits = $this->_xmlrpc('backups.getLimits', array($this->_get_destination())); + } + return $this->limits; + } + + /** + * Check that a destination is valid. + */ + function confirm_destination() { + return $this->check_limits(); + } + + /** + * Get the form for the settings for this destination. + */ + function edit_form() { + $form = parent::edit_form(); + + // If this is a new destination but the default NS destination has not been created yet, + // redirect to the NS config screen. + if (!$this->get_id() && !variable_get('nodesquirrel_secret_key', '')) { + drupal_goto(BACKUP_MIGRATE_MENU_PATH . '/nodesquirrel'); + } + + $form['settings'] = array('#tree' => TRUE); + $activate_link = backup_migrate_nodesquirrel_get_activate_link(); + + // Retrieve the key from the settings or get it from the get string if this is an auto-config action. + $key = $this->settings('secret_key'); + if (!empty($_GET['key']) && preg_match(NODESQUIRREL_SECRET_KEY_PATTERN, $_GET['key'])) { + $key = $_GET['key']; + } + + $form['settings']['secret_key'] = array( + '#type' => 'textfield', + '#title' => t('Secret Key'), + '#default_value' => $key, + ); + $form['settings']['location'] = array('#type' => 'value', '#value' => ''); + + $form['settings']['secret_key_help'] = array( + '#type' => 'item', + '#title' => t('Need a Secret Key?'), + '#markup' => t('Visit !nodesquirrel.', array('!nodesquirrel' => $activate_link)), + ); + + return $form; + } + + /** + * Submit the configuration form. Glue the url together and add the old password back if a new one was not specified. + */ + function edit_form_validate($form, &$form_state) { + $key = trim($form_state['values']['settings']['secret_key']); + if ($error = $this->vaidate_key($key)) { + form_set_error('secret_key', $error); + } + } + + /** + * Validate a secret key. Returns error text if the key is invalid. + */ + function vaidate_key($key) { + $error = FALSE; + if ($key) { + if (!preg_match(NODESQUIRREL_SECRET_KEY_PATTERN, $key)) { + return 'The secret key you entered is not the right format. Please make sure you copy it exactly.'; + } + $this->settings['secret_key'] = check_plain($key); + + $limits = $this->check_limits(); + + if (!$limits) { + $err = xmlrpc_error(); + if (!empty($err->code) && $err->code == '401') { + return 'Could not log in to the NodeSquirrel server. Please check that your secret key is correct.'; + } + else { + return 'Your site could not be found on NodeSquirrel. Please check that your secret key is correct.'; + } + } + } + else { + return 'You must enter a NodeSquirrel secret key.'; + } + return FALSE; + } + + /** + * Submit the configuration form. Glue the url together and add the old password back if a new one was not specified. + */ + function edit_form_submit($form, &$form_state) { + $form_state['values']['secret_key'] = check_plain($form_state['values']['settings']['secret_key']); + parent::edit_form_submit($form, $form_state); + } + + /** + * Get the destination id or warn the user that it has not been set. + */ + function _get_destination($warn = TRUE) { + list($id, $key) = $this->get_user_pass(); + return $id; + } + + /** + * Get the destination id or warn the user that it has not been set. + */ + function _get_private_key($warn = TRUE) { + list($id, $key) = $this->get_user_pass(); + return $key; + } + + /** + * Break the secret key into the public/private key (user/pass). + */ + function get_user_pass() { + $key = $this->settings('secret_key'); + // The username is the first 32 chars. + $user = substr($key, 0, 32); + // The pass is the last 32 chars. There may be a separating character. + $pass = substr($key, strlen($key) - 32); + return array($user, $pass); + } + + function get_display_location() { + return t('NodeSquirrel.com'); + } + + function add_scheme($url) { + return 'http://' . $url; + } + + /** + * Get the form for the settings for this destination. + */ + function _xmlrpc($method, $args = array()) { + // Retrieve the severs or read them from a stored variable. + $servers = $this->_get_endpoints(); + + // Do the actual call. + return $this->__xmlrpc($method, $args, $servers); + } + + /** + * Get the form for the settings for this destination. + */ + function __xmlrpc($method, $args, $servers, $retry = 3) { + if ($servers && --$retry > 0) { + // Add the key authentication arguments if we can. + if ($this->_sign_request($args)) { + $url = reset($servers); + // Try each available server in order. + while ($url) { + + $url = $this->add_scheme($url); + + $out = xmlrpc($url, array($method => $args)); + + // Check for errors. + $err = xmlrpc_error(); + if ($err && $err->is_error) { + switch ($err->code) { + case '500': + case '503': + case '404': + // Some sort of server error. Try the next one. + $url = next($servers); + + // If we're at the end of the line then try refetching the urls + if (!$url) { + $servers = $this->_get_endpoints(TRUE, $retry); + return $this->__xmlrpc($method, $args, $servers, $retry); + } + break; + case '300': + // 'Multiple Choices' means that the existing server list needs to be refreshed. + $servers = $this->_get_endpoints(TRUE, $retry); + return $this->__xmlrpc($method, $args, $servers, $retry); + break; + case '401': + case '403': + // Authentication failed. + _backup_migrate_message('Couldn\'t log in to NodeSquirrel. The server error was: %err', array('%err' => $err->message), 'error'); + return FALSE; + break; + default: + // Some sort of client error. Don't try the next server because it'll probably say the same thing. + _backup_migrate_message('The NodeSquirrel server returned the following error: %err', array('%err' => $err->message), 'error'); + return FALSE; + break; + } + } + // No error, return the result. + else { + return $out; + } + } + } + } + } + + /** + * Genrate a hash with a given secret key, timestamp and random value. + */ + function _get_hash($time, $nonce) { + if ($private_key = $this->_get_private_key()) { + $message = $time . ':' . $nonce . ':' . $private_key; + // Use HMAC-SHA1 to authenticate the call. + $hash = base64_encode( + pack('H*', sha1((str_pad($private_key, 64, chr(0x00)) ^ (str_repeat(chr(0x5c), 64))) . + pack('H*', sha1((str_pad($private_key, 64, chr(0x00)) ^ (str_repeat(chr(0x36), 64))) . + $message)))) + ); + return $hash; + } + _backup_migrate_message('You must enter a valid secret key to use NodeSquirrel.', array(), 'error'); + return FALSE; + } + + /** + * Genrate a hash with a given secret key, timestamp and random value. + */ + function _sign_request(&$args) { + $nonce = md5(mt_rand()); + $time = time(); + $hash = $this->_get_hash($time, $nonce); + if ($hash) { + array_unshift($args, $nonce); + array_unshift($args, $time); + array_unshift($args, $hash); + return TRUE; + } + else { + return FALSE; + } + } + + /** + * Retrieve the list of servers. + */ + function _get_endpoints($refresh = FALSE, $retry = 3) { + $servers = (array)variable_get('nodesquirrel_endpoint_urls', array()); + + // No servers saved or a force refreshr required. + if ($refresh || empty($servers)) { + $servers = array_unique(array_merge($servers, variable_get('nodesquirrel_default_endpoint_urls', array('api.nodesquirrel.com/services/xmlrpc')))); + // Call the get endpoints method but use the default or previous servers to avoid infinite loops. + $new_servers = $this->__xmlrpc('backups.getEndpoints', array($this->_get_destination(), 'xmlrpc'), $servers, $retry); + if ($new_servers) { + variable_set('nodesquirrel_endpoint_urls', $new_servers); + $servers = $new_servers; + } + } + return $servers; + } + + /** + * Post a file via http. + * + * This looks a lot like a clone of drupal_http_request but it can post a large + * file without reading the whole file into memory. + */ + function _post_file($url, $method = 'GET', $params = array(), $file = NULL, $retry = 3) { + global $db_prefix; + + $result = new stdClass(); + + // Parse the URL and make sure we can handle the schema. + $uri = parse_url($url); + + if ($uri == FALSE) { + $result->error = 'unable to parse URL'; + $result->code = -1001; + return $result; + } + + if (!isset($uri['scheme'])) { + $result->error = 'missing schema'; + $result->code = -1002; + return $result; + } + + switch ($uri['scheme']) { + case 'http': + case 'feed': + $port = isset($uri['port']) ? $uri['port'] : 80; + $host = $uri['host'] . ($port != 80 ? ':'. $port : ''); + $fp = @fsockopen($uri['host'], $port, $errno, $errstr, 15); + break; + case 'https': + // Note: Only works for PHP 4.3 compiled with OpenSSL. + $port = isset($uri['port']) ? $uri['port'] : 443; + $host = $uri['host'] . ($port != 443 ? ':'. $port : ''); + $fp = @fsockopen('ssl://'. $uri['host'], $port, $errno, $errstr, 20); + break; + default: + $result->error = 'invalid schema '. $uri['scheme']; + $result->code = -1003; + return $result; + } + + // Make sure the socket opened properly. + if (!$fp) { + // When a network error occurs, we use a negative number so it does not + // clash with the HTTP status codes. + $result->code = -$errno; + $result->error = trim($errstr); + + // Mark that this request failed. This will trigger a check of the web + // server's ability to make outgoing HTTP requests the next time that + // requirements checking is performed. + // @see system_requirements() + variable_set('drupal_http_request_fails', TRUE); + + return $result; + } + + // Construct the path to act on. + $path = isset($uri['path']) ? $uri['path'] : '/'; + if (isset($uri['query'])) { + $path .= '?'. $uri['query']; + } + + // Prepare the data payload. + $boundary = '---------------------------'. substr(md5(rand(0,32000)),0,10); + $data_footer = "\r\n--$boundary--\r\n"; + + $data_header = ''; + foreach ($params as $key => $value) { + $data_header .="--$boundary\r\n"; + $data_header .= "Content-Disposition: form-data; name=\"".$key."\"\r\n"; + $data_header .= "\r\n".$value."\r\n"; + $data_header .="--$boundary\r\n"; + } + + // Add the file header to the post payload. + $data_header .="--$boundary\r\n"; + $data_header .= "Content-Disposition: form-data; name=\"file\"; filename=\"". $file->filename() ."\"\r\n"; + $data_header .= "Content-Type: application/octet-stream;\r\n"; + $data_header .= "\r\n"; + + // Calculate the content length. + $content_length = strlen($data_header . $data_footer) + filesize($file->filepath()); + + //file_get_contents($file->filepath())); + + // Create HTTP request. + $defaults = array( + // RFC 2616: "non-standard ports MUST, default ports MAY be included". + // We don't add the port to prevent from breaking rewrite rules checking the + // host that do not take into account the port number. + 'Host' => "Host: $host", + 'Content-type' => "Content-type: multipart/form-data, boundary=$boundary", + 'User-Agent' => 'User-Agent: NodeSquirrel Client/1.x (+http://www.nodesquirrel.com) (Drupal '. VERSION .'; Backup and Migrate 2.x)', + 'Content-Length' => 'Content-Length: '. $content_length + ); + + // If the server url has a user then attempt to use basic authentication + if (isset($uri['user'])) { + $defaults['Authorization'] = 'Authorization: Basic '. base64_encode($uri['user'] . (!empty($uri['pass']) ? ":". $uri['pass'] : '')); + } + + $request = $method .' '. $path ." HTTP/1.0\r\n"; + $request .= implode("\r\n", $defaults); + $request .= "\r\n\r\n"; + $result->request = $request; + + // Write the headers and start of the headers + fwrite($fp, $request); + fwrite($fp, $data_header); + + // Copy the file 512k at a time to prevent memory issues. + if ($fp_in = fopen($file->filepath(), 'rb')) { + while (!feof($fp_in)) { + fwrite($fp, fread($fp_in, 1024 * 512)); + } + $success = TRUE; + } + @fclose($fp_in); + + // Finish the write. + fwrite($fp, $data_footer); + + // Fetch response. + $response = ''; + while (!feof($fp) && $chunk = fread($fp, 1024)) { + $response .= $chunk; + } + fclose($fp); + + if (variable_get('debug_http_request', FALSE)) { + drupal_debug(date('r')); + drupal_debug($request); + drupal_debug($response); + } + + // Parse response. + list($split, $result->data) = explode("\r\n\r\n", $response, 2); + $split = preg_split("/\r\n|\n|\r/", $split); + + list($protocol, $code, $status_message) = explode(' ', trim(array_shift($split)), 3); + $result->protocol = $protocol; + $result->status_message = $status_message; + + $result->headers = array(); + + // Parse headers. + while ($line = trim(array_shift($split))) { + list($header, $value) = explode(':', $line, 2); + if (isset($result->headers[$header]) && $header == 'Set-Cookie') { + // RFC 2109: the Set-Cookie response header comprises the token Set- + // Cookie:, followed by a comma-separated list of one or more cookies. + $result->headers[$header] .= ','. trim($value); + } + else { + $result->headers[$header] = trim($value); + } + } + + $responses = array( + 100 => 'Continue', 101 => 'Switching Protocols', + 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', + 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 307 => 'Temporary Redirect', + 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed', + 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported' + ); + // RFC 2616 states that all unknown HTTP codes must be treated the same as the + // base code in their class. + if (!isset($responses[$code])) { + $code = floor($code / 100) * 100; + } + + switch ($code) { + case 200: // OK + case 304: // Not modified + break; + case 301: // Moved permanently + case 302: // Moved temporarily + case 307: // Moved temporarily + $location = $result->headers['Location']; + + if ($retry) { + $result = drupal_http_request($result->headers['Location'], $headers, $method, $data, --$retry); + $result->redirect_code = $result->code; + } + $result->redirect_url = $location; + + break; + default: + $result->error = $status_message; + } + + $result->code = $code; + return $result; + } + +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/destinations.s3.inc b/sites/all/modules/contrib/backup_migrate/includes/destinations.s3.inc new file mode 100644 index 0000000..c049806 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/destinations.s3.inc @@ -0,0 +1,184 @@ +s3_object()) { + $path = $file->filename(); + if ($s3->putObject($s3->inputFile($file->filepath(), FALSE), $this->get_bucket(), $this->remote_path($file->filename()), S3::ACL_PRIVATE)) { + return $file; + } + } + return FALSE; + } + + /** + * Load from the s3 destination. + */ + function load_file($file_id) { + backup_migrate_include('files'); + $file = new backup_file(array('filename' => $file_id)); + if ($s3 = $this->s3_object()) { + $data = $s3->getObject($this->get_bucket(), $this->remote_path($file_id), $file->filepath()); + if (!$data->error) { + return $file; + } + } + return NULL; + } + + /** + * Delete from the s3 destination. + */ + function _delete_file($file_id) { + if ($s3 = $this->s3_object()) { + $s3->deleteObject($this->get_bucket(), $this->remote_path($file_id)); + } + } + + /** + * List all files from the s3 destination. + */ + function _list_files() { + backup_migrate_include('files'); + $files = array(); + if ($s3 = $this->s3_object()) { + $s3_files = $s3->getBucket($this->get_bucket(), $this->get_subdir()); + foreach ((array)$s3_files as $id => $file) { + $info = array( + 'filename' => $this->local_path($file['name']), + 'filesize' => $file['size'], + 'filetime' => $file['time'], + ); + $files[$info['filename']] = new backup_file($info); + } + } + return $files; + } + + /** + * Get the form for the settings for this filter. + */ + function edit_form() { + // Check for the library. + $this->s3_object(); + + $form = parent::edit_form(); + $form['scheme']['#type'] = 'value'; + $form['scheme']['#value'] = 'https'; + $form['host']['#type'] = 'value'; + $form['host']['#value'] = 's3.amazonaws.com'; + + $form['path']['#title'] = 'S3 Bucket'; + $form['path']['#default_value'] = $this->get_bucket(); + $form['path']['#description'] = 'This bucket must already exist. It will not be created for you.'; + + $form['user']['#title'] = 'Access Key ID'; + $form['pass']['#title'] = 'Secret Access Key'; + + $form['subdir'] = array( + '#type' => 'textfield', + '#title' => t('Subdirectory'), + '#default_value' => $this->get_subdir(), + '#weight' => 25 + ); + $form['settings']['#weight'] = 50; + + return $form; + } + + /** + * Submit the form for the settings for the s3 destination. + */ + function edit_form_submit($form, &$form_state) { + // Append the subdir onto the path. + + if (!empty($form_state['values']['subdir'])) { + $form_state['values']['path'] .= '/'. trim($form_state['values']['subdir'], '/'); + } + parent::edit_form_submit($form, $form_state); + } + + /** + * Generate a filepath with the correct prefix. + */ + function remote_path($path) { + if ($subdir = $this->get_subdir()) { + $path = $subdir .'/'. $path; + } + return $path; + } + + /** + * Generate a filepath with the correct prefix. + */ + function local_path($path) { + if ($subdir = $this->get_subdir()) { + $path = str_replace($subdir .'/', '', $path); + } + return $path; + } + + /** + * Get the bucket which is the first part of the path. + */ + function get_bucket() { + $parts = explode('/', @$this->dest_url['path']); + return $parts[0]; + } + + /** + * Get the bucket which is the first part of the path. + */ + function get_subdir() { + // Support the older style of subdir saving. + if ($subdir = $this->settings('subdir')) { + return $subdir; + } + $parts = explode('/', @$this->dest_url['path']); + array_shift($parts); + return implode('/', array_filter($parts)); + } + + function s3_object() { + // Try to use libraries module if available to find the path. + if (function_exists('libraries_get_path')) { + $library_paths[] = libraries_get_path('s3-php5-curl'); + } + else { + $library_paths[] = 'sites/all/libraries/s3-php5-curl'; + } + $library_paths[] = drupal_get_path('module', 'backup_migrate') . '/includes/s3-php5-curl'; + $library_paths[] = drupal_get_path('module', 'backup_migrate') . '/includes'; + + foreach($library_paths as $path) { + if (file_exists($path . '/S3.php')) { + require_once $path . '/S3.php'; + if (!$this->s3 && !empty($this->dest_url['user'])) { + $this->s3 = new S3($this->dest_url['user'], $this->dest_url['pass']); + } + return $this->s3; + } + } + drupal_set_message(t('Due to drupal.org code hosting policies, the S3 library needed to use an S3 destination is no longer distributed with this module. You must download the library from !link and place it in one of these locations: %locations.', array('%locations' => implode(', ', $library_paths), '!link' => l('http://undesigned.org.za/2007/10/22/amazon-s3-php-class', 'http://undesigned.org.za/2007/10/22/amazon-s3-php-class'))), 'error', FALSE); + return NULL; + } +} diff --git a/sites/all/modules/contrib/backup_migrate/includes/files.inc b/sites/all/modules/contrib/backup_migrate/includes/files.inc new file mode 100644 index 0000000..8931189 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/files.inc @@ -0,0 +1,564 @@ + $tmp), 'error'); + } + + // Use a full path so that the files can be deleted during the shutdown function if needed. + $file = $tmp .'/'. uniqid('backup_migrate_'); + mkdir($file); + backup_migrate_temp_files_add($file); + return $file; +} + +/** + * Return a list of backup filetypes. + */ +function _backup_migrate_filetypes() { + backup_migrate_include('filters'); + + $out = backup_migrate_filters_file_types(); + + foreach ($out as $key => $info) { + $out[$key]['id'] = empty($info['id']) ? $key : $info['id']; + } + return $out; +} + +/** + * Adjust the length of a filename to allow for a string to be appended, + * staying within the maximum filename limit. + */ +function _backup_migrate_filename_append_prepare($filename, $append_str) { + $max_name_len = BACKUP_MIGRATE_FILENAME_MAXLENGTH - drupal_strlen($append_str); + if (drupal_strlen($filename) > $max_name_len) { + $filename = drupal_substr($filename, 0, $max_name_len); + } + return $filename; +} + +/** + * Construct a filename using token and some cleaning. + */ +function _backup_migrate_construct_filename($settings) { + // Get the raw filename from the settings. + $filename = $settings->filename; + + // Replace any tokens + if (module_exists('token') && function_exists('token_replace')) { + $filename = token_replace($filename); + } + + // Remove illegal characters + $filename = _backup_migrate_clean_filename($filename); + + // Generate a timestamp if needed. + $timestamp = ''; + if ($settings->append_timestamp && $settings->timestamp_format) { + $timestamp = format_date(time(), 'custom', $settings->timestamp_format); + } + + // Trim to length if needed to allow the timestamp to fit into the max filename. + $filename = _backup_migrate_filename_append_prepare($filename, $timestamp); + $filename .= '-' . $timestamp; + $filename = trim($filename, '-'); + + // If there is no filename, then just call it untitled. + if (drupal_strlen($filename) == 0) { + $filename = 'untitled'; + } + return $filename; +} + +/** + * Construct a default filename using the site's name. + */ +function _backup_migrate_default_filename() { + if (module_exists('token')) { + return '[site:name]'; + } + else { + // Cleaning the string isn't strictly necessary but it looks better in the settings field. + return _backup_migrate_clean_filename(variable_get('site_name', "backup_migrate")); + } +} + +/** + * Clean up a filename to remove unsafe characters. + */ +function _backup_migrate_clean_filename($filename) { + $filename = preg_replace("/[^a-zA-Z0-9\.\-_]/", "", $filename); + return $filename; +} + +/** + * An output buffer callback which simply throws away the buffer instead of sending it to the browser. + */ +function _backup_migrate_file_dispose_buffer($buffer) { + return ""; +} + + +/** + * A backup file which allows for saving to and reading from the server. + */ +class backup_file { + var $file_info = array(); + var $type = array(); + var $ext = array(); + var $path = ""; + var $name = ""; + var $handle = NULL; + + /** + * Construct a file object given a file path, or create a temp file for writing. + */ + function backup_file($params = array()) { + if (isset($params['filepath']) && file_exists($params['filepath'])) { + $this->set_filepath($params['filepath']); + } + else { + $this->set_file_info($params); + $this->temporary_file(); + } + } + + /** + * Get the file_id if the file has been saved to a destination. + */ + function file_id() { + // The default file_id is the filename. Destinations can override the file_id if needed. + return isset($this->file_info['file_id']) ? $this->file_info['file_id'] : $this->filename(); + } + + /** + * Get the current filepath. + */ + function filepath() { + return drupal_realpath($this->path); + } + + /** + * Get the final filename. + */ + function filename($name = NULL) { + if ($name) { + $this->name = $name; + } + $extension_str = '.' . $this->extension(); + $this->name = _backup_migrate_filename_append_prepare($this->name, $extension_str); + return $this->name . $extension_str; + } + + /** + * Set the current filepath. + */ + function set_filepath($path) { + $this->path = $path; + $params = array( + 'filename' => basename($path), + 'file_id' => basename($path) + ); + if (file_exists($path)) { + $params['filesize'] = filesize($path); + $params['filetime'] = filectime($path); + } + $this->set_file_info($params); + } + + /** + * Get one or all pieces of info for the file. + */ + function info($key = NULL) { + if ($key) { + return @$this->file_info[$key]; + } + return $this->file_info; + } + + + /** + * Get one or all pieces of info for the file. + */ + function info_set($key, $value) { + $this->file_info[$key] = $value; + } + /** + * Get the file extension. + */ + function extension() { + return implode(".", $this->ext); + } + + /** + * Get the file type. + */ + function type() { + return $this->type; + } + + /** + * Get the file mimetype. + */ + function mimetype() { + return @$this->type['filemime'] ? $this->type['filemime'] : 'application/octet-stream'; + } + + /** + * Get the file mimetype. + */ + function type_id() { + return @$this->type['id']; + } + + function filesize() { + if (empty($this->file_info['filesize'])) { + $this->calculate_filesize(); + } + return $this->file_info['filesize']; + } + + function calculate_filesize() { + $this->file_info['filesize'] = ''; + if (!empty($this->path) && file_exists($this->path)) { + $this->file_info['filesize'] = filesize($this->path); + } + } + + /** + * Can this file be used to backup to. + */ + function can_backup() { + return @$this->type['backup']; + } + + /** + * Can this file be used to restore to. + */ + function can_restore() { + return @$this->type['restore']; + } + + /** + * Can this file be used to restore to. + */ + function is_recognized_type() { + return @$this->type['restore'] || @$this->type['backup']; + } + + /** + * Open a file for reading or writing. + */ + function open($write = FALSE, $binary = FALSE) { + if (!$this->handle) { + $path = $this->filepath(); + + // Check if the file can be read/written. + if ($write && ((file_exists($path) && !is_writable($path)) || !is_writable(dirname($path)))) { + _backup_migrate_message('The file %path cannot be written to.', array('%path' => $path), 'error'); + return FALSE; + } + if (!$write && !is_readable($path)) { + _backup_migrate_message('The file %path cannot be read.', array('%path' => $path), 'error'); + return FALSE; + } + + // Open the file. + $mode = ($write ? "w" : "r") . ($binary ? "b" : ""); + $this->handle = fopen($path, $mode); + return $this->handle; + } + return NULL; + } + + /** + * Close a file when we're done reading/writing. + */ + function close() { + fclose($this->handle); + $this->handle = NULL; + } + + /** + * Write a line to the file. + */ + function write($data) { + if (!$this->handle) { + $this->handle = $this->open(TRUE); + } + if ($this->handle) { + fwrite($this->handle, $data); + } + } + + /** + * Read a line from the file. + */ + function read($size = NULL) { + if (!$this->handle) { + $this->handle = $this->open(); + } + if ($this->handle && !feof($this->handle)) { + return $size ? fread($this->handle, $size) : fgets($this->handle); + } + return NULL; + } + + /** + * Write data to the file. + */ + function put_contents($data) { + file_put_contents($this->filepath(), $data); + } + + /** + * Read data from the file. + */ + function get_contents() { + return file_get_contents($this->filepath()); + } + + /** + * Transfer file using http to client. Similar to the built in file_transfer, + * but it calls module_invoke_all('exit') so that temp files can be deleted. + */ + function transfer() { + $headers = array( + array('key' => 'Content-Type', 'value' => $this->mimetype()), + array('key' => 'Content-Disposition', 'value' => 'attachment; filename="'. $this->filename() .'"'), + ); + // In some circumstances, web-servers will double compress gzipped files. + // This may help aleviate that issue by disabling mod-deflate. + if ($this->mimetype() == 'application/x-gzip') { + $headers[] = array('key' => 'Content-Encoding', 'value' => 'gzip'); + } + if ($size = $this->info('filesize')) { + $headers[] = array('key' => 'Content-Length', 'value' => $size); + } + + // Suppress the warning you get when the buffer is empty. + @ob_end_clean(); + + if ($this->open(FALSE, TRUE)) { + foreach ($headers as $header) { + // To prevent HTTP header injection, we delete new lines that are + // not followed by a space or a tab. + // See http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2 + $header['value'] = preg_replace('/\r?\n(?!\t| )/', '', $header['value']); + drupal_add_http_header($header['key'], $header['value']); + } + // Transfer file in 1024 byte chunks to save memory usage. + while ($data = $this->read(1024)) { + print $data; + } + $this->close(); + + // Ask devel.module not to print it's footer. + $GLOBALS['devel_shutdown'] = FALSE; + } + else { + drupal_not_found(); + } + + // Start buffering and throw away the results so that errors don't get appended to the file. + ob_start('_backup_migrate_file_dispose_buffer'); + backup_migrate_cleanup(); + module_invoke_all('exit'); + exit(); + } + + /** + * Push a file extension onto the file and return the previous file path. + */ + function push_type($extension) { + $types = _backup_migrate_filetypes(); + if ($type = @$types[$extension]) { + $this->push_filetype($type); + } + + $out = $this->filepath(); + $this->temporary_file(); + return $out; + } + + /** + * Push a file extension onto the file and return the previous file path. + */ + function pop_type() { + $out = new backup_file(array('filepath' => $this->filepath())); + $this->pop_filetype(); + $this->temporary_file(); + return $out; + } + + /** + * Set the current file type. + */ + function set_filetype($type) { + $this->type = $type; + $this->ext = array($type['extension']); + } + + /** + * Set the current file type. + */ + function push_filetype($type) { + $this->ext[] = $type['extension']; + $this->type = $type; + } + + /** + * Pop the current file type. + */ + function pop_filetype() { + array_pop($this->ext); + $this->detect_filetype_from_extension(); + } + + /** + * Set the file info. + */ + function set_file_info($file_info) { + $this->file_info = $file_info; + + $this->ext = explode('.', @$this->file_info['filename']); + // Remove the underscores added to file extensions by Drupal's upload security. + foreach ($this->ext as $key => $val) { + $this->ext[$key] = trim($val, '_'); + } + $this->filename(array_shift($this->ext)); + $this->detect_filetype_from_extension(); + } + + /** + * Get the filetype info of the given file, or false if the file is not a valid type. + */ + function detect_filetype_from_extension() { + $ext = end($this->ext); + $this->type = array(); + $types = _backup_migrate_filetypes(); + foreach ($types as $key => $type) { + if (trim($ext, "_0123456789") === $type['extension']) { + $this->type = $type; + $this->type['id'] = $key; + } + } + } + + /** + * Get a temporary file name with path. + */ + function temporary_file() { + $file = drupal_tempnam('temporary://', 'backup_migrate_'); + // Add the version without the extension. The tempnam function creates this for us. + backup_migrate_temp_files_add($file); + + if ($this->extension()) { + $file .= '.'. $this->extension(); + // Add the version with the extension. This is the one we will actually use. + backup_migrate_temp_files_add($file); + } + $this->path = $file; + } +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/filters.backup_restore.inc b/sites/all/modules/contrib/backup_migrate/includes/filters.backup_restore.inc new file mode 100644 index 0000000..cf80b59 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/filters.backup_restore.inc @@ -0,0 +1,249 @@ + 0, 'restore' => 0); + + /** + * Get the default destinations for this filter. + */ + function destinations() { + $out = array(); + foreach ($this->_get_destination_types() as $destination) { + if (method_exists($destination, 'destinations')) { + $out += $destination->destinations(); + } + } + return $out; + } + + /** + * Get the default sources for this filter. + */ + function sources() { + $out = array(); + foreach ($this->_get_source_types() as $type) { + if (method_exists($type, 'sources')) { + $out += $type->sources(); + } + } + return $out; + } + + /** + * Get the default backup settings for this filter. + */ + function backup_settings_default() { + backup_migrate_include('sources'); + $out = array(); + foreach (backup_migrate_get_sources() as $source) { + $out['sources'][$source->get_id()] = $source->backup_settings_default(); + } + return $out; + } + + /** + * Get the form for the settings for this filter. + */ + function backup_settings_form_validate($form, &$form_state) { + foreach ($this->_get_destination_types() as $destination) { + $destination->backup_settings_form_validate($form, $form_state); + } + } + + /** + * Submit the settings form. Any values returned will be saved. + */ + function backup_settings_form_submit($form, &$form_state) { + foreach ($this->_get_destination_types() as $destination) { + $destination->backup_settings_form_submit($form, $form_state); + } + } + + /** + * Get the default restore settings for this filter. + */ + function restore_settings_default() { + $out = array(); + foreach ($this->_get_destination_types() as $destination) { + $out += $destination->restore_settings_default(); + } + return $out; + } + + /** + * Get the form for the backup settings for this filter. + */ + function backup_settings_form($settings) { + backup_migrate_include('sources'); + $out = array('sources' => array( + '#tree' => TRUE, + )); + foreach (backup_migrate_get_sources() as $source) { + $source_settings = (array)(@$settings['sources'][$source->get_id()]) + $settings; + if ($form = $source->backup_settings_form($source_settings)) { + $out['sources'][$source->get_id()] = array( + '#type' => 'fieldset', + '#title' => t('!name Backup Options', array('!name' => $source->get('name'))), + "#collapsible" => TRUE, + "#collapsed" => TRUE, + '#tree' => TRUE, + '#parents' => array('filters', 'sources', $source->get_id()), + ) + $form; + } + } + return $out; + } + + /** + * Get the form for the restore settings for this filter. + */ + function restore_settings_form($settings) { + $form = array(); + foreach ($this->_get_destination_types() as $destination) { + $destination->restore_settings_form($form, $settings); + } + return $form; + } + + /** + * Get the before-backup form for the active sources and destinations. + */ + function before_action_form($op, $settings) { + $form = array(); + $method = 'before_' . $op . '_form'; + if ($source = $settings->get_source()) { + if (method_exists($source, $method)) { + $form += $source->{$method}($settings); + } + } + foreach ($settings->get_destinations() as $destination) { + if (method_exists($destination, $method)) { + $form += $destination->{$method}($settings); + } + } + return $form; + } + + /** + * Get the before-backup form for the active sources and destinations. + */ + function before_action_form_validate($op, $settings, $form, $form_state) { + $method = 'before_' . $op . '_form_validate'; + foreach ($settings->get_all_locations() as $location) { + if (method_exists($location, $method)) { + $location->{$method}($settings, $form, $form_state); + } + } + } + + /** + * Get the before-backup form for the active sources and destinations. + */ + function before_action_form_submit($op, $settings, $form, $form_state) { + $method = 'before_' . $op . '_form_submit'; + foreach ($settings->get_all_locations() as $location) { + if (method_exists($location, $method)) { + $location->{$method}($settings, $form, $form_state); + } + } + } + + /** + * Get the file types supported by this destination. + */ + function file_types() { + $types = array(); + foreach ($this->_get_destination_types() as $destination) { + $types += $destination->file_types(); + } + foreach ($this->_get_source_types() as $source) { + $types += $source->file_types(); + } + return $types; + } + + /** + * Backup the data from the source specified in the settings. + */ + function backup($file, $settings) { + if ($source = $settings->get_source()) { + if (!empty($settings->filters['sources'][$source->get_id()])) { + $settings->filters = (array)($settings->filters['sources'][$source->get_id()]) + $settings->filters; + } + + $file = $source->backup_to_file($file, $settings); + return $file; + } + backup_migrate_backup_fail("Could not run backup because the source '%source' is missing.", array('%source' => $settings->source_id), $settings); + return FALSE; + } + + /** + * Restore the data from to source specified in the settings. + */ + function restore($file, $settings) { + if ($source = $settings->get_source()) { + if (!empty($settings->filters['sources'][$source->get_id()])) { + $settings->filters = (array)($settings->filters['sources'][$source->get_id()]) + $settings->filters; + } + $num = $source->restore_from_file($file, $settings); + return $num ? $file : FALSE; + } + backup_migrate_restore_fail("Could not run restore because the source '%source' is missing.", array('%source' => $settings->source_id), $settings); + return FALSE; + } + + /** + * Get a list of dummy destinations representing each of the available destination types. + */ + function _get_destination_types() { + backup_migrate_include('destinations'); + static $destinations = NULL; + if (!is_array($destinations)) { + $destinations = array(); + $types = backup_migrate_get_destination_subtypes(); + // If no (valid) node type has been provided, display a node type overview. + foreach ($types as $key => $type) { + // Include the necessary file if specified by the type. + if (!empty($type['file'])) { + require_once './'. $type['file']; + } + $destinations[] = new $type['class'](array()); + } + } + return $destinations; + } + + /** + * Get a list of dummy destinations representing each of the available source types. + */ + function _get_source_types() { + backup_migrate_include('sources'); + static $sources = NULL; + if (!is_array($sources)) { + $sources = array(); + $types = backup_migrate_get_source_subtypes(); + // If no (valid) node type has been provided, display a node type overview. + foreach ($types as $key => $type) { + // Include the necessary file if specified by the type. + if (!empty($type['file'])) { + require_once './'. $type['file']; + } + $sources[] = new $type['class'](array()); + } + } + return $sources; + } + +} diff --git a/sites/all/modules/contrib/backup_migrate/includes/filters.compression.inc b/sites/all/modules/contrib/backup_migrate/includes/filters.compression.inc new file mode 100644 index 0000000..e399d29 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/filters.compression.inc @@ -0,0 +1,298 @@ + 100, 'restore' => -100); + + /** + * This function is called on a backup file after the backup has been completed. + */ + function backup($file, $settings) { + return $this->_backup_migrate_file_compress($file, $settings); + } + + /** + * This function is called on a backup file before importing it. + */ + function restore($file, $settings) { + return $this->_backup_migrate_file_decompress($file, $settings); + } + + /** + * Get the form for the settings for this filter. + */ + function backup_settings_default() { + $options = $this->_backup_migrate_get_compression_form_item_options(); + return array('compression' => isset($options['gzip']) ? 'gzip' : 'none'); + } + + /** + * Get the form for the settings for this filter. + */ + function backup_settings_form($settings) { + $form = array(); + $compression_options = $this->_backup_migrate_get_compression_form_item_options(); + $form['file']['compression'] = array( + "#type" => count($compression_options) > 1 ? "select" : 'value', + "#title" => t("Compression"), + "#options" => $compression_options, + "#default_value" => $settings['compression'], + ); + return $form; + } + + /** + * Return a list of backup filetypes. + */ + function file_types() { + return array( + "gzip" => array( + "extension" => "gz", + "filemime" => "application/x-gzip", + "backup" => TRUE, + "restore" => TRUE, + ), + "bzip" => array( + "extension" => "bz", + "filemime" => "application/x-bzip", + "backup" => TRUE, + "restore" => TRUE, + ), + "bzip2" => array( + "extension" => "bz2", + "filemime" => "application/x-bzip", + "backup" => TRUE, + "restore" => TRUE, + ), + "zip" => array( + "extension" => "zip", + "filemime" => "application/zip", + "backup" => TRUE, + "restore" => TRUE, + ), + ); + } + + /** + * Get the compression options as an options array for a form item. + */ + function _backup_migrate_get_compression_form_item_options() { + $compression_options = array("none" => t("No Compression")); + if (@function_exists("gzencode")) { + $compression_options['gzip'] = t("GZip"); + } + if (@function_exists("bzcompress")) { + $compression_options['bzip'] = t("BZip"); + } + if (class_exists('ZipArchive')) { + $compression_options['zip'] = t("Zip", array(), array('context' => 'compression format')); + } + return $compression_options; + } + + /** + * Gzip encode a file. + */ + function _backup_migrate_gzip_encode($source, $dest, $level = 9, $settings) { + $success = FALSE; + // Try command line gzip first. + + if (!empty($settings->filters['use_cli'])) { + $success = backup_migrate_exec("gzip -c -$level %input > %dest", array('%input' => $source, '%dest' => $dest, '%level' => $level)); + } + if (!$success && @function_exists("gzopen")) { + if (($fp_out = gzopen($dest, 'wb'. $level)) && ($fp_in = fopen($source, 'rb'))) { + while (!feof($fp_in)) { + gzwrite($fp_out, fread($fp_in, 1024 * 512)); + } + $success = TRUE; + } + @fclose($fp_in); + @gzclose($fp_out); + } + return $success; + } + + /** + * Gzip decode a file. + */ + function _backup_migrate_gzip_decode($source, $dest, $settings) { + $success = FALSE; + + if (!empty($settings->filters['use_cli'])) { + $success = backup_migrate_exec("gzip -d -c %input > %dest", array('%input' => $source, '%dest' => $dest)); + } + + if (!$success && @function_exists("gzopen")) { + if (($fp_out = fopen($dest, 'wb')) && ($fp_in = gzopen($source, 'rb'))) { + while (!feof($fp_in)) { + fwrite($fp_out, gzread($fp_in, 1024 * 512)); + } + $success = TRUE; + } + @gzclose($fp_in); + @fclose($fp_out); + } + return $success; + } + + /** + * Bzip encode a file. + */ + function _backup_migrate_bzip_encode($source, $dest) { + $success = FALSE; + if (@function_exists("bzopen")) { + if (($fp_out = bzopen($dest, 'w')) && ($fp_in = fopen($source, 'rb'))) { + while (!feof($fp_in)) { + bzwrite($fp_out, fread($fp_in, 1024 * 512)); + } + $success = TRUE; + } + else { + $error = TRUE; + } + @fclose($fp_in); + @bzclose($fp_out); + } + return $success; + } + + /** + * Bzip decode a file. + */ + function _backup_migrate_bzip_decode($source, $dest) { + $success = FALSE; + if (@function_exists("bzopen")) { + if (($fp_out = fopen($dest, 'w')) && ($fp_in = bzopen($source, 'r'))) { + while (!feof($fp_in)) { + fwrite($fp_out, gzread($fp_in, 1024 * 512)); + } + $success = TRUE; + } + else { + $error = TRUE; + } + @bzclose($fp_in); + @fclose($fp_out); + } + return $success; + } + + /** + * Zip encode a file. + */ + function _backup_migrate_zip_encode($source, $dest, $filename) { + $success = FALSE; + if (class_exists('ZipArchive')) { + $zip = new ZipArchive; + $res = $zip->open($dest, constant("ZipArchive::CREATE")); + if ($res === TRUE) { + $zip->addFile($source, $filename); + $success = $zip->close(); + } + } + return $success; + } + + /** + * Zip decode a file. + */ + function _backup_migrate_zip_decode($source, $dest) { + $success = FALSE; + if (class_exists('ZipArchive')) { + $zip = new ZipArchive; + if (($fp_out = fopen($dest, "w")) && ($zip->open($source))) { + $filename = ($zip->getNameIndex(0)); + if ($fp_in = $zip->getStream($filename)) { + while (!feof($fp_in)) { + fwrite($fp_out, fread($fp_in, 1024 * 512)); + } + $success = TRUE; + } + } + @fclose($fp_in); + @fclose($fp_out); + } + return $success; + } + + /** + * Compress a file with the given settings. + * Also updates settings to reflect new file mime and file extension. + */ + function _backup_migrate_file_compress($file, $settings) { + switch ($settings->filters['compression']) { + case "gzip": + $from = $file->push_type('gzip'); + if (!$success = $this->_backup_migrate_gzip_encode($from, $file->filepath(), 9, $settings)) { + $file = NULL; + } + break; + + case "bzip": + $from = $file->push_type('bzip2'); + if (!$success = $this->_backup_migrate_bzip_encode($from, $file->filepath())) { + $file = NULL; + } + break; + + case "zip": + $filename = $file->filename(); + $from = $file->push_type('zip'); + if (!$success = $this->_backup_migrate_zip_encode($from, $file->filepath(), $filename)) { + $file = NULL; + } + break; + } + if (!$file) { + _backup_migrate_message("Could not compress backup file. Try backing up without compression.", array(), 'error'); + } + return $file; + } + + /** + * Decompress a file with the given settings. + * Also updates settings to reflect new file mime and file extension. + */ + function _backup_migrate_file_decompress($file, $settings) { + $success = FALSE; + + switch ($file->type_id()) { + case "gzip": + $from = $file->pop_type(); + $success = $this->_backup_migrate_gzip_decode($from->filepath(), $file->filepath(), $settings); + break; + + case "bzip": + case "bzip2": + $from = $file->pop_type(); + $success = $this->_backup_migrate_bzip_decode($from->filepath(), $file->filepath()); + break; + + case "zip": + $from = $file->pop_type(); + $success = $this->_backup_migrate_zip_decode($from->filepath(), $file->filepath()); + break; + + default: + return $file; + break; + } + + if (!$success) { + _backup_migrate_message("Could not decompress backup file. Please check that the file is valid.", array(), 'error'); + } + return $success ? $file : NULL; + } +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/filters.encryption.inc b/sites/all/modules/contrib/backup_migrate/includes/filters.encryption.inc new file mode 100644 index 0000000..74b679c --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/filters.encryption.inc @@ -0,0 +1,177 @@ + 170, 'restore' => -170); + + /** + * This function is called on a backup file after the backup has been completed. + */ + function backup($file, $settings) { + return $this->file_encrypt($file, $settings); + } + + /** + * This function is called on a backup file before importing it. + */ + function restore($file, $settings) { + return $this->file_decrypt($file); + } + + /** + * Get the form for the settings for this filter. + */ + function backup_settings_default() { + return array('encryption' => 'none'); + } + + /** + * Get the form for the settings for this filter. + */ + function backup_settings_form($settings) { + $form = array(); + $options = $this->_backup_migrate_get_encryption_form_item_options(); + if (count($options) > 1) { + $form['file']['encryption'] = array( + "#type" => "select", + "#title" => t("File Encryption"), + "#options" => $options, + "#default_value" => @$settings['encryption'], + '#description' => t('Encrypted files can only be restored by Backup and Migrate and only on sites with the same encryption key.'), + ); + } + else { + $form['file']['encryption'] = array( + "#type" => 'item', + "#title" => t("File Encryption"), + "#markup" => t('Install the !link to enable backup file encryption.', array('!link' => l(t('AES Encryption Module'), 'http://drupal.org/project/aes'))), + ); + } + + return $form; + } + + /** + * Return a list of backup filetypes. + */ + function file_types() { + return array( + "aes" => array( + "extension" => "aes", + "filemime" => "application/octet-stream", + "backup" => TRUE, + "restore" => TRUE, + ), + ); + } + + /** + * Get the compression options as an options array for a form item. + */ + function _backup_migrate_get_encryption_form_item_options() { + $options = array(); + $options = array('' => t('No Encryption')); + if (@function_exists("aes_encrypt")) { + $options['aes'] = t("AES Encryption"); + } + return $options; + } + + /** + * AES encrypt a file. + */ + function aes_encrypt($source, $dest) { + $success = FALSE; + if (function_exists('aes_encrypt')) { + if ($data = $source->get_contents()) { + // Add a marker to the end of the data so we can trim the padding on decrpypt. + $data = pack("a*H2", $data, "80"); + if ($data = aes_encrypt($data, FALSE)) { + $dest->put_contents($data); + $success = TRUE; + } + } + } + return $success; + } + + /** + * Gzip decode a file. + */ + function aes_decrypt($source, $dest) { + $success = FALSE; + if (function_exists('aes_decrypt')) { + if ($data = $source->get_contents()) { + if ($data = aes_decrypt($data, FALSE)) { + // Trim all the padding zeros plus our non-zero marker. + $data = substr(rtrim($data, "\0"), 0, -1); + $dest->put_contents($data); + $success = TRUE; + } + } + } + return $success; + } + + /** + * Compress a file with the given settings. + * Also updates settings to reflect new file mime and file extension. + */ + function file_encrypt($file, $settings) { + if (!empty($settings->filters['encryption'])) { + switch ($settings->filters['encryption']) { + case "aes": + $from = $file->push_type('aes'); + $from = new backup_file(array('filepath' => $from)); + if (!$success = $this->aes_encrypt($from, $file)) { + $file = NULL; + } + break; + } + if (!$file) { + _backup_migrate_message("Could not encrypt backup file. Try backing up without encryption.", array(), 'error'); + } + } + return $file; + } + + /** + * Decompress a file with the given settings. + * Also updates settings to reflect new file mime and file extension. + */ + function file_decrypt($file) { + $success = FALSE; + if ($file) { + switch ($file->type_id()) { + case "aes": + $from = $file->pop_type(); + $success = $this->aes_decrypt($from, $file); + break; + default: + return $file; + break; + } + + if (!$success) { + if (function_exists('aes_decrypt')) { + _backup_migrate_message("Could not decrpyt backup file. Please check that the file is valid and that the encryption key of the server matches the server that created the backup.", array(), 'error'); + } + else { + _backup_migrate_message('You must install the !link to restore encrypted backkups.', array('!link' => l(t('AES Encryption Module'), 'http://drupal.org/project/aes')), 'error'); + } + } + } + return $success ? $file : NULL; + } +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/filters.inc b/sites/all/modules/contrib/backup_migrate/includes/filters.inc new file mode 100644 index 0000000..d0ed9ec --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/filters.inc @@ -0,0 +1,332 @@ +weight($op); + } + array_multisort($sort, SORT_ASC, SORT_NUMERIC, $filters); + return $filters; +} + +/** + * Implementation of hook_backup_migrate_filters(). + * + * Get the built in Backup and Migrate filters. + */ +function backup_migrate_backup_migrate_filters() { + return array( + 'backup_restore' => array( + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.backup_restore.inc', + 'class' => 'backup_migrate_filter_backup_restore', + ), + 'compression' => array( + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.compression.inc', + 'class' => 'backup_migrate_filter_compression', + ), + 'encryption' => array( + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.encryption.inc', + 'class' => 'backup_migrate_filter_encryption', + ), + 'statusnotify' => array( + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.statusnotify.inc', + 'class' => 'backup_migrate_filter_statusnotify', + ), + 'utils' => array( + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/filters.utils.inc', + 'class' => 'backup_migrate_filter_utils', + ), + ); +} + +/** + * Invoke the given method on all of the available filters. + */ +function backup_migrate_filters_invoke_all() { + $args = func_get_args(); + $op = array_shift($args); + $out = array(); + $filters = backup_migrate_get_filters($op); + foreach ($filters as $filter) { + if (method_exists($filter, $op)) { + /* call_user_func_array() ignores the function signature, so we cannot + * use it to pass references. (Call-time pass-by-reference is deprecated + * in PHP5.3.) Work around it, since we have unknown function signatures. + */ + switch (count($args)) { + case 1: + $ret = $filter->$op($args[0]); + break; + + case 2: + $ret = $filter->$op($args[0], $args[1]); + break; + + default: + // This assumes that no functions with more than 2 arguments expect a + // reference as argument. If so, add another 'case block'. + $ret = call_user_func_array(array($filter, $op), $args); + } + $out = array_merge_recursive($out, (array) $ret); + } + } + return $out; +} + +/** + * Filter a backup file before sending it to the destination. + */ +function backup_migrate_filters_backup($file, &$settings) { + backup_migrate_filters_invoke_all('pre_backup', $file, $settings); + $filters = backup_migrate_get_filters('backup'); + foreach ($filters as $filter) { + if ($file) { + $file = $filter->backup($file, $settings); + } + } + backup_migrate_filters_invoke_all('post_backup', $file, $settings); + + return $file; +} + +/** + * Filter a backup file before sending it to the destination. + */ +function backup_migrate_filters_restore($file, &$settings) { + backup_migrate_filters_invoke_all('pre_restore', $file, $settings); + $filters = backup_migrate_get_filters('restore'); + foreach ($filters as $filter) { + if ($file) { + $file = $filter->restore($file, $settings); + } + } + backup_migrate_filters_invoke_all('post_restore', $file, $settings); + return $file; +} + +/** + * Get the backup settings for all of the filters. + */ +function backup_migrate_filters_settings_form($settings, $op) { + $out = backup_migrate_filters_invoke_all($op .'_settings_form', $settings); + $out = backup_migrate_filters_settings_form_set_parents($out); + return $out; +} + +/** + * Add a form parent to the filter settings so that the filter values are saved in the right table. + */ +function backup_migrate_filters_settings_form_set_parents($form) { + foreach (element_children($form) as $key) { + if (!isset($form[$key]['#parents'])) { + $form[$key]['#parents'] = array('filters', $key); + $form[$key] = backup_migrate_filters_settings_form_set_parents($form[$key]); + } + } + return $form; +} + +/** + * Validate all the filters. + */ +function backup_migrate_filters_settings_form_validate($op, $form, &$form_state) { + backup_migrate_filters_invoke_all($op .'_settings_form_validate', $form, $form_state); +} + +/** + * Submit all of the filters. + */ +function backup_migrate_filters_settings_form_submit($op, $form, &$form_state) { + backup_migrate_filters_invoke_all($op .'_settings_form_submit', $form, $form_state); +} + +/** + * Get the default settings for the filters. + */ +function backup_migrate_filters_settings_default($op) { + return backup_migrate_filters_invoke_all($op .'_settings_default'); +} + +/** + * Get the backup settings for all of the filters. + */ +function backup_migrate_filters_before_action_form($settings, $op) { + $out = array(); + $out += backup_migrate_filters_invoke_all('before_action_form', $op, $settings); + $out += backup_migrate_filters_invoke_all('before_' . $op .'_form', $settings); + return $out; +} + +/** + * Get the backup settings for all of the filters. + */ +function backup_migrate_filters_before_action_form_validate($settings, $op, $form, &$form_state) { + backup_migrate_filters_invoke_all('before_action_form_validate', $op, $settings, $form, $form_state); + backup_migrate_filters_invoke_all('before_' . $op .'_form_validate', $settings, $form, $form_state); +} + +/** + * Get the backup settings for all of the filters. + */ +function backup_migrate_filters_before_action_form_submit($settings, $op, $form, &$form_state) { + backup_migrate_filters_invoke_all('before_action_form_submit', $op, $settings, $form, $form_state); + backup_migrate_filters_invoke_all('before_' . $op .'_form_submit', $settings, $form, $form_state); +} + +/** + * Get the file types for all of the filters. + */ +function backup_migrate_filters_file_types() { + return backup_migrate_filters_invoke_all('file_types'); +} + +/** + * A base class for basing filters on. + */ +class backup_migrate_filter { + var $weight = 0; + var $op_weights = array(); + + /** + * Get the weight of the filter for the given op. + */ + function weight($op = NULL) { + if ($op && isset($this->op_weights[$op])) { + return $this->op_weights[$op]; + } + return $this->weight; + } + + /** + * Get the form for the settings for this filter. + */ + function backup_settings_default() { + return array(); + } + + /** + * Get the form for the settings for this filter. + */ + function backup_settings_form($settings) { + return array(); + } + + /** + * Get the form for the settings for this filter. + */ + function backup_settings_form_validate($form, &$form_state) { + } + + /** + * Submit the settings form. Any values returned will be saved. + */ + function backup_settings_form_submit($form, &$form_state) { + } + + /** + * Get the form for the settings for this filter. + */ + function restore_settings_default() { + return array(); + } + + /** + * Get the form for the settings for this filter. + */ + function restore_settings_form($settings) { + return array(); + } + + /** + * Get the form for the settings for this filter. + */ + function restore_settings_form_validate($form, &$form_state) { + } + + /** + * Submit the settings form. Any values returned will be saved. + */ + function restore_settings_form_submit($form, &$form_state) { + return $form_state['values']; + } + + /** + * Get a list of file types handled by this filter. + */ + function file_types() { + return array(); + } + + /** + * Declare any default destinations for this filter. + */ + function destinations() { + return array(); + } + + + /** + * This function is called on a backup file after the backup has been completed. + */ + function backup($file, $settings) { + return $file; + } + + /** + * This function is called immediately prior to backup. + */ + function pre_backup($file, $settings) { + + } + + /** + * This function is called immediately post backup. + */ + function post_backup($file, $settings) { + + } + + /** + * This function is called on a backup file before importing it. + */ + function restore($file, $settings) { + return $file; + } + + /** + * This function is called immediately prior to restore. + */ + function pre_restore($file, $settings) { + + } + + /** + * This function is called immediately post restore. + */ + function post_restore($file, $settings) { + + } +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/filters.statusnotify.inc b/sites/all/modules/contrib/backup_migrate/includes/filters.statusnotify.inc new file mode 100644 index 0000000..28387a2 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/filters.statusnotify.inc @@ -0,0 +1,114 @@ + FALSE, + 'notify_failure_enable' => FALSE, + 'notify_success_email' => variable_get('site_mail', ''), + 'notify_failure_email' => variable_get('site_mail', ''), + ); + } + + /** + * Get the form for the settings for this filter. + */ + function backup_settings_form($settings) { + $form = array(); + $form['advanced']['notify_success_enable'] = array( + "#type" => 'checkbox', + "#title" => t("Send an email if backup succeeds"), + "#default_value" => @$settings['notify_success_enable'], + ); + $form['advanced']['notify_success_email_wrapper'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'filters[notify_success_enable]' => TRUE, + ), + ); + $form['advanced']['notify_success_email_wrapper']['notify_success_email'] = array( + "#type" => "textfield", + "#title" => t("Email Address for Success Notices"), + "#default_value" => @$settings['notify_success_email'], + ); + $form['advanced']['notify_failure_enable'] = array( + "#type" => 'checkbox', + "#title" => t("Send an email if backup fails"), + "#default_value" => @$settings['notify_failure_enable'], + ); + $form['advanced']['notify_failure_email_wrapper'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'filters[notify_failure_enable]' => TRUE, + ), + ); + $form['advanced']['notify_failure_email_wrapper']['notify_failure_email'] = array( + "#type" => "textfield", + "#title" => t("Email Address for Failure Notices"), + "#default_value" => @$settings['notify_failure_email'], + ); + return $form; + } + + /** + * Send the success email. + */ + function backup_succeed($settings) { + if (@$settings->filters['notify_success_enable'] && $to = @$settings->filters['notify_success_email']) { + $messages = $this->get_messages(); + $subject = t('!site backup succeeded', array('!site' => variable_get('site_name', 'Drupal site'))); + if ($messages = $this->get_messages()) { + $body = t("The site backup has completed successfully with the following messages:\n!messages", array('!messages' => $messages)); + } + else { + $body = t("The site backup has completed successfully.\n"); + } + mail($settings->filters['notify_success_email'], $subject, $body); + } + } + + /** + * Send the failure email. + */ + function backup_fail($settings) { + if (@$settings->filters['notify_failure_enable'] && $to = @$settings->filters['notify_failure_email']) { + $messages = $this->get_messages(); + $subject = t('!site backup failed', array('!site' => variable_get('site_name', 'Drupal site'))); + if ($messages = $this->get_messages()) { + $body = t("The site backup has failed with the following messages:\n!messages", array('!messages' => $messages)); + } + else { + $body = t("The site backup has failed for an unknown reason."); + } + mail($settings->filters['notify_failure_email'], $subject, $body); + } + } + + /** + * Render the messages and errors for the email. + */ + function get_messages() { + $out = ""; + $messages = _backup_migrate_messages(); + foreach ($messages as $message) { + $out .= strip_tags(t($message['message'], $message['replace'])) . "\n"; + } + return $out; + } +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/filters.utils.inc b/sites/all/modules/contrib/backup_migrate/includes/filters.utils.inc new file mode 100644 index 0000000..861e09f --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/filters.utils.inc @@ -0,0 +1,239 @@ + -1000, 'post_backup' => 1000); + var $saved_devel_query = NULL; + + /** + * Get the default backup settings for this filter. + */ + function backup_settings_default() { + return array( + 'utils_disable_query_log' => TRUE, + 'utils_site_offline' => FALSE, + 'utils_description' => '', + ); + } + + /** + * Get the default restore settings for this filter. + */ + function restore_settings_default() { + return array( + 'utils_disable_query_log' => TRUE, + 'utils_site_offline' => FALSE, + ); + } + + /** + * Get the form for the backup settings for this filter. + */ + function backup_settings_form($settings) { + $form = array(); + if (module_exists('devel') && variable_get('dev_query', 0)) { + $form['database']['utils_disable_query_log'] = array( + '#type' => 'checkbox', + '#title' => t('Disable query log'), + '#default_value' => !empty($settings['utils_disable_query_log']) ? $settings['utils_disable_query_log'] : NULL, + '#description' => t('Disable the devel module\'s query logging during the backup operation. It will be enabled again after backup is complete. This is very highly recommended.'), + ); + } + $form['advanced']['utils_site_offline'] = array( + '#type' => 'checkbox', + '#title' => t('Take site offline'), + '#default_value' => !empty($settings['utils_site_offline']) ? $settings['utils_site_offline'] : NULL, + '#description' => t('Take the site offline during backup and show a maintenance message. Site will be taken back online once the backup is complete.'), + ); + $form['advanced']['utils_site_offline_message_wrapper'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'filters[utils_site_offline]' => TRUE, + ), + ); + $form['advanced']['utils_site_offline_message_wrapper']['utils_site_offline_message'] = array( + '#type' => 'textarea', + '#title' => t('Site off-line message'), + '#default_value' => !empty($settings['utils_site_offline_message']) ? $settings['utils_site_offline_message'] : variable_get('site_offline_message', t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal')))), + '#description' => t('Message to show visitors when the site is in off-line mode.') + ); + $form['advanced']['utils_description'] = array( + '#type' => 'textarea', + '#title' => t('Add a note'), + '#default_value' => !empty($settings['utils_description']) ? $settings['utils_description'] : NULL, + '#description' => t('Add a short note to the backup file.'), + ); + $form['advanced']['use_cli'] = array( + "#type" => "checkbox", + "#title" => t("Use cli commands"), + "#default_value" => !empty($settings['use_cli']), + "#description" => t("Use the command line tools (mysqldump, tar, gzip etc.) if available. This can be faster for large sites but will not work on all servers. EXPERIMENTAL"), + ); + $form['advanced']['ignore_errors'] = array( + "#type" => "checkbox", + "#title" => t("Ignore errors"), + "#default_value" => !empty($settings['ignore_errors']), + "#description" => t("Will attempt to complete backup even if certain recoverable errors occur. This may make the backup files invalid. Enable this if you have unreadable files that you want to ignore during backup."), + ); + + return $form; + } + + /** + * Get the form for the restore settings for this filter. + */ + function restore_settings_form($settings) { + $form = array(); + if (module_exists('devel') && variable_get('dev_query', 0)) { + $form['advanced']['utils_disable_query_log'] = array( + '#type' => 'checkbox', + '#title' => t('Disable query log'), + '#default_value' => @$settings['utils_disable_query_log'], + '#description' => t('Disable the devel module\'s query logging during the restore operation. It will be enabled again after restore is complete. This is very highly recommended.'), + ); + } + $form['advanced']['utils_site_offline'] = array( + '#type' => 'checkbox', + '#title' => t('Take site offline'), + '#default_value' => !empty($settings['utils_site_offline']) ? $settings['utils_site_offline'] : NULL, + '#description' => t('Take the site offline during restore and show a maintenance message. Site will be taken back online once the restore is complete.'), + ); + $form['advanced']['utils_site_offline_message_wrapper'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'filters[utils_site_offline]' => TRUE, + ), + ); + $form['advanced']['utils_site_offline_message_wrapper']['utils_site_offline_message'] = array( + '#type' => 'textarea', + '#title' => t('Site off-line message'), + '#default_value' => !empty($settings['utils_site_offline_message']) ? $settings['utils_site_offline_message'] : variable_get('site_offline_message', t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal')))), + '#description' => t('Message to show visitors when the site is in off-line mode.') + ); + $form['advanced']['use_cli'] = array( + "#type" => "checkbox", + "#title" => t("Use cli commands"), + "#default_value" => !empty($settings['use_cli']), + "#description" => t("Use the command line tools (mysqldump, tar, gzip etc.) if available. This can be faster for large sites but will not work on all servers. EXPERIMENTAL"), + ); + // $form['advanced']['ignore_errors'] = array( + // "#type" => "checkbox", + // "#title" => t("Ignore errors"), + // "#default_value" => !empty($settings['ignore_errors']), + // "#description" => t("Will attempt to complete restore even if certain recoverable errors occur. This may could corrupt your site."), + // ); + return $form; + } + + function pre_backup($file, $settings) { + $this->take_site_offline($settings); + $this->disable_devel_query($settings); + } + + function post_backup($file, $settings) { + $this->enable_devel_query($settings); + $this->take_site_online($settings); + if ($file) { + $this->add_file_info($file, $settings); + } + } + + function pre_restore($file, $settings) { + $this->disable_devel_query($settings); + $this->take_site_offline($settings); + } + + function post_restore($file, $settings) { + $this->enable_devel_query($settings); + $this->take_site_online($settings); + } + + /** + * Disable devel query logging if it's active and the user has chosen to do so. + */ + function disable_devel_query($settings) { + $this->saved_devel_query = variable_get('dev_query', 0); + if (module_exists('devel') && variable_get('dev_query', 0) && !empty($settings->filters['utils_disable_query_log'])) { + variable_set('dev_query', 0); + } + } + + /** + * Restore devel query to previous state. + */ + function enable_devel_query($settings) { + if (module_exists('devel')) { + variable_set('dev_query', $this->saved_devel_query); + } + } + + /** + * Add the backup metadata to the file. + */ + function add_file_info($file, $settings) { + $file->file_info['description'] = $settings->filters['utils_description']; + $file->file_info['datestamp'] = time(); + $file->file_info['generator'] = 'Backup and Migrate (http://drupal.org/project/backup_migrate)'; + $file->file_info['generatorversion'] = BACKUP_MIGRATE_VERSION; + $file->file_info['siteurl'] = url('', array('absolute' => TRUE)); + $file->file_info['sitename'] = variable_get('site_name', ''); + $file->file_info['drupalversion'] = VERSION; + $file->calculate_filesize(); + + $source = $settings->get('source'); + $file->file_info['bam_sourceid'] = $source->get('id'); + $file->file_info['bam_sourcetype'] = $source->get('subtype'); + $file->file_info['bam_sourcename'] = $source->get('name'); + + // Add any additional info that has been added to the settings by other plugins. + if (!empty($settings->file_info)) { + $file->file_info += $settings->file_info; + } + } + + /** + * Take the site offline if configured to do so. + */ + function take_site_offline($settings) { + // If the site is already offline then don't do anything. + if (!variable_get('maintenance_mode', 0)) { + // Save the current state of the site in case a restore overwrites it. + if (!empty($settings->filters['utils_site_offline'])) { + $this->saved_site_offline = TRUE; + $this->saved_site_offline_message = variable_get('maintenance_mode_message', NULL); + if (!empty($settings->filters['utils_site_offline_message'])) { + $this->saved_site_offline_message = variable_get('maintenance_mode_message', NULL); + variable_set('maintenance_mode_message', $settings->filters['utils_site_offline_message']); + } + variable_set('maintenance_mode', 1); + _backup_migrate_message('Site was taken offline.'); + } + } + } + + /** + * Take the site online again after backup or restore. + */ + function take_site_online($settings) { + // Take the site back off/online because the restored db may have changed that setting. + if (variable_get('maintenance_mode', 0) && !empty($this->saved_site_offline)) { + variable_set('maintenance_mode', 0); + if ($settings->filters['utils_site_offline']) { + if (!empty($this->saved_site_offline_message)) { + variable_set('maintenance_mode_message', $this->saved_site_offline_message); + } + _backup_migrate_message('Site was taken online.'); + } + } + } +} diff --git a/sites/all/modules/contrib/backup_migrate/includes/locations.inc b/sites/all/modules/contrib/backup_migrate/includes/locations.inc new file mode 100644 index 0000000..7cacd0f --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/locations.inc @@ -0,0 +1,543 @@ + $location) { + if ($location->op($op)) { + $out[$key] = $location; + } + } + } + return $out; +} + +/** + * Get the location of the given id. + */ +function backup_migrate_get_location($id) { + $locations = backup_migrate_get_locations('all'); + return empty($locations[$id]) ? NULL : $locations[$id]; +} + + +/** + * A base class for creating locations. + */ +class backup_migrate_location extends backup_migrate_item { + var $db_table = "backup_migrate_destinations"; + var $type_name = "location"; + var $default_values = array('settings' => array()); + var $singular = 'location'; + var $plural = 'locations'; + var $title_plural = 'Locations'; + var $title_singular = 'Location'; + + var $subtype = ""; + var $supported_ops = array(); + + /** + * This function is not supposed to be called. It is just here to help the po extractor out. + */ + function strings() { + // Help the pot extractor find these strings. + t('location'); + t('locations'); + t('Location'); + t('Locations'); + } + + function ops() { + return $this->supported_ops; + } + + /** + * Does this location support the given operation. + */ + function op($op) { + $ops = (array)$this->ops(); + return in_array($op, $ops); + } + + /** + * Remove the given op from the support list. + */ + function remove_op($op) { + $key = array_search($op, $this->supported_ops); + if ($key !== FALSE) { + unset($this->supported_ops[$key]); + } + } + + function get_name() { + return @$this->name; + } + + function set_name($name) { + return $this->name = $name; + } + + function set_location($location) { + $this->location = $location; + } + + function get_location() { + return @$this->location; + } + + function get_display_location() { + return $this->get_location(); + } + + function settings($key = NULL) { + $out = $this->settings; + if ($key) { + $out = isset($out[$key]) ? $out[$key] : NULL; + } + return $out; + } + + /** + * Get the type name of this location for display to the user. + */ + function get_subtype_name() { + if ($type = $this->get('subtype')) { + $types = $this->location_types(); + return isset($types[$type]['type_name']) ? $types[$type]['type_name'] : $type; + } + } + + /** + * Get the edit form for the item. + */ + function edit_form() { + if (!empty($this->supported_ops)) { + $form = parent::edit_form(); + $form['subtype'] = array( + "#type" => "value", + "#default_value" => $this->get('subtype'), + ); + } + else { + $types = $this->location_types(); + $items = array(); + // If no (valid) node type has been provided, display a node type overview. + foreach ($types as $key => $type) { + if (@$type['can_create']) { + $type_url_str = str_replace('_', '-', $key); + $out = '
'. l($type['type_name'], BACKUP_MIGRATE_MENU_PATH . "/settings/$this->type_name/add/$type_url_str", array('attributes' => array('title' => t('Add a new @s location.', array('@s' => $type['type_name']))))) .'
'; + $out .= '
'. filter_xss_admin($type['description']) .'
'; + $items[] = $out; + } + } + if (count($items)) { + $output = t('Choose the type of location you would like to create:') .'
'. implode('', $items) .'
'; + } + else { + $output = t('No types available.'); + } + $form['select_type'] = array( + '#type' => 'markup', + '#markup' => $output, + ); + } + return $form; + } + + /** + * Get the available location types. + */ + function location_types() { + return backup_migrate_get_location_subtypes(); + } + + /** + * Get the message to send to the user when confirming the deletion of the item. + */ + function delete_confirm_message() { + return t('Are you sure you want to delete the %name?', array('%name' => $this->get_name())); + } + + /** + * Get the columns needed to list the type. + */ + function get_list_column_info() { + $out = parent::get_list_column_info(); + $out = array( + 'name' => array('title' => t('Name')), + 'subtype_name' => array('title' => t('Type')), + 'display_location' => array('title' => t('Location')), + ) + $out; + return $out; + } + + /** + * Get a row of data to be used in a list of items of this type. + */ + function get_list_row() { + $out = parent::get_list_row(); + + // Supress locations with no actions as there's no value in showing them (and they may confuse new users). + if (empty($out['actions'])) { + return NULL; + } + return $out; + } + + /** + * Get the action links for a location. + */ + function get_action_links() { + $out = parent::get_action_links(); + $item_id = $this->get_id(); + + // Don't display the download/delete/restore ops if they are not available for this location. + if ($this->op('list files') && user_access("access backup files")) { + $out = array('list files' => l(t("list files"), BACKUP_MIGRATE_MENU_PATH . "/$this->type_name/list/files/". $item_id)) + $out; + } + if (!$this->op('configure') || !user_access('administer backup and migrate')) { + unset($out['edit']); + } + return $out; + } + + /** + * Determine if we can read the given file. + */ + function can_read_file($file_id) { + return $this->op('restore'); + } + + /** + * Get the form for the settings for this location type. + */ + function settings_default() { + return array(); + } + + /** + * Get the form for the settings for this location. + */ + function settings_form($form) { + return $form; + } + + /** + * Validate the form for the settings for this location. + */ + function settings_form_validate($form_values) { + } + + /** + * Submit the settings form. Any values returned will be saved. + */ + function settings_form_submit($form_values) { + return $form_values; + } + + /** + * Get the form for the settings for this filter. + */ + function backup_settings_default() { + return array(); + } + + /** + * Get the form for the settings for this filter. + */ + function backup_settings_form($settings) { + return array(); + } + + /** + * Get the form for the settings for this filter. + */ + function backup_settings_form_validate($form, &$form_state) { + } + + /** + * Submit the settings form. Any values returned will be saved. + */ + function backup_settings_form_submit($form, &$form_state) { + } + + /** + * Get the form for the settings for this filter. + */ + function restore_settings_default() { + return array(); + } + + /** + * Get the form for the settings for this filter. + */ + function restore_settings_form($settings) { + return array(); + } + + /** + * Get the form for the settings for this filter. + */ + function restore_settings_form_validate($form_values) { + } + + /** + * Submit the settings form. Any values returned will be saved. + */ + function restore_settings_form_submit($form_values) { + return $form_values; + } + + /** + * Create a new location of the correct type. + */ + function create($params = array()) { + $out = NULL; + $types = backup_migrate_get_location_subtypes(); + // Get the type passed in in the params, or if none, check the url for a valid type name. + // This is to allow new location type to be specified in the path. + $location_type = !empty($params['subtype']) ? $params['subtype'] : NULL; + + if ($location_type && ($type = @$types[$location_type])) { + // Include the necessary file if specified by the type. + if (!empty($type['file'])) { + require_once './'. $type['file']; + } + $out = new $type['class']($params + array('subtype' => $location_type)); + } + + if (empty($out)) { + $out = parent::create($params); + } + return $out; + } + + /** + * Get a url from the parts. + */ + function url($hide_password = TRUE) { + return $this->glue_url($this->dest_url, $hide_password); + } + + /** + * Glue a URLs component parts back into a URL. + */ + function glue_url($parts, $hide_password = TRUE) { + // Obscure the password if we need to. + $parts['pass'] = $hide_password ? "" : $parts['pass']; + + // Assemble the URL. + $out = ""; + $out .= $parts['scheme'] .'://'; + $out .= $parts['user'] ? urlencode($parts['user']) : ''; + $out .= ($parts['user'] && $parts['pass']) ? ":". urlencode($parts['pass']) : ''; + $out .= ($parts['user'] || $parts['pass']) ? "@" : ""; + $out .= $parts['host']; + $out .= !empty($parts['port']) ? ':'. $parts['port'] : ''; + $out .= "/". $parts['path']; + return $out; + } + + /** + * Break a URL into it's component parts. + */ + function set_url($url) { + $parts = (array)parse_url($url); + $parts['user'] = urldecode(@$parts['user']); + $parts['pass'] = urldecode(@$parts['pass']); + $parts['path'] = urldecode(@$parts['path']); + $parts['path'] = ltrim(@$parts['path'], "/"); + $this->dest_url = $parts; + } + + /** + * Retrieve a list of filetypes supported by this source/destination. + */ + function file_types() { + return array(); + } + +} + +/** + * A base class for creating locations. + */ +class backup_migrate_location_remote extends backup_migrate_location { + /** + * The location is a URI so parse it and store the parts. + */ + function get_location() { + return $this->url(FALSE); + } + + /** + * The location to display is the url without the password. + */ + function get_display_location() { + return $this->url(TRUE); + } + + /** + * Return the location with the password. + */ + function set_location($location) { + $this->location = $location; + $this->set_url($location); + } + + /** + * Get a url from the parts. + */ + function url($hide_password = TRUE) { + return $this->glue_url($this->dest_url, $hide_password); + } + + /** + * Glue a URLs component parts back into a URL. + */ + function glue_url($parts, $hide_password = TRUE) { + // Obscure the password if we need to. + $parts['pass'] = $hide_password ? "" : $parts['pass']; + + // Assemble the URL. + $out = ""; + $out .= $parts['scheme'] .'://'; + $out .= $parts['user'] ? urlencode($parts['user']) : ''; + $out .= ($parts['user'] && $parts['pass']) ? ":". urlencode($parts['pass']) : ''; + $out .= ($parts['user'] || $parts['pass']) ? "@" : ""; + $out .= $parts['host']; + $out .= !empty($parts['port']) ? ':'. $parts['port'] : ''; + $out .= "/". $parts['path']; + return $out; + } + + /** + * Break a URL into it's component parts. + */ + function set_url($url) { + $parts = (array)parse_url($url); + $parts['user'] = urldecode(@$parts['user']); + $parts['pass'] = urldecode(@$parts['pass']); + $parts['path'] = urldecode(@$parts['path']); + $parts['path'] = ltrim(@$parts['path'], "/"); + $this->dest_url = $parts; + } + + /** + * location configuration callback. + */ + function edit_form() { + $form = parent::edit_form(); + $form['scheme'] = array( + "#type" => "select", + "#title" => t("Scheme"), + "#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : 'mysql', + "#required" => TRUE, + "#options" => array($GLOBALS['db_type'] => $GLOBALS['db_type']), + "#weight" => 0, + ); + $form['host'] = array( + "#type" => "textfield", + "#title" => t("Host"), + "#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost', + "#required" => TRUE, + "#weight" => 10, + ); + $form['path'] = array( + "#type" => "textfield", + "#title" => t("Path"), + "#default_value" => @$this->dest_url['path'], + "#required" => TRUE, + "#weight" => 20, + ); + $form['user'] = array( + "#type" => "textfield", + "#title" => t("Username"), + "#default_value" => @$this->dest_url['user'], + "#required" => TRUE, + "#weight" => 30, + ); + $form['pass'] = array( + "#type" => "password", + "#title" => t("Password"), + "#default_value" => @$this->dest_url['pass'], + '#description' => '', + "#weight" => 40, + ); + if (@$this->dest_url['pass']) { + $form['old_password'] = array( + "#type" => "value", + "#value" => @$this->dest_url['pass'], + ); + $form['pass']["#description"] .= t(' You do not need to enter a password unless you wish to change the currently saved password.'); + } + return $form; + } + + /** + * Submit the configuration form. Glue the url together and add the old password back if a new one was not specified. + */ + function edit_form_submit($form, &$form_state) { + $form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password']; + $form_state['values']['location'] = $this->glue_url($form_state['values'], FALSE); + parent::edit_form_submit($form, $form_state); + } +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/profiles.inc b/sites/all/modules/contrib/backup_migrate/includes/profiles.inc new file mode 100644 index 0000000..47265b7 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/profiles.inc @@ -0,0 +1,369 @@ + array( + 'include' => 'profiles', + 'type_name' => t('Backup Settings Profile'), + 'class' => 'backup_migrate_profile', + ), + ); + return $out; +} + + +/** + * Get all the available backup profiles. + */ +function backup_migrate_get_profiles() { + backup_migrate_include('filters'); + static $profiles = NULL; + + // Get the list of profiles and cache them locally. + if ($profiles === NULL) { + $profiles = backup_migrate_crud_get_items('profile'); + } + return $profiles; +} + +/** + * Implementation of hook_backup_migrate_profiles_alter(). + * + * Add default settings for any plugins which didn't exist when the profile was saved. + */ +function backup_migrate_backup_migrate_profiles_alter(&$profiles) { + foreach ($profiles as $id => $profile) { + // Set the default values for filter setting which don't exist in the profile. + $profiles[$id]->filters = (array)@$profile->filters + (array)backup_migrate_filters_settings_default('backup'); + } +} + +/** + * Get the profile info for the profile with the given ID, or NULL if none exists. + */ +function backup_migrate_get_profile($profile_id) { + $profiles = backup_migrate_get_profiles(); + return @$profiles[$profile_id]; +} + +/** + * Implementation of hook_backup_migrate_profiles(). + */ +function backup_migrate_backup_migrate_profiles() { + $out = array(); + + // Get the module default profile. + $out['default'] = backup_migrate_crud_create_item('profile', array('name' => t("Default Settings"), 'machine_name' => 'default')); + + return $out; +} + +/* Utilities */ + +/** + * Get the available profiles as an options array for a form item. + */ +function _backup_migrate_get_profile_form_item_options() { + $out = array(); + foreach ((array)backup_migrate_get_profiles() as $key => $profile) { + $out[$key] = $profile->get('name'); + } + return $out; +} + +/** + * Get a form to configure the profile. + */ +function _backup_migrate_ui_backup_settings_form($profile) { + drupal_add_js(array('backup_migrate' => array('checkboxLinkText' => t('View as checkboxes'))), array('type' => 'setting')); + drupal_add_js(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.js', array('type' => 'file', 'scope' => 'footer')); + drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css'); + + backup_migrate_include('files', 'destinations', 'filters'); + + $form = array(); + + $form['file'] = array( + "#type" => "fieldset", + "#title" => t("Backup File"), + "#collapsible" => TRUE, + "#collapsed" => FALSE, + "#tree" => FALSE, + ); + $form['file']['filename'] = array( + "#type" => "textfield", + "#title" => t("Backup file name"), + "#default_value" => $profile->filename, + ); + if (module_exists('token')) { + $form['file']['token_help'] = array( + '#title' => t('Replacement patterns'), + '#type' => 'fieldset', + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + $form['file']['token_help']['help'] = array( + '#theme' => 'token_tree', + '#token_types' => array('current-date', 'site'), + '#global_types' => FALSE, + ); + $form['file']['filename']['#description'] = t('You can use tokens in the file name.'); + } + + $form['file']['append_timestamp'] = array( + "#type" => "checkbox", + "#title" => t("Append a timestamp."), + "#default_value" => $profile->append_timestamp, + ); + $form['file']['timestamp_format_wrapper'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'append_timestamp' => TRUE, + ), + ); + $form['file']['timestamp_format_wrapper']['timestamp_format'] = array( + "#type" => "textfield", + "#title" => t("Timestamp format"), + "#default_value" => $profile->timestamp_format, + "#description" => t('Should be a PHP date() format string.', array('!url' => 'http://www.php.net/date')), + ); + + $form['advanced'] = array('#weight' => 10); + $form = array_merge_recursive($form, backup_migrate_filters_settings_form($profile->filters, 'backup')); + + // Add the advanced fieldset if there are any fields in it. + if ($form['advanced']) { + $form['advanced']['#type'] = 'fieldset'; + $form['advanced']['#title'] = t('Advanced Options'); + $form['advanced']['#collapsed'] = true; + $form['advanced']['#collapsible'] = true; + } + + $form['#validate'][] = '_backup_migrate_ui_backup_settings_form_validate'; + $form['#submit'][] = '_backup_migrate_ui_backup_settings_form_submit'; + + return $form; +} + +/** + * Validate the profile form. + */ +function _backup_migrate_ui_backup_settings_form_validate($form, &$form_state) { + backup_migrate_filters_settings_form_validate('backup', $form, $form_state); +} + +/** + * Submit the profile form. + */ +function _backup_migrate_ui_backup_settings_form_submit($form, &$form_state) { + backup_migrate_filters_settings_form_submit('backup', $form, $form_state); +} + +/** + * Get the default profile. + */ +function _backup_migrate_profile_default_profile() { + backup_migrate_include('files', 'filters'); + return array( + 'source_id' => 'db', + 'filename' => _backup_migrate_default_filename(), + 'append_timestamp' => 1, + 'timestamp_format' => 'Y-m-d\TH-i-s', + 'filters' => backup_migrate_filters_settings_default('backup'), + ); +} + +/** + * Get the default profile saved by the user (or the module default if none exists). + */ +function _backup_migrate_profile_saved_default_profile($profile_id = NULL) { + $profile_id = $profile_id ? $profile_id : variable_get("backup_migrate_profile_id", 'default'); + $profile = NULL; + if ($profile_id) { + $profile = backup_migrate_get_profile($profile_id); + } + if (!$profile) { + $profile = backup_migrate_get_profile('default'); + } + return $profile; +} + +/** + * A profile class for crud operations. + */ +class backup_migrate_profile extends backup_migrate_item { + var $db_table = "backup_migrate_profiles"; + var $type_name = "profile"; + var $singular = 'settings profile'; + var $plural = 'settings profiles'; + var $title_plural = 'Settings Profiles'; + var $title_singular = 'Settings Profile'; + + /** + * This function is not supposed to be called. It is just here to help the po extractor out. + */ + function strings() { + // Help the pot extractor find these strings. + t('Settings Profile'); + t('Settings Profiles'); + t('settings profile'); + t('settings profiles'); + } + + /** + * Get the default values for standard parameters. + */ + function get_default_values() { + return _backup_migrate_profile_default_profile() + array('name' => t("Untitled Profile")); + } + + /** + * Get a table of all items of this type. + */ + function get_list() { + drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css'); + return parent::get_list(); + } + + /** + * Get the columns needed to list the type. + */ + function get_list_column_info() { + $out = parent::get_list_column_info(); + $out = array( + 'name' => array('title' => t('Name')), + 'source_name' => array('title' => t('Source')), + 'filename' => array('title' => t('Filename')), + ) + $out; + return $out; + } + + /** + * Set the source of this setings profile. Takes either a source object or source id. + */ + function set_source($source) { + if (is_object($source)) { + $this->source = $source; + $this->source_id = $source->get_id(); + } + else { + $this->source_id = $source; + unset($this->source); + } + } + + /** + * Get the source of the profile. + */ + function get_source() { + backup_migrate_include('locations'); + if (!empty($this->source_id) && (empty($this->source) || $this->source->get_id() !== $this->source_id)) { + $this->source = backup_migrate_get_source($this->source_id); + } + return empty($this->source) ? NULL : $this->source; + } + + /** + * Get the name of the source. + */ + function get_source_name() { + if ($source = $this->get_source()) { + return $source->get_name(); + } + return t("Missing"); + } + + /** + * Get the destination of the profile. + */ + function get_destination() { + $destinations = (array)$this->get_destinations(); + return reset($destinations); + } + + /** + * Get the destination of the profile. + */ + function get_destinations() { + backup_migrate_include('destinations'); + if (empty($this->destinations)) { + $this->destinations = array(); + $ids = $weights = array(); + if (!empty($this->destination_id)) { + foreach ((array)$this->destination_id as $destination_id) { + if (!in_array($destination_id, $ids) && $destination = backup_migrate_get_destination($destination_id)) { + $this->destinations[] = $destination; + $weights[] = $destination->get('weight'); + $ids[] = $destination_id; + } + } + } + // Sort the destinations by weight. + array_multisort($weights, SORT_NUMERIC, $this->destinations); + } + + return $this->destinations; + } + + /** + * Get the name of the destination. + */ + function get_destination_name() { + $out = array(); + foreach ($this->get_destinations() as $destination) { + $out[] = $destination->get_name(); + } + if ($out) { + return implode(', ', $out); + } + return t("Missing"); + } + + /** + * Get the source and destinations specified in the given settings profile + */ + function get_all_locations() { + $out = array(); + $out += $this->get('destinations'); + $out[] = $this->get('source'); + return $out; + } + + /** + * Get the edit form. + */ + function edit_form() { + $form = parent::edit_form(); + $form['name'] = array( + "#type" => "textfield", + "#title" => t("Profile Name"), + '#required' => TRUE, + "#default_value" => $this->get('name'), + ); + $form += _backup_migrate_ui_backup_settings_form($this); + return $form; + } + + /** + * Get the message to send to the user when confirming the deletion of the item. + */ + function delete_confirm_message() { + return t('Are you sure you want to delete the profile %name? Any schedules using this profile will be disabled.', array('%name' => $this->get('name'))); + } +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/schedules.inc b/sites/all/modules/contrib/backup_migrate/includes/schedules.inc new file mode 100644 index 0000000..6d9ed60 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/schedules.inc @@ -0,0 +1,803 @@ + array( + 'include' => 'schedule', + 'type_name' => t('Backup Schedule'), + 'class' => 'backup_migrate_schedule', + ), + ); + return $out; +} + +/** + * Run the preconfigured schedules. Called on cron. + */ +function backup_migrate_schedules_cron() { + backup_migrate_include('profiles'); + foreach (backup_migrate_get_schedules() as $schedule) { + $schedule->cron(); + } + backup_migrate_cleanup(); +} + +/** + * Run the preconfigured schedules regardless of scheduled time settings. + */ +function backup_migrate_schedules_run() { + backup_migrate_include('profiles'); + foreach (backup_migrate_get_schedules() as $schedule) { + $schedule->run(); + } + backup_migrate_cleanup(); +} + +/** + * Run the preconfigured schedules. Called on cron. + */ +function backup_migrate_schedule_run($schedule_id) { + backup_migrate_include('profiles'); + if ($schedule = backup_migrate_get_schedule($schedule_id)) { + $schedule->run(); + } + backup_migrate_cleanup(); +} + +/** + * Get all the available backup schedules. + */ +function backup_migrate_get_schedules() { + static $schedules = NULL; + // Get the list of schedules and cache them locally. + if ($schedules === NULL) { + $schedules = backup_migrate_crud_get_items('schedule'); + } + return $schedules; +} + +/** + * Get the schedule info for the schedule with the given ID, or NULL if none exists. + */ +function backup_migrate_get_schedule($schedule_id) { + $schedules = backup_migrate_get_schedules(); + return @$schedules[$schedule_id]; +} + +/** + * A schedule class for crud operations. + */ +class backup_migrate_schedule extends backup_migrate_item { + var $db_table = "backup_migrate_schedules"; + var $type_name = 'schedule'; + var $singular = 'schedule'; + var $plural = 'schedules'; + var $title_plural = 'Schedules'; + var $title_singular = 'Schedule'; + var $default_values = array(); + + /** + * This function is not supposed to be called. It is just here to help the po extractor out. + */ + function strings() { + // Help the pot extractor find these strings. + t('Schedule'); + t('Schedules'); + t('schedule'); + t('schedules'); + } + + /** + * Get the default values for this item. + */ + function get_default_values() { + return array( + 'name' => t("Untitled Schedule"), + 'source_id' => 'db', + 'enabled' => 1, + 'keep' => BACKUP_MIGRATE_KEEP_ALL, + 'period' => 60 * 60 * 24, + 'storage' => BACKUP_MIGRATE_STORAGE_NONE, + 'cron' => BACKUP_MIGRATE_CRON_BUILTIN, + 'cron_schedule' => '0 4 * * *', + ); + } + + /** + * Return as an array of values. + */ + function to_array() { + $out = parent::to_array(); + unset($out['last_run']); + return $out; + } + + /** + * Get the columns needed to list the type. + */ + function get_list_column_info() { + $out = parent::get_list_column_info(); + $out = array( + 'name' => array('title' => t('Name')), + 'destination_name' => array('title' => t('Destinations'), 'html' => TRUE), + 'profile_name' => array('title' => t('Profile'), 'html' => TRUE), + 'frequency_description' => array('title' => t('Frequency')), + 'keep_description' => array('title' => t('Keep')), + 'enabled_description' => array('title' => t('Enabled')), + 'last_run_description' => array('title' => t('Last run')), + ) + $out; + return $out; + } + + /** + * Get the columns needed to list the type. + */ + function get_settings_path() { + // Pull the schedules tab up a level to the top. + return BACKUP_MIGRATE_MENU_PATH . '/' . $this->type_name; + } + + /** + * Get the menu items for manipulating this type. + */ + function get_menu_items() { + $items = parent::get_menu_items(); + $path = $this->get_settings_path(); + return $items; + } + + /** + * Get a row of data to be used in a list of items of this type. + */ + function get_list_row() { + drupal_add_css(drupal_get_path('module', 'backup_migrate') .'/backup_migrate.css'); + $row = parent::get_list_row(); + if (!$this->is_enabled()) { + foreach ($row as $key => $field) { + if (!is_array($field)) { + $row[$key] = array('data' => $field, 'class' => 'schedule-list-disabled'); + } + else if (isset($row[$key]['class'])) { + $row[$key]['class'] .= ' schedule-list-disabled'; + } + else { + $row[$key]['class'] = 'schedule-list-disabled'; + } + } + } + return $row; + } + + /** + * Is the schedule enabled and valid. + */ + function is_enabled() { + $destination = $this->get_destination(); + $profile = $this->get_profile(); + return (!empty($this->enabled) && !empty($destination) && !empty($profile)); + } + + /** + * Get the destination object of the schedule. + */ + function get_destination() { + $destinations = (array)$this->get_destinations(); + return reset($destinations); + } + + /** + * Get the destination object of the schedule. + */ + function get_destination_ids() { + $out = array(); + foreach (array('destination_id', 'copy_destination_id') as $key) { + if ($id = $this->get($key)) { + $out[$key] = $id; + } + } + return $out; + } + + /** + * Get the destination object of the schedule. + */ + function get_destinations() { + backup_migrate_include('destinations'); + $out = array(); + foreach ($this->get_destination_ids() as $id) { + if ($dest = backup_migrate_get_destination($id)) { + $out[$id] = $dest; + } + } + return $out; + } + + /** + * Get the destination object of the schedule. + */ + function get_destination_remote() { + backup_migrate_include('destinations'); + return backup_migrate_get_destination($this->get('destination_remote_id')); + } + /** + * Get the destination object of the schedule. + */ + function get_destination_local() { + backup_migrate_include('destinations'); + return backup_migrate_get_destination($this->get('destination_local_id')); + } + + /** + * Get the name of the destination. + */ + function get_destination_name() { + if ($destinations = $this->get_destinations()) { + $out = array(); + foreach ((array)$destinations as $destination) { + $out[] = check_plain($destination->get_name()); + } + return implode(', ', $out); + } + return '
'. t("Missing") .'
'; + } + + /** + * Get the destination of the schedule. + */ + function get_profile() { + backup_migrate_include('profiles'); + if ($settings = backup_migrate_get_profile($this->get('profile_id'))) { + $settings->file_info = empty($settings->file_info) ? array() : $settings->file_info; + $settings->file_info += array( + 'schedule_id' => $this->get_id(), + 'schedule_name' => $this->get('name'), + ); + } + return $settings; + + } + + /** + * Get the name of the source. + */ + function get_profile_name() { + if ($profile = $this->get_profile()) { + return check_plain($profile->get_name()); + } + return '
'. t("Missing") .'
'; + } + + /** + * Format a frequency in human-readable form. + */ + function get_frequency_description() { + $period = $this->get_frequency_period(); + $cron = $this->get('cron'); + if ($cron == BACKUP_MIGRATE_CRON_BUILTIN) { + $out = format_plural(($this->period / $period['seconds']), $period['singular'], $period['plural']); + } + else if ($cron == BACKUP_MIGRATE_CRON_ELYSIA) { + $out = $this->get('cron_schedule'); + } + else { + $out = t('None'); + } + return $out; + } + + /** + * Format the number to keep in human-readable form. + */ + function get_keep_description() { + return $this->generate_keep_description($this->keep); + } + + /** + * Format a number to keep in human readable from + */ + function generate_keep_description($keep, $terse = TRUE) { + if ($keep == BACKUP_MIGRATE_KEEP_ALL) { + return t('all backups'); + } + else if ($keep == BACKUP_MIGRATE_SMART_DELETE) { + $keep_hourly = variable_get('backup_migrate_smart_keep_hourly', BACKUP_MIGRATE_SMART_KEEP_HOURLY); + $keep_daily = variable_get('backup_migrate_smart_keep_daily', BACKUP_MIGRATE_SMART_KEEP_DAILY); + $keep_weekly = variable_get('backup_migrate_smart_keep_weekly', BACKUP_MIGRATE_SMART_KEEP_WEEKLY); + if ($terse) { + return t('!hours hourly, !days daily, !weeks weekly backups', + array( + '!hours' => $keep_hourly == PHP_INT_MAX ? t('all') : $keep_hourly, + '!days' => $keep_daily == PHP_INT_MAX ? t('all') : $keep_daily, + '!weeks' => $keep_weekly == PHP_INT_MAX ? t('all') : $keep_weekly, + )); + } + else { + return t('hourly backups !hours, daily backups !days and weekly backups !weeks', + array( + '!hours' => $keep_hourly == PHP_INT_MAX ? t('forever') : format_plural($keep_hourly, 'for 1 hour', 'for the past @count hours'), + '!days' => $keep_daily == PHP_INT_MAX ? t('forever') : format_plural($keep_daily, 'for 1 day', 'for the past @count days'), + '!weeks' => $keep_weekly == PHP_INT_MAX ? t('forever') : format_plural($keep_weekly, 'for 1 week', 'for the past @count weeks'), + ) + ); + } + } + + return format_plural($keep, 'last 1 backup', 'last @count backups'); + } + + /** + * Format the enabled status in human-readable form. + */ + function get_enabled_description() { + return !empty($this->enabled) ? t('Enabled') : t('Disabled'); + } + + /** + * Format the enabled status in human-readable form. + */ + function get_last_run_description() { + $last_run = $this->get('last_run'); + return !empty($last_run) ? format_date($last_run, 'small') : t('Never'); + } + + /** + * Get the number of excluded tables. + */ + function get_exclude_tables_count() { + return count($this->exclude_tables) ? count($this->exclude_tables) : t("No tables excluded"); + } + + /** + * Get the number of excluded tables. + */ + function get_nodata_tables_count() { + return count($this->nodata_tables) ? count($this->nodata_tables) : t("No data omitted"); + } + + /** + * Get the edit form. + */ + function edit_form() { + $form = parent::edit_form(); + backup_migrate_include('destinations', 'sources', 'profiles'); + + $form['name'] = array( + "#type" => "textfield", + "#title" => t("Schedule Name"), + "#default_value" => $this->get('name'), + ); + + $form += _backup_migrate_get_source_form($this->get('source_id')); + + $form['profile_id'] = array( + "#type" => "select", + "#title" => t("Settings Profile"), + "#options" => _backup_migrate_get_profile_form_item_options(), + "#default_value" => $this->get('profile_id'), + ); + $form['profile_id']['#description'] = ' '. l(t('Create new profile'), BACKUP_MIGRATE_MENU_PATH . '/settings/profile/add'); + if (!$form['profile_id']['#options']) { + $form['profile_id']['#options'] = array('0' => t('-- None Available --')); + } + + $period_options = array(); + foreach ($this->frequency_periods() as $type => $period) { + $period_options[$type] = $period['title']; + } + $default_period = $this->get_frequency_period(); + $default_period_num = $this->get('period') / $default_period['seconds']; + + $form['enabled'] = array( + '#type' => "checkbox", + '#title' => t('Enabled'), + '#default_value' => $this->get('enabled'), + ); + $form['cron_settings'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'enabled' => TRUE, + ), + ); + + $cron = $this->get('cron'); + $form['cron_settings']['cron_builtin'] = array( + "#type" => "radio", + "#title" => t('Run using Drupal\'s cron'), + '#return_value' => BACKUP_MIGRATE_CRON_BUILTIN, + "#description" => t('Run this schedule when !cron runs.', array('!cron' => l(t('your cron task'), 'http://drupal.org/cron'))), + "#default_value" => $cron ? $cron : BACKUP_MIGRATE_CRON_BUILTIN, + '#parents' => array('cron'), + ); + + $form['cron_settings']['period_settings'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'cron' => BACKUP_MIGRATE_CRON_BUILTIN, + ), + ); + $form['cron_settings']['period_settings']['period'] = array( + "#type" => "item", + "#title" => t("Backup every"), + "#prefix" => '
', + "#suffix" => '
', + "#tree" => TRUE, + '#parents' => array('period'), + ); + $form['cron_settings']['period_settings']['period']['number'] = array( + "#type" => "textfield", + "#size" => 6, + "#default_value" => $default_period_num, + '#parents' => array('period', 'number'), + ); + $form['cron_settings']['period_settings']['period']['type'] = array( + "#type" => "select", + "#options" => $period_options, + "#default_value" => $default_period['type'], + '#parents' => array('period', 'type'), + ); + + + $form['cron_settings']['cron_elysia'] = array( + "#type" => "radio", + "#title" => t('Run using Elysia cron'), + '#return_value' => BACKUP_MIGRATE_CRON_ELYSIA, + "#description" => t('You can specify exactly when this schedule should run using !elysia.', array('!elysia' => l(t('the Elysia Cron module'), 'http://drupal.org/project/elysia_cron'))), + "#default_value" => $cron ? $cron : BACKUP_MIGRATE_CRON_BUILTIN, + '#parents' => array('cron'), + ); + if (!module_exists('elysia_cron')) { + $form['cron_settings']['cron_elysia']['#disabled'] = TRUE; + $form['cron_settings']['cron_elysia']['#description'] .= ' ' . t('Install !elysia to enable this option.', array('!elysia' => l(t('Elysia Cron'), 'http://drupal.org/project/elysia_cron'))); + } + $form['cron_settings']['cron_schedule_settings'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'cron' => BACKUP_MIGRATE_CRON_ELYSIA, + ), + ); + $form['cron_settings']['cron_schedule_settings']['cron_schedule'] = array( + "#type" => "textfield", + "#title" => t('Cron Schedule'), + '#length' => 10, + "#description" => t('Specify the frequecy of the schedule using standard cron notation. For more information see the !elysiareadme.', array('!elysiareadme' => l(t('the Elysia Cron README'), 'http://drupalcode.org/project/elysia_cron.git/blob/refs/heads/7.x-1.x:/README.txt'))), + "#default_value" => $this->get('cron_schedule'), + '#parents' => array('cron_schedule'), + ); + + $form['cron_settings']['cron_none'] = array( + "#type" => "radio", + "#title" => t('Do not run automatically'), + '#return_value' => 'none', + "#description" => t('Do not run this schedule automatically. You can still run it using !drush.', array('!drush' => l(t('Drush'), 'http://drupal.org/project/drush'))), + "#default_value" => $cron ? $cron : BACKUP_MIGRATE_CRON_BUILTIN, + '#parents' => array('cron'), + ); + + + + $keep = $this->get('keep'); + $form['delete'] = array( + '#type' => 'checkbox', + '#default_value' => $keep != 0, + '#title' => t('Automatically delete old backups'), + ); + $form['delete_settings'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'delete' => TRUE, + ), + ); + + $keep_hourly = variable_get('backup_migrate_smart_keep_hourly', BACKUP_MIGRATE_SMART_KEEP_HOURLY); + $keep_daily = variable_get('backup_migrate_smart_keep_daily', BACKUP_MIGRATE_SMART_KEEP_DAILY); + $keep_weekly = variable_get('backup_migrate_smart_keep_weekly', BACKUP_MIGRATE_SMART_KEEP_WEEKLY); + $form['delete_settings']['smartdelete'] = array( + "#type" => "radio", + "#title" => t('Smart Delete'), + '#return_value' => BACKUP_MIGRATE_SMART_DELETE, + "#description" => t('Keep !keep. Recommended', array('!keep' => $this->generate_keep_description(BACKUP_MIGRATE_SMART_DELETE, FALSE))), + "#default_value" => $keep ? $keep : BACKUP_MIGRATE_SMART_DELETE, + '#parents' => array('deletetype'), + ); + $form['delete_settings']['standarddelete'] = array( + "#type" => "radio", + "#title" => t('Simple Delete'), + '#return_value' => BACKUP_MIGRATE_STANDARD_DELETE, + "#description" => t("Keep a specified number of files deleting the oldest ones first."), + "#default_value" => $keep > 0 ? BACKUP_MIGRATE_STANDARD_DELETE : 0, + '#parents' => array('deletetype'), + ); + $form['delete_settings']['keep-settings'] = array( + '#type' => 'backup_migrate_dependent', + '#dependencies' => array( + 'deletetype' => BACKUP_MIGRATE_STANDARD_DELETE, + ), + ); + $form['delete_settings']['keep-settings']['keep'] = array( + "#type" => "textfield", + "#size" => 6, + "#title" => t("Number of Backup files to keep"), + "#description" => t("The number of backup files to keep before deleting old ones."), + "#default_value" => $keep > 0 ? $keep : BACKUP_MIGRATE_KEEP_DEFAULT, + ); + + $form['destination'] = _backup_migrate_get_destination_pulldown('scheduled backup', $this->get('destination_id'), $this->get('copy_destination_id')); + + return $form; + } + + /** + * Submit the edit form. + */ + function edit_form_validate($form, &$form_state) { + if (!is_numeric($form_state['values']['period']['number']) || $form_state['values']['period']['number'] <= 0) { + form_set_error('period][number', t('Backup period must be a number greater than 0.')); + } + + if (!$form_state['values']['delete']) { + $form_state['values']['keep'] = 0; + } + else if ($form_state['values']['deletetype'] == BACKUP_MIGRATE_SMART_DELETE) { + $form_state['values']['keep'] = BACKUP_MIGRATE_SMART_DELETE; + } + else if (!is_numeric($form_state['values']['keep']) || $form_state['values']['keep'] <= 0) { + form_set_error('keep', t('Number to keep must be a number greater than 0.')); + } + parent::edit_form_validate($form, $form_state); + } + + /** + * Submit the edit form. + */ + function edit_form_submit($form, &$form_state) { + $periods = $this->frequency_periods(); + $period = $periods[$form_state['values']['period']['type']]; + $form_state['values']['period'] = $form_state['values']['period']['number'] * $period['seconds']; + parent::edit_form_submit($form, $form_state); + } + + /** + * Get the period of the frequency (ie: seconds, minutes etc.) + */ + function get_frequency_period() { + foreach (array_reverse($this->frequency_periods()) as $period) { + if ($period['seconds'] && ($this->period % $period['seconds']) === 0) { + return $period; + } + } + } + + /** + * Get a list of available backup periods. Only returns time periods which have a + * (reasonably) consistent number of seconds (ie: no months). + */ + function frequency_periods() { + return array( + 'seconds' => array('type' => 'seconds', 'seconds' => 1, 'title' => t('Seconds'), 'singular' => t('Once a second'), 'plural' => t('Every @count seconds')), + 'minutes' => array('type' => 'minutes', 'seconds' => 60, 'title' => t('Minutes'), 'singular' => t('Once a minute'), 'plural' => t('Every @count minutes')), + 'hours' => array('type' => 'hours', 'seconds' => 3600, 'title' => t('Hours'), 'singular' => t('Once an hour'), 'plural' => t('Every @count hours')), + 'days' => array('type' => 'days', 'seconds' => 86400, 'title' => t('Days'), 'singular' => t('Once a day'), 'plural' => t('Every @count days')), + 'weeks' => array('type' => 'weeks', 'seconds' => 604800, 'title' => t('Weeks'), 'singular' => t('Once a week'), 'plural' => t('Every @count weeks')), + ); + } + + /** + * Get the message to send to the user when confirming the deletion of the item. + */ + function delete_confirm_message() { + return t('Are you sure you want to delete the schedule %name? Backups made with this schedule will not be deleted.', array('%name' => $this->get('name'))); + } + + /** + * Perform the cron action. Run the backup if enough time has elapsed. + */ + function cron() { + $now = time(); + + // Add a small negative buffer (1% of the entire period) to the time to account for slight difference in cron run length. + $wait_time = $this->period - ($this->period * variable_get('backup_migrate_schedule_buffer', 0.01)); + + $cron = $this->get('cron'); + if ($cron == BACKUP_MIGRATE_CRON_BUILTIN && $this->is_enabled() && ($now - $this->get('last_run')) >= $wait_time) { + $this->run(); + } + } + + /** + * Run the actual schedule. + */ + function run() { + if ($settings = $this->get_profile()) { + $settings->source_id = $this->get('source_id'); + $settings->destination_id = $this->get('destination_ids'); + + $this->update_last_run(time()); + backup_migrate_perform_backup($settings); + $this->remove_expired_backups(); + } + else { + backup_migrate_backup_fail("Schedule '%schedule' could not be run because requires a profile which is missing.", array('%schedule' => $schedule->get_name()), $settings); + } + } + + /** + * Set the last run time of a schedule to the given timestamp, or now if none specified. + */ + function update_last_run($timestamp = NULL) { + if ($timestamp === NULL) { + $timestamp = time(); + } + variable_set('backup_migrate_schedule_last_run_' . $this->get('id'), $timestamp); + } + + /** + * Set the last run time of a schedule to the given timestamp, or now if none specified. + */ + function get_last_run() { + return variable_get('backup_migrate_schedule_last_run_' . $this->get('id'), 0); + } + + /** + * Remove older backups keeping only the number specified by the aministrator. + */ + function remove_expired_backups() { + backup_migrate_include('destinations'); + + $num_to_keep = $this->keep; + // If num to keep is not 0 (0 is infinity). + foreach ((array)$this->get_destinations() as $destination) { + if ($destination && $destination->op('delete') && $destination_files = $destination->list_files()) { + if ($num_to_keep == BACKUP_MIGRATE_SMART_DELETE) { + $this->smart_delete_backups( + $destination, + $destination_files, + variable_get('backup_migrate_smart_keep_subhourly', BACKUP_MIGRATE_SMART_KEEP_SUBHOURLY), + variable_get('backup_migrate_smart_keep_hourly', BACKUP_MIGRATE_SMART_KEEP_HOURLY), + variable_get('backup_migrate_smart_keep_daily', BACKUP_MIGRATE_SMART_KEEP_DAILY), + variable_get('backup_migrate_smart_keep_weekly', BACKUP_MIGRATE_SMART_KEEP_WEEKLY) + ); + } + else if ($num_to_keep != BACKUP_MIGRATE_KEEP_ALL) { + $this->delete_backups($destination, $destination_files, $num_to_keep); + } + } + } + } + + /** + * Remove older backups keeping only the number specified by the aministrator. + */ + function delete_backups($destination, $files, $num_to_keep) { + backup_migrate_include('destinations'); + + $num_to_keep = $this->keep; + + // Sort the files by modified time. + $i = 0; + foreach ($files as $id => $file) { + if ($file->is_recognized_type()) { + $time = $file->info('filetime'); + $sorted[$id] = $time; + } + } + asort($sorted); + + // If we are beyond our limit, remove as many as we need. + $num_files = count($files); + + if ($num_files > $num_to_keep) { + $num_to_delete = $num_files - $num_to_keep; + // Delete from the start of the list (earliest). + foreach ($sorted as $id => $time) { + if (!$num_to_delete--) { + break; + } + $destination->delete_file($id); + } + } + } + + /** + * Delete files keeping the specified number of hourly, daily, weekly and monthly backups. + */ + function smart_delete_backups($destination, $files, $keep_subhourly = 3600, $keep_hourly = 24, $keep_daily = 14, $keep_weekly = PHP_INT_MAX, $keep_monthly = PHP_INT_MAX) { + $now = time(); + $periods = array( + 'subhourly' => array( + 'delta' => 1, + 'keep' => $keep_subhourly, + 'last_time' => 0, + 'files' => array(), + ), + 'hourly' => array( + 'delta' => 60*60, + 'keep' => $keep_hourly, + 'last_time' => 0, + 'files' => array(), + ), + 'daily' => array( + 'delta' => 60*60*24, + 'keep' => $keep_daily, + 'last_time' => 0, + 'files' => array(), + ), + 'weekly' => array( + 'delta' => 60*60*24*7, + 'keep' => $keep_weekly, + 'last_time' => 0, + 'files' => array(), + ), + /* + 'monthly' => array( + 'delta' => 60*60*24*7*4, + 'keep' => $keep_monthly, + 'last_time' => 0, + 'files' => array(), + ), + */ + ); + + $keep_files = $filetimes = $times = $groups = $sorted = $saved = array(); + foreach ($files as $id => $file) { + if ($file->is_recognized_type()) { + $time = $file->info('filetime'); + $sorted[$id] = $time; + } + } + arsort($sorted); + + $now = time(); + foreach ($periods as $i => $period) { + foreach ($sorted as $id => $time) { + if ($time < ($now - ($period['delta'] * $period['keep']))) { + break; + } + if ($period['last_time'] == 0 || $time <= ($period['last_time'] - $period['delta'])) { + $period['last_time'] = $time; + $keep_files[$id] = $id; + } + } + // Keep oldest backup or it will get deleted if it doesn't fall on an exact multiple of the period + if ($id) { + $keep_files[$id] = $id; + } + } + + // Do the delete. + foreach ($files as $id => $file) { + if (!isset($keep_files[$id])) { + $destination->delete_file($file->file_id()); + } + } + } +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/sources.archivesource.inc b/sites/all/modules/contrib/backup_migrate/includes/sources.archivesource.inc new file mode 100644 index 0000000..c8712eb --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/sources.archivesource.inc @@ -0,0 +1,287 @@ + 'archive', 'location' => '.', 'name' => t('Entire Site (code, files & DB)'), 'show_in_list' => FALSE)); + return $out; + } + + /** + * Return a list of backup filetypes. + */ + function file_types() { + return array( + "sitearchive" => array( + "extension" => "sitearchive.tar", + "filemime" => "application/x-tar", + "backup" => TRUE, + "restore" => FALSE, + ), + ); + } + + /** + * Get the form for the settings for this destination. + */ + function backup_settings_default() { + $out = parent::backup_settings_default(); + $excludes = explode("\n", $out['exclude_filepaths']); + foreach ($excludes as $i => $exclude) { + $excludes[$i] = 'public://' . $exclude; + } + $excludes[] = 'private://backup_migrate'; + $excludes[] = conf_path() . '/settings.php'; + $excludes[] = file_directory_temp(); + + return array( + 'exclude_filepaths' => implode("\n", $excludes), + ); + } + + /** + * Backup from this source. + */ + function _backup_to_file_php($file, $settings) { + if ($this->check_libs()) { + $base_dir = $this->get_realpath(); + + $excluded_paths = empty($settings->filters['exclude_filepaths']) ? '' : $settings->filters['exclude_filepaths']; + $exclude = $this->get_excluded_paths($settings); + $files = $this->get_files_to_backup($this->get_realpath(), $settings, $exclude); + if ($files) { + $manifest = $this->generate_manifest(); + $db = $this->get_db(); + + $file->push_type('sitearchive'); + $gz = new Archive_Tar($file->filepath(), false); + + $gz->addModify(array($manifest), $file->name .'/', dirname($manifest)); + $gz->addModify($files, $file->name .'/docroot', $base_dir); + $gz->addModify($db, $file->name . '/', dirname($db)); + + unlink($manifest); + rmdir(dirname($manifest)); + unlink($db); + rmdir(dirname($db)); + + return $file; + } + backup_migrate_backup_fail('No files available.', array(), $settings); + return FALSE; + } + return FALSE; + } + + /** + * Backup from this source. + */ + function _backup_to_file_cli($file, $settings) { + if (!empty($settings->filters['use_cli']) && function_exists('backup_migrate_exec') && function_exists('escapeshellarg')) { + $excluded_paths = empty($settings->filters['exclude_filepaths']) ? '' : $settings->filters['exclude_filepaths']; + foreach ($this->get_excluded_paths($excluded_paths) as $path) { + $exclude[] = '--exclude=' . escapeshellarg($path); + } + $exclude = implode(' ', $exclude); + + // Create a symlink in a temp directory so we can rename the file in the archive. + $temp = backup_migrate_temp_directory(); + + $manifest = $this->generate_manifest(); + $db = $this->get_db(); + rename($db, $temp . '/database.sql'); + rename($manifest, $temp . '/MANIFEST.ini'); + + $file->push_type('sitearchive'); + $link = $temp . '/docroot'; + $input = realpath($this->get_location()); + backup_migrate_exec("ln -s %input %link; tar --dereference -C %temp -rf %output $exclude .", array('%output' => $file->filepath(), '%input' => $input, '%temp' => $temp, '%link' => $link)); + + return $file; + } + return FALSE; + } + + /** + * Generate a manifest file. + */ + function generate_manifest() { + $info = array( + 'Global' => array( + 'datestamp' => time(), + 'formatversion' => '2011-07-02', + 'generator' => 'Backup and Migrate (http://drupal.org/project/backup_migrate)', + 'generatorversion' => BACKUP_MIGRATE_VERSION, + ), + 'Site 0' => array( + 'version' => VERSION, + 'name' => variable_get('site_name', ''), + 'docroot' => 'docroot', + 'sitedir' => 'docroot/' . conf_path(), + 'database-file-default' => 'database.sql', + 'database-file-driver' => 'mysql', + ), + ); + if ($private = variable_get('file_private_path', FALSE)) { + $info['Site 0']['files-private'] = 'docroot/' . $private; + } + $info['Site 0']['files-public'] = 'docroot/' . variable_get('file_public_path', FALSE); + $ini = $this->_array_to_ini($info); + + $tmpdir = backup_migrate_temp_directory(); + $filepath = $tmpdir . '/MANIFEST.ini'; + file_put_contents($filepath, $ini); + return $filepath; + } + + /** + * Get a database dump to add to the archive. + */ + function get_db() { + backup_migrate_include('destinations', 'files', 'filters', 'profiles'); + + $file = new backup_file(); + // Clone the default settings so we can make changes without them leaking out of this function. + $settings = clone _backup_migrate_profile_saved_default_profile(); + $settings->source_id = 'db'; + $settings->filters['compression'] = 'none'; + + // Execute the backup on the db with the default settings. + $file = backup_migrate_filters_backup($file, $settings); + + // Generate a tmp file with the correct final title (because ArchiveTar doesn't seem to allow renaming). + $tmpdir = backup_migrate_temp_directory(); + $filepath = $tmpdir . '/database.sql'; + rename($file->filepath(), $filepath); + + return $filepath; + } + + /** + * Restore to this source. + */ + function _restore_from_file_php($file, &$settings) { + $success = false; + if ($this->check_libs()) { + $from = $file->pop_type(); + $temp = backup_migrate_temp_directory(); + + $tar = new Archive_Tar($from->filepath()); + $tar->extractModify($temp, $file->name); + + // Parse the manifest + $manifest = $this->read_manifest($temp); + + // Currently only the first site in the archive is supported. + $site = $manifest['Site 0']; + + $docroot = $temp . '/' . $site['docroot']; + $sqlfile = $temp . '/' . $site['database-file-default']; + $filepath = NULL; + if (isset($site['files-private'])) { + $filepath = $temp . '/' . $site['files-private']; + } + else if (isset($site['files-public'])) { + $filepath = $temp . '/' . $site['files-public']; + } + + // Move the files from the temp directory. + if ($filepath && file_exists($filepath)) { + _backup_migrate_move_files($filepath, file_directory_path()); + } + else { + _backup_migrate_message('Files were not restored because the archive did not seem to contain a files directory or was in a format that Backup and Migrate couldn\'t read', array(), 'warning'); + } + + // Restore the sql db. + if ($sqlfile && file_exists($sqlfile)) { + $db_settings = clone($settings); + $db_settings->source_id = 'db'; + $file = new backup_file(array('filepath' => $sqlfile)); + $success = backup_migrate_filters_restore($file, $db_settings); + } + else { + _backup_migrate_message('The database was not restored because the archive did not seem to contain a database backup or was in a format that Backup and Migrate couldn\'t read', array(), 'warning'); + } + + if ($docroot) { + _backup_migrate_message('Backup and Migrate cannot restore the php code of the site for security reasons. You will have to copy the code to the server by hand if you wish to restore the full site.', array(), 'warning'); + } + + return $success && $file; + } + return FALSE; + } + + /** + * Restore to this source. + */ + function _restore_from_file_cli($file, &$settings) { + // @TODO: implement the cli version of the restore. + return FALSE; + } + + /** + * Generate a manifest file. + */ + function read_manifest($directory) { + // Assume some defaults if values ore the manifest is missing. + $defaults = array( + 'docroot' => 'docroot', + 'database-file-default' => 'database.sql', + 'database-file-driver' => 'mysql', + ); + + $out = $this->_ini_to_array($directory . '/MANIFEST.ini'); + + // Set the defaults. + $out['Site 0'] = isset($out['Site 0']) ? $out['Site 0'] : array(); + $out['Site 0'] += $defaults; + + return $out; + } + + /** + * Convert an associated array to an ini format string. Only allows 2 levels of depth to allow parse_ini_file to parse. + */ + function _array_to_ini($sections) { + $content = ""; + foreach ($sections as $section => $data) { + $content .= '['. $section .']' . "\n"; + foreach ($data as $key => $val) { + $content .= $key . " = \"". $val ."\"\n"; + } + $content .= "\n"; + } + return $content; + } + + /** + * Convert an associated array to an ini format string. Only allows 2 levels of depth to allow parse_ini_file to parse. + */ + function _ini_to_array($path) { + return parse_ini_file($path, TRUE); + } +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/sources.db.inc b/sites/all/modules/contrib/backup_migrate/includes/sources.db.inc new file mode 100644 index 0000000..8bd75c3 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/sources.db.inc @@ -0,0 +1,318 @@ +set_source($this->get_id()); + + // Restore the file to the source database. + $file = backup_migrate_perform_restore($this->get_id(), $file, $settings); + + return $file; + } + + /** + * Destination configuration callback. + */ + function edit_form() { + $form = parent::edit_form(); + $form['scheme']['#default_value'] = $this->default_scheme(); + $form['scheme']['#access'] = FALSE; + $form['path']['#title'] = t('Database name'); + $form['path']['#description'] = t('The name of the database. The database must exist, it will not be created for you.'); + $form['user']['#description'] = t('Enter the name of a user who has write access to the database.'); + return $form; + } + + /** + * Validate the configuration form. Make sure the db info is valid. + */ + function edit_form_validate($form, &$form_state) { + if (!preg_match('/[a-zA-Z0-9_\$]+/', $form_state['values']['path'])) { + form_set_error('path', t('The database name is not valid.')); + } + parent::edit_form_validate($form, $form_state); + } + + /** + * Get the form for the settings for this destination. + * + * Return the default tables whose data can be ignored. These tables mostly contain + * info which can be easily reproducted (such as cache or search index) + * but also tables which can become quite bloated but are not necessarily extremely + * important to back up or migrate during development (such ass access log and watchdog) + */ + function backup_settings_default() { + $core = array( + 'cache', + 'cache_admin_menu', + 'cache_browscap', + 'cache_content', + 'cache_filter', + 'cache_calendar_ical', + 'cache_location', + 'cache_menu', + 'cache_page', + 'cache_reptag', + 'cache_views', + 'cache_views_data', + 'cache_block', + 'cache_update', + 'cache_form', + 'cache_bootstrap', + 'cache_field', + 'cache_image', + 'cache_path', + 'sessions', + 'search_dataset', + 'search_index', + 'search_keywords_log', + 'search_total', + 'watchdog', + 'accesslog', + 'devel_queries', + 'devel_times', + ); + $nodata_tables = array_merge($core, module_invoke_all('devel_caches')); + return array( + 'nodata_tables' => $nodata_tables, + 'exclude_tables' => array(), + 'utils_lock_tables' => FALSE, + ); + } + + /** + * Get the form for the backup settings for this destination. + */ + function backup_settings_form($settings) { + $objects = $this->get_object_names(); + $form['#description'] = t("You may omit specific tables, or specific table data from the backup file. Only omit data that you know you will not need such as cache data, or tables from other applications. Excluding tables can break your Drupal install, so do not change these settings unless you know what you're doing."); + $form['exclude_tables'] = array( + "#type" => "select", + "#multiple" => TRUE, + "#title" => t("Exclude the following tables altogether"), + "#options" => $objects, + "#default_value" => $settings['exclude_tables'], + "#description" => t("The selected tables will not be added to the backup file."), + ); + $form['nodata_tables'] = array( + "#type" => "select", + "#multiple" => TRUE, + "#title" => t("Exclude the data from the following tables"), + "#options" => $objects, + "#default_value" => $settings['nodata_tables'], + "#description" => t("The selected tables will have their structure backed up but not their contents. This is useful for excluding cache data to reduce file size."), + ); + $form['utils_lock_tables'] = array( + '#type' => 'checkbox', + '#title' => t('Lock tables during backup'), + '#default_value' => !empty($settings['utils_lock_tables']) ? $settings['utils_lock_tables'] : NULL, + '#description' => t('This can help reduce data corruption, but will make your site unresponsive.'), + ); + return $form; + } + + /** + * Backup from this source. + */ + function backup_to_file($file, $settings) { + $file->push_type($this->get_file_type_id()); + + //$this->lock_tables($settings); + + // Switch to a different db if specified. + $success = $this->_backup_db_to_file($file, $settings); + + //$this->unlock_tables($settings); + + return $success ? $file : FALSE; + } + + /** + * Restore to this source. + */ + function restore_from_file($file, &$settings) { + $num = 0; + $type = $this->get_file_type_id(); + // Open the file using the file wrapper. Check that the dump is of the right type (allow .sql for legacy reasons). + if ($file->type_id() !== $this->get_file_type_id() && $file->type_id() !== 'sql') { + _backup_migrate_message("Unable to restore from file %file because a %type file can't be restored to this database.", array("%file" => $file->filepath(), '%type' => $file->type_id()), 'error'); + } + else { + backup_migrate_filters_invoke_all('pre_restore', $file, $settings); + + // Restore the database. + $num = $this->_restore_db_from_file($file, $settings); + $settings->performed_action = $num ? t('%num SQL commands executed.', array('%num' => $num)) : ''; + + backup_migrate_filters_invoke_all('post_restore', $file, $settings, $num); + } + return $num; + } + + /** + * Get the db connection for the specified db. + */ + function _get_db_connection() { + if (!$this->connection) { + $target = $key = ''; + $parts = explode(':', $this->get_id()); + // One of the predefined databases (set in settings.php) + if ($parts[0] == 'db') { + $key = empty($parts[1]) ? 'default' : $parts[1]; + $target = empty($parts[2]) ? 'default' : $parts[2]; + } + // Another db url. + else { + // If the url is specified build it into a connection info array. + if (!empty($this->dest_url)) { + $info = array( + 'driver' => empty($this->dest_url['scheme']) ? NULL : $this->dest_url['scheme'], + 'host' => empty($this->dest_url['host']) ? NULL : $this->dest_url['host'], + 'port' => empty($this->dest_url['port']) ? NULL : $this->dest_url['port'], + 'username' => empty($this->dest_url['user']) ? NULL : $this->dest_url['user'], + 'password' => empty($this->dest_url['pass']) ? NULL : $this->dest_url['pass'], + 'database' => empty($this->dest_url['path']) ? NULL : $this->dest_url['path'], + ); + $key = uniqid('backup_migrate_tmp_'); + $target = 'default'; + Database::addConnectionInfo($key, $target, $info); + } + // No database selected. Assume the default. + else { + $key = $target = 'default'; + } + } + if ($target && $key) { + $this->connection = Database::getConnection($target, $key); + } + } + return $this->connection; + } + + /** + * Backup the databases to a file. + */ + function _backup_db_to_file($file, $settings) { + // Must be overridden. + } + + /** + * Backup the databases to a file. + */ + function _restore_db_from_file($file, $settings) { + // Must be overridden. + } + + /** + * Get a list of objects in the database. + */ + function get_object_names() { + // Must be overridden. + $out = $this->_get_table_names(); + if (method_exists($this, '_get_view_names')) { + $out += $this->_get_view_names(); + } + return $out; + } + + /** + * Get a list of tables in the database. + */ + function get_table_names() { + // Must be overridden. + $out = $this->_get_table_names(); + return $out; + } + + /** + * Get a list of tables in the database. + */ + function _get_table_names() { + // Must be overridden. + return array(); + } + + /** + * Lock the database in anticipation of a backup. + */ + function lock_tables($settings) { + if ($settings->filters['utils_lock_tables']) { + $tables = array(); + foreach ($this->get_table_names() as $table) { + // There's no need to lock excluded or structure only tables because it doesn't matter if they change. + if (empty($settings->filters['exclude_tables']) || !in_array($table, (array)$settings->filters['exclude_tables'])) { + $tables[] = $table; + } + } + $this->_lock_tables($tables); + } + } + + /** + * Lock the list of given tables in the database. + */ + function _lock_tables($tables) { + // Must be overridden. + } + + /** + * Unlock any tables that have been locked. + */ + function unlock_tables($settings) { + if ($settings->filters['utils_lock_tables']) { + $this->_unlock_tables(); + } + } + + /** + * Unlock the list of given tables in the database. + */ + function _unlock_tables($tables) { + // Must be overridden. + } + + /** + * Get the file type for to backup this destination to. + */ + function get_file_type_id() { + return 'sql'; + } + + /** + * Get the version info for the given DB. + */ + function _db_info() { + return array( + 'type' => FALSE, + 'version' => t('Unknown'), + ); + } + +} diff --git a/sites/all/modules/contrib/backup_migrate/includes/sources.db.mysql.inc b/sites/all/modules/contrib/backup_migrate/includes/sources.db.mysql.inc new file mode 100644 index 0000000..49ded37 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/sources.db.mysql.inc @@ -0,0 +1,497 @@ + array( + "extension" => "sql", + "filemime" => "text/x-sql", + "backup" => TRUE, + "restore" => TRUE, + ), + "mysql" => array( + "extension" => "mysql", + "filemime" => "text/x-sql", + "backup" => TRUE, + "restore" => TRUE, + ), + ); + } + + /** + * Return the scheme for this db type. + */ + function default_scheme() { + return 'mysql'; + } + + + /** + * Declare any mysql databases defined in the settings.php file as a possible source. + */ + function sources() { + $out = array(); + global $databases; + foreach ((array)$databases as $db_key => $target) { + foreach ((array)$target as $tgt_key => $info) { + // Only mysql/mysqli supported by this source. + $key = $db_key . ':' . $tgt_key; + if ($info['driver'] === 'mysql') { + $url = $info['driver'] . '://' . $info['username'] . ':' . $info['password'] . '@' . $info['host'] . (isset($info['port']) ? ':' . $info['port'] : '') . '/' . $info['database']; + if ($source = backup_migrate_create_destination('mysql', array('url' => $url))) { + // Treat the default database differently because it is probably the only one available. + if ($key == 'default:default') { + $source->set_id('db'); + $source->set_name(t('Default Database')); + // Dissalow backing up to the default database because that's confusing and potentially dangerous. + $source->remove_op('scheduled backup'); + $source->remove_op('manual backup'); + } + else { + $source->set_id('db:'. $key); + $source->set_name($key .": ". $source->get_display_location()); + } + $out[$source->get_id()] = $source; + } + } + } + } + return $out; + } + + /** + * Get the file type for to backup this source to. + */ + function get_file_type_id() { + return 'mysql'; + } + + /** + * Backup the databases to a file. + * + * Returns a list of sql commands, one command per line. + * That makes it easier to import without loading the whole file into memory. + * The files are a little harder to read, but human-readability is not a priority + */ + function _backup_db_to_file($file, $settings) { + if (!empty($settings->filters['use_cli']) && $this->_backup_db_to_file_mysqldump($file, $settings)) { + return TRUE; + } + + $lines = 0; + $exclude = !empty($settings->filters['exclude_tables']) ? $settings->filters['exclude_tables'] : array(); + $nodata = !empty($settings->filters['nodata_tables']) ? $settings->filters['nodata_tables'] : array(); + if ($file->open(TRUE)) { + $file->write($this->_get_sql_file_header()); + $alltables = $this->_get_tables(); + $allviews = $this->_get_views(); + + foreach ($alltables as $table) { + if (_backup_migrate_check_timeout()) { + return FALSE; + } + if ($table['name'] && !isset($exclude[$table['name']])) { + $file->write($this->_get_table_structure_sql($table)); + $lines++; + if (!in_array($table['name'], $nodata)) { + $lines += $this->_dump_table_data_sql_to_file($file, $table); + } + } + } + foreach ($allviews as $view) { + if (_backup_migrate_check_timeout()) { + return FALSE; + } + if ($view['name'] && !isset($exclude[$view['name']])) { + $file->write($this->_get_view_create_sql($view)); + } + } + $file->write($this->_get_sql_file_footer()); + $file->close(); + return $lines; + } + else { + return FALSE; + } + } + + + /** + * Backup the databases to a file using the mysqldump command. + */ + function _backup_db_to_file_mysqldump($file, $settings) { + $success = FALSE; + $nodata_tables = array(); + $alltables = $this->_get_tables(); + + + $command = 'mysqldump --result-file=%file --opt -Q --host=%host --port=%port --user=%user --password=%pass %db'; + $args = array( + '%file' => $file->filepath(), + '%host' => $this->dest_url['host'], + '%port' => isset($this->dest_url['port']) ? $this->dest_url['port'] : '3306', + '%user' => $this->dest_url['user'], + '%pass' => $this->dest_url['pass'], + '%db' => $this->dest_url['path'], + ); + + // Ignore the excluded and no-data tables. + if (!empty($settings->filters['exclude_tables'])) { + $db = $this->dest_url['path']; + foreach ((array)$settings->filters['exclude_tables'] as $table) { + if (isset($alltables[$table])) { + $command .= ' --ignore-table='. $db .'.'. $table; + } + } + foreach ((array)$settings->filters['nodata_tables'] as $table) { + if (isset($alltables[$table])) { + $nodata_tables[] = $table; + $command .= ' --ignore-table='. $db .'.'. $table; + } + } + } + $success = backup_migrate_exec($command, $args); + + // Get the nodata tables. + if ($success && !empty($nodata_tables)) { + $tables = implode(' ', array_unique($nodata_tables)); + $command = "mysqldump --no-data --opt -Q --host=%host --port=%port --user=%user --password=%pass %db $tables >> %file"; + $success = backup_migrate_exec($command, $args); + } + return $success; + } + + /** + * Backup the databases to a file. + */ + function _restore_db_from_file($file, $settings) { + $num = 0; + + if ($file->open() && $conn = $this->_get_db_connection()) { + // Read one line at a time and run the query. + while ($line = $this->_read_sql_command_from_file($file)) { + if (_backup_migrate_check_timeout()) { + return FALSE; + } + if ($line) { + // Prepeare and exexute the statement instead of the api function to avoid substitution of '{' etc. + $stmt = $conn->prepare($line); + $stmt->execute(); + $num++; + } + } + // Close the file with fclose/gzclose. + $file->close(); + } + else { + drupal_set_message(t("Unable to open file %file to restore database", array("%file" => $file->filepath())), 'error'); + $num = FALSE; + } + return $num; + } + + + /** + * Read a multiline sql command from a file. + * + * Supports the formatting created by mysqldump, but won't handle multiline comments. + */ + function _read_sql_command_from_file($file) { + $out = ''; + while ($line = $file->read()) { + $first2 = substr($line, 0, 2); + $first3 = substr($line, 0, 2); + + // Ignore single line comments. This function doesn't support multiline comments or inline comments. + if ($first2 != '--' && ($first2 != '/*' || $first3 == '/*!')) { + $out .= ' ' . trim($line); + // If a line ends in ; or */ it is a sql command. + if (substr($out, strlen($out) - 1, 1) == ';') { + return trim($out); + } + } + } + return trim($out); + } + + /** + * Get a list of tables in the database. + */ + function _get_table_names() { + $out = array(); + foreach ($this->_get_tables() as $table) { + $out[$table['name']] = $table['name']; + } + return $out; + } + + /** + * Get a list of views in the database. + */ + function _get_view_names() { + $out = array(); + foreach ($this->_get_views() as $view) { + $out[$view['name']] = $view['name']; + } + return $out; + } + + /** + * Lock the list of given tables in the database. + */ + function _lock_tables($tables) { + if ($tables) { + $tables_escaped = array(); + foreach ($tables as $table) { + $tables_escaped[] = '`'. db_escape_table($table) .'` WRITE'; + } + $this->query('LOCK TABLES '. implode(', ', $tables_escaped)); + } + } + + /** + * Unlock all tables in the database. + */ + function _unlock_tables($settings) { + $this->query('UNLOCK TABLES'); + } + + /** + * Get a list of tables in the db. + */ + function _get_tables() { + $out = array(); + // get auto_increment values and names of all tables + $tables = $this->query("show table status", array(), array('fetch' => PDO::FETCH_ASSOC)); + foreach ($tables as $table) { + // Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed. + // See: http://drupal.org/node/1171866 + $table = array_change_key_case($table); + if (!empty($table['engine'])) { + $out[$table['name']] = $table; + } + } + return $out; + } + + /** + * Get a list of views in the db. + */ + function _get_views() { + $out = array(); + // get auto_increment values and names of all tables + $tables = $this->query("show table status", array(), array('fetch' => PDO::FETCH_ASSOC)); + foreach ($tables as $table) { + // Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed. + // See: http://drupal.org/node/1171866 + $table = array_change_key_case($table); + if (empty($table['engine'])) { + $out[$table['name']] = $table; + } + } + return $out; + } + + /** + * Get the sql for the structure of the given view. + */ + function _get_view_create_sql($view) { + $out = ""; + // Switch SQL mode to get rid of "CREATE ALGORITHM..." what requires more permissions + troubles with the DEFINER user + $sql_mode = $this->query("SELECT @@SESSION.sql_mode")->fetchField(); + $this->query("SET sql_mode = 'ANSI'"); + $result = $this->query("SHOW CREATE VIEW `" . $view['name'] . "`", array(), array('fetch' => PDO::FETCH_ASSOC)); + $this->query("SET SQL_mode = :mode", array(':mode' => $sql_mode)); + foreach ($result as $create) { + // Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed. + // See: http://drupal.org/node/1171866 + $create = array_change_key_case($create); + $out .= "DROP VIEW IF EXISTS `". $view['name'] ."`;\n"; + $out .= "SET sql_mode = 'ANSI';\n"; + $out .= strtr($create['create view'], "\n", " ") . ";\n"; + $out .= "SET sql_mode = '$sql_mode';\n"; + } + return $out; + } + + + + /** + * Get the sql for the structure of the given table. + */ + function _get_table_structure_sql($table) { + $out = ""; + $result = $this->query("SHOW CREATE TABLE `". $table['name'] ."`", array(), array('fetch' => PDO::FETCH_ASSOC)); + foreach ($result as $create) { + // Lowercase the keys because between Drupal 7.12 and 7.13/14 the default query behavior was changed. + // See: http://drupal.org/node/1171866 + $create = array_change_key_case($create); + $out .= "DROP TABLE IF EXISTS `". $table['name'] ."`;\n"; + // Remove newlines and convert " to ` because PDO seems to convert those for some reason. + $out .= strtr($create['create table'], array("\n" => ' ', '"' => '`')); + if ($table['auto_increment']) { + $out .= " AUTO_INCREMENT=". $table['auto_increment']; + } + $out .= ";\n"; + } + return $out; + } + + /** + * Get the sql to insert the data for a given table + */ + function _dump_table_data_sql_to_file($file, $table) { + $rows_per_line = variable_get('backup_migrate_data_rows_per_line', 30); + $bytes_per_line = variable_get('backup_migrate_data_bytes_per_line', 2000); + + $lines = 0; + $data = $this->query("SELECT * FROM `". $table['name'] ."`", array(), array('fetch' => PDO::FETCH_ASSOC)); + $rows = $bytes = 0; + + // Escape backslashes, PHP code, special chars + $search = array('\\', "'", "\x00", "\x0a", "\x0d", "\x1a"); + $replace = array('\\\\', "''", '\0', '\n', '\r', '\Z'); + + $line = array(); + foreach ($data as $row) { + // DB Escape the values. + $items = array(); + foreach ($row as $key => $value) { + $items[] = is_null($value) ? "null" : "'". str_replace($search, $replace, $value) ."'"; + } + + // If there is a row to be added. + if ($items) { + // Start a new line if we need to. + if ($rows == 0) { + $file->write("INSERT INTO `". $table['name'] ."` VALUES "); + $bytes = $rows = 0; + } + // Otherwise add a comma to end the previous entry. + else { + $file->write(","); + } + + // Write the data itself. + $sql = implode(',', $items); + $file->write('('. $sql .')'); + $bytes += strlen($sql); + $rows++; + + // Finish the last line if we've added enough items + if ($rows >= $rows_per_line || $bytes >= $bytes_per_line) { + $file->write(";\n"); + $lines++; + $bytes = $rows = 0; + } + } + } + // Finish any unfinished insert statements. + if ($rows > 0) { + $file->write(";\n"); + $lines++; + } + + return $lines; + } + + /** + * Get the db connection for the specified db. + */ + function _get_db_connection() { + if (!$this->connection) { + $this->connection = parent::_get_db_connection(); + // Set the sql mode because the default is ANSI,TRADITIONAL which is not aware of collation or storage engine. + $this->connection->exec("SET sql_mode=''"); + } + return $this->connection; + } + + /** + * Run a db query on this destination's db. + */ + function query($query, $args = array(), $options = array()) { + if ($conn = $this->_get_db_connection()) { + return $conn->query($query, $args, $options); + } + } + + /** + * The header for the top of the sql dump file. These commands set the connection + * character encoding to help prevent encoding conversion issues. + */ + function _get_sql_file_header() { + $info = $this->_db_info(); + + return "-- Backup and Migrate (Drupal) MySQL Dump +-- Backup and Migrate Version: ". BACKUP_MIGRATE_VERSION ." +-- http://drupal.org/project/backup_migrate +-- Drupal Version: " . VERSION . " +-- http://drupal.org/ +-- +-- Host: " . url('', array('absolute' => TRUE)) . " +-- Site Name: " . url('', array('absolute' => TRUE)) . " +-- Generation Time: " . format_date(time(), 'custom', 'r') . " +-- MySQL Version: " . $info['version'] . " + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=NO_AUTO_VALUE_ON_ZERO */; + +SET SQL_MODE=\"NO_AUTO_VALUE_ON_ZERO\"; +SET NAMES utf8; + +"; + } + + /** + * The footer of the sql dump file. + */ + function _get_sql_file_footer() { + return " + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +"; + } + + /** + * Get the version info for the given DB. + */ + function _db_info() { + $db = $this->_get_db_connection(); + return array( + 'type' => 'mysql', + 'version' => $db ? $db->version() : t('Unknown'), + ); + } + +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/sources.filesource.inc b/sites/all/modules/contrib/backup_migrate/includes/sources.filesource.inc new file mode 100644 index 0000000..b031e76 --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/sources.filesource.inc @@ -0,0 +1,314 @@ + 'files', 'location' => 'public://', 'name' => t('Public Files Directory'), 'show_in_list' => FALSE)); + if (variable_get('file_private_path', FALSE)) { + $out['files_private'] = backup_migrate_create_destination('filesource', array('machine_name' => 'files', 'location' => 'private://', 'name' => t('Private Files Directory'), 'show_in_list' => FALSE)); + } + return $out; + } + + /** + * Get the form for the settings for the files destination. + */ + function edit_form() { + $form = parent::edit_form(); + $form['location'] = array( + "#type" => "textfield", + "#title" => t("Directory path"), + "#default_value" => $this->get_location(), + "#required" => TRUE, + "#description" => t('Enter the path to the directory to save the backups to. Use a relative path to pick a path relative to your Drupal root directory. The web server must be able to write to this path.'), + ); + return $form; + } + + /** + * Return a list of backup filetypes. + */ + function file_types() { + return array( + "tar" => array( + "extension" => "tar", + "filemime" => "application/x-tar", + "backup" => TRUE, + "restore" => TRUE, + ), + ); + } + + /** + * Get the form for the settings for this destination. + * + * Return the default directories whose data can be ignored. These directories contain + * info which can be easily reproducted. Also exclude the backup and migrate folder + * to prevent exponential bloat. + */ + function backup_settings_default() { + return array( + 'exclude_filepaths' => "backup_migrate\nstyles\ncss\njs\nctools\nless", + ); + } + + /** + * Get the form for the backup settings for this destination. + */ + function backup_settings_form($settings) { + $form['exclude_filepaths'] = array( + "#type" => "textarea", + "#multiple" => TRUE, + "#title" => t("Exclude the following files or directories"), + "#default_value" => $settings['exclude_filepaths'], + "#description" => t("A list of files or directories to be excluded from backups. Add one path per line relative to the directory being backed up."), + ); + return $form; + } + + /** + * Backup from this source. + */ + function backup_to_file($file, $settings) { + if ($out = $this->_backup_to_file_cli($file, $settings)) { + return $out; + } + else { + return $this->_backup_to_file_php($file, $settings); + } + } + + /** + * Backup from this source. + */ + function _backup_to_file_php($file, $settings) { + if ($this->check_libs()) { + $excluded = $this->get_excluded_paths($settings); + $files = $this->get_files_to_backup($this->get_realpath(), $settings, $excluded); + if ($files) { + $file->push_type('tar'); + $gz = new Archive_Tar($file->filepath(), false); + $gz->addModify($files, '', $this->get_realpath()); + return $file; + } + backup_migrate_backup_fail('No files available.', array(), $settings); + return FALSE; + } + return FALSE; + } + + /** + * Backup from this source. + */ + function _backup_to_file_cli($file, $settings) { + if (!empty($settings->filters['use_cli']) && function_exists('backup_migrate_exec') && function_exists('escapeshellarg')) { + $excluded = $this->get_excluded_paths($settings); + $exclude = array(); + foreach ($excluded as $path) { + $exclude[] = '--exclude=' . escapeshellarg($path); + } + $exclude = implode(' ', $exclude); + + // Create a symlink in a temp directory so we can rename the file in the archive. + $temp = backup_migrate_temp_directory(); + + $file->push_type('tar'); + backup_migrate_exec("tar --dereference -C %input -rf %output $exclude .", array('%output' => $file->filepath(), '%input' => $this->get_realpath(), '%temp' => $temp)); + return $file; + } + return FALSE; + } + + /** + * Restore to this source. + */ + function restore_from_file($file, &$settings) { + if ($out = $this->_restore_from_file_cli($file, $settings)) { + return $out; + } + else { + return $this->_restore_from_file_php($file, $settings); + } + } + + /** + * Restore to this source. + */ + function _restore_from_file_php($file, &$settings) { + if ($this->check_libs()) { + $from = $file->pop_type(); + $temp = backup_migrate_temp_directory(); + + $tar = new Archive_Tar($from->filepath()); + $tar->extractModify($temp, $file->name); + + // Older B&M Files format included a base 'files' directory. + if (file_exists($temp .'/files')) { + $temp = $temp . '/files'; + } + if (file_exists($temp .'/'. $file->name .'/files')) { + $temp = $temp . '/files'; + } + + // Move the files from the temp directory. + _backup_migrate_move_files($temp, $this->get_realpath()); + + return $file; + } + return FALSE; + } + + /** + * Restore to this source. + */ + function _restore_from_file_cli($file, &$settings) { + if (!empty($settings->filters['use_cli']) && function_exists('backup_migrate_exec')) { + $temp = backup_migrate_temp_directory(); + backup_migrate_exec("tar -C %temp -xf %input", array('%input' => $file->filepath(), '%temp' => $temp)); + + // Older B&M Files format included a base 'files' directory. + if (file_exists($temp .'/files')) { + $temp = $temp . '/files'; + } + if (file_exists($temp .'/'. $file->name .'/files')) { + $temp = $temp . '/files'; + } + + // Move the files from the temp directory. + backup_migrate_exec("mv -rf %temp/* %output", array('%output' => $this->get_realpath(), '%temp' => $temp)); + return $file; + } + return FALSE; + } + + /** + * Get a list of files to backup from the given set if dirs. Exclude any that match the array $exclude. + */ + function get_files_to_backup($dir, $settings, $exclude = array()) { + $out = $errors = array(); + + if (!file_exists($dir)) { + backup_migrate_backup_fail('Directory %dir does not exist.', array('%dir' => $dir), $settings); + return FALSE; + } + if ($handle = @opendir($dir)) { + while (($file = readdir($handle)) !== FALSE) { + if ($file != '.' && $file != '..' && !in_array($file, $exclude)) { + $real = realpath($dir . '/' . $file); + // If the path is not excluded. + if (!in_array($real, $exclude)) { + if (is_dir($real)) { + $subdir = $this->get_files_to_backup($real, $settings, $exclude); + // If there was an error reading the subdirectory then abort the backup. + if ($subdir === FALSE) { + closedir($handle); + return FALSE; + } + // If the directory is empty, add an empty directory. + if (count($subdir) == 0) { + $out[] = $real; + } + $out = array_merge($out, $subdir); + } + else { + if (is_readable($real)) { + $out[] = $real; + } + else { + $errors[] = $dir . '/' . $file; + } + } + } + } + } + closedir($handle); + } + else { + backup_migrate_backup_fail('Could not open directory %dir', array('%dir' => $dir), $settings); + return FALSE; + } + + // Alert the user to any errors there might have been. + if ($errors) { + if (count($errors < 5)) { + $filesmsg = t('The following files: !files', array('!files' => theme('item_list', array('items' => $errors)))); + } + else { + $filesmsg = t('!count files', array('!count' => count($errors))); + } + + if (empty($settings->filters['ignore_errors'])) { + backup_migrate_backup_fail('The backup could not be completed because !files could not be read. If you want to skip unreadable files use the \'Ignore Errors\' setting under \'Advanced Options\' in \'Advanced Backup\' or in your schedule settings profile.', array('!files' => $filesmsg), 'error'); + $out = FALSE; + } + else { + backup_migrate_backup_fail('!files could not be read and were skipped', array('!files' => $filesmsg), 'error'); + } + } + + return $out; + } + + /** + * Break the excpluded paths string into a usable list of paths. + */ + function get_excluded_paths($settings) { + $base_dir = $this->get_realpath() . '/'; + $paths = empty($settings->filters['exclude_filepaths']) ? '' : $settings->filters['exclude_filepaths']; + $out = explode("\n", $paths); + foreach ($out as $key => $val) { + $path = trim($val, "/ \t\r\n"); + // If the path specified is a stream url or absolute path add the normalized version. + if ($real = drupal_realpath($path)) { + $out[$key] = $real; + } + // If the path is a relative path add it. + else if ($real = drupal_realpath($base_dir . $path)) { + $out[$key] = $real; + } + // Otherwise add it as is even though it probably won't match any files. + else { + $out[$key] = $path; + } + } + return $out; + } + + /** + * Check that the required libraries are installed. + */ + function check_libs() { + $result = true; + // Drupal 7 has Archive_Tar built in so there should be no need to include anything here. + return $result; + } + + /** + * Get the file location. + */ + function get_realpath() { + return drupal_realpath($this->get_location()); + } +} + diff --git a/sites/all/modules/contrib/backup_migrate/includes/sources.inc b/sites/all/modules/contrib/backup_migrate/includes/sources.inc new file mode 100644 index 0000000..6c650fa --- /dev/null +++ b/sites/all/modules/contrib/backup_migrate/includes/sources.inc @@ -0,0 +1,268 @@ + array( + 'type_name' => t('Database'), + 'description' => t('Import the backup directly into another database. Database sources can also be used as a source to backup from.'), + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/sources.db.inc', + 'class' => 'backup_migrate_source_db', + 'can_create' => FALSE, + ), + 'mysql' => array( + 'type_name' => t('MySQL Database'), + 'description' => t('Import the backup directly into another MySQL database. Database sources can also be used as a source to backup from.'), + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/sources.db.mysql.inc', + 'class' => 'backup_migrate_source_db_mysql', + 'can_create' => TRUE, + ), + 'filesource' => array( + 'description' => t('A files directory which can be backed up from.'), + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/sources.filesource.inc', + 'class' => 'backup_migrate_destination_filesource', + 'type_name' => t('File Directory'), + 'can_create' => TRUE, + ), + 'archive' => array( + 'description' => t('Create an archive of your entire site.'), + 'file' => drupal_get_path('module', 'backup_migrate') .'/includes/sources.archivesource.inc', + 'class' => 'backup_migrate_files_destination_archivesource', + 'type_name' => t('Site Archive'), + 'can_create' => FALSE, + ), + ); + + return $out; +} + +/** + * Implementation of hook_backup_migrate_sources(). + * + * Get the built in backup sources and those in the db. + */ +function backup_migrate_backup_migrate_sources() { + $out = array(); + + // Expose the configured databases as sources. + backup_migrate_include('filters'); + $out += backup_migrate_filters_invoke_all('sources'); + + return $out; +} + +/** + * Get the source options as a form element. + */ +function _backup_migrate_get_source_form($source_id = 'db') { + backup_migrate_include('destinations'); + + $form = array(); + $sources = _backup_migrate_get_source_pulldown($source_id); + if (count($sources['#options']) > 1) { + $form['source'] = array( + "#type" => "fieldset", + "#title" => t("Backup Source"), + "#collapsible" => TRUE, + "#collapsed" => FALSE, + "#tree" => FALSE, + ); + $sources['#description'] = t("Choose the database to backup. Any database destinations you have created and any databases specified in your settings.php can be backed up."); + + $form['source']['source_id'] = $sources; + } + else { + $form = array(); + $form['source']['source_id'] = array( + "#type" => "value", + "#value" => $source_id, + ); + } + return $form; +} + +/** + * Get pulldown to select existing source options. + */ +function _backup_migrate_get_source_pulldown($source_id = NULL) { + $sources = _backup_migrate_get_source_form_item_options(); + $form = array( + "#type" => "select", + "#title" => t("Backup Source"), + "#options" => _backup_migrate_get_source_form_item_options(), + "#default_value" => $source_id, + ); + return $form; +} + +/** + * Get the location options as an options array for a form item. + */ +function _backup_migrate_get_source_form_item_options() { + $out = array(); + foreach (backup_migrate_get_sources() as $key => $location) { + $out[$key] = $location->get_name(); + } + return $out; +} + +/** + * A base class for creating sources. + */ +class backup_migrate_source extends backup_migrate_location { + var $db_table = "backup_migrate_sources"; + var $type_name = 'source'; + var $singular = 'source'; + var $plural = 'sources'; + var $title_plural = 'Sources'; + var $title_singular = 'Source'; + + /** + * This function is not supposed to be called. It is just here to help the po extractor out. + */ + function strings() { + // Help the pot extractor find these strings. + t('source'); + t('sources'); + t('Sources'); + t('Source'); + } + + /** + * Get the available location types. + */ + function location_types() { + return backup_migrate_get_source_subtypes(); + } + +} + +/** + * A base class for creating sources. + */ +class backup_migrate_source_remote extends backup_migrate_source { + /** + * The location is a URI so parse it and store the parts. + */ + function get_location() { + return $this->url(FALSE); + } + + /** + * The location to display is the url without the password. + */ + function get_display_location() { + return $this->url(TRUE); + } + + /** + * Return the location with the password. + */ + function set_location($location) { + $this->location = $location; + $this->set_url($location); + } + + /** + * source configuration callback. + */ + function edit_form() { + $form = parent::edit_form(); + $form['scheme'] = array( + "#type" => "textfield", + "#title" => t("Scheme"), + "#default_value" => @$this->dest_url['scheme'] ? $this->dest_url['scheme'] : '', + "#required" => TRUE, + "#weight" => 0, + ); + $form['host'] = array( + "#type" => "textfield", + "#title" => t("Host"), + "#default_value" => @$this->dest_url['host'] ? $this->dest_url['host'] : 'localhost', + "#required" => TRUE, + "#weight" => 10, + ); + $form['path'] = array( + "#type" => "textfield", + "#title" => t("Path"), + "#default_value" => @$this->dest_url['path'], + "#required" => TRUE, + "#weight" => 20, + ); + $form['user'] = array( + "#type" => "textfield", + "#title" => t("Username"), + "#default_value" => @$this->dest_url['user'], + "#required" => TRUE, + "#weight" => 30, + ); + $form['pass'] = array( + "#type" => "password", + "#title" => t("Password"), + "#default_value" => @$this->dest_url['pass'], + '#description' => '', + "#weight" => 40, + ); + if (@$this->dest_url['pass']) { + $form['old_password'] = array( + "#type" => "value", + "#value" => @$this->dest_url['pass'], + ); + $form['pass']["#description"] .= t(' You do not need to enter a password unless you wish to change the currently saved password.'); + } + return $form; + } + + /** + * Submit the configuration form. Glue the url together and add the old password back if a new one was not specified. + */ + function edit_form_submit($form, &$form_state) { + $form_state['values']['pass'] = $form_state['values']['pass'] ? $form_state['values']['pass'] : $form_state['values']['old_password']; + $form_state['values']['location'] = $this->glue_url($form_state['values'], FALSE); + parent::edit_form_submit($form, $form_state); + } +} + diff --git a/sites/all/modules/contrib/ctools/API.txt b/sites/all/modules/contrib/ctools/API.txt new file mode 100644 index 0000000..b698b79 --- /dev/null +++ b/sites/all/modules/contrib/ctools/API.txt @@ -0,0 +1,54 @@ +Current API Version: 2.0.8 + +Please note that the API version is an internal number and does not match release numbers. It is entirely possible that releases will not increase the API version number, and increasing this number too often would burden contrib module maintainers who need to keep up with API changes. + +This file contains a log of changes to the API. +API Version 2.0.9 +Changed import permissions to use the new 'use ctools import' permission. + +API Version 2.0.8 + Introduce ctools_class_add(). + Introduce ctools_class_remove(). + +API Version 2.0.7 + All ctools object cache database functions can now accept session_id as an optional + argument to facilitate using non-session id keys. + +API Version 2.0.6 + Introduce a hook to alter the implementors of a certain api via hook_[ctools_api_hook]_alter. + +API Version 2.0.5 + Introduce ctools_fields_get_fields_by_type(). + Add language.inc + Introduce hook_ctools_content_subtype_alter($subtype, $plugin); + +API Version 2.0.4 + Introduce ctools_form_include_file() + +API Version 2.0.3 + Introduce ctools_field_invoke_field() and ctools_field_invoke_field_default(). + +API Version 2.0.2 + Introduce ctools_export_crud_load_multiple() and 'load multiple callback' to + export schema. + +API Version 2.0.1 + Introduce ctools_export_crud_enable(), ctools_export_crud_disable() and + ctools_export_crud_set_status() and requisite changes. + Introduce 'object factory' to export schema, allowing modules to control + how the exportable objects are instantiated. + Introduce 'hook_ctools_math_expression_functions_alter'. + +API Version 2.0 + Remove the deprecated callback-based behavior of the 'defaults' property on + plugin types; array addition is now the only option. If you need more + complex logic, do it with the 'process' callback. + Introduce a global plugin type registration hook and remove the per-plugin + type magic callbacks. + Introduce $owner . '_' . $api . '_hook_name' allowing modules to use their own + API hook in place of 'hook_ctools_plugin_api'. + Introduce ctools_plugin_api_get_hook() to get the hook name above. + Introduce 'cache defaults' and 'default cache bin' keys to export.inc + +Versions prior to 2.0 have been removed from this document. See the D6 version +for that information. diff --git a/sites/all/modules/contrib/ctools/CHANGELOG.txt b/sites/all/modules/contrib/ctools/CHANGELOG.txt new file mode 100644 index 0000000..c5bd5e6 --- /dev/null +++ b/sites/all/modules/contrib/ctools/CHANGELOG.txt @@ -0,0 +1,82 @@ +Current API VERSION: 2.0. See API.txt for more information. + +ctools 7.x-1.x-dev +================== +#1008120: "New custom content" shows empty form if custom content panes module is not enabled. +#999302 by troky: Fix jump menu. Apparently this wasn't actually committed the last time it was committed. +#1065976 by tekante and David_Rothstein: Reset plugin static cache during module enable to prevent stale data from harming export ui. +#1016510 by EclipseGC: Make the taxonomy system page functional. + +ctools 7.x-1.x-alpha2 (05-Jan-2011) +=================================== + +#911396 by alex_b: Prevent notices in export UI. +#919768 by mikey_p: Allow url options to be sent to ctools_ajax_command_url(). +#358953 by cedarm: Allow term context to return lowercase, spaces to dashes versions of terms. +#931434 by EclipseGc: Argument plugin for node revision ID. +#910656: CTools AJAX sample wizard demo "domesticated" checkbox value not stored. +#922442 by EugenMayer, neclimdul and voxpelli: Make sure ctools_include can handle '' or NULL directory. +#919956 by traviss359: Correct example in wizard advanced help. +#942968: Fix taxonomy term access rule with tag term vocabs. +#840344: node add argument had crufty code causing notices. +#944462 by longhairedgit: Invalid character in regex causes rare notice. +#938778 by dereine: Fix profile content type for D7 updates. +Add detach event to modal close so that wysiwyg can detach the editor. +Variant titles showing up as blank if more than one variant on a page. +#940016: token support was not yet updated for D7. +#940446: Skip validation on back and cancel buttons in all wizards. +#954492: Redirect not always working in wizard.inc +#955348: Lack of redirect on "Update" button in Page Manager causing data loss sometimes. +#941778: Update and save button should not appear in the "Add variant" path. +#955070 by EclipseGc: Update ctools internal page tokens to work properly on content all content. +#956890 by EclipseGc: Update views_content to not use views dependency since that is gone. +#954728 by EclipseGc: Update node template page function name to not collide with new hook_node_view(). +#946534 by EclipseGc: Add support for field content on all entitities. +#952586 by EclipseGc: Fix node_author content type. +#959206: If a context is not set when rendering content, attempt to guess the context (fixes Views panes where "From context" was added but pane was never edited.) +#961654 by benshell: drupal_alter() only supports 4 arguments. +#911362 by alex_b: Facilitate plugin cache resets for tests. +#945360 by naxoc: node_tag_new() not updated to D7. +#953804 by EclipseGc: Fix node comment rendering. +#953542 by EclipseGc: Fix node rendering. +#953776 by EclipseGc: Fix node link rendering. +#954772 by EclipseGc: Fix node build mode selection in node content type. +#954762 by EclipseGc: Fix comment forbidden theme call. +#954894 by EclipseGc: Fix breadcrumb content type. +#955180 by EclipseGc: Fix page primary navigation type. +#957190 by EclipseGc: Fix page secondary navigation type. +#957194 by EclipseGc: Remove mission content type, since D7 no longer has a site mission. +#957348 by EclipseGc: Fix search form URL path. +#952586 by andypost: Use format_username for displaying unlinked usernames. +#963800 by benshell: Fix query to fetch custom block title. +#983496 by Amitaibu: Fix term argument to use proper load function. +#989484 by Amitaibu: Fix notice in views plugin. +#982496: Fix token context. +#995026: Fix export UI during enable/disable which would throw notices and not properly set/unset menu items. +#998870 by Amitaibu: Fix notice when content has no icon by using function already designed for that. +#983576 by Amitaibu: Node view fallback task showed white screen. +#1004644 by pillarsdotnet: Update a missed theme() call to D7. +#1006162 by aspilicious: .info file cleanup. +#998312 by dereine: Support the expanded/hidden options that Views did for dependent.js +#955030: Remove no longer supported footer message content type. +Fix broken query in term context config. +#992022 by pcambra: Fix node autocomplete. +#946302 by BerdArt and arywyr: Fix PHP 5.3 reference error. +#980528 by das-peter: Notice fix with entity settings. +#999302 by troky: ctools_jump_menu() needed updating to new form parameters. +#964174: stylizer plugin theme delegation was in the wrong place, causing errors. +#991658 by burlap: Fully load the "user" context for the logged in user because not all fields are in $user. +#1014866 by das-peter: Smarter title panes, notice fix on access plugin descriptions. +#1015662 by troky: plugin .info files were not using correct filepaths. +#941780 by EclipseGc: Restore the "No blocks" functionality. +#951048 by EclipseGc: Tighter entity integration so that new entities are automatic contexts and relationships. +#941800 by me and aspilicious: Use Drupal 7 #machine_name automation on page manager pages and all export_ui defaults. +Disabled exportables and pages not properly greyed out. +#969208 by me and benshell: Get user_view and user profile working. +#941796: Recategorize blocks + +ctools 7.x-1.x-alpha1 +===================== + +Changelog reset for 7.x +Basic conversion done during sprint. diff --git a/sites/all/modules/contrib/ctools/LICENSE.txt b/sites/all/modules/contrib/ctools/LICENSE.txt new file mode 100644 index 0000000..d159169 --- /dev/null +++ b/sites/all/modules/contrib/ctools/LICENSE.txt @@ -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. + + + Copyright (C) + + 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. + + , 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. diff --git a/sites/all/modules/contrib/ctools/UPGRADE.txt b/sites/all/modules/contrib/ctools/UPGRADE.txt new file mode 100644 index 0000000..844ecce --- /dev/null +++ b/sites/all/modules/contrib/ctools/UPGRADE.txt @@ -0,0 +1,63 @@ +Upgrading from ctools-6.x-1.x to ctools-7.x-2.x: + + - Remove ctools_ajax_associate_url_to_element as it shouldn't be necessary + with the new AJAX api's in Drupal core. + + - All calls to the ctools_ajax_command_prepend() should be replace with + the core function ajax_command_prepend(); + This is also the case for append, insert, after, before, replace, html, + and remove commands. + Each of these commands have been incorporated into the + Drupal.ajax.prototype.commands.insert + function with a corresponding parameter specifying which method to use. + + - All calls to ctools_ajax_render() should be replaced with calls to core + ajax_render(). Note that ctools_ajax_render() printed the json object and + exited, ajax_render() gives you this responsibility. + + ctools_ajax_render() + + becomes + + print ajax_render(); + exit; + + - All calls to ctools_static*() should be replaced with corresponding calls + to drupal_static*(). + + - All calls to ctools_css_add_css should be replaced with calls to + drupal_add_css(). Note that the arguments to drupal_add_css() have changed. + + - All wizard form builder functions must now return a form array(). + + - ctools_build_form is very close to being removed. In anticipation of this, + all $form_state['wrapper callback']s must now be + $form_state['wrapper_callback']. In addition to this $form_state['args'] + must now be $form_state['build_info']['args']. + + NOTE: Previously checking to see if the return from ctools_build_form() + is empty would be enough to see if the form was submitted. This is no + longer true. Please check for $form_state['executed']. If using a wizard + check for $form_state['complete']. + + - Plugin types now must be explicitly registered via a registration hook, + hook_ctools_plugin_type(); info once provided in magically-named functions + (e.g., ctools_ctools_plugin_content_types() was the old function to + provide plugin type info for ctools' content_type plugins) now must be + provided in that global hook. See http://drupal.org/node/910538 for more + details. + + - Plugins that use 'theme arguments' now use 'theme variables' instead. + + - Context, argument and relationship plugins now use 'add form' and/or + 'edit form' rather than 'settings form'. These plugins now support + form wizards just like content plugins. These forms now all take + $form, &$form_state as arguments, and the configuration for the plugin + can be found in $form_state['conf']. + + For all these forms, the submit handler MUST put appropriate data in + $form_state['conf']. Data will no longer be stored automatically. + + For all of these forms, the separate settings #trees in the form are now + gone, so form ids may be adjusted. Also, these are now all real forms + using CTools form wizard instead of fake subforms as previously. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/bulk_export/bulk_export.css b/sites/all/modules/contrib/ctools/bulk_export/bulk_export.css new file mode 100644 index 0000000..45a172d --- /dev/null +++ b/sites/all/modules/contrib/ctools/bulk_export/bulk_export.css @@ -0,0 +1,18 @@ +.export-container { + width: 48%; + float: left; + padding: 5px 1% 0; +} +.export-container table { + width: 100%; +} +.export-container table input, +.export-container table th, +.export-container table td { + padding: 0 0 .2em .5em; + margin: 0; + vertical-align: middle; +} +.export-container .select-all { + width: 1.5em; +} diff --git a/sites/all/modules/contrib/ctools/bulk_export/bulk_export.info b/sites/all/modules/contrib/ctools/bulk_export/bulk_export.info new file mode 100644 index 0000000..dd9f3e9 --- /dev/null +++ b/sites/all/modules/contrib/ctools/bulk_export/bulk_export.info @@ -0,0 +1,14 @@ +name = Bulk Export +description = Performs bulk exporting of data objects known about by Chaos tools. +core = 7.x +dependencies[] = ctools +package = Chaos tool suite +version = CTOOLS_MODULE_VERSION + + +; Information added by Drupal.org packaging script on 2015-08-19 +version = "7.x-1.9" +core = "7.x" +project = "ctools" +datestamp = "1440020680" + diff --git a/sites/all/modules/contrib/ctools/bulk_export/bulk_export.js b/sites/all/modules/contrib/ctools/bulk_export/bulk_export.js new file mode 100644 index 0000000..a4fb3f2 --- /dev/null +++ b/sites/all/modules/contrib/ctools/bulk_export/bulk_export.js @@ -0,0 +1,29 @@ + +/** + * @file + * CTools Bulk Export javascript functions. + */ + +(function ($) { + +Drupal.behaviors.CToolsBulkExport = { + attach: function (context) { + + $('#bulk-export-export-form .vertical-tabs-pane', context).drupalSetSummary(function (context) { + + // Check if any individual checkbox is checked. + if ($('.bulk-selection input:checked', context).length > 0) { + return Drupal.t('Exportables selected'); + } + + return ''; + }); + + // Special bind click on the select-all checkbox. + $('.select-all').bind('click', function(context) { + $(this, '.vertical-tabs-pane').drupalSetSummary(context); + }); + } +}; + +})(jQuery); diff --git a/sites/all/modules/contrib/ctools/bulk_export/bulk_export.module b/sites/all/modules/contrib/ctools/bulk_export/bulk_export.module new file mode 100644 index 0000000..afb15b9 --- /dev/null +++ b/sites/all/modules/contrib/ctools/bulk_export/bulk_export.module @@ -0,0 +1,279 @@ + array( + 'title' => t('Access Bulk Exporter'), + 'description' => t('Export various system objects into code.'), + ), + ); +} + +/** + * Implements hook_menu(). + */ +function bulk_export_menu() { + $items['admin/structure/bulk-export'] = array( + 'title' => 'Bulk Exporter', + 'description' => 'Bulk-export multiple CTools-handled data objects to code.', + 'access arguments' => array('use bulk exporter'), + 'page callback' => 'bulk_export_export', + ); + $items['admin/structure/bulk-export/results'] = array( + 'access arguments' => array('use bulk exporter'), + 'page callback' => 'bulk_export_export', + 'type' => MENU_CALLBACK, + ); + return $items; +} + +/** + * FAPI gateway to the bulk exporter. + * + * @param $cli + * Whether this function is called from command line. + * @param $options + * A collection of options, only passed in by drush_ctools_export(). + */ +function bulk_export_export($cli = FALSE, $options = array()) { + ctools_include('export'); + $form = array(); + $schemas = ctools_export_get_schemas(TRUE); + $exportables = $export_tables = array(); + + foreach ($schemas as $table => $schema) { + if (!empty($schema['export']['list callback']) && function_exists($schema['export']['list callback'])) { + $exportables[$table] = $schema['export']['list callback'](); + } + else { + $exportables[$table] = ctools_export_default_list($table, $schema); + } + natcasesort($exportables[$table]); + $export_tables[$table] = $schema['module']; + } + if ($exportables) { + $form_state = array( + 're_render' => FALSE, + 'no_redirect' => TRUE, + 'exportables' => $exportables, + 'export_tables' => $export_tables, + 'name' => '', + 'code' => '', + 'module' => '', + ); + + // If called from drush_ctools_export, get the module name and + // select all exportables and call the submit function directly. + if ($cli) { + $module_name = $options['name']; + $form_state['values']['name'] = $module_name; + if (isset($options['selections'])) { + $exportables = $options['selections']; + } + $form_state['values']['tables'] = array(); + foreach ($exportables as $table => $names) { + if (!empty($names)) { + $form_state['values']['tables'][] = $table; + $form_state['values'][$table] = array(); + foreach ($names as $name => $title) { + $form_state['values'][$table][$name] = $name; + } + } + } + $output = bulk_export_export_form_submit($form, $form_state); + } + else { + $output = drupal_build_form('bulk_export_export_form', $form_state); + $module_name = $form_state['module']; + } + + if (!empty($form_state['submitted']) || $cli) { + drupal_set_title(t('Bulk export results')); + $output = ''; + $module_code = ''; + $api_code = array(); + $dependencies = $file_data = array(); + foreach ($form_state['code'] as $module => $api_info) { + if ($module == 'general') { + $module_code .= $api_info; + } + else { + foreach ($api_info as $api => $info) { + $api_hook = ctools_plugin_api_get_hook($module, $api); + if (empty($api_code[$api_hook])) { + $api_code[$api_hook] = ''; + } + $api_code[$api_hook] .= " if (\$module == '$module' && \$api == '$api') {\n"; + $api_code[$api_hook] .= " return array('version' => $info[version]);\n"; + $api_code[$api_hook] .= " }\n"; + $dependencies[$module] = TRUE; + + $file = $module_name . '.' . $api . '.inc'; + $code = " $file))); + $output .= drupal_render($export_form); + } + } + } + } + + // Add hook_ctools_plugin_api at the top of the module code, if there is any. + if ($api_code) { + foreach ($api_code as $api_hook => $text) { + $api = "\n/**\n"; + $api .= " * Implements hook_$api_hook().\n"; + $api .= " */\n"; + $api .= "function {$module_name}_$api_hook(\$module, \$api) {\n"; + $api .= $text; + $api .= "}\n"; + $module_code = $api . $module_code; + } + } + + if ($module_code) { + $module = " $form_state['module'] . '.module'))); + $output = drupal_render($export_form) . $output; + } + } + + $info = strtr("name = @module export module\n", array('@module' => $form_state['module'])); + $info .= strtr("description = Export objects from CTools\n", array('@module' => $form_state['values']['name'])); + foreach ($dependencies as $module => $junk) { + $info .= "dependencies[] = $module\n"; + } + $info .= "package = Chaos tool suite\n"; + $info .= "core = 7.x\n"; + if ($cli) { + $file_data[$module_name . '.info'] = $info; + } + else { + $export_form = drupal_get_form('ctools_export_form', $info, t('Place this in @file', array('@file' => $form_state['module'] . '.info'))); + $output = drupal_render($export_form) . $output; + } + } + + if ($cli) { + return $file_data; + } + else { + return $output; + } + } + else { + return t('There are no objects to be exported at this time.'); + } +} + +/** + * FAPI definition for the bulk exporter form. + * + */ +function bulk_export_export_form($form, &$form_state) { + + $files = system_rebuild_module_data(); + + $form['additional_settings'] = array( + '#type' => 'vertical_tabs', + ); + + $options = $tables = array(); + foreach ($form_state['exportables'] as $table => $list) { + if (empty($list)) { + continue; + } + + foreach ($list as $id => $title) { + $options[$table][$id] = array($title); + $options[$table][$id]['#attributes'] = array('class' => array('bulk-selection')); + } + + $module = $form_state['export_tables'][$table]; + $header = array($table); + $module_name = $files[$module]->info['name']; + $tables[] = $table; + + if (!isset($form[$module_name])) { + $form[$files[$module]->info['name']] = array( + '#type' => 'fieldset', + '#group' => 'additional_settings', + '#title' => $module_name, + ); + } + + $form[$module_name]['tables'][$table] = array( + '#prefix' => '
', + '#suffix' => '
', + '#type' => 'tableselect', + '#header' => $header, + '#options' => $options[$table], + ); + } + + $form['tables'] = array( + '#type' => 'value', + '#value' => $tables, + ); + + $form['name'] = array( + '#type' => 'textfield', + '#title' => t('Module name'), + '#description' => t('Enter the module name to export code to.'), + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Export'), + ); + + $form['#action'] = url('admin/structure/bulk-export/results'); + $form['#attached']['css'][] = drupal_get_path('module', 'bulk_export') . '/bulk_export.css'; + $form['#attached']['js'][] = drupal_get_path('module', 'bulk_export') . '/bulk_export.js'; + return $form; +} + +/** + * Process the bulk export submit form and make the results available. + */ +function bulk_export_export_form_submit($form, &$form_state) { + $code = array(); + $name = empty($form_state['values']['name']) ? 'foo' : $form_state['values']['name']; + $tables = $form_state['values']['tables']; + + foreach ($tables as $table) { + $names = array_keys(array_filter($form_state['values'][$table])); + if ($names) { + natcasesort($names); + ctools_export_to_hook_code($code, $table, $names, $name); + } + } + + $form_state['code'] = $code; + $form_state['module'] = $name; +} diff --git a/sites/all/modules/contrib/ctools/css/button.css b/sites/all/modules/contrib/ctools/css/button.css new file mode 100644 index 0000000..15e484b --- /dev/null +++ b/sites/all/modules/contrib/ctools/css/button.css @@ -0,0 +1,31 @@ + +.ctools-button-processed { + border-style: solid; + border-width: 1px; + display: inline-block; + line-height: 1; +} + +.ctools-button-processed:hover { + cursor: pointer; +} + +.ctools-button-processed .ctools-content { + padding-bottom: 2px; + padding-top: 2px; +} + +.ctools-no-js .ctools-content ul, +.ctools-button-processed .ctools-content ul { + list-style-image: none; + list-style-type: none; +} + +.ctools-button-processed li { + line-height: 1.3333; +} + +.ctools-button li a { + padding-left: 12px; + padding-right: 12px; +} diff --git a/sites/all/modules/contrib/ctools/css/collapsible-div.css b/sites/all/modules/contrib/ctools/css/collapsible-div.css new file mode 100644 index 0000000..ff64813 --- /dev/null +++ b/sites/all/modules/contrib/ctools/css/collapsible-div.css @@ -0,0 +1,26 @@ + +.ctools-collapsible-container .ctools-toggle { + float: left; + width: 21px; + height: 21px; + cursor: pointer; + background-position: 7px 7px; + background-repeat: no-repeat; + background-image: url(../images/collapsible-expanded.png); +} + +.ctools-collapsible-container .ctools-collapsible-handle { + display: none; +} + +html.js .ctools-collapsible-container .ctools-collapsible-handle { + display: block; +} + +.ctools-collapsible-container .ctools-collapsible-handle { + cursor: pointer; +} + +.ctools-collapsible-container .ctools-toggle-collapsed { + background-image: url(../images/collapsible-collapsed.png); +} diff --git a/sites/all/modules/contrib/ctools/css/context.css b/sites/all/modules/contrib/ctools/css/context.css new file mode 100644 index 0000000..5093104 --- /dev/null +++ b/sites/all/modules/contrib/ctools/css/context.css @@ -0,0 +1,10 @@ +.ctools-context-holder .ctools-context-title { + float: left; + width: 49%; + font-style: italic; +} + +.ctools-context-holder .ctools-context-content { + float: right; + width: 49%; +} diff --git a/sites/all/modules/contrib/ctools/css/ctools.css b/sites/all/modules/contrib/ctools/css/ctools.css new file mode 100644 index 0000000..7372988 --- /dev/null +++ b/sites/all/modules/contrib/ctools/css/ctools.css @@ -0,0 +1,25 @@ +.ctools-locked { + color: red; + border: 1px solid red; + padding: 1em; +} + +.ctools-owns-lock { + background: #FFFFDD none repeat scroll 0 0; + border: 1px solid #F0C020; + padding: 1em; +} + +a.ctools-ajaxing, +input.ctools-ajaxing, +button.ctools-ajaxing, +select.ctools-ajaxing { + padding-right: 18px !important; + background: url(../images/status-active.gif) right center no-repeat; +} + +div.ctools-ajaxing { + float: left; + width: 18px; + background: url(../images/status-active.gif) center center no-repeat; +} diff --git a/sites/all/modules/contrib/ctools/css/dropbutton.css b/sites/all/modules/contrib/ctools/css/dropbutton.css new file mode 100644 index 0000000..5e3ea24 --- /dev/null +++ b/sites/all/modules/contrib/ctools/css/dropbutton.css @@ -0,0 +1,66 @@ + +.ctools-dropbutton-processed { + padding-right: 18px; + position: relative; + background-color: inherit; +} + +.ctools-dropbutton-processed.open { + z-index: 200; +} + +.ctools-dropbutton-processed .ctools-content li, +.ctools-dropbutton-processed .ctools-content a { + display: block; +} + +.ctools-dropbutton-processed .ctools-link { + bottom: 0; + display: block; + height: auto; + position: absolute; + right: 0; + text-indent: -9999px; /* LTR */ + top: 0; + width: 17px; +} + +.ctools-dropbutton-processed .ctools-link a { + overflow: hidden; +} + +.ctools-dropbutton-processed .ctools-content ul { + margin: 0; + overflow: hidden; +} + +.ctools-dropbutton-processed.open li + li { + padding-top: 4px; +} + +/** + * This creates the dropbutton arrow and inherits the link color + */ +.ctools-twisty { + border-bottom-color: transparent; + border-left-color: transparent; + border-right-color: transparent; + border-style: solid; + border-width: 4px 4px 0; + line-height: 0; + right: 6px; + position: absolute; + top: 0.75em; +} + +.ctools-dropbutton-processed.open .ctools-twisty { + border-bottom: 4px solid; + border-left-color: transparent; + border-right-color: transparent; + border-top-color: transparent; + top: 0.5em; +} + +.ctools-no-js .ctools-twisty { + display: none; +} diff --git a/sites/all/modules/contrib/ctools/css/dropdown.css b/sites/all/modules/contrib/ctools/css/dropdown.css new file mode 100644 index 0000000..bb50f3f --- /dev/null +++ b/sites/all/modules/contrib/ctools/css/dropdown.css @@ -0,0 +1,73 @@ +html.js div.ctools-dropdown div.ctools-dropdown-container { + z-index: 1001; + display: none; + text-align: left; + position: absolute; +} + +html.js div.ctools-dropdown div.ctools-dropdown-container ul li a { + display: block; +} + +html.js div.ctools-dropdown div.ctools-dropdown-container ul { + list-style-type: none; + margin: 0; + padding: 0; +} + +html.js div.ctools-dropdown div.ctools-dropdown-container ul li { + display: block; + /* prevent excess right margin in IE */ + margin-right: 0; + margin-left: 0; + padding-right: 0; + padding-left: 0; + background-image: none; /* prevent list backgrounds from mucking things up */ +} + +.ctools-dropdown-no-js .ctools-dropdown-link, +.ctools-dropdown-no-js span.text { + display: none; +} + +/* Everything from here down is purely visual style and can be overridden. */ + +html.js div.ctools-dropdown a.ctools-dropdown-text-link { + background: url(../images/collapsible-expanded.png) 3px 5px no-repeat; + padding-left: 12px; +} + +html.js div.ctools-dropdown div.ctools-dropdown-container { + width: 175px; + background: #fff; + border: 1px solid black; + margin: 4px 1px 0 0; + padding: 0; + color: #494949; +} + +html.js div.ctools-dropdown div.ctools-dropdown-container ul li li a { + padding-left: 25px; + width: 150px; + color: #027AC6; +} + +html.js div.ctools-dropdown div.ctools-dropdown-container ul li a { + text-decoration: none; + padding-left: 5px; + width: 170px; + color: #027AC6; +} + +html.js div.ctools-dropdown div.ctools-dropdown-container ul li span { + display: block; +} + +html.js div.ctools-dropdown div.ctools-dropdown-container ul li span.text { + font-style: italic; + padding-left: 5px; +} + +html.js .ctools-dropdown-hover { + background-color: #ECECEC; +} diff --git a/sites/all/modules/contrib/ctools/css/export-ui-list.css b/sites/all/modules/contrib/ctools/css/export-ui-list.css new file mode 100644 index 0000000..170d128 --- /dev/null +++ b/sites/all/modules/contrib/ctools/css/export-ui-list.css @@ -0,0 +1,45 @@ +body form#ctools-export-ui-list-form { + margin: 0 0 20px 0; +} + +#ctools-export-ui-list-form .form-item { + padding-right: 1em; /* LTR */ + float: left; /* LTR */ + margin-top: 0; + margin-bottom: 0; +} + +#ctools-export-ui-list-items { + width: 100%; +} + +#edit-order-wrapper { + clear: left; /* LTR */ +} + +#ctools-export-ui-list-form .form-submit { + margin-top: 1.65em; + float: left; /* LTR */ +} + +tr.ctools-export-ui-disabled td { + color: #999; +} + +th.ctools-export-ui-operations, +td.ctools-export-ui-operations { + text-align: right; /* LTR */ + vertical-align: top; +} + +/* Force the background color to inherit so that the dropbuttons do not need + a specific background color. */ +td.ctools-export-ui-operations { + background-color: inherit; +} + +td.ctools-export-ui-operations .ctools-dropbutton { + text-align: left; /* LTR */ + position: absolute; + right: 10px; +} diff --git a/sites/all/modules/contrib/ctools/css/modal.css b/sites/all/modules/contrib/ctools/css/modal.css new file mode 100644 index 0000000..def374b --- /dev/null +++ b/sites/all/modules/contrib/ctools/css/modal.css @@ -0,0 +1,130 @@ +div.ctools-modal-content { + background: #fff; + color: #000; + padding: 0; + margin: 2px; + border: 1px solid #000; + width: 600px; + text-align: left; +} + +div.ctools-modal-content .modal-title { + font-size: 120%; + font-weight: bold; + color: white; + overflow: hidden; + white-space: nowrap; +} + +div.ctools-modal-content .modal-header { + background-color: #2385c2; + padding: 0 .25em 0 1em; +} + +div.ctools-modal-content .modal-header a { + color: white; +} + +div.ctools-modal-content .modal-content { + padding: 1em 1em 0 1em; + overflow: auto; + position: relative; /* Keeps IE7 from flowing outside the modal. */ +} + +div.ctools-modal-content .modal-form { +} + +div.ctools-modal-content a.close { + color: white; + float: right; +} + +div.ctools-modal-content a.close:hover { + text-decoration: none; +} + +div.ctools-modal-content a.close img { + position: relative; + top: 1px; +} + +div.ctools-modal-content .modal-content .modal-throbber-wrapper { + text-align: center; +} + +div.ctools-modal-content .modal-content .modal-throbber-wrapper img { + margin-top: 160px; +} + +/** modal forms CSS **/ +div.ctools-modal-content .form-item label { + width: 15em; + float: left; +} + +div.ctools-modal-content .form-item label.option { + width: auto; + float: none; +} + +div.ctools-modal-content .form-item .description { + clear: left; +} + +div.ctools-modal-content .form-item .description .tips { + margin-left: 2em; +} + +div.ctools-modal-content .no-float .form-item * { + float: none; +} + +div.ctools-modal-content .modal-form .no-float label { + width: auto; +} + +div.ctools-modal-content fieldset, +div.ctools-modal-content .form-radios, +div.ctools-modal-content .form-checkboxes { + clear: left; +} + +div.ctools-modal-content .vertical-tabs-panes > fieldset { + clear: none; +} + +div.ctools-modal-content .resizable-textarea { + width: auto; + margin-left: 15em; + margin-right: 5em; +} + +div.ctools-modal-content .container-inline .form-item { + margin-right: 2em; +} + +#views-exposed-pane-wrapper .form-item { + margin-top: 0; + margin-bottom: 0; +} + +div.ctools-modal-content label.hidden-options { + background: transparent url(../images/arrow-active.png) no-repeat right; + height: 12px; + padding-right: 12px; +} + +div.ctools-modal-content label.expanded-options { + background: transparent url(../images/expanded-options.png) no-repeat right; + height: 12px; + padding-right: 16px; +} + +div.ctools-modal-content .option-text-aligner label.expanded-options, +div.ctools-modal-content .option-text-aligner label.hidden-options { + background: none; +} + +div.ctools-modal-content .dependent-options { + padding-left: 30px; +} diff --git a/sites/all/modules/contrib/ctools/css/ruleset.css b/sites/all/modules/contrib/ctools/css/ruleset.css new file mode 100644 index 0000000..891455f --- /dev/null +++ b/sites/all/modules/contrib/ctools/css/ruleset.css @@ -0,0 +1,11 @@ +.ctools-right-container { + float: right; + padding: 0 0 0 .5em; + margin: 0; + width: 48.5%; +} + +.ctools-left-container { + padding-right: .5em; + width: 48.5%; +} diff --git a/sites/all/modules/contrib/ctools/css/stylizer.css b/sites/all/modules/contrib/ctools/css/stylizer.css new file mode 100644 index 0000000..a16ec78 --- /dev/null +++ b/sites/all/modules/contrib/ctools/css/stylizer.css @@ -0,0 +1,129 @@ + +/* Farbtastic placement */ +.color-form { + max-width: 50em; + position: relative; + min-height: 195px; +} +#placeholder { +/* + position: absolute; + top: 0; + right: 0; +*/ + margin: 0 auto; + width: 195px; +} + +/* Palette */ +.color-form .form-item { + height: 2em; + line-height: 2em; + padding-left: 1em; /* LTR */ + margin: 0.5em 0; +} + +.color-form .form-item input { + margin-top: .2em; +} + +.color-form label { + float: left; /* LTR */ + clear: left; /* LTR */ + width: 14em; +} +.color-form .form-text, .color-form .form-select { + float: left; /* LTR */ +} +.color-form .form-text { + text-align: center; + margin-right: 5px; /* LTR */ + cursor: pointer; +} + +#palette .hook { + float: left; /* LTR */ + margin-top: 3px; + width: 16px; + height: 16px; +} +#palette .up { + background-position: 100% -27px; /* LTR */ +} +#palette .both { + background-position: 100% -54px; /* LTR */ +} + + +#palette .form-item { + width: 24em; +} +#palette .item-selected { + background: #eee; +} + +/* Preview */ +#preview { + width: 45%; + float: right; + margin: 0; +} + +#ctools_stylizer_color_scheme_form { + float: left; + width: 45%; + margin: 0; +} + +/* general style for the layout-icon */ +.ctools-style-icon .caption { + width: 100px; + margin-bottom: 1em; + line-height: 1em; + text-align: center; + cursor: default; +} + +.ctools-style-icons .form-item { + width: 100px; + float: left; + margin: 0 3px !important; +} + +.ctools-style-icons .form-item .ctools-style-icon { + float: none; + height: 150px; + width: 100px; +} + +.ctools-style-icons .form-item label.option { + width: 100px; + display: block; + text-align: center; +} + +.ctools-style-icons .form-item label.option input { + margin: 0 auto; +} + +.ctools-style-icons .ctools-style-category { + height: 190px; +} + +.ctools-style-icons .ctools-style-category label { + font-weight: bold; + width: 100%; + float: left; +} + +/** + * Stylizer font editor widget + */ +.ctools-stylizer-spacing-form .form-item { + float: left; + margin: .25em; +} + +#edit-font-font { + width: 9em; +} diff --git a/sites/all/modules/contrib/ctools/css/wizard.css b/sites/all/modules/contrib/ctools/css/wizard.css new file mode 100644 index 0000000..d42a2db --- /dev/null +++ b/sites/all/modules/contrib/ctools/css/wizard.css @@ -0,0 +1,8 @@ + +.wizard-trail { + font-size: 120%; +} + +.wizard-trail-current { + font-weight: bold; +} diff --git a/sites/all/modules/contrib/ctools/ctools.api.php b/sites/all/modules/contrib/ctools/ctools.api.php new file mode 100644 index 0000000..a7ab783 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools.api.php @@ -0,0 +1,268 @@ + TRUE, + ); + + return $plugins; +} + +/** + * This hook is used to inform the CTools plugin system about the location of a + * directory that should be searched for files containing plugins of a + * particular type. CTools invokes this same hook for all plugins, using the + * two passed parameters to indicate the specific type of plugin for which it + * is searching. + * + * The $plugin_type parameter is self-explanatory - it is the string name of the + * plugin type (e.g., Panels' 'layouts' or 'styles'). The $owner parameter is + * necessary because CTools internally namespaces plugins by the module that + * owns them. This is an extension of Drupal best practices on avoiding global + * namespace pollution by prepending your module name to all its functions. + * Consequently, it is possible for two different modules to create a plugin + * type with exactly the same name and have them operate in harmony. In fact, + * this system renders it impossible for modules to encroach on other modules' + * plugin namespaces. + * + * Given this namespacing, it is important that implementations of this hook + * check BOTH the $owner and $plugin_type parameters before returning a path. + * If your module does not implement plugins for the requested module/plugin + * combination, it is safe to return nothing at all (or NULL). As a convenience, + * it is also safe to return a path that does not exist for plugins your module + * does not implement - see form 2 for a use case. + * + * Note that modules implementing a plugin also must implement this hook to + * instruct CTools as to the location of the plugins. See form 3 for a use case. + * + * The conventional structure to return is "plugins/$plugin_type" - that is, a + * 'plugins' subdirectory in your main module directory, with individual + * directories contained therein named for the plugin type they contain. + * + * @param string $owner + * The system name of the module owning the plugin type for which a base + * directory location is being requested. + * @param string $plugin_type + * The name of the plugin type for which a base directory is being requested. + * @return string + * The path where CTools' plugin system should search for plugin files, + * relative to your module's root. Omit leading and trailing slashes. + */ +function hook_ctools_plugin_directory($owner, $plugin_type) { + // Form 1 - for a module implementing only the 'content_types' plugin owned + // by CTools, this would cause the plugin system to search the + // /plugins/content_types directory for .inc plugin files. + if ($owner == 'ctools' && $plugin_type == 'content_types') { + return 'plugins/content_types'; + } + + // Form 2 - if your module implements only Panels plugins, and has 'layouts' + // and 'styles' plugins but no 'cache' or 'display_renderers', it is OK to be + // lazy and return a directory for a plugin you don't actually implement (so + // long as that directory doesn't exist). This lets you avoid ugly in_array() + // logic in your conditional, and also makes it easy to add plugins of those + // types later without having to change this hook implementation. + if ($owner == 'panels') { + return "plugins/$plugin_type"; + } + + // Form 3 - CTools makes no assumptions about where your plugins are located, + // so you still have to implement this hook even for plugins created by your + // own module. + if ($owner == 'mymodule') { + // Yes, this is exactly like Form 2 - just a different reasoning for it. + return "plugins/$plugin_type"; + } + // Finally, if nothing matches, it's safe to return nothing at all (or NULL). +} + +/** + * Alter a plugin before it has been processed. + * + * This hook is useful for altering flags or other information that will be + * used or possibly overriden by the process hook if defined. + * + * @param $plugin + * An associative array defining a plugin. + * @param $info + * An associative array of plugin type info. + */ +function hook_ctools_plugin_pre_alter(&$plugin, &$info) { + // Override a function defined by the plugin. + if ($info['type'] == 'my_type') { + $plugin['my_flag'] = 'new_value'; + } +} + +/** + * Alter a plugin after it has been processed. + * + * This hook is useful for overriding the final values for a plugin after it + * has been processed. + * + * @param $plugin + * An associative array defining a plugin. + * @param $info + * An associative array of plugin type info. + */ +function hook_ctools_plugin_post_alter(&$plugin, &$info) { + // Override a function defined by the plugin. + if ($info['type'] == 'my_type') { + $plugin['my_function'] = 'new_function'; + } +} + +/** + * Alter the list of modules/themes which implement a certain api. + * + * The hook named here is just an example, as the real existing hooks are named + * for example 'hook_views_api_alter'. + * + * @param array $list + * An array of informations about the implementors of a certain api. + * The key of this array are the module names/theme names. + */ +function hook_ctools_api_hook_alter(&$list) { + // Alter the path of the node implementation. + $list['node']['path'] = drupal_get_path('module', 'node'); +} + +/** + * Alter the available functions to be used in ctools math expression api. + * + * One usecase would be to create your own function in your module and + * allow to use it in the math expression api. + * + * @param $functions + * An array which has the functions as value. + */ +function hook_ctools_math_expression_functions_alter(&$functions) { + // Allow to convert from degrees to radiant. + $functions[] = 'deg2rad'; +} + +/** + * Alter everything. + * + * @param $info + * An associative array containing the following keys: + * - content: The rendered content. + * - title: The content's title. + * - no_blocks: A boolean to decide if blocks should be displayed. + * @param $page + * If TRUE then this renderer owns the page and can use theme('page') + * for no blocks; if false, output is returned regardless of any no + * blocks settings. + * @param $context + * An associative array containing the following keys: + * - args: The raw arguments behind the contexts. + * - contexts: The context objects in use. + * - task: The task object in use. + * - subtask: The subtask object in use. + * - handler: The handler object in use. + */ +function hook_ctools_render_alter(&$info, &$page, &$context) { + if ($context['handler']->name == 'my_handler') { + ctools_add_css('my_module.theme', 'my_module'); + } +} + +/** + * Alter a content plugin subtype. + * + * While content types can be altered via hook_ctools_plugin_pre_alter() or + * hook_ctools_plugin_post_alter(), the subtypes that content types rely on + * are special and require their own hook. + * + * This hook can be used to add things like 'render last' or change icons + * or categories or to rename content on specific sites. + */ +function hook_ctools_content_subtype_alter($subtype, $plugin) { + $subtype['render last'] = TRUE; +} + +/** + * Alter the definition of an entity context plugin. + * + * @param array $plugin + * An associative array defining a plugin. + * @param array $entity + * The entity info array of a specific entity type. + * @param string $plugin_id + * The plugin ID, in the format NAME:KEY. + */ +function hook_ctools_entity_context_alter(&$plugin, &$entity, $plugin_id) { + ctools_include('context'); + switch ($plugin_id) { + case 'entity_id:taxonomy_term': + $plugin['no ui'] = TRUE; + case 'entity:user': + $plugin = ctools_get_context('user'); + unset($plugin['no ui']); + unset($plugin['no required context ui']); + break; + } +} + +/** + * Alter the definition of entity context plugins. + * + * @param array $plugins + * An associative array of plugin definitions, keyed by plugin ID. + * + * @see hook_ctools_entity_context_alter() + */ +function hook_ctools_entity_contexts_alter(&$plugins) { + $plugins['entity_id:taxonomy_term']['no ui'] = TRUE; +} + +/** + * Change cleanstring settings. + * + * @param array $settings + * An associative array of cleanstring settings. + * + * @see ctools_cleanstring() + */ +function hook_ctools_cleanstring_alter(&$settings) { + // Convert all strings to lower case. + $settings['lower case'] = TRUE; +} + +/** + * Change cleanstring settings for a specific clean ID. + * + * @param array $settings + * An associative array of cleanstring settings. + * + * @see ctools_cleanstring() + */ +function hook_ctools_cleanstring_CLEAN_ID_alter(&$settings) { + // Convert all strings to lower case. + $settings['lower case'] = TRUE; +} + +/** + * @} End of "addtogroup hooks". + */ diff --git a/sites/all/modules/contrib/ctools/ctools.info b/sites/all/modules/contrib/ctools/ctools.info new file mode 100644 index 0000000..02c86f5 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools.info @@ -0,0 +1,17 @@ +name = Chaos tools +description = A library of helpful tools by Merlin of Chaos. +core = 7.x +package = Chaos tool suite +version = CTOOLS_MODULE_VERSION +files[] = includes/context.inc +files[] = includes/css-cache.inc +files[] = includes/math-expr.inc +files[] = includes/stylizer.inc +files[] = tests/css_cache.test + +; Information added by Drupal.org packaging script on 2015-08-19 +version = "7.x-1.9" +core = "7.x" +project = "ctools" +datestamp = "1440020680" + diff --git a/sites/all/modules/contrib/ctools/ctools.install b/sites/all/modules/contrib/ctools/ctools.install new file mode 100644 index 0000000..e96c743 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools.install @@ -0,0 +1,265 @@ + t('CTools CSS Cache'), + 'severity' => REQUIREMENT_OK, + 'value' => t('Exists'), + ); + + $path = 'public://ctools/css'; + if (!file_prepare_directory($path, FILE_CREATE_DIRECTORY)) { + $requirements['ctools_css_cache']['description'] = t('The CTools CSS cache directory, %path could not be created due to a misconfigured files directory. Please ensure that the files directory is correctly configured and that the webserver has permission to create directories.', array('%path' => file_uri_target($path))); + $requirements['ctools_css_cache']['severity'] = REQUIREMENT_ERROR; + $requirements['ctools_css_cache']['value'] = t('Unable to create'); + } + + if (!function_exists('error_get_last')) { + $requirements['ctools_php_52']['title'] = t('CTools PHP requirements'); + $requirements['ctools_php_52']['description'] = t('CTools requires certain features only available in PHP 5.2.0 or higher.'); + $requirements['ctools_php_52']['severity'] = REQUIREMENT_WARNING; + $requirements['ctools_php_52']['value'] = t('PHP !version', array('!version' => phpversion())); + } + } + + return $requirements; +} + +/** + * Implements hook_schema(). + */ +function ctools_schema() { + return ctools_schema_3(); +} + +/** + * Version 3 of the CTools schema. + */ +function ctools_schema_3() { + $schema = ctools_schema_2(); + + // update the 'obj' field to be 128 bytes long: + $schema['ctools_object_cache']['fields']['obj']['length'] = 128; + + return $schema; +} + +/** + * Version 2 of the CTools schema. + */ +function ctools_schema_2() { + $schema = ctools_schema_1(); + + // update the 'name' field to be 128 bytes long: + $schema['ctools_object_cache']['fields']['name']['length'] = 128; + + // Update the 'data' field to be type 'blob'. + $schema['ctools_object_cache']['fields']['data'] = array( + 'type' => 'blob', + 'size' => 'big', + 'description' => 'Serialized data being stored.', + 'serialize' => TRUE, + ); + + // DO NOT MODIFY THIS TABLE -- this definition is used to create the table. + // Changes to this table must be made in schema_3 or higher. + $schema['ctools_css_cache'] = array( + 'description' => 'A special cache used to store CSS that must be non-volatile.', + 'fields' => array( + 'cid' => array( + 'type' => 'varchar', + 'length' => '128', + 'description' => 'The CSS ID this cache object belongs to.', + 'not null' => TRUE, + ), + 'filename' => array( + 'type' => 'varchar', + 'length' => '255', + 'description' => 'The filename this CSS is stored in.', + ), + 'css' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'CSS being stored.', + 'serialize' => TRUE, + ), + 'filter' => array( + 'type' => 'int', + 'size' => 'tiny', + 'description' => 'Whether or not this CSS needs to be filtered.', + ), + ), + 'primary key' => array('cid'), + ); + + return $schema; +} + +/** + * CTools' initial schema; separated for the purposes of updates. + * + * DO NOT MAKE CHANGES HERE. This schema version is locked. + */ +function ctools_schema_1() { + $schema['ctools_object_cache'] = array( + 'description' => t('A special cache used to store objects that are being edited; it serves to save state in an ordinarily stateless environment.'), + 'fields' => array( + 'sid' => array( + 'type' => 'varchar', + 'length' => '64', + 'not null' => TRUE, + 'description' => 'The session ID this cache object belongs to.', + ), + 'name' => array( + 'type' => 'varchar', + 'length' => '32', + 'not null' => TRUE, + 'description' => 'The name of the object this cache is attached to.', + ), + 'obj' => array( + 'type' => 'varchar', + 'length' => '32', + 'not null' => TRUE, + 'description' => 'The type of the object this cache is attached to; this essentially represents the owner so that several sub-systems can use this cache.', + ), + 'updated' => array( + 'type' => 'int', + 'unsigned' => TRUE, + 'not null' => TRUE, + 'default' => 0, + 'description' => 'The time this cache was created or updated.', + ), + 'data' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'Serialized data being stored.', + 'serialize' => TRUE, + ), + ), + 'primary key' => array('sid', 'obj', 'name'), + 'indexes' => array('updated' => array('updated')), + ); + return $schema; +} + +/** + * Implements hook_install(). + */ +function ctools_install() { + // Activate our custom cache handler for the CSS cache. + variable_set('cache_class_cache_ctools_css', 'CToolsCssCache'); +} + +/** + * Implements hook_uninstall(). + */ +function ctools_uninstall() { + variable_del('cache_class_cache_ctools_css'); +} + +/** + * Enlarge the ctools_object_cache.name column to prevent truncation and weird + * errors. + */ +function ctools_update_6001() { + // Perform updates like this to reduce code duplication. + $schema = ctools_schema_2(); + + db_change_field('ctools_object_cache', 'name', 'name', $schema['ctools_object_cache']['fields']['name']); +} + +/** + * Add the new css cache table. + */ +function ctools_update_6002() { + // Schema 2 is locked and should not be changed. + $schema = ctools_schema_2(); + + db_create_table('ctools_css_cache', $schema['ctools_css_cache']); +} + +/** + * Take over for the panels_views module if it was on. + */ +function ctools_update_6003() { + $result = db_query('SELECT status FROM {system} WHERE name = :name', array(':name' => 'panels_views'))->fetchField(); + if ($result) { + db_delete('system')->condition('name', 'panels_views')->execute(); + module_enable(array('views_content'), TRUE); + } +} + +/** + * Add primary key to the ctools_object_cache table. + */ +function ctools_update_6004() { + db_add_primary_key('ctools_object_cache', array('sid', 'obj', 'name')); + db_drop_index('ctools_object_cache', 'sid_obj_name'); +} + +/** + * Removed update. + */ +function ctools_update_6005() { + return array(); +} + +/** + * ctools_custom_content table was originally here, but is now moved to + * its own module. + */ +function ctools_update_6007() { + $ret = array(); + if (db_table_exists('ctools_custom_content')) { + // Enable the module to make everything as seamless as possible. + module_enable(array('ctools_custom_content'), TRUE); + } + + return $ret; +} + +/** + * ctools_object_cache needs to be defined as a blob. + */ +function ctools_update_6008() { + db_delete('ctools_object_cache') + ->execute(); + + db_change_field('ctools_object_cache', 'data', 'data', array( + 'type' => 'blob', + 'size' => 'big', + 'description' => 'Serialized data being stored.', + 'serialize' => TRUE, + ) + ); +} + +/** + * Enable the custom CSS cache handler. + */ +function ctools_update_7000() { + variable_set('cache_class_cache_ctools_css', 'CToolsCssCache'); +} + +/** + * Increase the length of the ctools_object_cache.obj column. + */ +function ctools_update_7001() { + db_change_field('ctools_object_cache', 'obj', 'obj', array( + 'type' => 'varchar', + 'length' => '128', + 'not null' => TRUE, + 'description' => 'The type of the object this cache is attached to; this essentially represents the owner so that several sub-systems can use this cache.', + )); +} diff --git a/sites/all/modules/contrib/ctools/ctools.module b/sites/all/modules/contrib/ctools/ctools.module new file mode 100644 index 0000000..008214e --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools.module @@ -0,0 +1,1088 @@ + 7.x-1.x. + * + * To define a specific version of CTools as a dependency for another module, + * simply include a dependency line in that module's info file, e.g.: + * ; Requires CTools v7.x-1.4 or newer. + * dependencies[] = ctools (>=1.4) + */ +define('CTOOLS_MODULE_VERSION', '7.x-1.9'); + +/** + * Test the CTools API version. + * + * This function can always be used to safely test if CTools has the minimum + * API version that your module can use. It can also try to protect you from + * running if the CTools API version is too new, but if you do that you need + * to be very quick about watching CTools API releases and release new versions + * of your software as soon as the new release is made, or people might end up + * updating CTools and having your module shut down without any recourse. + * + * It is recommended that every hook of your module that might use CTools or + * might lead to a use of CTools be guarded like this: + * + * @code + * if (!module_invoke('ctools', 'api_version', '1.0')) { + * return; + * } + * @endcode + * + * Note that some hooks such as _menu() or _theme() must return an array(). + * + * You can use it in your hook_requirements to report this error condition + * like this: + * + * @code + * define('MODULENAME_MINIMUM_CTOOLS_API_VERSION', '1.0'); + * define('MODULENAME_MAXIMUM_CTOOLS_API_VERSION', '1.1'); + * + * function MODULENAME_requirements($phase) { + * $requirements = array(); + * if (!module_invoke('ctools', 'api_version', MODULENAME_MINIMUM_CTOOLS_API_VERSION, MODULENAME_MAXIMUM_CTOOLS_API_VERSION)) { + * $requirements['MODULENAME_ctools'] = array( + * 'title' => $t('MODULENAME required Chaos Tool Suite (CTools) API Version'), + * 'value' => t('Between @a and @b', array('@a' => MODULENAME_MINIMUM_CTOOLS_API_VERSION, '@b' => MODULENAME_MAXIMUM_CTOOLS_API_VERSION)), + * 'severity' => REQUIREMENT_ERROR, + * ); + * } + * return $requirements; + * } + * @endcode + * + * Please note that the version is a string, not an floating point number. + * This will matter once CTools reaches version 1.10. + * + * A CTools API changes history will be kept in API.txt. Not every new + * version of CTools will necessarily update the API version. + * @param $minimum + * The minimum version of CTools necessary for your software to run with it. + * @param $maximum + * The maximum version of CTools allowed for your software to run with it. + */ +function ctools_api_version($minimum, $maximum = NULL) { + if (version_compare(CTOOLS_API_VERSION, $minimum, '<')) { + return FALSE; + } + + if (isset($maximum) && version_compare(CTOOLS_API_VERSION, $maximum, '>')) { + return FALSE; + } + + return TRUE; +} + +// ----------------------------------------------------------------------- +// General utility functions + +/** + * Include .inc files as necessary. + * + * This fuction is helpful for including .inc files for your module. The + * general case is including ctools funcitonality like this: + * + * @code + * ctools_include('plugins'); + * @endcode + * + * Similar funcitonality can be used for other modules by providing the $module + * and $dir arguments like this: + * + * @code + * // include mymodule/includes/import.inc + * ctools_include('import', 'mymodule'); + * // include mymodule/plugins/foobar.inc + * ctools_include('foobar', 'mymodule', 'plugins'); + * @endcode + * + * @param $file + * The base file name to be included. + * @param $module + * Optional module containing the include. + * @param $dir + * Optional subdirectory containing the include file. + */ +function ctools_include($file, $module = 'ctools', $dir = 'includes') { + static $used = array(); + + $dir = '/' . ($dir ? $dir . '/' : ''); + + if (!isset($used[$module][$dir][$file])) { + require_once DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "$dir$file.inc"; + $used[$module][$dir][$file] = TRUE; + } +} + +/** + * Include .inc files in a form context. + * + * This is a variant of ctools_include that will save information in the + * the form_state so that cached forms will properly include things. + */ +function ctools_form_include(&$form_state, $file, $module = 'ctools', $dir = 'includes') { + if (!isset($form_state['build_info']['args'])) { + $form_state['build_info']['args'] = array(); + } + + $dir = '/' . ($dir ? $dir . '/' : ''); + form_load_include($form_state, 'inc', $module, $dir . $file); +} + +/** + * Add an arbitrary path to the $form_state so it can work with form cache. + * + * module_load_include uses an unfortunately annoying syntax to work, making it + * difficult to translate the more simple $path + $file syntax. + */ +function ctools_form_include_file(&$form_state, $filename) { + if (!isset($form_state['build_info']['args'])) { + $form_state['build_info']['args'] = array(); + } + + // Now add this to the build info files so that AJAX requests will know to load it. + $form_state['build_info']['files']["$filename"] = $filename; + require_once DRUPAL_ROOT . '/' . $filename; +} + +/** + * Provide the proper path to an image as necessary. + * + * This helper function is used by ctools but can also be used in other + * modules in the same way as explained in the comments of ctools_include. + * + * @param $image + * The base file name (with extension) of the image to be included. + * @param $module + * Optional module containing the include. + * @param $dir + * Optional subdirectory containing the include file. + */ +function ctools_image_path($image, $module = 'ctools', $dir = 'images') { + return drupal_get_path('module', $module) . "/$dir/" . $image; +} + +/** + * Include css files as necessary. + * + * This helper function is used by ctools but can also be used in other + * modules in the same way as explained in the comments of ctools_include. + * + * @param $file + * The base file name to be included. + * @param $module + * Optional module containing the include. + * @param $dir + * Optional subdirectory containing the include file. + */ +function ctools_add_css($file, $module = 'ctools', $dir = 'css') { + drupal_add_css(drupal_get_path('module', $module) . "/$dir/$file.css"); +} + +/** + * Format a css file name for use with $form['#attached']['css']. + * + * This helper function is used by ctools but can also be used in other + * modules in the same way as explained in the comments of ctools_include. + * + * @code + * $form['#attached']['css'] = array(ctools_attach_css('collapsible-div')); + * $form['#attached']['css'][ctools_attach_css('collapsible-div')] = array('preprocess' => FALSE); + * @endcode + * + * @param $file + * The base file name to be included. + * @param $module + * Optional module containing the include. + * @param $dir + * Optional subdirectory containing the include file. + */ +function ctools_attach_css($file, $module = 'ctools', $dir = 'css') { + return drupal_get_path('module', $module) . "/$dir/$file.css"; +} + +/** + * Include js files as necessary. + * + * This helper function is used by ctools but can also be used in other + * modules in the same way as explained in the comments of ctools_include. + * + * @param $file + * The base file name to be included. + * @param $module + * Optional module containing the include. + * @param $dir + * Optional subdirectory containing the include file. + */ +function ctools_add_js($file, $module = 'ctools', $dir = 'js') { + drupal_add_js(drupal_get_path('module', $module) . "/$dir/$file.js"); +} + +/** + * Format a javascript file name for use with $form['#attached']['js']. + * + * This helper function is used by ctools but can also be used in other + * modules in the same way as explained in the comments of ctools_include. + * + * @code + * $form['#attached']['js'] = array(ctools_attach_js('auto-submit')); + * @endcode + * + * @param $file + * The base file name to be included. + * @param $module + * Optional module containing the include. + * @param $dir + * Optional subdirectory containing the include file. + */ +function ctools_attach_js($file, $module = 'ctools', $dir = 'js') { + return drupal_get_path('module', $module) . "/$dir/$file.js"; +} + +/** + * Get a list of roles in the system. + * + * @return + * An array of role names keyed by role ID. + * + * @deprecated + * user_roles() should be used instead. + */ +function ctools_get_roles() { + return user_roles(); +} + +/* + * Break x,y,z and x+y+z into an array. Numeric only. + * + * @param $str + * The string to parse. + * + * @return $object + * An object containing + * - operator: Either 'and' or 'or' + * - value: An array of numeric values. + */ +function ctools_break_phrase($str) { + $object = new stdClass(); + + if (preg_match('/^([0-9]+[+ ])+[0-9]+$/', $str)) { + // The '+' character in a query string may be parsed as ' '. + $object->operator = 'or'; + $object->value = preg_split('/[+ ]/', $str); + } + else if (preg_match('/^([0-9]+,)*[0-9]+$/', $str)) { + $object->operator = 'and'; + $object->value = explode(',', $str); + } + + // Keep an 'error' value if invalid strings were given. + if (!empty($str) && (empty($object->value) || !is_array($object->value))) { + $object->value = array(-1); + $object->invalid_input = TRUE; + return $object; + } + + if (empty($object->value)) { + $object->value = array(); + } + + // Doubly ensure that all values are numeric only. + foreach ($object->value as $id => $value) { + $object->value[$id] = intval($value); + } + + return $object; +} + +/** + * Set a token/value pair to be replaced later in the request, specifically in + * ctools_page_token_processing(). + * + * @param $token + * The token to be replaced later, during page rendering. This should + * ideally be a string inside of an HTML comment, so that if there is + * no replacement, the token will not render on the page. + * @param $type + * The type of the token. Can be either 'variable', which will pull data + * directly from the page variables + * @param $argument + * If $type == 'variable' then argument should be the key to fetch from + * the $variables. If $type == 'callback' then it should either be the + * callback, or an array that will be sent to call_user_func_array(). + * + * @return + * A array of token/variable names to be replaced. + */ +function ctools_set_page_token($token = NULL, $type = NULL, $argument = NULL) { + static $tokens = array(); + + if (isset($token)) { + $tokens[$token] = array($type, $argument); + } + return $tokens; +} + +/** + * Easily set a token from the page variables. + * + * This function can be used like this: + * $token = ctools_set_variable_token('tabs'); + * + * $token will then be a simple replacement for the 'tabs' about of the + * variables available in the page template. + */ +function ctools_set_variable_token($token) { + $string = ''; + ctools_set_page_token($string, 'variable', $token); + return $string; +} + +/** + * Easily set a token from the page variables. + * + * This function can be used like this: + * $token = ctools_set_variable_token('id', 'mymodule_myfunction'); + */ +function ctools_set_callback_token($token, $callback) { + // If the callback uses arguments they are considered in the token. + if (is_array($callback)) { + $token .= '-' . md5(serialize($callback)); + } + $string = ''; + ctools_set_page_token($string, 'callback', $callback); + return $string; +} + +/** + * Tell CTools that sidebar blocks should not be rendered. + * + * It is often desirable to not display sidebars when rendering a page, + * particularly when using Panels. This informs CTools to alter out any + * sidebar regions during block render. + */ +function ctools_set_no_blocks($blocks = FALSE) { + $status = &drupal_static(__FUNCTION__, TRUE); + $status = $blocks; +} + +/** + * Wrapper function to create UUIDs via ctools, falls back on UUID module + * if it is enabled. This code is a copy of uuid.inc from the uuid module. + * @see http://php.net/uniqid#65879 + */ + +function ctools_uuid_generate() { + if (!module_exists('uuid')) { + ctools_include('uuid'); + + $callback = drupal_static(__FUNCTION__); + + if (empty($callback)) { + if (function_exists('uuid_create') && !function_exists('uuid_make')) { + $callback = '_ctools_uuid_generate_pecl'; + } + elseif (function_exists('com_create_guid')) { + $callback = '_ctools_uuid_generate_com'; + } + else { + $callback = '_ctools_uuid_generate_php'; + } + } + return $callback(); + } + else { + return uuid_generate(); + } +} + +/** + * Check that a string appears to be in the format of a UUID. + * @see http://drupal.org/project/uuid + * + * @param $uuid + * The string to test. + * + * @return + * TRUE if the string is well formed. + */ +function ctools_uuid_is_valid($uuid = '') { + if (empty($uuid)) { + return FALSE; + } + if (function_exists('uuid_is_valid') || module_exists('uuid')) { + return uuid_is_valid($uuid); + } + else { + ctools_include('uuid'); + return uuid_is_valid($uuid); + } +} + +/** + * Add an array of classes to the body. + * + * @param mixed $classes + * A string or an array of class strings to add. + * @param string $hook + * The theme hook to add the class to. The default is 'html' which will + * affect the body tag. + */ +function ctools_class_add($classes, $hook = 'html') { + if (!is_array($classes)) { + $classes = array($classes); + } + + $static = &drupal_static('ctools_process_classes', array()); + if (!isset($static[$hook]['add'])) { + $static[$hook]['add'] = array(); + } + foreach ($classes as $class) { + $static[$hook]['add'][] = $class; + } +} + +/** + * Remove an array of classes from the body. + * + * @param mixed $classes + * A string or an array of class strings to remove. + * @param string $hook + * The theme hook to remove the class from. The default is 'html' which will + * affect the body tag. + */ +function ctools_class_remove($classes, $hook = 'html') { + if (!is_array($classes)) { + $classes = array($classes); + } + + $static = &drupal_static('ctools_process_classes', array()); + if (!isset($static[$hook]['remove'])) { + $static[$hook]['remove'] = array(); + } + foreach ($classes as $class) { + $static[$hook]['remove'][] = $class; + } +} + +// ----------------------------------------------------------------------- +// Drupal core hooks + +/** + * Implement hook_init to keep our global CSS at the ready. + */ +function ctools_init() { + ctools_add_css('ctools'); + // If we are sure that CTools' AJAX is in use, change the error handling. + if (!empty($_REQUEST['ctools_ajax'])) { + ini_set('display_errors', 0); + register_shutdown_function('ctools_shutdown_handler'); + } + + // Clear plugin cache on the module page submit. + if ($_GET['q'] == 'admin/modules/list/confirm' && !empty($_POST)) { + cache_clear_all('ctools_plugin_files:', 'cache', TRUE); + } +} + +/** + * Shutdown handler used during ajax operations to help catch fatal errors. + */ +function ctools_shutdown_handler() { + if (function_exists('error_get_last') AND ($error = error_get_last())) { + switch ($error['type']) { + case E_ERROR: + case E_CORE_ERROR: + case E_COMPILE_ERROR: + case E_USER_ERROR: + // Do this manually because including files here is dangerous. + $commands = array( + array( + 'command' => 'alert', + 'title' => t('Error'), + 'text' => t('Unable to complete operation. Fatal error in @file on line @line: @message', array( + '@file' => $error['file'], + '@line' => $error['line'], + '@message' => $error['message'], + )), + ), + ); + + // Change the status code so that the client will read the AJAX returned. + header('HTTP/1.1 200 OK'); + drupal_json($commands); + } + } +} + +/** + * Implements hook_theme(). + */ +function ctools_theme() { + ctools_include('utility'); + $items = array(); + ctools_passthrough('ctools', 'theme', $items); + return $items; +} + +/** + * Implements hook_menu(). + */ +function ctools_menu() { + ctools_include('utility'); + $items = array(); + ctools_passthrough('ctools', 'menu', $items); + return $items; +} + +/** + * Implements hook_permission(). + */ +function ctools_permission() { + return array( + 'use ctools import' => array( + 'title' => t('Use CTools importer'), + 'description' => t('The import functionality allows users to execute arbitrary PHP code, so extreme caution must be taken.'), + 'restrict access' => TRUE, + ), + ); +} + +/** + * Implementation of hook_cron. Clean up old caches. + */ +function ctools_cron() { + ctools_include('utility'); + $items = array(); + ctools_passthrough('ctools', 'cron', $items); +} + +/** + * Implements hook_flush_caches(). + */ +function ctools_flush_caches() { + // Only return the CSS cache bin if it has been activated, to avoid + // drupal_flush_all_caches() from trying to truncate a non-existing table. + return variable_get('cache_class_cache_ctools_css', FALSE) ? array('cache_ctools_css') : array(); +} + +/** + * Implements hook_element_info_alter(). + * + */ +function ctools_element_info_alter(&$type) { + ctools_include('dependent'); + ctools_dependent_element_info_alter($type); +} + +/** + * Implementation of hook_file_download() + * + * When using the private file system, we have to let Drupal know it's ok to + * download CSS and image files from our temporary directory. + */ +function ctools_file_download($filepath) { + if (strpos($filepath, 'ctools') === 0) { + $mime = file_get_mimetype($filepath); + // For safety's sake, we allow only text and images. + if (strpos($mime, 'text') === 0 || strpos($mime, 'image') === 0) { + return array('Content-type:' . $mime); + } + } +} + +/** + * Implements hook_registry_files_alter(). + * + * Alter the registry of files to automagically include all classes in + * class-based plugins. + */ +function ctools_registry_files_alter(&$files, $indexed_modules) { + ctools_include('registry'); + return _ctools_registry_files_alter($files, $indexed_modules); +} + +// ----------------------------------------------------------------------- +// FAPI hooks that must be in the .module file. + +/** + * Alter the comment form to get a little more control over it. + */ +function ctools_form_comment_form_alter(&$form, &$form_state) { + if (!empty($form_state['ctools comment alter'])) { + // Force the form to post back to wherever we are. + $form['#action'] = url($_GET['q'], array('fragment' => 'comment-form')); + if (empty($form['#submit'])) { + $form['#submit'] = array('comment_form_submit'); + } + $form['#submit'][] = 'ctools_node_comment_form_submit'; + } +} + +function ctools_node_comment_form_submit(&$form, &$form_state) { + $form_state['redirect'][0] = $_GET['q']; +} + +// ----------------------------------------------------------------------- +// CTools hook implementations. + +/** + * Implementation of hook_ctools_plugin_directory() to let the system know + * where all our own plugins are. + */ +function ctools_ctools_plugin_directory($owner, $plugin_type) { + if ($owner == 'ctools') { + return 'plugins/' . $plugin_type; + } +} + +/** + * Implements hook_ctools_plugin_type(). + */ +function ctools_ctools_plugin_type() { + ctools_include('utility'); + $items = array(); + // Add all the plugins that have their own declaration space elsewhere. + ctools_passthrough('ctools', 'plugin-type', $items); + + return $items; +} + +// ----------------------------------------------------------------------- +// Drupal theme preprocess hooks that must be in the .module file. + +/** + * A theme preprocess function to automatically allow panels-based node + * templates based upon input when the panel was configured. + */ +function ctools_preprocess_node(&$vars) { + // The 'ctools_template_identifier' attribute of the node is added when the pane is + // rendered. + if (!empty($vars['node']->ctools_template_identifier)) { + $vars['ctools_template_identifier'] = check_plain($vars['node']->ctools_template_identifier); + $vars['theme_hook_suggestions'][] = 'node__panel__' . check_plain($vars['node']->ctools_template_identifier); + } +} + + +/** + * Implements hook_page_alter(). + * + * Last ditch attempt to remove sidebar regions if the "no blocks" + * functionality has been activated. + * + * @see ctools_block_list_alter(). + */ +function ctools_page_alter(&$page) { + $check = drupal_static('ctools_set_no_blocks', TRUE); + if (!$check) { + foreach ($page as $region_id => $region) { + // @todo -- possibly we can set configuration for this so that users can + // specify which blocks will not get rendered. + if (strpos($region_id, 'sidebar') !== FALSE) { + unset($page[$region_id]); + } + } + } + $page['#post_render'][] = 'ctools_page_token_processing'; +} + +/** + * A theme post_render callback to allow content type plugins to use page + * template variables which are not yet available when the content type is + * rendered. + */ +function ctools_page_token_processing($children, $elements) { + $tokens = ctools_set_page_token(); + if (!empty($tokens)) { + foreach ($tokens as $token => $key) { + list($type, $argument) = $key; + switch ($type) { + case 'variable': + $tokens[$token] = isset($elements[$argument]) ? $elements[$argument] : ''; + break; + case 'callback': + if (is_string($argument) && function_exists($argument)) { + $tokens[$token] = $argument($elements); + } + if (is_array($argument) && function_exists($argument[0])) { + $function = array_shift($argument); + $argument = array_merge(array(&$elements), $argument); + $tokens[$token] = call_user_func_array($function, $argument); + } + break; + } + } + $children = strtr($children, $tokens); + } + return $children; +} + +/** + * Implements hook_process(). + * + * Add and remove CSS classes from the variables array. We use process so that + * we alter anything added in the preprocess hooks. + */ +function ctools_process(&$variables, $hook) { + if (!isset($variables['classes'])) { + return; + } + + $classes = drupal_static('ctools_process_classes', array()); + + // Process the classses to add. + if (!empty($classes[$hook]['add'])) { + $add_classes = array_map('drupal_clean_css_identifier', $classes[$hook]['add']); + $variables['classes_array'] = array_unique(array_merge($variables['classes_array'], $add_classes)); + } + + // Process the classes to remove. + if (!empty($classes[$hook]['remove'])) { + $remove_classes = array_map('drupal_clean_css_identifier', $classes[$hook]['remove']); + $variables['classes_array'] = array_diff($variables['classes_array'], $remove_classes); + } + + // Since this runs after template_process(), we need to re-implode the + // classes array. + $variables['classes'] = implode(' ', $variables['classes_array']); +} + +// ----------------------------------------------------------------------- +// Menu callbacks that must be in the .module file. + +/** + * Determine if the current user has access via a plugin. + * + * This function is meant to be embedded in the Drupal menu system, and + * therefore is in the .module file since sub files can't be loaded, and + * takes arguments a little bit more haphazardly than ctools_access(). + * + * @param $access + * An access control array which contains the following information: + * - 'logic': and or or. Whether all tests must pass or one must pass. + * - 'plugins': An array of access plugins. Each contains: + * - - 'name': The name of the plugin + * - - 'settings': The settings from the plugin UI. + * - - 'context': Which context to use. + * @param ... + * zero or more context arguments generated from argument plugins. These + * contexts must have an 'id' attached to them so that they can be + * properly associated. The argument plugin system should set this, but + * if the context is coming from elsewhere it will need to be set manually. + * + * @return + * TRUE if access is granted, false if otherwise. + */ +function ctools_access_menu($access) { + // Short circuit everything if there are no access tests. + if (empty($access['plugins'])) { + return TRUE; + } + + $contexts = array(); + foreach (func_get_args() as $arg) { + if (is_object($arg) && get_class($arg) == 'ctools_context') { + $contexts[$arg->id] = $arg; + } + } + + ctools_include('context'); + return ctools_access($access, $contexts); +} + +/** + * Determine if the current user has access via checks to multiple different + * permissions. + * + * This function is a thin wrapper around user_access that allows multiple + * permissions to be easily designated for use on, for example, a menu callback. + * + * @param ... + * An indexed array of zero or more permission strings to be checked by + * user_access(). + * + * @return + * Iff all checks pass will this function return TRUE. If an invalid argument + * is passed (e.g., not a string), this function errs on the safe said and + * returns FALSE. + */ +function ctools_access_multiperm() { + foreach (func_get_args() as $arg) { + if (!is_string($arg) || !user_access($arg)) { + return FALSE; + } + } + return TRUE; +} + +/** + * Check to see if the incoming menu item is js capable or not. + * + * This can be used as %ctools_js as part of a path in hook menu. CTools + * ajax functions will automatically change the phrase 'nojs' to 'ajax' + * when it attaches ajax to a link. This can be used to autodetect if + * that happened. + */ +function ctools_js_load($js) { + if ($js == 'ajax') { + return TRUE; + } + return 0; +} + +/** + * Provides the default value for %ctools_js. + * + * This allows drupal_valid_path() to work with %ctools_js. + */ +function ctools_js_to_arg($arg) { + return empty($arg) || $arg == '%' ? 'nojs' : $arg; +} + +/** + * Menu _load hook. + * + * This function will be called to load an object as a replacement for + * %ctools_export_ui in menu paths. + */ +function ctools_export_ui_load($item_name, $plugin_name) { + $return = &drupal_static(__FUNCTION__, FALSE); + + if (!$return) { + ctools_include('export-ui'); + $plugin = ctools_get_export_ui($plugin_name); + $handler = ctools_export_ui_get_handler($plugin); + + if ($handler) { + return $handler->load_item($item_name); + } + } + + return $return; +} + +// ----------------------------------------------------------------------- +// Caching callbacks on behalf of export-ui. + +/** + * Menu access callback for various tasks of export-ui. + */ +function ctools_export_ui_task_access($plugin_name, $op, $item = NULL) { + ctools_include('export-ui'); + $plugin = ctools_get_export_ui($plugin_name); + $handler = ctools_export_ui_get_handler($plugin); + + if ($handler) { + return $handler->access($op, $item); + } + + // Deny access if the handler cannot be found. + return FALSE; +} + +/** + * Callback for access control ajax form on behalf of export ui. + * + * Returns the cached access config and contexts used. + * Note that this is assuming that access will be in $item->access -- if it + * is not, an export UI plugin will have to make its own callbacks. + */ +function ctools_export_ui_ctools_access_get($argument) { + ctools_include('export-ui'); + list($plugin_name, $key) = explode(':', $argument, 2); + + $plugin = ctools_get_export_ui($plugin_name); + $handler = ctools_export_ui_get_handler($plugin); + + if ($handler) { + ctools_include('context'); + $item = $handler->edit_cache_get($key); + if (!$item) { + $item = ctools_export_crud_load($handler->plugin['schema'], $key); + } + + $contexts = ctools_context_load_contexts($item); + return array($item->access, $contexts); + } +} + +/** + * Callback for access control ajax form on behalf of export ui + * + * Returns the cached access config and contexts used. + * Note that this is assuming that access will be in $item->access -- if it + * is not, an export UI plugin will have to make its own callbacks. + */ +function ctools_export_ui_ctools_access_set($argument, $access) { + ctools_include('export-ui'); + list($plugin_name, $key) = explode(':', $argument, 2); + + $plugin = ctools_get_export_ui($plugin_name); + $handler = ctools_export_ui_get_handler($plugin); + + if ($handler) { + ctools_include('context'); + $item = $handler->edit_cache_get($key); + if (!$item) { + $item = ctools_export_crud_load($handler->plugin['schema'], $key); + } + $item->access = $access; + return $handler->edit_cache_set_key($item, $key); + } +} + +/** + * Implements hook_menu_local_tasks_alter(). + */ +function ctools_menu_local_tasks_alter(&$data, $router_item, $root_path) { + ctools_include('menu'); + _ctools_menu_add_dynamic_items($data, $router_item, $root_path); +} + +/** + * Implement hook_block_list_alter() to potentially remove blocks. + * + * This exists in order to replicate Drupal 6's "no blocks" functionality. + */ +function ctools_block_list_alter(&$blocks) { + $check = drupal_static('ctools_set_no_blocks', TRUE); + if (!$check) { + foreach ($blocks as $block_id => $block) { + // @todo -- possibly we can set configuration for this so that users can + // specify which blocks will not get rendered. + if (strpos($block->region, 'sidebar') !== FALSE) { + unset($blocks[$block_id]); + } + } + } +} + +/** + * Implements hook_modules_enabled(). + * + * Clear caches for detecting new plugins. + */ +function ctools_modules_enabled($modules) { + ctools_include('plugins'); + ctools_get_plugins_reset(); + cache_clear_all('ctools_plugin_files:', 'cache', TRUE); +} + +/** + * Implements hook_modules_disabled(). + * + * Clear caches for removing disabled plugins. + */ +function ctools_modules_disabled($modules) { + ctools_include('plugins'); + ctools_get_plugins_reset(); + cache_clear_all('ctools_plugin_files:', 'cache', TRUE); +} + +/** + * Menu theme callback. + * + * This simply ensures that Panels ajax calls are rendered in the same + * theme as the original page to prevent .css file confusion. + * + * To use this, set this as the theme callback on AJAX related menu + * items. Since the ajax page state won't be sent during ajax requests, + * it should be safe to use even if ajax isn't invoked. + */ +function ctools_ajax_theme_callback() { + if (!empty($_POST['ajax_page_state']['theme'])) { + return $_POST['ajax_page_state']['theme']; + } +} + +/** + * Implements hook_ctools_entity_context_alter(). + */ +function ctools_ctools_entity_context_alter(&$plugin, &$entity, $plugin_id) { + ctools_include('context'); + switch ($plugin_id) { + case 'entity_id:taxonomy_term': + $plugin['no ui'] = TRUE; + break; + case 'entity:user': + $plugin = ctools_get_context('user'); + unset($plugin['no ui']); + unset($plugin['no required context ui']); + break; + } + + // Apply restrictions on taxonomy term reverse relationships whose + // restrictions are in the settings on the field. + if (!empty($plugin['parent']) && + $plugin['parent'] == 'entity_from_field' && + !empty($plugin['reverse']) && + $plugin['to entity'] == 'taxonomy_term') { + $field = field_info_field($plugin['field name']); + if (isset($field['settings']['allowed_values'][0]['vocabulary'])) { + $plugin['required context']->restrictions = array('vocabulary' => array($field['settings']['allowed_values'][0]['vocabulary'])); + } + } +} + +/** + * Implements hook_field_create_field(). + */ +function ctools_field_create_field($field) { + ctools_flush_field_caches(); +} + +/** + * Implements hook_field_create_instance(). + */ +function ctools_field_create_instance($instance) { + ctools_flush_field_caches(); +} +/** + * Implements hook_field_delete_field(). + */ +function ctools_field_delete_field($field) { + ctools_flush_field_caches(); +} +/** + * Implements hook_field_delete_instance(). + */ +function ctools_field_delete_instance($instance) { + ctools_flush_field_caches(); +} +/** + * Implements hook_field_update_field(). + */ +function ctools_field_update_field($field, $prior_field, $has_data) { + ctools_flush_field_caches(); +} + +/** + * Implements hook_field_update_instance(). + */ +function ctools_field_update_instance($instance, $prior_instance) { + ctools_flush_field_caches(); +} + +/** + * Clear field related caches. + */ +function ctools_flush_field_caches() { + // Clear caches of 'Entity field' content type plugin. + cache_clear_all('ctools_entity_field_content_type_content_types', 'cache'); +} diff --git a/sites/all/modules/contrib/ctools/ctools_access_ruleset/ctools_access_ruleset.info b/sites/all/modules/contrib/ctools/ctools_access_ruleset/ctools_access_ruleset.info new file mode 100644 index 0000000..66ca12c --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_access_ruleset/ctools_access_ruleset.info @@ -0,0 +1,13 @@ +name = Custom rulesets +description = Create custom, exportable, reusable access rulesets for applications like Panels. +core = 7.x +package = Chaos tool suite +version = CTOOLS_MODULE_VERSION +dependencies[] = ctools + +; Information added by Drupal.org packaging script on 2015-08-19 +version = "7.x-1.9" +core = "7.x" +project = "ctools" +datestamp = "1440020680" + diff --git a/sites/all/modules/contrib/ctools/ctools_access_ruleset/ctools_access_ruleset.install b/sites/all/modules/contrib/ctools/ctools_access_ruleset/ctools_access_ruleset.install new file mode 100644 index 0000000..3f00877 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_access_ruleset/ctools_access_ruleset.install @@ -0,0 +1,82 @@ + 'Contains exportable customized access rulesets.', + 'export' => array( + 'identifier' => 'ruleset', + 'bulk export' => TRUE, + 'primary key' => 'rsid', + 'api' => array( + 'owner' => 'ctools_access_ruleset', + 'api' => 'ctools_rulesets', + 'minimum_version' => 1, + 'current_version' => 1, + ), + ), + 'fields' => array( + 'rsid' => array( + 'type' => 'serial', + 'description' => 'A database primary key to ensure uniqueness', + 'not null' => TRUE, + 'no export' => TRUE, + ), + 'name' => array( + 'type' => 'varchar', + 'length' => '255', + 'description' => 'Unique ID for this ruleset. Used to identify it programmatically.', + ), + 'admin_title' => array( + 'type' => 'varchar', + 'length' => '255', + 'description' => 'Administrative title for this ruleset.', + ), + 'admin_description' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'Administrative description for this ruleset.', + 'object default' => '', + ), + 'requiredcontexts' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'Any required contexts for this ruleset.', + 'serialize' => TRUE, + 'object default' => array(), + ), + 'contexts' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'Any embedded contexts for this ruleset.', + 'serialize' => TRUE, + 'object default' => array(), + ), + 'relationships' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'Any relationships for this ruleset.', + 'serialize' => TRUE, + 'object default' => array(), + ), + 'access' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'The actual group of access plugins for this ruleset.', + 'serialize' => TRUE, + 'object default' => array(), + ), + ), + 'primary key' => array('rsid'), + ); + + return $schema; +} diff --git a/sites/all/modules/contrib/ctools/ctools_access_ruleset/ctools_access_ruleset.module b/sites/all/modules/contrib/ctools/ctools_access_ruleset/ctools_access_ruleset.module new file mode 100644 index 0000000..fb39f37 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_access_ruleset/ctools_access_ruleset.module @@ -0,0 +1,85 @@ + array( + 'title' => t('Administer access rulesets'), + 'description' => t('Add, delete and edit custom access rulesets.'), + ), + ); +} + +/** + * Implementation of hook_ctools_plugin_directory() to let the system know + * we implement task and task_handler plugins. + */ +function ctools_access_ruleset_ctools_plugin_directory($module, $plugin) { + // Most of this module is implemented as an export ui plugin, and the + // rest is in ctools/includes/ctools_access_ruleset.inc + if ($module == 'ctools' && ($plugin == 'export_ui' || $plugin == 'access')) { + return 'plugins/' . $plugin; + } +} + +/** + * Implementation of hook_panels_dashboard_blocks(). + * + * Adds page information to the Panels dashboard. + */ +function ctools_access_ruleset_panels_dashboard_blocks(&$vars) { + $vars['links']['ctools_access_ruleset'] = array( + 'title' => l(t('Custom ruleset'), 'admin/structure/ctools-rulesets/add'), + 'description' => t('Custom rulesets are combinations of access plugins you can use for access control, selection criteria and pane visibility.'), + ); + + // Load all mini panels and their displays. + ctools_include('export'); + $items = ctools_export_crud_load_all('ctools_access_ruleset'); + $count = 0; + $rows = array(); + + foreach ($items as $item) { + $rows[] = array( + check_plain($item->admin_title), + array( + 'data' => l(t('Edit'), "admin/structure/ctools-rulesets/list/$item->name/edit"), + 'class' => 'links', + ), + ); + + // Only show 10. + if (++$count >= 10) { + break; + } + } + + if ($rows) { + $content = theme('table', array('rows' => $rows, 'attributes' => array('class' => 'panels-manage'))); + } + else { + $content = '

' . t('There are no custom rulesets.') . '

'; + } + + $vars['blocks']['ctools_access_ruleset'] = array( + 'title' => t('Manage custom rulesets'), + 'link' => l(t('Go to list'), 'admin/structure/ctools-rulesets'), + 'content' => $content, + 'class' => 'dashboard-ruleset', + 'section' => 'right', + ); +} diff --git a/sites/all/modules/contrib/ctools/ctools_access_ruleset/plugins/access/ruleset.inc b/sites/all/modules/contrib/ctools/ctools_access_ruleset/plugins/access/ruleset.inc new file mode 100644 index 0000000..f8abea6 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_access_ruleset/plugins/access/ruleset.inc @@ -0,0 +1,109 @@ + '', + 'description' => '', + 'callback' => 'ctools_ruleset_ctools_access_check', + 'settings form' => 'ctools_ruleset_ctools_access_settings', + 'summary' => 'ctools_ruleset_ctools_access_summary', + + // This access plugin actually just contains child plugins that are + // exportable, UI configured rulesets. + 'get child' => 'ctools_ruleset_ctools_access_get_child', + 'get children' => 'ctools_ruleset_ctools_access_get_children', +); + +/** + * Merge the main access plugin with a loaded ruleset to form a child plugin. + */ +function ctools_ruleset_ctools_access_merge_plugin($plugin, $parent, $item) { + $plugin['name'] = $parent . ':' . $item->name; + $plugin['title'] = check_plain($item->admin_title); + $plugin['description'] = check_plain($item->admin_description); + + // TODO: Generalize this in CTools. + if (!empty($item->requiredcontexts)) { + $plugin['required context'] = array(); + foreach ($item->requiredcontexts as $context) { + $info = ctools_get_context($context['name']); + // TODO: allow an optional setting + $plugin['required context'][] = new ctools_context_required($context['identifier'], $info['context name']); + } + } + + // Store the loaded ruleset in the plugin. + $plugin['ruleset'] = $item; + return $plugin; +} + +/** + * Get a single child access plugin. + */ +function ctools_ruleset_ctools_access_get_child($plugin, $parent, $child) { + ctools_include('export'); + $item = ctools_export_crud_load('ctools_access_ruleset', $child); + if ($item) { + return ctools_ruleset_ctools_access_merge_plugin($plugin, $parent, $item); + } +} + +/** + * Get all child access plugins. + */ +function ctools_ruleset_ctools_access_get_children($plugin, $parent) { + $plugins = array(); + ctools_include('export'); + $items = ctools_export_crud_load_all('ctools_access_ruleset'); + foreach ($items as $name => $item) { + $child = ctools_ruleset_ctools_access_merge_plugin($plugin, $parent, $item); + $plugins[$child['name']] = $child; + } + + return $plugins; +} + +/** + * Settings form for the 'by ruleset' access plugin + */ +function ctools_ruleset_ctools_access_settings(&$form, &$form_state, $conf) { + if (!empty($form_state['plugin']['ruleset']->admin_description)) { + $form['markup'] = array( + '#markup' => '
' . check_plain($form_state['plugin']['ruleset']->admin_description) . '
', + ); + } + + return $form; +} + +/** + * Check for access. + */ +function ctools_ruleset_ctools_access_check($conf, $context, $plugin) { + // Load up any contexts we might be using. + $contexts = ctools_context_match_required_contexts($plugin['ruleset']->requiredcontexts, $context); + $contexts = ctools_context_load_contexts($plugin['ruleset'], FALSE, $contexts); + + return ctools_access($plugin['ruleset']->access, $contexts); +} + +/** + * Provide a summary description based upon the checked roles. + */ +function ctools_ruleset_ctools_access_summary($conf, $context, $plugin) { + if (!empty($plugin['ruleset']->admin_description)) { + return check_plain($plugin['ruleset']->admin_description); + } + else { + return check_plain($plugin['ruleset']->admin_title); + } +} + diff --git a/sites/all/modules/contrib/ctools/ctools_access_ruleset/plugins/export_ui/ctools_access_ruleset.inc b/sites/all/modules/contrib/ctools/ctools_access_ruleset/plugins/export_ui/ctools_access_ruleset.inc new file mode 100644 index 0000000..d2a1c60 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_access_ruleset/plugins/export_ui/ctools_access_ruleset.inc @@ -0,0 +1,29 @@ + 'ctools_access_ruleset', + 'access' => 'administer ctools access ruleset', + + 'menu' => array( + 'menu item' => 'ctools-rulesets', + 'menu title' => 'Custom access rulesets', + 'menu description' => 'Add, edit or delete custom access rulesets for use with Panels and other systems that utilize CTools content plugins.', + ), + + 'title singular' => t('ruleset'), + 'title singular proper' => t('Ruleset'), + 'title plural' => t('rulesets'), + 'title plural proper' => t('Rulesets'), + + 'handler' => 'ctools_access_ruleset_ui', + + 'use wizard' => TRUE, + 'form info' => array( + 'order' => array( + 'basic' => t('Basic information'), + 'context' => t('Contexts'), + 'rules' => t('Rules'), + ), + ), +); + diff --git a/sites/all/modules/contrib/ctools/ctools_access_ruleset/plugins/export_ui/ctools_access_ruleset_ui.class.php b/sites/all/modules/contrib/ctools/ctools_access_ruleset/plugins/export_ui/ctools_access_ruleset_ui.class.php new file mode 100644 index 0000000..b181464 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_access_ruleset/plugins/export_ui/ctools_access_ruleset_ui.class.php @@ -0,0 +1,53 @@ + '
', + '#suffix' => '
', + ); + + $form['left'] = array( + '#prefix' => '
', + '#suffix' => '
', + ); + + // Set this up and we can use CTools' Export UI's built in wizard caching, + // which already has callbacks for the context cache under this name. + $module = 'export_ui::' . $this->plugin['name']; + $name = $this->edit_cache_get_key($form_state['item'], $form_state['form type']); + + ctools_context_add_context_form($module, $form, $form_state, $form['right']['contexts_table'], $form_state['item'], $name); + ctools_context_add_required_context_form($module, $form, $form_state, $form['left']['required_contexts_table'], $form_state['item'], $name); + ctools_context_add_relationship_form($module, $form, $form_state, $form['right']['relationships_table'], $form_state['item'], $name); + } + + function edit_form_rules(&$form, &$form_state) { + // The 'access' UI passes everything via $form_state, unlike the 'context' UI. + // The main difference is that one is about 3 years newer than the other. + ctools_include('context'); + ctools_include('context-access-admin'); + + $form_state['access'] = $form_state['item']->access; + $form_state['contexts'] = ctools_context_load_contexts($form_state['item']); + + $form_state['module'] = 'ctools_export_ui'; + $form_state['callback argument'] = $form_state['object']->plugin['name'] . ':' . $form_state['object']->edit_cache_get_key($form_state['item'], $form_state['form type']); + $form_state['no buttons'] = TRUE; + + $form = ctools_access_admin_form($form, $form_state); + } + + function edit_form_rules_submit(&$form, &$form_state) { + $form_state['item']->access['logic'] = $form_state['values']['logic']; + } + + function edit_form_submit(&$form, &$form_state) { + parent::edit_form_submit($form, $form_state); + } +} diff --git a/sites/all/modules/contrib/ctools/ctools_ajax_sample/css/ctools-ajax-sample.css b/sites/all/modules/contrib/ctools/ctools_ajax_sample/css/ctools-ajax-sample.css new file mode 100644 index 0000000..8df17de --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_ajax_sample/css/ctools-ajax-sample.css @@ -0,0 +1,134 @@ +div.ctools-sample-modal-content { + background:none; + border:0; + color:#000000; + margin:0; + padding:0; + text-align:left; +} +div.ctools-sample-modal-content .modal-scroll{ + overflow:hidden; + overflow-y:auto; +} +div.ctools-sample-modal-content #popups-overlay { + background-color:transparent; +} +div.ctools-sample-modal-content #popups-loading { + width:248px; + position:absolute; + display:none; + opacity:1; + -moz-border-radius: 8px; + -webkit-border-radius: 8px; + z-index:99; +} +div.ctools-sample-modal-content #popups-loading span.popups-loading-message { + background:#FFF url(../images/loading-large.gif) no-repeat 8px center; + display:block; + color:#444444; + font-family:Arial; + font-size:22px; + font-weight:bold; + height:36px; + line-height:36px; + padding:0 40px; +} +div.ctools-sample-modal-content #popups-loading table, +div.ctools-sample-modal-content .popups-box table { + margin:0px; +} +div.ctools-sample-modal-content #popups-loading tbody, +div.ctools-sample-modal-content .popups-box tbody { + border:none; +} +div.ctools-sample-modal-content .popups-box tr { + background-color:transparent; +} +div.ctools-sample-modal-content td.popups-border { + background: url(../images/popups-border.png); + background-color:transparent; + border: none; +} +div.ctools-sample-modal-content td.popups-tl, +div.ctools-sample-modal-content td.popups-tr, +div.ctools-sample-modal-content td.popups-bl, +div.ctools-sample-modal-content td.popups-br { + background-repeat: no-repeat; + height:10px; + padding:0px; +} +div.ctools-sample-modal-content td.popups-tl { background-position: 0px 0px; } +div.ctools-sample-modal-content td.popups-t, +div.ctools-sample-modal-content td.popups-b { + background-position: 0px -40px; + background-repeat: repeat-x; +} +div.ctools-sample-modal-content td.popups-tr { background-position: 0px -10px; width: 10px; } +div.ctools-sample-modal-content td.popups-cl, +div.ctools-sample-modal-content td.popups-cr { + background-position: -10px 0; + background-repeat: repeat-y; + width:10px; +} +div.ctools-sample-modal-content td.popups-cl, +div.ctools-sample-modal-content td.popups-cr, +div.ctools-sample-modal-content td.popups-c { padding:0; border: none; } +div.ctools-sample-modal-content td.popups-c { background:#fff; } +div.ctools-sample-modal-content td.popups-bl { background-position: 0px -20px; } +div.ctools-sample-modal-content td.popups-br { background-position: 0px -30px; width: 10px; } + +div.ctools-sample-modal-content .popups-box, +div.ctools-sample-modal-content #popups-loading { + border: 0px solid #454545; + opacity:1; + overflow:hidden; + padding:0; + background-color:transparent; +} +div.ctools-sample-modal-content .popups-container { + overflow:hidden; + height:100%; + background-color:#fff; +} +div.ctools-sample-modal-content div.popups-title { + -moz-border-radius-topleft: 0px; + -webkit-border-radius-topleft: 0px; + margin-bottom:0px; + background-color:#ff7200; + border:1px solid #ce5c00; + padding:4px 10px 5px; + color:white; + font-size:1em; + font-weight:bold; +} +div.ctools-sample-modal-content .popups-body { + background-color:#fff; + padding:8px; +} +div.ctools-sample-modal-content .popups-box .popups-buttons, +div.ctools-sample-modal-content .popups-box .popups-footer { + background-color:#fff; +} +div.ctools-sample-modal-content .popups-title a.close { + color: #fff; + text-decoration:none; +} +div.ctools-sample-modal-content .popups-close { + font-size:120%; + float:right; + text-align:right; +} +div.ctools-sample-modal-content .modal-loading-wrapper { + width:220px; + height:19px; + margin:0 auto; + margin-top:2%; +} + +div.ctools-sample-modal-content tbody{ + border:none; +} + +div.ctools-sample-modal-content .modal-content .modal-throbber-wrapper img { + margin-top: 100px; +} diff --git a/sites/all/modules/contrib/ctools/ctools_ajax_sample/ctools_ajax_sample.info b/sites/all/modules/contrib/ctools/ctools_ajax_sample/ctools_ajax_sample.info new file mode 100644 index 0000000..f5d1e74 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_ajax_sample/ctools_ajax_sample.info @@ -0,0 +1,13 @@ +name = Chaos Tools (CTools) AJAX Example +description = Shows how to use the power of Chaos AJAX. +package = Chaos tool suite +version = CTOOLS_MODULE_VERSION +dependencies[] = ctools +core = 7.x + +; Information added by Drupal.org packaging script on 2015-08-19 +version = "7.x-1.9" +core = "7.x" +project = "ctools" +datestamp = "1440020680" + diff --git a/sites/all/modules/contrib/ctools/ctools_ajax_sample/ctools_ajax_sample.install b/sites/all/modules/contrib/ctools/ctools_ajax_sample/ctools_ajax_sample.install new file mode 100644 index 0000000..04325db --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_ajax_sample/ctools_ajax_sample.install @@ -0,0 +1,19 @@ + 'Chaos Tools AJAX Demo', + 'page callback' => 'ctools_ajax_sample_page', + 'access callback' => TRUE, + 'type' => MENU_NORMAL_ITEM, + ); + $items['ctools_ajax_sample/simple_form'] = array( + 'title' => 'Simple Form', + 'page callback' => 'ctools_ajax_simple_form', + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + $items['ctools_ajax_sample/%ctools_js/hello'] = array( + 'title' => 'Hello World', + 'page callback' => 'ctools_ajax_sample_hello', + 'page arguments' => array(1), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + $items['ctools_ajax_sample/%ctools_js/tablenix/%'] = array( + 'title' => 'Hello World', + 'page callback' => 'ctools_ajax_sample_tablenix', + 'page arguments' => array(1, 3), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + $items['ctools_ajax_sample/%ctools_js/login'] = array( + 'title' => 'Login', + 'page callback' => 'ctools_ajax_sample_login', + 'page arguments' => array(1), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + $items['ctools_ajax_sample/%ctools_js/animal'] = array( + 'title' => 'Animal', + 'page callback' => 'ctools_ajax_sample_animal', + 'page arguments' => array(1), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + $items['ctools_ajax_sample/%ctools_js/login/%'] = array( + 'title' => 'Post-Login Action', + 'page callback' => 'ctools_ajax_sample_login_success', + 'page arguments' => array(1, 3), + 'access callback' => TRUE, + 'type' => MENU_CALLBACK, + ); + $items['ctools_ajax_sample/jumped'] = array( + 'title' => 'Successful Jumping', + 'page callback' => 'ctools_ajax_sample_jump_menu_page', + 'access callback' => TRUE, + 'type' => MENU_NORMAL_ITEM, + ); + + return $items; +} + +function ctools_ajax_simple_form() { + ctools_include('content'); + ctools_include('context'); + $node = node_load(1); + $context = ctools_context_create('node', $node); + $context = array('context_node_1' => $context); + return ctools_content_render('node_comment_form', 'node_comment_form', ctools_ajax_simple_form_pane(), array(), array(), $context); +} + +function ctools_ajax_simple_form_pane() { + $configuration = array( + 'anon_links' => 0, + 'context' => 'context_node_1', + 'override_title' => 0, + 'override_title_text' => '', + ); + return $configuration; +} + +/** + * Implementation of hook_theme() + * + * Render some basic output for this module. + */ +function ctools_ajax_sample_theme() { + return array( + // Sample theme functions. + 'ctools_ajax_sample_container' => array( + 'arguments' => array('content' => NULL), + ), + ); +} + +// --------------------------------------------------------------------------- +// Page callbacks + +/** + * Page callback to display links and render a container for AJAX stuff. + */ +function ctools_ajax_sample_page() { + global $user; + + // Include the CTools tools that we need. + ctools_include('ajax'); + ctools_include('modal'); + + // Add CTools' javascript to the page. + ctools_modal_add_js(); + + // Create our own javascript that will be used to theme a modal. + $sample_style = array( + 'ctools-sample-style' => array( + 'modalSize' => array( + 'type' => 'fixed', + 'width' => 500, + 'height' => 300, + 'addWidth' => 20, + 'addHeight' => 15, + ), + 'modalOptions' => array( + 'opacity' => .5, + 'background-color' => '#000', + ), + 'animation' => 'fadeIn', + 'modalTheme' => 'CToolsSampleModal', + 'throbber' => theme('image', array('path' => ctools_image_path('ajax-loader.gif', 'ctools_ajax_sample'), 'alt' => t('Loading...'), 'title' => t('Loading'))), + ), + ); + + drupal_add_js($sample_style, 'setting'); + + // Since we have our js, css and images in well-known named directories, + // CTools makes it easy for us to just use them without worrying about + // using drupal_get_path() and all that ugliness. + ctools_add_js('ctools-ajax-sample', 'ctools_ajax_sample'); + ctools_add_css('ctools-ajax-sample', 'ctools_ajax_sample'); + + // Create a list of clickable links. + $links = array(); + + // Only show login links to the anonymous user. + if ($user->uid == 0) { + $links[] = ctools_modal_text_button(t('Modal Login (default style)'), 'ctools_ajax_sample/nojs/login', t('Login via modal')); + + // The extra class points to the info in ctools-sample-style which we added + // to the settings, prefixed with 'ctools-modal'. + $links[] = ctools_modal_text_button(t('Modal Login (custom style)'), 'ctools_ajax_sample/nojs/login', t('Login via modal'), 'ctools-modal-ctools-sample-style'); + } + + // Four ways to do our animal picking wizard. + $button_form = ctools_ajax_sample_ajax_button_form(); + $links[] = l(t('Wizard (no modal)'), 'ctools_ajax_sample/nojs/animal'); + $links[] = ctools_modal_text_button(t('Wizard (default modal)'), 'ctools_ajax_sample/nojs/animal', t('Pick an animal')); + $links[] = ctools_modal_text_button(t('Wizard (custom modal)'), 'ctools_ajax_sample/nojs/animal', t('Pick an animal'), 'ctools-modal-ctools-sample-style'); + $links[] = drupal_render($button_form); + + $links[] = ctools_ajax_text_button(t('Hello world!'), "ctools_ajax_sample/nojs/hello", t('Replace text with "hello world"')); + + $output = theme('item_list', array('items' => $links, 'title' => t('Actions'))); + + // This container will have data AJAXed into it. + $output .= theme('ctools_ajax_sample_container', array('content' => '

' . t('Sample Content') . '

')); + + // Create a table that we can have data removed from via AJAX. + $header = array(t('Row'), t('Content'), t('Actions')); + $rows = array(); + for($i = 1; $i < 11; $i++) { + $rows[] = array( + 'class' => array('ajax-sample-row-'. $i), + 'data' => array( + $i, + md5($i), + ctools_ajax_text_button("remove", "ctools_ajax_sample/nojs/tablenix/$i", t('Delete this row')), + ), + ); + } + + $output .= theme('table', array('header' => $header, 'rows' => $rows, array('class' => array('ajax-sample-table')))); + + // Show examples of ctools javascript widgets + $output .= '

'. t('CTools Javascript Widgets') .'

'; + + // Create a drop down menu + $links = array(); + $links[] = array('title' => t('Link 1'), 'href' => $_GET['q']); + $links[] = array('title' => t('Link 2'), 'href' => $_GET['q']); + $links[] = array('title' => t('Link 3'), 'href' => $_GET['q']); + + $output .= '

' . t('Drop Down Menu') . '

'; + $output .= theme('ctools_dropdown', array('title' => t('Click to Drop Down'), 'links' => $links)); + + // Create a collapsible div + $handle = t('Click to Collapse'); + $content = 'Nulla ligula ante, aliquam at adipiscing egestas, varius vel arcu. Etiam laoreet elementum mi vel consequat. Etiam scelerisque lorem vel neque consequat quis bibendum libero congue. Nulla facilisi. Mauris a elit a leo feugiat porta. Phasellus placerat cursus est vitae elementum.'; + $output .= '

'. t('Collapsible Div') .'

'; + $output .= theme('ctools_collapsible', array('handle' => $handle, 'content' => $content, 'collapsed' => FALSE)); + + // Create a jump menu + ctools_include('jump-menu'); + $form = drupal_get_form('ctools_ajax_sample_jump_menu_form'); + $output .= '

'. t('Jump Menu') .'

'; + $output .= drupal_render($form); + + return array('markup' => array('#markup' => $output)); +} + +/** + * Returns a "take it all over" hello world style request. + */ +function ctools_ajax_sample_hello($js = NULL) { + $output = '

' . t('Hello World') . '

'; + if ($js) { + ctools_include('ajax'); + $commands = array(); + $commands[] = ajax_command_html('#ctools-sample', $output); + print ajax_render($commands); // this function exits. + exit; + } + else { + return $output; + } +} + +/** + * Nix a row from a table and restripe. + */ +function ctools_ajax_sample_tablenix($js, $row) { + if (!$js) { + // We don't support degrading this from js because we're not + // using the server to remember the state of the table. + return MENU_ACCESS_DENIED; + } + ctools_include('ajax'); + + $commands = array(); + $commands[] = ajax_command_remove("tr.ajax-sample-row-$row"); + $commands[] = ajax_command_restripe("table.ajax-sample-table"); + print ajax_render($commands); + exit; +} + +/** + * A modal login callback. + */ +function ctools_ajax_sample_login($js = NULL) { + // Fall back if $js is not set. + if (!$js) { + return drupal_get_form('user_login'); + } + + ctools_include('modal'); + ctools_include('ajax'); + $form_state = array( + 'title' => t('Login'), + 'ajax' => TRUE, + ); + $output = ctools_modal_form_wrapper('user_login', $form_state); + if (!empty($form_state['executed'])) { + // We'll just overwrite the form output if it was successful. + $output = array(); + $inplace = ctools_ajax_text_button(t('remain here'), 'ctools_ajax_sample/nojs/login/inplace', t('Go to your account')); + $account = ctools_ajax_text_button(t('your account'), 'ctools_ajax_sample/nojs/login/user', t('Go to your account')); + $output[] = ctools_modal_command_display(t('Login Success'), ''); + } + print ajax_render($output); + exit; +} + +/** + * Post-login processor: should we go to the user account or stay in place? + */ +function ctools_ajax_sample_login_success($js, $action) { + if (!$js) { + // we should never be here out of ajax context + return MENU_NOT_FOUND; + } + + ctools_include('ajax'); + ctools_add_js('ajax-responder'); + $commands = array(); + if ($action == 'inplace') { + // stay here + $commands[] = ctools_ajax_command_reload(); + } + else { + // bounce bounce + $commands[] = ctools_ajax_command_redirect('user'); + } + print ajax_render($commands); + exit; +} + +/** + * A modal login callback. + */ +function ctools_ajax_sample_animal($js = NULL, $step = NULL) { + if ($js) { + ctools_include('modal'); + ctools_include('ajax'); + } + + $form_info = array( + 'id' => 'animals', + 'path' => "ctools_ajax_sample/" . ($js ? 'ajax' : 'nojs') . "/animal/%step", + 'show trail' => TRUE, + 'show back' => TRUE, + 'show cancel' => TRUE, + 'show return' => FALSE, + 'next callback' => 'ctools_ajax_sample_wizard_next', + 'finish callback' => 'ctools_ajax_sample_wizard_finish', + 'cancel callback' => 'ctools_ajax_sample_wizard_cancel', + // this controls order, as well as form labels + 'order' => array( + 'start' => t('Choose animal'), + ), + // here we map a step to a form id. + 'forms' => array( + // e.g. this for the step at wombat/create + 'start' => array( + 'form id' => 'ctools_ajax_sample_start' + ), + ), + ); + + // We're not using any real storage here, so we're going to set our + // object_id to 1. When using wizard forms, id management turns + // out to be one of the hardest parts. Editing an object with an id + // is easy, but new objects don't usually have ids until somewhere + // in creation. + // + // We skip all this here by just using an id of 1. + + $object_id = 1; + + if (empty($step)) { + // We reset the form when $step is NULL because that means they have + // for whatever reason started over. + ctools_ajax_sample_cache_clear($object_id); + $step = 'start'; + } + + // This automatically gets defaults if there wasn't anything saved. + $object = ctools_ajax_sample_cache_get($object_id); + + $animals = ctools_ajax_sample_animals(); + + // Make sure we can't somehow accidentally go to an invalid animal. + if (empty($animals[$object->type])) { + $object->type = 'unknown'; + } + + // Now that we have our object, dynamically add the animal's form. + if ($object->type == 'unknown') { + // If they haven't selected a type, add a form that doesn't exist yet. + $form_info['order']['unknown'] = t('Configure animal'); + $form_info['forms']['unknown'] = array('form id' => 'nothing'); + } + else { + // Add the selected animal to the order so that it shows up properly in the trail. + $form_info['order'][$object->type] = $animals[$object->type]['config title']; + } + + // Make sure all animals forms are represented so that the next stuff can + // work correctly: + foreach ($animals as $id => $animal) { + $form_info['forms'][$id] = array('form id' => $animals[$id]['form']); + } + + $form_state = array( + 'ajax' => $js, + // Put our object and ID into the form state cache so we can easily find + // it. + 'object_id' => $object_id, + 'object' => &$object, + ); + + // Send this all off to our form. This is like drupal_get_form only wizardy. + ctools_include('wizard'); + $form = ctools_wizard_multistep_form($form_info, $step, $form_state); + $output = drupal_render($form); + + if ($output === FALSE || !empty($form_state['complete'])) { + // This creates a string based upon the animal and its setting using + // function indirection. + $animal = $animals[$object->type]['output']($object); + } + + // If $output is FALSE, there was no actual form. + if ($js) { + // If javascript is active, we have to use a render array. + $commands = array(); + if ($output === FALSE || !empty($form_state['complete'])) { + // Dismiss the modal. + $commands[] = ajax_command_html('#ctools-sample', $animal); + $commands[] = ctools_modal_command_dismiss(); + } + else if (!empty($form_state['cancel'])) { + // If cancelling, return to the activity. + $commands[] = ctools_modal_command_dismiss(); + } + else { + $commands = ctools_modal_form_render($form_state, $output); + } + print ajax_render($commands); + exit; + } + else { + if ($output === FALSE || !empty($form_state['complete'])) { + return $animal; + } + else if (!empty($form_state['cancel'])) { + drupal_goto('ctools_ajax_sample'); + } + else { + return $output; + } + } +} + +// --------------------------------------------------------------------------- +// Themes + +/** + * Theme function for main rendered output. + */ +function theme_ctools_ajax_sample_container($vars) { + $output = '
'; + $output .= $vars['content']; + $output .= '
'; + + return $output; +} + +// --------------------------------------------------------------------------- +// Stuff needed for our little wizard. + +/** + * Get a list of our animals and associated forms. + * + * What we're doing is making it easy to add more animals in just one place, + * which is often how it will work in the real world. If using CTools, what + * you would probably really have, here, is a set of plugins for each animal. + */ +function ctools_ajax_sample_animals() { + return array( + 'sheep' => array( + 'title' => t('Sheep'), + 'config title' => t('Configure sheep'), + 'form' => 'ctools_ajax_sample_configure_sheep', + 'output' => 'ctools_ajax_sample_show_sheep', + ), + 'lizard' => array( + 'title' => t('Lizard'), + 'config title' => t('Configure lizard'), + 'form' => 'ctools_ajax_sample_configure_lizard', + 'output' => 'ctools_ajax_sample_show_lizard', + ), + 'raptor' => array( + 'title' => t('Raptor'), + 'config title' => t('Configure raptor'), + 'form' => 'ctools_ajax_sample_configure_raptor', + 'output' => 'ctools_ajax_sample_show_raptor', + ), + ); +} + +// --------------------------------------------------------------------------- +// Wizard caching helpers. + +/** + * Store our little cache so that we can retain data from form to form. + */ +function ctools_ajax_sample_cache_set($id, $object) { + ctools_include('object-cache'); + ctools_object_cache_set('ctools_ajax_sample', $id, $object); +} + +/** + * Get the current object from the cache, or default. + */ +function ctools_ajax_sample_cache_get($id) { + ctools_include('object-cache'); + $object = ctools_object_cache_get('ctools_ajax_sample', $id); + if (!$object) { + // Create a default object. + $object = new stdClass; + $object->type = 'unknown'; + $object->name = ''; + } + + return $object; +} + +/** + * Clear the wizard cache. + */ +function ctools_ajax_sample_cache_clear($id) { + ctools_include('object-cache'); + ctools_object_cache_clear('ctools_ajax_sample', $id); +} + +// --------------------------------------------------------------------------- +// Wizard in-between helpers; what to do between or after forms. + +/** + * Handle the 'next' click on the add/edit pane form wizard. + * + * All we need to do is store the updated pane in the cache. + */ +function ctools_ajax_sample_wizard_next(&$form_state) { + ctools_ajax_sample_cache_set($form_state['object_id'], $form_state['object']); +} + +/** + * Handle the 'finish' click on the add/edit pane form wizard. + * + * All we need to do is set a flag so the return can handle adding + * the pane. + */ +function ctools_ajax_sample_wizard_finish(&$form_state) { + $form_state['complete'] = TRUE; +} + +/** + * Handle the 'cancel' click on the add/edit pane form wizard. + */ +function ctools_ajax_sample_wizard_cancel(&$form_state) { + $form_state['cancel'] = TRUE; +} + +// --------------------------------------------------------------------------- +// Wizard forms for our simple info collection wizard. + +/** + * Wizard start form. Choose an animal. + */ +function ctools_ajax_sample_start($form, &$form_state) { + $form_state['title'] = t('Choose animal'); + + $animals = ctools_ajax_sample_animals(); + foreach ($animals as $id => $animal) { + $options[$id] = $animal['title']; + } + + $form['type'] = array( + '#title' => t('Choose your animal'), + '#type' => 'radios', + '#options' => $options, + '#default_value' => $form_state['object']->type, + '#required' => TRUE, + ); + + return $form; +} + +/** + * They have selected a sheep. Set it. + */ +function ctools_ajax_sample_start_submit(&$form, &$form_state) { + $form_state['object']->type = $form_state['values']['type']; + // Override where to go next based on the animal selected. + $form_state['clicked_button']['#next'] = $form_state['values']['type']; +} + +/** + * Wizard form to configure your sheep. + */ +function ctools_ajax_sample_configure_sheep($form, &$form_state) { + $form_state['title'] = t('Configure sheep'); + + $form['name'] = array( + '#type' => 'textfield', + '#title' => t('Name your sheep'), + '#default_value' => $form_state['object']->name, + '#required' => TRUE, + ); + + $form['sheep'] = array( + '#title' => t('What kind of sheep'), + '#type' => 'radios', + '#options' => array( + t('Wensleydale') => t('Wensleydale'), + t('Merino') => t('Merino'), + t('Corriedale') => t('Coriedale'), + ), + '#default_value' => !empty($form_state['object']->sheep) ? $form_state['object']->sheep : '', + '#required' => TRUE, + ); + return $form; +} + +/** + * Submit the sheep and store the values from the form. + */ +function ctools_ajax_sample_configure_sheep_submit(&$form, &$form_state) { + $form_state['object']->name = $form_state['values']['name']; + $form_state['object']->sheep = $form_state['values']['sheep']; +} + +/** + * Provide some output for our sheep. + */ +function ctools_ajax_sample_show_sheep($object) { + return t('You have a @type sheep named "@name".', array( + '@type' => $object->sheep, + '@name' => $object->name, + )); +} + +/** + * Wizard form to configure your lizard. + */ +function ctools_ajax_sample_configure_lizard($form, &$form_state) { + $form_state['title'] = t('Configure lizard'); + + $form['name'] = array( + '#type' => 'textfield', + '#title' => t('Name your lizard'), + '#default_value' => $form_state['object']->name, + '#required' => TRUE, + ); + + $form['lizard'] = array( + '#title' => t('Venomous'), + '#type' => 'checkbox', + '#default_value' => !empty($form_state['object']->lizard), + ); + return $form; +} + +/** + * Submit the lizard and store the values from the form. + */ +function ctools_ajax_sample_configure_lizard_submit(&$form, &$form_state) { + $form_state['object']->name = $form_state['values']['name']; + $form_state['object']->lizard = $form_state['values']['lizard']; +} + +/** + * Provide some output for our raptor. + */ +function ctools_ajax_sample_show_lizard($object) { + return t('You have a @type lizard named "@name".', array( + '@type' => empty($object->lizard) ? t('non-venomous') : t('venomous'), + '@name' => $object->name, + )); +} + +/** + * Wizard form to configure your raptor. + */ +function ctools_ajax_sample_configure_raptor($form, &$form_state) { + $form_state['title'] = t('Configure raptor'); + + $form['name'] = array( + '#type' => 'textfield', + '#title' => t('Name your raptor'), + '#default_value' => $form_state['object']->name, + '#required' => TRUE, + ); + + $form['raptor'] = array( + '#title' => t('What kind of raptor'), + '#type' => 'radios', + '#options' => array( + t('Eagle') => t('Eagle'), + t('Hawk') => t('Hawk'), + t('Owl') => t('Owl'), + t('Buzzard') => t('Buzzard'), + ), + '#default_value' => !empty($form_state['object']->raptor) ? $form_state['object']->raptor : '', + '#required' => TRUE, + ); + + $form['domesticated'] = array( + '#title' => t('Domesticated'), + '#type' => 'checkbox', + '#default_value' => !empty($form_state['object']->domesticated), + ); + return $form; +} + +/** + * Submit the raptor and store the values from the form. + */ +function ctools_ajax_sample_configure_raptor_submit(&$form, &$form_state) { + $form_state['object']->name = $form_state['values']['name']; + $form_state['object']->raptor = $form_state['values']['raptor']; + $form_state['object']->domesticated = $form_state['values']['domesticated']; +} + +/** + * Provide some output for our raptor. + */ +function ctools_ajax_sample_show_raptor($object) { + return t('You have a @type @raptor named "@name".', array( + '@type' => empty($object->domesticated) ? t('wild') : t('domesticated'), + '@raptor' => $object->raptor, + '@name' => $object->name, + )); +} + +/** + * Helper function to provide a sample jump menu form + */ +function ctools_ajax_sample_jump_menu_form() { + $url = url('ctools_ajax_sample/jumped'); + $form_state = array(); + $form = ctools_jump_menu(array(), $form_state, array($url => t('Jump!')), array()); + return $form; +} + +/** + * Provide a message to the user that the jump menu worked + */ +function ctools_ajax_sample_jump_menu_page() { + $return_link = l(t('Return to the examples page.'), 'ctools_ajax_sample'); + $output = t('You successfully jumped! !return_link', array('!return_link' => $return_link)); + return $output; +} + +/** + * Provide a form for an example ajax modal button + */ +function ctools_ajax_sample_ajax_button_form() { + $form = array(); + + $form['url'] = array( + '#type' => 'hidden', + // The name of the class is the #id of $form['ajax_button'] with "-url" + // suffix. + '#attributes' => array('class' => array('ctools-ajax-sample-button-url')), + '#value' => url('ctools_ajax_sample/nojs/animal'), + ); + + $form['ajax_button'] = array( + '#type' => 'button', + '#value' => 'Wizard (button modal)', + '#attributes' => array('class' => array('ctools-use-modal')), + '#id' => 'ctools-ajax-sample-button', + ); + + return $form; +} diff --git a/sites/all/modules/contrib/ctools/ctools_ajax_sample/images/ajax-loader.gif b/sites/all/modules/contrib/ctools/ctools_ajax_sample/images/ajax-loader.gif new file mode 100644 index 0000000..d84f653 Binary files /dev/null and b/sites/all/modules/contrib/ctools/ctools_ajax_sample/images/ajax-loader.gif differ diff --git a/sites/all/modules/contrib/ctools/ctools_ajax_sample/images/loading-large.gif b/sites/all/modules/contrib/ctools/ctools_ajax_sample/images/loading-large.gif new file mode 100644 index 0000000..1c72ebb Binary files /dev/null and b/sites/all/modules/contrib/ctools/ctools_ajax_sample/images/loading-large.gif differ diff --git a/sites/all/modules/contrib/ctools/ctools_ajax_sample/images/loading.gif b/sites/all/modules/contrib/ctools/ctools_ajax_sample/images/loading.gif new file mode 100644 index 0000000..dc21df1 Binary files /dev/null and b/sites/all/modules/contrib/ctools/ctools_ajax_sample/images/loading.gif differ diff --git a/sites/all/modules/contrib/ctools/ctools_ajax_sample/images/popups-border.png b/sites/all/modules/contrib/ctools/ctools_ajax_sample/images/popups-border.png new file mode 100644 index 0000000..ba939f8 Binary files /dev/null and b/sites/all/modules/contrib/ctools/ctools_ajax_sample/images/popups-border.png differ diff --git a/sites/all/modules/contrib/ctools/ctools_ajax_sample/js/ctools-ajax-sample.js b/sites/all/modules/contrib/ctools/ctools_ajax_sample/js/ctools-ajax-sample.js new file mode 100644 index 0000000..0cbfc87 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_ajax_sample/js/ctools-ajax-sample.js @@ -0,0 +1,42 @@ +/** +* Provide the HTML to create the modal dialog. +*/ +Drupal.theme.prototype.CToolsSampleModal = function () { + var html = ''; + + html += '
'; + html += '
'; + html += ' '; + html += ' '; + html += ' '; + html += ' '; + html += ' '; + html += ' '; + html += ' '; + html += ' '; + html += ' '; + html += ' '; + html += ' '; + html += ' '; + html += ' '; + html += ' '; + html += ' '; + html += ' '; + html += '
'; + html += '
'; + html += ' '; + html += ' '; + html += '
'; //Maybe someday add the option for some specific buttons. + html += ' '; //Maybe someday add some footer. + html += '
'; + html += '
'; + html += '
'; + html += '
'; + + return html; + +} diff --git a/sites/all/modules/contrib/ctools/ctools_custom_content/ctools_custom_content.info b/sites/all/modules/contrib/ctools/ctools_custom_content/ctools_custom_content.info new file mode 100644 index 0000000..185d8b6 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_custom_content/ctools_custom_content.info @@ -0,0 +1,13 @@ +name = Custom content panes +description = Create custom, exportable, reusable content panes for applications like Panels. +core = 7.x +package = Chaos tool suite +version = CTOOLS_MODULE_VERSION +dependencies[] = ctools + +; Information added by Drupal.org packaging script on 2015-08-19 +version = "7.x-1.9" +core = "7.x" +project = "ctools" +datestamp = "1440020680" + diff --git a/sites/all/modules/contrib/ctools/ctools_custom_content/ctools_custom_content.install b/sites/all/modules/contrib/ctools/ctools_custom_content/ctools_custom_content.install new file mode 100644 index 0000000..b4512f2 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_custom_content/ctools_custom_content.install @@ -0,0 +1,67 @@ + 'Contains exportable customized content for this site.', + 'export' => array( + 'identifier' => 'content', + 'bulk export' => TRUE, + 'primary key' => 'cid', + 'api' => array( + 'owner' => 'ctools_custom_content', + 'api' => 'ctools_content', + 'minimum_version' => 1, + 'current_version' => 1, + ), + 'create callback' => 'ctools_content_type_new', + ), + 'fields' => array( + 'cid' => array( + 'type' => 'serial', + 'description' => 'A database primary key to ensure uniqueness', + 'not null' => TRUE, + 'no export' => TRUE, + ), + 'name' => array( + 'type' => 'varchar', + 'length' => '255', + 'description' => 'Unique ID for this content. Used to identify it programmatically.', + ), + 'admin_title' => array( + 'type' => 'varchar', + 'length' => '255', + 'description' => 'Administrative title for this content.', + ), + 'admin_description' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'Administrative description for this content.', + 'object default' => '', + ), + 'category' => array( + 'type' => 'varchar', + 'length' => '255', + 'description' => 'Administrative category for this content.', + ), + 'settings' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'Serialized settings for the actual content to be used', + 'serialize' => TRUE, + 'object default' => array(), + ), + ), + 'primary key' => array('cid'), + ); + + return $schema; +} diff --git a/sites/all/modules/contrib/ctools/ctools_custom_content/ctools_custom_content.module b/sites/all/modules/contrib/ctools/ctools_custom_content/ctools_custom_content.module new file mode 100644 index 0000000..3e316a9 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_custom_content/ctools_custom_content.module @@ -0,0 +1,118 @@ + array( + 'title' => t('Administer custom content'), + 'description' => t('Add, edit and delete CTools custom stored custom content'), + ), + ); +} + +/** + * Implementation of hook_ctools_plugin_directory() to let the system know + * we implement task and task_handler plugins. + */ +function ctools_custom_content_ctools_plugin_directory($module, $plugin) { + // Most of this module is implemented as an export ui plugin, and the + // rest is in ctools/includes/ctools_custom_content.inc + if ($module == 'ctools' && $plugin == 'export_ui') { + return 'plugins/' . $plugin; + } +} + +/** + * Implements hook_get_pane_links_alter(). + */ +function ctools_custom_content_get_pane_links_alter(&$links, $pane, $content_type) { + if ($pane->type == 'custom') { + if(!isset($pane->configuration['name'])) { + $name_of_pane = $pane->subtype; + } + else { + $name_of_pane = $pane->configuration['name']; + } + + $links['top']['edit_custom_content'] = array( + 'title' => t('Edit custom content pane'), + 'href' => url('admin/structure/ctools-content/list/' . $name_of_pane . '/edit', array('absolute' => TRUE)), + 'attributes' => array('target' => array('_blank')), + ); + } +} + +/** + * Create callback for creating a new CTools custom content type. + * + * This ensures we get proper defaults from the plugin for its settings. + */ +function ctools_content_type_new($set_defaults) { + $item = ctools_export_new_object('ctools_custom_content', $set_defaults); + ctools_include('content'); + $plugin = ctools_get_content_type('custom'); + $item->settings = ctools_content_get_defaults($plugin, array()); + return $item; +} + +/** + * Implementation of hook_panels_dashboard_blocks(). + * + * Adds page information to the Panels dashboard. + */ +function ctools_custom_content_panels_dashboard_blocks(&$vars) { + $vars['links']['ctools_custom_content'] = array( + 'title' => l(t('Custom content'), 'admin/structure/ctools-content/add'), + 'description' => t('Custom content panes are basic HTML you enter that can be reused in all of your panels.'), + ); + + // Load all mini panels and their displays. + ctools_include('export'); + $items = ctools_export_crud_load_all('ctools_custom_content'); + $count = 0; + $rows = array(); + + foreach ($items as $item) { + $rows[] = array( + check_plain($item->admin_title), + array( + 'data' => l(t('Edit'), "admin/structure/ctools-content/list/$item->name/edit"), + 'class' => 'links', + ), + ); + + // Only show 10. + if (++$count >= 10) { + break; + } + } + + if ($rows) { + $content = theme('table', array('rows' => $rows, 'attributes' => array('class' => 'panels-manage'))); + } + else { + $content = '

' . t('There are no custom content panes.') . '

'; + } + + $vars['blocks']['ctools_custom_content'] = array( + 'title' => t('Manage custom content'), + 'link' => l(t('Go to list'), 'admin/structure/ctools-content'), + 'content' => $content, + 'class' => 'dashboard-content', + 'section' => 'right', + ); +} diff --git a/sites/all/modules/contrib/ctools/ctools_custom_content/plugins/export_ui/ctools_custom_content.inc b/sites/all/modules/contrib/ctools/ctools_custom_content/plugins/export_ui/ctools_custom_content.inc new file mode 100644 index 0000000..467dc58 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_custom_content/plugins/export_ui/ctools_custom_content.inc @@ -0,0 +1,20 @@ + 'ctools_custom_content', + 'access' => 'administer custom content', + + 'menu' => array( + 'menu item' => 'ctools-content', + 'menu title' => 'Custom content panes', + 'menu description' => 'Add, edit or delete custom content panes.', + ), + + 'title singular' => t('content pane'), + 'title singular proper' => t('Content pane'), + 'title plural' => t('content panes'), + 'title plural proper' => t('Content panes'), + + 'handler' => 'ctools_custom_content_ui', +); + diff --git a/sites/all/modules/contrib/ctools/ctools_custom_content/plugins/export_ui/ctools_custom_content_ui.class.php b/sites/all/modules/contrib/ctools/ctools_custom_content/plugins/export_ui/ctools_custom_content_ui.class.php new file mode 100644 index 0000000..56fe4b2 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_custom_content/plugins/export_ui/ctools_custom_content_ui.class.php @@ -0,0 +1,129 @@ +settings['body'])) { + $form_state['item']->settings['format'] = $form_state['item']->settings['body']['format']; + $form_state['item']->settings['body'] = $form_state['item']->settings['body']['value']; + } + parent::edit_form($form, $form_state); + + $form['category'] = array( + '#type' => 'textfield', + '#title' => t('Category'), + '#description' => t('What category this content should appear in. If left blank the category will be "Miscellaneous".'), + '#default_value' => $form_state['item']->category, + ); + + $form['title'] = array( + '#type' => 'textfield', + '#default_value' => $form_state['item']->settings['title'], + '#title' => t('Title'), + ); + + $form['body'] = array( + '#type' => 'text_format', + '#title' => t('Body'), + '#default_value' => $form_state['item']->settings['body'], + '#format' => $form_state['item']->settings['format'], + ); + + $form['substitute'] = array( + '#type' => 'checkbox', + '#title' => t('Use context keywords'), + '#description' => t('If checked, context keywords will be substituted in this content.'), + '#default_value' => !empty($form_state['item']->settings['substitute']), + ); + } + + function edit_form_submit(&$form, &$form_state) { + parent::edit_form_submit($form, $form_state); + + // Since items in our settings are not in the schema, we have to do these manually: + $form_state['item']->settings['title'] = $form_state['values']['title']; + $form_state['item']->settings['body'] = $form_state['values']['body']['value']; + $form_state['item']->settings['format'] = $form_state['values']['body']['format']; + $form_state['item']->settings['substitute'] = $form_state['values']['substitute']; + } + + function list_form(&$form, &$form_state) { + parent::list_form($form, $form_state); + + $options = array('all' => t('- All -')); + foreach ($this->items as $item) { + $options[$item->category] = $item->category; + } + + $form['top row']['category'] = array( + '#type' => 'select', + '#title' => t('Category'), + '#options' => $options, + '#default_value' => 'all', + '#weight' => -10, + ); + } + + function list_filter($form_state, $item) { + if ($form_state['values']['category'] != 'all' && $form_state['values']['category'] != $item->category) { + return TRUE; + } + + return parent::list_filter($form_state, $item); + } + + function list_sort_options() { + return array( + 'disabled' => t('Enabled, title'), + 'title' => t('Title'), + 'name' => t('Name'), + 'category' => t('Category'), + 'storage' => t('Storage'), + ); + } + + function list_build_row($item, &$form_state, $operations) { + // Set up sorting + switch ($form_state['values']['order']) { + case 'disabled': + $this->sorts[$item->name] = empty($item->disabled) . $item->admin_title; + break; + case 'title': + $this->sorts[$item->name] = $item->admin_title; + break; + case 'name': + $this->sorts[$item->name] = $item->name; + break; + case 'category': + $this->sorts[$item->name] = $item->category; + break; + case 'storage': + $this->sorts[$item->name] = $item->type . $item->admin_title; + break; + } + + $ops = theme('links__ctools_dropbutton', array('links' => $operations, 'attributes' => array('class' => array('links', 'inline')))); + + $this->rows[$item->name] = array( + 'data' => array( + array('data' => check_plain($item->name), 'class' => array('ctools-export-ui-name')), + array('data' => check_plain($item->admin_title), 'class' => array('ctools-export-ui-title')), + array('data' => check_plain($item->category), 'class' => array('ctools-export-ui-category')), + array('data' => $ops, 'class' => array('ctools-export-ui-operations')), + ), + 'title' => check_plain($item->admin_description), + 'class' => array(!empty($item->disabled) ? 'ctools-export-ui-disabled' : 'ctools-export-ui-enabled'), + ); + } + + function list_table_header() { + return array( + array('data' => t('Name'), 'class' => array('ctools-export-ui-name')), + array('data' => t('Title'), 'class' => array('ctools-export-ui-title')), + array('data' => t('Category'), 'class' => array('ctools-export-ui-category')), + array('data' => t('Operations'), 'class' => array('ctools-export-ui-operations')), + ); + } + +} diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/README.txt b/sites/all/modules/contrib/ctools/ctools_plugin_example/README.txt new file mode 100644 index 0000000..42edcdc --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/README.txt @@ -0,0 +1,14 @@ + +The CTools Plugin Example is an example for developers of how to CTools +access, argument, content type, context, and relationship plugins. + +There are a number of ways to profit from this: + +1. The code itself intends to be as simple and self-explanatory as possible. + Nothing fancy is attempted: It's just trying to use the plugin API to show + how it can be used. + +2. There is a sample panel. You can access it at /ctools_plugin_example/xxxx + to see how it works. + +3. There is Advanced Help at admin/advanced_help/ctools_plugin_example. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/ctools_plugin_example.info b/sites/all/modules/contrib/ctools/ctools_plugin_example/ctools_plugin_example.info new file mode 100644 index 0000000..d378641 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/ctools_plugin_example.info @@ -0,0 +1,16 @@ +name = Chaos Tools (CTools) Plugin Example +description = Shows how an external module can provide ctools plugins (for Panels, etc.). +package = Chaos tool suite +version = CTOOLS_MODULE_VERSION +dependencies[] = ctools +dependencies[] = panels +dependencies[] = page_manager +dependencies[] = advanced_help +core = 7.x + +; Information added by Drupal.org packaging script on 2015-08-19 +version = "7.x-1.9" +core = "7.x" +project = "ctools" +datestamp = "1440020680" + diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/ctools_plugin_example.module b/sites/all/modules/contrib/ctools/ctools_plugin_example/ctools_plugin_example.module new file mode 100644 index 0000000..01d5338 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/ctools_plugin_example.module @@ -0,0 +1,94 @@ + 'CTools plugin example', + 'description' => t("Demonstration code, advanced help, and a demo panel to show how to build ctools plugins."), + 'page callback' => 'ctools_plugin_example_explanation_page', + 'access arguments' => array('administer site configuration'), + 'type' => MENU_NORMAL_ITEM, + ); + + return $items; +} + +/** + * Implements hook_ctools_plugin_directory(). + * + * It simply tells panels where to find the .inc files that define various + * args, contexts, content_types. In this case the subdirectories of + * ctools_plugin_example/panels are used. + */ +function ctools_plugin_example_ctools_plugin_directory($module, $plugin) { + if ($module == 'ctools' && !empty($plugin)) { + return "plugins/$plugin"; + } +} + +/** + * Implement hook_ctools_plugin_api(). + * + * If you do this, CTools will pick up default panels pages in + * .pages_default.inc + */ +function ctools_plugin_example_ctools_plugin_api($module, $api) { + // @todo -- this example should explain how to put it in a different file. + if ($module == 'panels_mini' && $api == 'panels_default') { + return array('version' => 1); + } + if ($module == 'page_manager' && $api == 'pages_default') { + return array('version' => 1); + } +} + +/** + * Just provide an explanation page for the admin section + * @return unknown_type + */ +function ctools_plugin_example_explanation_page() { + $content = '

' . t("The CTools Plugin Example is simply a developer's demo of how to create plugins for CTools. It provides no useful functionality for an ordinary user.") . '

'; + + $content .= '

' . t( + 'There is a demo panel demonstrating much of the functionality provided at + CTools demo panel, and you can find documentation on the examples at + !ctools_plugin_example_help. + CTools itself provides documentation at !ctools_help. Mostly, though, the code itself is intended to be the teacher. + You can find it in %path.', + array( + '@demo_url' => url('ctools_plugin_example/xxxxx'), + '!ctools_plugin_example_help' => theme('advanced_help_topic', array('module' => 'ctools_plugin_example', 'topic' => 'Chaos-Tools--CTools--Plugin-Examples', 'type' => 'title')), + '!ctools_help' => theme('advanced_help_topic', array('module' => 'ctools', 'topic' => 'plugins', 'type' => 'title')), + '%path' => drupal_get_path('module', 'ctools_plugin_example'), + )) . '

'; + + return $content; +} diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/ctools_plugin_example.pages_default.inc b/sites/all/modules/contrib/ctools/ctools_plugin_example/ctools_plugin_example.pages_default.inc new file mode 100644 index 0000000..10a7619 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/ctools_plugin_example.pages_default.inc @@ -0,0 +1,451 @@ +.pages_default.inc + * With this naming, no additional code needs to be provided. CTools will just find the file. + * The name of the hook is _default_page_manager_pages() + * + * This example provides two pages, but the returned array could + * have several pages. + * + * @return + * Array of pages, normally exported from Panels. + */ + +function ctools_plugin_example_default_page_manager_pages() { + + // begin exported panel. + + $page = new stdClass; + $page->disabled = FALSE; /* Edit this to true to make a default page disabled initially */ + $page->api_version = 1; + $page->name = 'ctools_plugin_example'; + $page->task = 'page'; + $page->admin_title = 'CTools plugin example'; + $page->admin_description = 'This panel provides no functionality to a working Drupal system. It\'s intended to display the various sample plugins provided by the CTools Plugin Example module. '; + $page->path = 'ctools_plugin_example/%sc'; + $page->access = array( + 'logic' => 'and', + ); + $page->menu = array( + 'type' => 'normal', + 'title' => 'CTools plugin example', + 'name' => 'navigation', + 'weight' => '0', + 'parent' => array( + 'type' => 'none', + 'title' => '', + 'name' => 'navigation', + 'weight' => '0', + ), + ); + $page->arguments = array( + 'sc' => array( + 'id' => 2, + 'identifier' => 'simplecontext-arg', + 'name' => 'simplecontext_arg', + 'settings' => array(), + ), + ); + $page->conf = array(); + $page->default_handlers = array(); + $handler = new stdClass; + $handler->disabled = FALSE; /* Edit this to true to make a default handler disabled initially */ + $handler->api_version = 1; + $handler->name = 'page_ctools_panel_context'; + $handler->task = 'page'; + $handler->subtask = 'ctools_plugin_example'; + $handler->handler = 'panel_context'; + $handler->weight = 0; + $handler->conf = array( + 'title' => 'Panel', + 'no_blocks' => FALSE, + 'css_id' => '', + 'css' => '', + 'contexts' => array( + '0' => array( + 'name' => 'simplecontext', + 'id' => 1, + 'identifier' => 'Configured simplecontext (not from argument)', + 'keyword' => 'configured_simplecontext', + 'context_settings' => array( + 'sample_simplecontext_setting' => 'default simplecontext setting', + ), + ), + ), + 'relationships' => array( + '0' => array( + 'context' => 'argument_simplecontext_arg_2', + 'name' => 'relcontext_from_simplecontext', + 'id' => 1, + 'identifier' => 'Relcontext from simplecontext (from relationship)', + 'keyword' => 'relcontext', + ), + ), + 'access' => array( + 'logic' => 'and', + ), + ); + $display = new panels_display; + $display->layout = 'threecol_33_34_33_stacked'; + $display->layout_settings = array(); + $display->panel_settings = array( + 'style' => 'rounded_corners', + 'style_settings' => array( + 'default' => array( + 'corner_location' => 'pane', + ), + ), + ); + $display->cache = array(); + $display->title = 'CTools plugin example panel'; + $display->hide_title = FALSE; + $display->title_pane = 1; + $display->content = array(); + $display->panels = array(); + $pane = new stdClass; + $pane->pid = 'new-1'; + $pane->panel = 'left'; + $pane->type = 'no_context_content_type'; + $pane->subtype = 'no_context_content_type'; + $pane->shown = TRUE; + $pane->access = array(); + $pane->configuration = array( + 'item1' => 'contents of config item 1', + 'item2' => 'contents of config item 2', + 'override_title' => 0, + 'override_title_text' => '', + ); + $pane->cache = array(); + $pane->style = array(); + $pane->css = array(); + $pane->extras = array(); + $pane->position = 0; + $display->content['new-1'] = $pane; + $display->panels['left'][0] = 'new-1'; + $pane = new stdClass; + $pane->pid = 'new-2'; + $pane->panel = 'left'; + $pane->type = 'custom'; + $pane->subtype = 'custom'; + $pane->shown = TRUE; + $pane->access = array( + 'plugins' => array( + '0' => array( + 'name' => 'arg_length', + 'settings' => array( + 'greater_than' => '1', + 'arg_length' => '4', + ), + 'context' => 'argument_simplecontext_arg_2', + ), + ), + ); + $pane->configuration = array( + 'title' => 'Long Arg Visibility Block', + 'body' => 'This block will be here when the argument is longer than configured arg length. It uses the \'arg_length\' access plugin to test against the length of the argument used for Simplecontext.', + 'format' => '1', + 'substitute' => 1, + ); + $pane->cache = array(); + $pane->style = array(); + $pane->css = array(); + $pane->extras = array(); + $pane->position = 1; + $display->content['new-2'] = $pane; + $display->panels['left'][1] = 'new-2'; + $pane = new stdClass; + $pane->pid = 'new-3'; + $pane->panel = 'left'; + $pane->type = 'custom'; + $pane->subtype = 'custom'; + $pane->shown = TRUE; + $pane->access = array( + 'plugins' => array( + '0' => array( + 'name' => 'arg_length', + 'settings' => array( + 'greater_than' => '0', + 'arg_length' => '4', + ), + 'context' => 'argument_simplecontext_arg_2', + ), + ), + ); + $pane->configuration = array( + 'title' => 'Short Arg Visibility', + 'body' => 'This block appears when the simplecontext argument is less than the configured length.', + 'format' => '1', + 'substitute' => 1, + ); + $pane->cache = array(); + $pane->style = array(); + $pane->css = array(); + $pane->extras = array(); + $pane->position = 2; + $display->content['new-3'] = $pane; + $display->panels['left'][2] = 'new-3'; + $pane = new stdClass; + $pane->pid = 'new-4'; + $pane->panel = 'middle'; + $pane->type = 'simplecontext_content_type'; + $pane->subtype = 'simplecontext_content_type'; + $pane->shown = TRUE; + $pane->access = array(); + $pane->configuration = array( + 'buttons' => NULL, + '#validate' => NULL, + '#submit' => NULL, + '#action' => NULL, + 'context' => 'argument_simplecontext_arg_2', + 'aligner_start' => NULL, + 'override_title' => 1, + 'override_title_text' => 'Simplecontext (with an arg)', + 'aligner_stop' => NULL, + 'override_title_markup' => NULL, + 'config_item_1' => 'Config item 1 contents', + '#build_id' => NULL, + '#type' => NULL, + '#programmed' => NULL, + 'form_build_id' => 'form-19c4ae6cb54fad8f096da46e95694e5a', + '#token' => NULL, + 'form_token' => '17141d3531eaa7b609da78afa6f3b560', + 'form_id' => 'simplecontext_content_type_edit_form', + '#id' => NULL, + '#description' => NULL, + '#attributes' => NULL, + '#required' => NULL, + '#tree' => NULL, + '#parents' => NULL, + '#method' => NULL, + '#post' => NULL, + '#processed' => NULL, + '#defaults_loaded' => NULL, + ); + $pane->cache = array(); + $pane->style = array(); + $pane->css = array(); + $pane->extras = array(); + $pane->position = 0; + $display->content['new-4'] = $pane; + $display->panels['middle'][0] = 'new-4'; + $pane = new stdClass; + $pane->pid = 'new-5'; + $pane->panel = 'middle'; + $pane->type = 'simplecontext_content_type'; + $pane->subtype = 'simplecontext_content_type'; + $pane->shown = TRUE; + $pane->access = array(); + $pane->configuration = array( + 'buttons' => NULL, + '#validate' => NULL, + '#submit' => NULL, + '#action' => NULL, + 'context' => 'context_simplecontext_1', + 'aligner_start' => NULL, + 'override_title' => 1, + 'override_title_text' => 'Configured simplecontext content type (not from arg)', + 'aligner_stop' => NULL, + 'override_title_markup' => NULL, + 'config_item_1' => '(configuration for simplecontext)', + '#build_id' => NULL, + '#type' => NULL, + '#programmed' => NULL, + 'form_build_id' => 'form-d016200490abd015dc5b8a7e366d76ea', + '#token' => NULL, + 'form_token' => '17141d3531eaa7b609da78afa6f3b560', + 'form_id' => 'simplecontext_content_type_edit_form', + '#id' => NULL, + '#description' => NULL, + '#attributes' => NULL, + '#required' => NULL, + '#tree' => NULL, + '#parents' => NULL, + '#method' => NULL, + '#post' => NULL, + '#processed' => NULL, + '#defaults_loaded' => NULL, + ); + $pane->cache = array(); + $pane->style = array(); + $pane->css = array(); + $pane->extras = array(); + $pane->position = 1; + $display->content['new-5'] = $pane; + $display->panels['middle'][1] = 'new-5'; + $pane = new stdClass; + $pane->pid = 'new-6'; + $pane->panel = 'middle'; + $pane->type = 'custom'; + $pane->subtype = 'custom'; + $pane->shown = TRUE; + $pane->access = array(); + $pane->configuration = array( + 'admin_title' => 'Simplecontext keyword usage', + 'title' => 'Simplecontext keyword usage', + 'body' => 'Demonstrating context keyword usage: + item1 is %sc:item1 + item2 is %sc:item2 + description is %sc:description', + 'format' => '1', + 'substitute' => 1, + ); + $pane->cache = array(); + $pane->style = array(); + $pane->css = array(); + $pane->extras = array(); + $pane->position = 2; + $display->content['new-6'] = $pane; + $display->panels['middle'][2] = 'new-6'; + $pane = new stdClass; + $pane->pid = 'new-7'; + $pane->panel = 'right'; + $pane->type = 'relcontext_content_type'; + $pane->subtype = 'relcontext_content_type'; + $pane->shown = TRUE; + $pane->access = array(); + $pane->configuration = array( + 'buttons' => NULL, + '#validate' => NULL, + '#submit' => NULL, + '#action' => NULL, + 'context' => 'relationship_relcontext_from_simplecontext_1', + 'aligner_start' => NULL, + 'override_title' => 0, + 'override_title_text' => '', + 'aligner_stop' => NULL, + 'override_title_markup' => NULL, + 'config_item_1' => 'some stuff in this one', + '#build_id' => NULL, + '#type' => NULL, + '#programmed' => NULL, + 'form_build_id' => 'form-8485f84511bd06e51b4a48e998448054', + '#token' => NULL, + 'form_token' => '1c3356396374d51d7d2531a10fd25310', + 'form_id' => 'relcontext_edit_form', + '#id' => NULL, + '#description' => NULL, + '#attributes' => NULL, + '#required' => NULL, + '#tree' => NULL, + '#parents' => NULL, + '#method' => NULL, + '#post' => NULL, + '#processed' => NULL, + '#defaults_loaded' => NULL, + ); + $pane->cache = array(); + $pane->style = array(); + $pane->css = array(); + $pane->extras = array(); + $pane->position = 0; + $display->content['new-7'] = $pane; + $display->panels['right'][0] = 'new-7'; + $pane = new stdClass; + $pane->pid = 'new-8'; + $pane->panel = 'top'; + $pane->type = 'custom'; + $pane->subtype = 'custom'; + $pane->shown = TRUE; + $pane->access = array(); + $pane->configuration = array( + 'title' => 'Demonstrating ctools plugins', + 'body' => 'The CTools Plugin Example module (and this panel page) are just here to demonstrate how to build CTools plugins. + + ', + 'format' => '2', + 'substitute' => 1, + ); + $pane->cache = array(); + $pane->style = array(); + $pane->css = array(); + $pane->extras = array(); + $pane->position = 0; + $display->content['new-8'] = $pane; + $display->panels['top'][0] = 'new-8'; + $handler->conf['display'] = $display; + $page->default_handlers[$handler->name] = $handler; + + // end of exported panel. + $pages['ctools_plugin_example_demo_page'] = $page; + + // begin exported panel + + $page = new stdClass; + $page->disabled = FALSE; /* Edit this to true to make a default page disabled initially */ + $page->api_version = 1; + $page->name = 'ctools_plugin_example_base'; + $page->task = 'page'; + $page->admin_title = 'CTools Plugin Example base page'; + $page->admin_description = 'This panel is for when people hit /ctools_plugin_example without an argument. We can use it to tell people to move on.'; + $page->path = 'ctools_plugin_example'; + $page->access = array(); + $page->menu = array(); + $page->arguments = array(); + $page->conf = array(); + $page->default_handlers = array(); + $handler = new stdClass; + $handler->disabled = FALSE; /* Edit this to true to make a default handler disabled initially */ + $handler->api_version = 1; + $handler->name = 'page_ctools_plugin_example_base_panel_context'; + $handler->task = 'page'; + $handler->subtask = 'ctools_plugin_example_base'; + $handler->handler = 'panel_context'; + $handler->weight = 0; + $handler->conf = array( + 'title' => 'Panel', + 'no_blocks' => FALSE, + 'css_id' => '', + 'css' => '', + 'contexts' => array(), + 'relationships' => array(), + ); + $display = new panels_display; + $display->layout = 'onecol'; + $display->layout_settings = array(); + $display->panel_settings = array(); + $display->cache = array(); + $display->title = ''; + $display->hide_title = FALSE; + $display->content = array(); + $display->panels = array(); + $pane = new stdClass; + $pane->pid = 'new-1'; + $pane->panel = 'middle'; + $pane->type = 'custom'; + $pane->subtype = 'custom'; + $pane->shown = TRUE; + $pane->access = array(); + $pane->configuration = array( + 'title' => 'Use this page with an argument', + 'body' => 'This demo page works if you use an argument, like ctools_plugin_example/xxxxx.', + 'format' => '1', + 'substitute' => NULL, + ); + $pane->cache = array(); + $pane->style = array(); + $pane->css = array(); + $pane->extras = array(); + $pane->position = 0; + $display->content['new-1'] = $pane; + $display->panels['middle'][0] = 'new-1'; + $handler->conf['display'] = $display; + $page->default_handlers[$handler->name] = $handler; + // end exported panel. + + $pages['base_page'] = $page; + + return $pages; +} \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Access-Plugins--Determining-access-and-visibility.html b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Access-Plugins--Determining-access-and-visibility.html new file mode 100644 index 0000000..781260e --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Access-Plugins--Determining-access-and-visibility.html @@ -0,0 +1,17 @@ +
+ + + + +
+

We can use access plugins to determine access to a page or visibility of the panes in a page. Basically, we just determine access based on configuration settings and the various contexts that are available to us.

+

The arbitrary example in plugins/access/arg_length.inc determines access based on the length of the simplecontext argument. You can configure whether access should be granted if the simplecontext argument is greater or less than some number.

+
+ +
+
+
+ +
+ +
diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Argument-Plugins--Starting-at-the-beginning.html b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Argument-Plugins--Starting-at-the-beginning.html new file mode 100644 index 0000000..4dd569d --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Argument-Plugins--Starting-at-the-beginning.html @@ -0,0 +1,20 @@ +
+ + + + +
+

Contexts are fundamental to CTools, and they almost always start with an argument to a panels page, so we'll start there too.

+

We first need to process an argument.

+

We're going to work with a "Simplecontext" context type and argument, and then with a content type that displays it. So we'll start by with the Simplecontext argument plugin in plugins/arguments/simplecontext_arg.inc.

+

Note that the name of the file (simplecontext_arg.inc) is built from the machine name of our plugin (simplecontext_arg). And note also that the primary function that we use to provide our argument (ctools_plugin_example_simplecontext_arg_ctools_arguments()) is also built from the machine name. This magic is most of the naming magic that you have to know.

+

You can browse plugins/arguments/simplecontext_arg.inc and see the little that it does.

+
+ +
+
+
+ +
+ +
diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Chaos-Tools--CTools--Plugin-Examples.html b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Chaos-Tools--CTools--Plugin-Examples.html new file mode 100644 index 0000000..7576c80 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Chaos-Tools--CTools--Plugin-Examples.html @@ -0,0 +1,19 @@ +
+ + + + +
+

This demonstration module is intended for developers to look at and play with. CTools plugins are not terribly difficult to do, but it can be hard to sort through the various arguments and required functions. The idea here is that you should have a starting point for most anything you want to do. Just work through the example, and then start experimenting with changing it.

+

There are two parts to this demo:

+

First, there is a sample panel provided that uses all the various plugins. It's at ctools_example/12345. You can edit the panel and configure all the panes on it.

+

Second, the code is there for you to experiment with and change as you see fit. Sometimes starting with simple code and working with it can take you places that it's hard to go when you're looking at more complex examples.

+
+ +
+
+
+ +
+ +
diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Content-Type-Plugins--Displaying-content-using-a-context.html b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Content-Type-Plugins--Displaying-content-using-a-context.html new file mode 100644 index 0000000..918a13c --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Content-Type-Plugins--Displaying-content-using-a-context.html @@ -0,0 +1,17 @@ +
+ + + + +
+

Now we get to the heart of the matter: Building a content type plugin. A content type plugin uses the contexts available to it to display something. plugins/content_types/simplecontext_content_type.inc does this work for us.

+

Note that our content type also has an edit form which can be used to configure its behavior. This settings form is accessed through the panels interface, and it's up to you what the settings mean to the code and the generation of content in the display rendering.

+
+ +
+
+
+ +
+ +
diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Context-plugins--Creating-a--context--from-an-argument.html b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Context-plugins--Creating-a--context--from-an-argument.html new file mode 100644 index 0000000..e8efbb3 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Context-plugins--Creating-a--context--from-an-argument.html @@ -0,0 +1,21 @@ +
+ + + + +
+

Now that we have a plugin for a simplecontext argument, we can create a plugin for a simplecontext context.

+

Normally, a context would take an argument which is a key like a node ID (nid) and retrieve a more complex object from a database or whatever. In our example, we'll artificially transform the argument into an arbitrary "context" data object.

+

plugins/contexts/simplecontext.inc implements our context.

+

Note that there are actually two ways to create a context. The normal one, which we've been referring to, is to create a context from an argument. However, it is also possible to configure a context in a panel using the panels interface. This is quite inflexible, but might be useful for configuring single page. However, it means that we have a settings form for exactly that purpose. Our context would have to know how to create itself from a settings form as well as from an argument. Simplecontext can do that.

+

A context plugin can also provide keywords that expose parts of its context using keywords like masterkeyword:dataitem. The node plugin for ctools has node:nid and node:title, for example. The simplecontext plugin here provides the simplest of keywords.

+ +
+ +
+
+
+ +
+ +
diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Module-setup-and-hooks.html b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Module-setup-and-hooks.html new file mode 100644 index 0000000..f816917 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Module-setup-and-hooks.html @@ -0,0 +1,20 @@ +
+ + + + +
+

Your module must provide a few things so that your plugins can be found.

+

First, you need to implement hook_ctools_plugin_directory(). Here we're telling CTools that our plugins will be found in the module's directory in the plugins/<plugintype> directory. Context plugins will be in ctools_plugin_example/plugins/contexts, Content-type plugins will be in ctools_plugin_example/plugins/content_types.

+

<?php
function ctools_plugin_example_ctools_plugin_directory($module, $plugin) {
  if (
$module == 'ctools' && !empty($plugin)) {
    return
"plugins/$plugin";
  }
}
?>

+

Second, if you module wants to provide default panels pages, you can implement hook_ctools_plugin_api(). CTools will then pick up your panels pages in the file named <modulename>.pages_default.inc.

+

<?php
function ctools_plugin_example_ctools_plugin_api($module, $api) {
  if (
$module == 'panels_mini' && $api == 'panels_default') {
    return array(
'version' => 1);
  }
  if (
$module == 'page_manager' && $api == 'pages_default') {
    return array(
'version' => 1);
  }
}
?>

+
+ +
+
+
+ +
+ +
diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Relationships--Letting-one-context-take-us-to-another.html b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Relationships--Letting-one-context-take-us-to-another.html new file mode 100644 index 0000000..7691245 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/Relationships--Letting-one-context-take-us-to-another.html @@ -0,0 +1,18 @@ +
+ + + + +
+

Often a single data type can lead us to other data types. For example, a node has a user (the author) and the user has data associated with it.

+

A relationship plugin allows this kind of data to be accessed.

+

An example relationship plugin is provided in plugins/relationships/relcontext_from_simplecontext.inc. It looks at a simplecontext (which we got from an argument) and builds an (artificial) "relcontext" from that.

+
+ +
+
+
+ +
+ +
diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/help/ctools_plugin_example.help.ini b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/ctools_plugin_example.help.ini new file mode 100644 index 0000000..6fb3d4c --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/help/ctools_plugin_example.help.ini @@ -0,0 +1,42 @@ +[Chaos-Tools--CTools--Plugin-Examples] +title = CTools Plugin Examples +file = Chaos-Tools--CTools--Plugin-Examples +weight = 0 +parent = + +[Module-setup-and-hooks] +title = Module setup and hooks +file = Module-setup-and-hooks +weight = -15 +parent = Chaos-Tools--CTools--Plugin-Examples + +[Argument-Plugins--Starting-at-the-beginning] +title = Argument Plugins: Starting at the beginning +file = Argument-Plugins--Starting-at-the-beginning +weight = -14 +parent = Chaos-Tools--CTools--Plugin-Examples + +[Context-plugins--Creating-a--context--from-an-argument] +title = Context plugins: Creating a context from an argument +file = Context-plugins--Creating-a--context--from-an-argument +weight = -13 +parent = Chaos-Tools--CTools--Plugin-Examples + +[Content-Type-Plugins--Displaying-content-using-a-context] +title = Content Type Plugins: Displaying content using a context +file = Content-Type-Plugins--Displaying-content-using-a-context +weight = -12 +parent = Chaos-Tools--CTools--Plugin-Examples + +[Access-Plugins--Determining-access-and-visibility] +title = Access Plugins: Determining access and visibility +file = Access-Plugins--Determining-access-and-visibility +weight = -11 +parent = Chaos-Tools--CTools--Plugin-Examples + +[Relationships--Letting-one-context-take-us-to-another] +title = Relationships: Letting one context take us to another +file = Relationships--Letting-one-context-take-us-to-another +weight = -10 +parent = Chaos-Tools--CTools--Plugin-Examples + diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/access/arg_length.inc b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/access/arg_length.inc new file mode 100644 index 0000000..2a09eea --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/access/arg_length.inc @@ -0,0 +1,65 @@ + t("Arg length"), + 'description' => t('Control access by length of simplecontext argument.'), + 'callback' => 'ctools_plugin_example_arg_length_ctools_access_check', + 'settings form' => 'ctools_plugin_example_arg_length_ctools_access_settings', + 'summary' => 'ctools_plugin_example_arg_length_ctools_access_summary', + 'required context' => new ctools_context_required(t('Simplecontext'), 'simplecontext'), +); + +/** + * Settings form for the 'by role' access plugin. + */ +function ctools_plugin_example_arg_length_ctools_access_settings(&$form, &$form_state, $conf) { + $form['settings']['greater_than'] = array( + '#type' => 'radios', + '#title' => t('Grant access if simplecontext argument length is'), + '#options' => array(1 => t('Greater than'), 0 => t('Less than or equal to')), + '#default_value' => $conf['greater_than'], + ); + $form['settings']['arg_length'] = array( + '#type' => 'textfield', + '#title' => t('Length of simplecontext argument'), + '#size' => 3, + '#default_value' => $conf['arg_length'], + '#description' => t('Access/visibility will be granted based on arg length.'), + ); +} + +/** + * Check for access. + */ +function ctools_plugin_example_arg_length_ctools_access_check($conf, $context) { + // As far as I know there should always be a context at this point, but this + // is safe. + if (empty($context) || empty($context->data)) { + return FALSE; + } + $compare = ($context->arg_length > $conf['arg_length']); + if (($compare && $conf['greater_than']) || (!$compare && !$conf['greater_than'])) { + return TRUE; + } + return FALSE; +} + +/** + * Provide a summary description based upon the checked roles. + */ +function ctools_plugin_example_arg_length_ctools_access_summary($conf, $context) { + return t('Simpletext argument must be !comp @length characters', + array('!comp' => $conf['greater_than'] ? 'greater than' : 'less than or equal to', + '@length' => $conf['arg_length'])); +} + diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/access/example_role.inc b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/access/example_role.inc new file mode 100644 index 0000000..bbe364c --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/access/example_role.inc @@ -0,0 +1,76 @@ + t("CTools example: role"), + 'description' => t('Control access by role.'), + 'callback' => 'ctools_plugin_example_example_role_ctools_access_check', + 'default' => array('rids' => array()), + 'settings form' => 'ctools_plugin_example_example_role_ctools_access_settings', + 'summary' => 'ctools_plugin_example_example_role_ctools_access_summary', + 'required context' => new ctools_context_required(t('User'), 'user'), +); + +/** + * Settings form for the 'by role' access plugin. + */ +function ctools_plugin_example_example_role_ctools_access_settings(&$form, &$form_state, $conf) { + $form['settings']['rids'] = array( + '#type' => 'checkboxes', + '#title' => t('Role'), + '#default_value' => $conf['rids'], + '#options' => ctools_get_roles(), + '#description' => t('Only the checked roles will be granted access.'), + ); +} + +/** + * Compress the roles allowed to the minimum. + */ +function ctools_plugin_example_example_role_ctools_access_settings_submit(&$form, &$form_state) { + $form_state['values']['settings']['rids'] = array_keys(array_filter($form_state['values']['settings']['rids'])); +} + +/** + * Check for access. + */ +function ctools_plugin_example_example_role_ctools_access_check($conf, $context) { + // As far as I know there should always be a context at this point, but this + // is safe. + if (empty($context) || empty($context->data) || !isset($context->data->roles)) { + return FALSE; + } + + $roles = array_keys($context->data->roles); + $roles[] = $context->data->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID; + return (bool) array_intersect($conf['rids'], $roles); +} + +/** + * Provide a summary description based upon the checked roles. + */ +function ctools_plugin_example_example_role_ctools_access_summary($conf, $context) { + if (!isset($conf['rids'])) { + $conf['rids'] = array(); + } + $roles = ctools_get_roles(); + $names = array(); + foreach (array_filter($conf['rids']) as $rid) { + $names[] = check_plain($roles[$rid]); + } + if (empty($names)) { + return t('@identifier can have any role', array('@identifier' => $context->identifier)); + } + return format_plural(count($names), '@identifier must have role "@roles"', '@identifier can be one of "@roles"', array('@roles' => implode(', ', $names), '@identifier' => $context->identifier)); +} + diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/arguments/simplecontext_arg.inc b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/arguments/simplecontext_arg.inc new file mode 100644 index 0000000..51c7c60 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/arguments/simplecontext_arg.inc @@ -0,0 +1,52 @@ + t("Simplecontext arg"), + // keyword to use for %substitution + 'keyword' => 'simplecontext', + 'description' => t('Creates a "simplecontext" from the arg.'), + 'context' => 'simplecontext_arg_context', + // 'settings form' => 'simplecontext_arg_settings_form', + + // placeholder_form is used in panels preview, for example, so we can + // preview without getting the arg from a URL + 'placeholder form' => array( + '#type' => 'textfield', + '#description' => t('Enter the simplecontext arg'), + ), +); + +/** + * Get the simplecontext context using the arg. In this case we're just going + * to manufacture the context from the data in the arg, but normally it would + * be an API call, db lookup, etc. + */ +function simplecontext_arg_context($arg = NULL, $conf = NULL, $empty = FALSE) { + // If $empty == TRUE it wants a generic, unfilled context. + if ($empty) { + return ctools_context_create_empty('simplecontext'); + } + // Do whatever error checking is required, returning FALSE if it fails the test + // Normally you'd check + // for a missing object, one you couldn't create, etc. + if (empty($arg)) { + return FALSE; + } + return ctools_context_create('simplecontext', $arg); +} diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/content_types/icon_example.png b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/content_types/icon_example.png new file mode 100644 index 0000000..58c6bee Binary files /dev/null and b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/content_types/icon_example.png differ diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/content_types/no_context_content_type.inc b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/content_types/no_context_content_type.inc new file mode 100644 index 0000000..3c02ab8 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/content_types/no_context_content_type.inc @@ -0,0 +1,116 @@ + t('CTools example no context content type'), + 'description' => t('No context content type - requires and uses no context.'), + + // 'single' => TRUE means has no subtypes. + 'single' => TRUE, + // Constructor. + 'content_types' => array('no_context_content_type'), + // Name of a function which will render the block. + 'render callback' => 'no_context_content_type_render', + // The default context. + 'defaults' => array(), + + // This explicitly declares the config form. Without this line, the func would be + // ctools_plugin_example_no_context_content_type_edit_form. + 'edit form' => 'no_context_content_type_edit_form', + + // Icon goes in the directory with the content type. + 'icon' => 'icon_example.png', + 'category' => array(t('CTools Examples'), -9), + + // this example does not provide 'admin info', which would populate the + // panels builder page preview. +); + +/** + * Run-time rendering of the body of the block. + * + * @param $subtype + * @param $conf + * Configuration as done at admin time. + * @param $args + * @param $context + * Context - in this case we don't have any. + * + * @return + * An object with at least title and content members. + */ +function no_context_content_type_render($subtype, $conf, $args, $context) { + $block = new stdClass(); + + $ctools_help = theme('advanced_help_topic', array('module' => 'ctools', 'topic' => 'plugins', 'type' => 'title')); + $ctools_plugin_example_help = theme('advanced_help_topic', array('module' => 'ctools_plugin_example', 'topic' => 'Chaos-Tools--CTools--Plugin-Examples', 'type' => 'title')); + + // The title actually used in rendering + $block->title = check_plain("No-context content type"); + $block->content = t(" +
Welcome to the CTools Plugin Example demonstration content type. + + This block is a content type which requires no context at all. It's like a custom block, + but not even that sophisticated. + + For more information on the example plugins, please see the advanced help for + + {$ctools_help} and {$ctools_plugin_example_help} +
+ "); + if (!empty($conf)) { + $block->content .= '
The only information that can be displayed in this block comes from the code and its settings form:
'; + $block->content .= '
' . var_export($conf, TRUE) . '
'; + } + + return $block; + +} + +/** + * 'Edit form' callback for the content type. + * This example just returns a form; validation and submission are standard drupal + * Note that if we had not provided an entry for this in hook_content_types, + * this could have had the default name + * ctools_plugin_example_no_context_content_type_edit_form. + * + */ +function no_context_content_type_edit_form($form, &$form_state) { + $conf = $form_state['conf']; + $form['item1'] = array( + '#type' => 'textfield', + '#title' => t('Item1'), + '#size' => 50, + '#description' => t('The setting for item 1.'), + '#default_value' => !empty($conf['item1']) ? $conf['item1'] : '', + '#prefix' => '
', + '#suffix' => '
', + ); + $form['item2'] = array( + '#type' => 'textfield', + '#title' => t('Item2'), + '#size' => 50, + '#description' => t('The setting for item 2'), + '#default_value' => !empty($conf['item2']) ? $conf['item2'] : '', + '#prefix' => '
', + '#suffix' => '
', + ); + return $form; +} + +function no_context_content_type_edit_form_submit($form, &$form_state) { + foreach (array('item1', 'item2') as $key) { + $form_state['conf'][$key] = $form_state['values'][$key]; + } +} diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/content_types/relcontext_content_type.inc b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/content_types/relcontext_content_type.inc new file mode 100644 index 0000000..bf54dce --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/content_types/relcontext_content_type.inc @@ -0,0 +1,103 @@ + t('CTools example relcontext content type'), + 'admin info' => 'ctools_plugin_example_relcontext_content_type_admin_info', + 'content_types' => 'relcontext_content_type', + 'single' => TRUE, + 'render callback' => 'relcontext_content_type_render', + // Icon goes in the directory with the content type. Here, in plugins/content_types. + 'icon' => 'icon_example.png', + 'description' => t('Relcontext content type - works with relcontext context.'), + 'required context' => new ctools_context_required(t('Relcontext'), 'relcontext'), + 'category' => array(t('CTools Examples'), -9), + 'edit form' => 'relcontext_edit_form', + + // this example does not provide 'admin info', which would populate the + // panels builder page preview. + +); + +/** + * Run-time rendering of the body of the block. + * + * @param $subtype + * @param $conf + * Configuration as done at admin time + * @param $args + * @param $context + * Context - in this case we don't have any + * + * @return + * An object with at least title and content members + */ +function relcontext_content_type_render($subtype, $conf, $args, $context) { + $data = $context->data; + $block = new stdClass(); + + // Don't forget to check this data if it's untrusted. + // The title actually used in rendering. + $block->title = "Relcontext content type"; + $block->content = t(" + This is a block of data created by the Relcontent content type. + Data in the block may be assembled from static text (like this) or from the + content type settings form (\$conf) for the content type, or from the context + that is passed in.
+ In our case, the configuration form (\$conf) has just one field, 'config_item_1; + and it's configured with: + "); + if (!empty($conf)) { + $block->content .= '
' . var_export($conf['config_item_1'], TRUE) . '
'; + } + if (!empty($context)) { + $block->content .= '
The args ($args) were
' . + var_export($args, TRUE) . '
'; + } + $block->content .= '
And the relcontext context ($context->data->description) + (which was created from a + simplecontext context) was
' . + print_r($context->data->description, TRUE) . '
'; + return $block; +} + +/** + * 'Edit' callback for the content type. + * This example just returns a form. + * + */ +function relcontext_edit_form($form, &$form_state) { + $conf = $form_state['conf']; + + $form['config_item_1'] = array( + '#type' => 'textfield', + '#title' => t('Config Item 1 (relcontext)'), + '#size' => 50, + '#description' => t('Setting for relcontext.'), + '#default_value' => !empty($conf['config_item_1']) ? $conf['config_item_1'] : '', + '#prefix' => '
', + '#suffix' => '
', + ); + return $form; +} + +function relcontext_edit_form_submit($form, &$form_state) { + foreach (element_children($form) as $key) { + if (!empty($form_state['values'][$key])) { + $form_state['conf'][$key] = $form_state['values'][$key]; + } + } +} diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/content_types/simplecontext_content_type.inc b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/content_types/simplecontext_content_type.inc new file mode 100644 index 0000000..a308683 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/content_types/simplecontext_content_type.inc @@ -0,0 +1,129 @@ + t('Simplecontext content type'), + 'content_types' => 'simplecontext_content_type', + // 'single' means not to be subtyped. + 'single' => TRUE, + // Name of a function which will render the block. + 'render callback' => 'simplecontext_content_type_render', + + // Icon goes in the directory with the content type. + 'icon' => 'icon_example.png', + 'description' => t('Simplecontext content type - works with a simplecontext context.'), + 'required context' => new ctools_context_required(t('Simplecontext'), 'simplecontext'), + 'edit form' => 'simplecontext_content_type_edit_form', + 'admin title' => 'ctools_plugin_example_simplecontext_content_type_admin_title', + + // presents a block which is used in the preview of the data. + // Pn Panels this is the preview pane shown on the panels building page. + 'admin info' => 'ctools_plugin_example_simplecontext_content_type_admin_info', + 'category' => array(t('CTools Examples'), -9), +); + +function ctools_plugin_example_simplecontext_content_type_admin_title($subtype, $conf, $context = NULL) { + $output = t('Simplecontext'); + if ($conf['override_title'] && !empty($conf['override_title_text'])) { + $output = filter_xss_admin($conf['override_title_text']); + } + return $output; +} + +/** + * Callback to provide administrative info (the preview in panels when building + * a panel). + * + * In this case we'll render the content with a dummy argument and + * a dummy context. + */ +function ctools_plugin_example_simplecontext_content_type_admin_info($subtype, $conf, $context = NULL) { + $context = new stdClass(); + $context->data = new stdClass(); + $context->data->description = t("no real context"); + $block = simplecontext_content_type_render($subtype, $conf, array("Example"), $context); + return $block; +} + +/** + * Run-time rendering of the body of the block (content type) + * + * @param $subtype + * @param $conf + * Configuration as done at admin time + * @param $args + * @param $context + * Context - in this case we don't have any + * + * @return + * An object with at least title and content members + */ +function simplecontext_content_type_render($subtype, $conf, $args, $context) { + $data = $context->data; + $block = new stdClass(); + + // Don't forget to check this data if it's untrusted. + // The title actually used in rendering. + $block->title = "Simplecontext content type"; + $block->content = t(" + This is a block of data created by the Simplecontext content type. + Data in the block may be assembled from static text (like this) or from the + content type settings form (\$conf) for the content type, or from the context + that is passed in.
+ In our case, the configuration form (\$conf) has just one field, 'config_item_1; + and it's configured with: + "); + if (!empty($conf)) { + $block->content .= '
' . print_r(filter_xss_admin($conf['config_item_1']), TRUE) . '
'; + } + if (!empty($context)) { + $block->content .= '
The args ($args) were
' . + var_export($args, TRUE) . '
'; + } + $block->content .= '
And the simplecontext context ($context->data->description) was
' . + print_r($context->data->description, TRUE) . '
'; + return $block; +} + +/** + * 'Edit' callback for the content type. + * This example just returns a form. + * + */ +function simplecontext_content_type_edit_form($form, &$form_state) { + $conf = $form_state['conf']; + $form['config_item_1'] = array( + '#type' => 'textfield', + '#title' => t('Config Item 1 for simplecontext content type'), + '#size' => 50, + '#description' => t('The stuff for item 1.'), + '#default_value' => !empty($conf['config_item_1']) ? $conf['config_item_1'] : '', + '#prefix' => '
', + '#suffix' => '
', + ); + + return $form; +} + +function simplecontext_content_type_edit_form_submit($form, &$form_state) { + foreach (element_children($form) as $key) { + if (!empty($form_state['values'][$key])) { + $form_state['conf'][$key] = $form_state['values'][$key]; + } + } +} diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/contexts/relcontext.inc b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/contexts/relcontext.inc new file mode 100644 index 0000000..0c7ef11 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/contexts/relcontext.inc @@ -0,0 +1,83 @@ + t("Relcontext"), + 'description' => t('A relcontext object.'), + // Function to create the relcontext. + 'context' => 'ctools_plugin_example_context_create_relcontext', + // Function that does the settings. + 'settings form' => 'relcontext_settings_form', + 'keyword' => 'relcontext', + 'context name' => 'relcontext', +); + +/** + * Create a context, either from manual configuration (form) or from an argument on the URL. + * + * @param $empty + * If true, just return an empty context. + * @param $data + * If from settings form, an array as from a form. If from argument, a string. + * @param $conf + * TRUE if the $data is coming from admin configuration, FALSE if it's from a URL arg. + * + * @return + * a Context object. + */ +function ctools_plugin_example_context_create_relcontext($empty, $data = NULL, $conf = FALSE) { + $context = new ctools_context('relcontext'); + $context->plugin = 'relcontext'; + if ($empty) { + return $context; + } + if ($conf) { + if (!empty($data)) { + $context->data = new stdClass(); + // For this simple item we'll just create our data by stripping non-alpha and + // adding 'sample_relcontext_setting' to it. + $context->data->description = 'relcontext_from__' . preg_replace('/[^a-z]/i', '', $data['sample_relcontext_setting']); + $context->data->description .= '_from_configuration_sample_simplecontext_setting'; + $context->title = t("Relcontext context from simplecontext"); + return $context; + } + } + else { + // $data is coming from an arg - it's just a string. + // This is used for keyword. + $context->title = "relcontext_" . $data->data->description; + $context->argument = $data->argument; + // Make up a bogus context. + $context->data = new stdClass(); + // For this simple item we'll just create our data by stripping non-alpha and + // prepend 'relcontext_' and adding '_created_from_from_simplecontext' to it. + $context->data->description = 'relcontext_' . preg_replace('/[^a-z]/i', '', $data->data->description); + $context->data->description .= '_created_from_simplecontext'; + return $context; + } +} + +function relcontext_settings_form($conf, $external = FALSE) { + $form = array(); + + $form['sample_relcontext_setting'] = array( + '#type' => 'textfield', + '#title' => t('Relcontext setting'), + '#size' => 50, + '#description' => t('Just an example setting.'), + '#default_value' => !empty($conf['sample_relcontext_setting']) ? $conf['sample_relcontext_setting'] : '', + '#prefix' => '
', + '#suffix' => '
', + ); + return $form; +} + diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/contexts/simplecontext.inc b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/contexts/simplecontext.inc new file mode 100644 index 0000000..e19a842 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/contexts/simplecontext.inc @@ -0,0 +1,134 @@ + t("Simplecontext"), + 'description' => t('A single "simplecontext" context, or data element.'), + 'context' => 'ctools_plugin_example_context_create_simplecontext', // func to create context + 'context name' => 'simplecontext', + 'settings form' => 'simplecontext_settings_form', + 'keyword' => 'simplecontext', + + // Provides a list of items which are exposed as keywords. + 'convert list' => 'simplecontext_convert_list', + // Convert keywords into data. + 'convert' => 'simplecontext_convert', + + 'placeholder form' => array( + '#type' => 'textfield', + '#description' => t('Enter some data to represent this "simplecontext".'), + ), +); + +/** + * Create a context, either from manual configuration or from an argument on the URL. + * + * @param $empty + * If true, just return an empty context. + * @param $data + * If from settings form, an array as from a form. If from argument, a string. + * @param $conf + * TRUE if the $data is coming from admin configuration, FALSE if it's from a URL arg. + * + * @return + * a Context object/ + */ +function ctools_plugin_example_context_create_simplecontext($empty, $data = NULL, $conf = FALSE) { + $context = new ctools_context('simplecontext'); + $context->plugin = 'simplecontext'; + + if ($empty) { + return $context; + } + + if ($conf) { + if (!empty($data)) { + $context->data = new stdClass(); + // For this simple item we'll just create our data by stripping non-alpha and + // adding '_from_configuration_item_1' to it. + $context->data->item1 = t("Item1"); + $context->data->item2 = t("Item2"); + $context->data->description = preg_replace('/[^a-z]/i', '', $data['sample_simplecontext_setting']); + $context->data->description .= '_from_configuration_sample_simplecontext_setting'; + $context->title = t("Simplecontext context from config"); + return $context; + } + } + else { + // $data is coming from an arg - it's just a string. + // This is used for keyword. + $context->title = $data; + $context->argument = $data; + // Make up a bogus context + $context->data = new stdClass(); + $context->data->item1 = t("Item1"); + $context->data->item2 = t("Item2"); + + // For this simple item we'll just create our data by stripping non-alpha and + // adding '_from_simplecontext_argument' to it. + $context->data->description = preg_replace('/[^a-z]/i', '', $data); + $context->data->description .= '_from_simplecontext_argument'; + $context->arg_length = strlen($context->argument); + return $context; + } +} + +function simplecontext_settings_form($conf, $external = FALSE) { + if (empty($conf)) { + $conf = array( + 'sample_simplecontext_setting' => 'default simplecontext setting', + ); + } + $form = array(); + $form['sample_simplecontext_setting'] = array( + '#type' => 'textfield', + '#title' => t('Setting for simplecontext'), + '#size' => 50, + '#description' => t('An example setting that could be used to configure a context'), + '#default_value' => $conf['sample_simplecontext_setting'], + '#prefix' => '
', + '#suffix' => '
', + ); + return $form; +} + + + +/** + * Provide a list of sub-keywords. + * + * This is used to provide keywords from the context for use in a content type, + * pane, etc. + */ +function simplecontext_convert_list() { + return array( + 'item1' => t('Item1'), + 'item2' => t('Item2'), + 'description' => t('Description'), + ); +} + +/** + * Convert a context into a string to be used as a keyword by content types, etc. + */ +function simplecontext_convert($context, $type) { + switch ($type) { + case 'item1': + return $context->data->item1; + case 'item2': + return $context->data->item2; + case 'description': + return $context->data->description; + } +} + diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/panels.pages.inc b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/panels.pages.inc new file mode 100644 index 0000000..d3022af --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/panels.pages.inc @@ -0,0 +1,214 @@ +pid = 'new'; + $page->did = 'new'; + $page->name = 'ctools_plugin_example_demo_panel'; + $page->title = 'Panels Plugin Example Demo Panel'; + $page->access = array(); + $page->path = 'demo_panel'; + $page->load_flags = 1; + $page->css_id = ''; + $page->arguments = array( + 0 => + array( + 'name' => 'simplecontext_arg', + 'id' => 1, + 'default' => '404', + 'title' => '', + 'identifier' => 'Simplecontext arg', + 'keyword' => 'simplecontext', + ), + ); + $page->relationships = array( + 0 => + array( + 'context' => 'argument_simplecontext_arg_1', + 'name' => 'relcontext_from_simplecontext', + 'id' => 1, + 'identifier' => 'Relcontext from Simplecontext', + 'keyword' => 'relcontext', + ), + ); + $page->no_blocks = '0'; + $page->switcher_options = array(); + $page->menu = '0'; + $page->contexts = array(); + $display = new ctools_display(); + $display->did = 'new'; + $display->layout = 'threecol_33_34_33_stacked'; + $display->layout_settings = array(); + $display->panel_settings = array(); + $display->content = array(); + $display->panels = array(); + $pane = new stdClass(); + $pane->pid = 'new-1'; + $pane->panel = 'left'; + $pane->type = 'custom'; + $pane->shown = '1'; + $pane->subtype = 'custom'; + $pane->access = array(); + $pane->configuration = array( + 'style' => 'default', + 'override_title' => 0, + 'override_title_text' => '', + 'css_id' => '', + 'css_class' => '', + 'title' => '"No Context Item"', + 'body' => 'The "no context item" content type is here to demonstrate that you can create a content_type that does not require a context. This is probably the same as just creating a custom php block on the fly, and might serve the same purpose.', + 'format' => '1', + ); + $pane->cache = array(); + $display->content['new-1'] = $pane; + $display->panels['left'][0] = 'new-1'; + $pane = new stdClass(); + $pane->pid = 'new-2'; + $pane->panel = 'left'; + $pane->type = 'no_context_item'; + $pane->shown = '1'; + $pane->subtype = 'description'; + $pane->access = array(); + $pane->configuration = array( + 'style' => 'default', + 'override_title' => 0, + 'override_title_text' => '', + 'css_id' => '', + 'css_class' => '', + 'item1' => 'one', + 'item2' => 'two', + 'item3' => 'three', + ); + $pane->cache = array(); + $display->content['new-2'] = $pane; + $display->panels['left'][1] = 'new-2'; + $pane = new stdClass(); + $pane->pid = 'new-3'; + $pane->panel = 'middle'; + $pane->type = 'custom'; + $pane->shown = '1'; + $pane->subtype = 'custom'; + $pane->access = array(); + $pane->configuration = array( + 'style' => 'default', + 'override_title' => 0, + 'override_title_text' => '', + 'css_id' => '', + 'css_class' => '', + 'title' => 'Simplecontext', + 'body' => 'The "Simplecontext" content and content type demonstrate a very basic context and how to display it. + + Simplecontext includes configuration, so it can get info from the config. It can also get its information to run from a simplecontext context, generated either from an arg to the panels page or via explicitly adding a context to the page.', + 'format' => '1', + ); + $pane->cache = array(); + $display->content['new-3'] = $pane; + $display->panels['middle'][0] = 'new-3'; + $pane = new stdClass(); + $pane->pid = 'new-4'; + $pane->panel = 'middle'; + $pane->type = 'simplecontext_item'; + $pane->shown = '1'; + $pane->subtype = 'description'; + $pane->access = array( + 0 => '2', + 1 => '4', + ); + $pane->configuration = array( + 'context' => 'argument_simplecontext_arg_1', + 'style' => 'default', + 'override_title' => 0, + 'override_title_text' => '', + 'css_id' => '', + 'css_class' => '', + 'config_item_1' => 'simplecontext called from arg', + ); + $pane->cache = array(); + $display->content['new-4'] = $pane; + $display->panels['middle'][1] = 'new-4'; + $pane = new stdClass(); + $pane->pid = 'new-5'; + $pane->panel = 'right'; + $pane->type = 'custom'; + $pane->shown = '1'; + $pane->subtype = 'custom'; + $pane->access = array(); + $pane->configuration = array( + 'style' => 'default', + 'override_title' => 0, + 'override_title_text' => '', + 'css_id' => '', + 'css_class' => '', + 'title' => 'Relcontext', + 'body' => 'The relcontext content_type gets its data from a relcontext, which is an example of a relationship. This panel should be run with an argument like "/xxx", which allows the simplecontext to get its context, and then the relcontext is configured in this panel to get (create) its data from the simplecontext.', + 'format' => '1', + ); + $pane->cache = array(); + $display->content['new-5'] = $pane; + $display->panels['right'][0] = 'new-5'; + $pane = new stdClass(); + $pane->pid = 'new-6'; + $pane->panel = 'right'; + $pane->type = 'relcontext_item'; + $pane->shown = '1'; + $pane->subtype = 'description'; + $pane->access = array(); + $pane->configuration = array( + 'context' => 'relationship_relcontext_from_simplecontext_1', + 'style' => 'default', + 'override_title' => 0, + 'override_title_text' => '', + 'css_id' => '', + 'css_class' => '', + 'config_item_1' => 'default1', + ); + $pane->cache = array(); + $display->content['new-6'] = $pane; + $display->panels['right'][1] = 'new-6'; + $pane = new stdClass(); + $pane->pid = 'new-7'; + $pane->panel = 'top'; + $pane->type = 'custom_php'; + $pane->shown = '1'; + $pane->subtype = 'custom_php'; + $pane->access = array(); + $pane->configuration = array( + 'style' => 'default', + 'override_title' => 0, + 'override_title_text' => '', + 'css_id' => '', + 'css_class' => '', + 'title' => '', + 'body' => '$arg = arg(1); + $arg0 = arg(0); + if (!$arg) { + $block->content = <<This page is intended to run with an arg and you don\'t have one. +
+ Without an arg, the page doesn\'t have any context. +
Please try something like "/$arg0/xxx" +END; + + $block->title = "This is intended to run with an argument"; + } else { + $block->content = "The arg for this page is \'$arg\'"; + }', + ); + $pane->cache = array(); + $display->content['new-7'] = $pane; + $display->panels['top'][0] = 'new-7'; + $page->display = $display; + $page->displays = array(); + $pages['ctools_plugin_example'] = $page; + + + return $pages; +} diff --git a/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/relationships/relcontext_from_simplecontext.inc b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/relationships/relcontext_from_simplecontext.inc new file mode 100644 index 0000000..6224621 --- /dev/null +++ b/sites/all/modules/contrib/ctools/ctools_plugin_example/plugins/relationships/relcontext_from_simplecontext.inc @@ -0,0 +1,50 @@ + t("Relcontext from simplecontext"), + 'keyword' => 'relcontext', + 'description' => t('Adds a relcontext from existing simplecontext.'), + 'required context' => new ctools_context_required(t('Simplecontext'), 'simplecontext'), + 'context' => 'ctools_relcontext_from_simplecontext_context', + 'settings form' => 'ctools_relcontext_from_simplecontext_settings_form', +); + +/** + * Return a new context based on an existing context. + */ +function ctools_relcontext_from_simplecontext_context($context = NULL, $conf) { + // If unset it wants a generic, unfilled context, which is just NULL. + if (empty($context->data)) { + return ctools_context_create_empty('relcontext', NULL); + } + + // You should do error-checking here. + + // Create the new context from some element of the parent context. + // In this case, we'll pass in the whole context so it can be used to + // create the relcontext. + return ctools_context_create('relcontext', $context); +} + +/** + * Settings form for the relationship. + */ +function ctools_relcontext_from_simplecontext_settings_form($conf) { + // We won't configure it in this case. + return array(); +} + diff --git a/sites/all/modules/contrib/ctools/drush/ctools.drush.inc b/sites/all/modules/contrib/ctools/drush/ctools.drush.inc new file mode 100644 index 0000000..1862dbe --- /dev/null +++ b/sites/all/modules/contrib/ctools/drush/ctools.drush.inc @@ -0,0 +1,1017 @@ + array('ctex'), + 'callback' => 'ctools_drush_export', + 'description' => 'Export multiple CTools exportable objects directly to code.', + 'arguments' => array( + 'module' => 'Name of your module.', + ), + 'options' => array( + 'subdir' => 'The name of the sub directory to create the module in. Defaults to ctools_export which will be placed into sites/all/modules.', + 'remove' => 'Remove existing files before writing, except the .module file.', + 'filter' => 'Filter the list of exportables by status. Available options are enabled, disabled, overridden, database, code and all. Defaults to enabled.', + 'tables' => 'Comma separated list of exportable table names to filter by.', + ), + 'examples' => array( + 'drush ctex export_module' => 'Export CTools exportables to a module called "export_module".', + 'drush ctex export_module --subdir=exports' => 'Same as above, but into the sites/all/modules/exports directory.', + 'drush ctex export_module --subdir=exports --remove' => 'Same as above, but automatically removing all files, except for the .module file.', + 'drush ctex --filter="views_view"' => 'Filter export selection to the views_view table only.', + ), + ); + + $items['ctools-export-info'] = array( + 'aliases' => array('ctei'), + 'callback' => 'ctools_drush_export_info', + 'description' => 'Show available CTools exportable objects.', + 'arguments' => array(), + 'options' => array( + 'format' => 'Display exportables info in a different format such as print_r, json, export. The default is to show in a tabular format.', + 'tables-only' => 'Only show list of exportable types/table names and not available objects.', + 'filter' => 'Filter the list of exportables by status. Available options are enabled, disabled, overridden, database, and code.', + 'module' => $module_text, + ), + 'examples' => array( + 'drush ctools-export-info' => 'View export info on all exportables.', + 'drush ctools-export-info views_view variable' => 'View export info for views_view and variable exportable types only.', + 'drush ctei --filter=enabled' => 'Show all enabled exportables.', + 'drush ctei views_view --filter=disabled' => 'Show all enabled exportables.', + 'drush ctei views_view --module=node' => 'Show all exportables provided by/on behalf of the node module.', + ), + ); + + $items['ctools-export-view'] = array( + 'aliases' => array('ctev'), + 'callback' => 'ctools_drush_export_op_command', + 'description' => 'View CTools exportable object code output.', + 'arguments' => array( + 'table name' => 'Base table of the exportable you want to view.', + 'machine names' => 'Space separated list of exportables you want to view.', + ), + 'options' => array( + 'indent' => 'The string to use for indentation when dispalying the exportable export code. Defaults to \'\'.', + 'no-colour' => 'Remove any colour formatting from export string output. Ideal if you are sending the output of this command to a file.', + 'module' => $module_text, + 'all' => $all_text, + ), + 'examples' => array( + 'drush ctools-export-view views_view' => 'View all views exportable objects.', + 'drush ctools-export-view views_view archive' => 'View default views archive view.', + ), + ); + + $items['ctools-export-revert'] = array( + 'aliases' => array('cter'), + 'callback' => 'ctools_drush_export_op_command', + 'description' => 'Revert CTools exportables from changes overridden in the database.', + 'arguments' => array( + 'table name' => 'Base table of the exportable you want to revert.', + 'machine names' => 'Space separated list of exportables you want to revert.', + ), + 'options' => array( + 'module' => $module_text, + 'all' => $all_text, + ), + 'examples' => array( + 'drush ctools-export-revert views_view' => 'Revert all overridden views exportable objects.', + 'drush ctools-export-revert views_view archive' => 'Revert overridden default views archive view.', + 'drush ctools-export-revert --all' => 'Revert all exportables on the system.', + ), + ); + + $items['ctools-export-enable'] = array( + 'aliases' => array('ctee'), + 'callback' => 'ctools_drush_export_op_command', + 'description' => 'Enable CTools exportables.', + 'arguments' => array( + 'table name' => 'Base table of the exportable you want to enable.', + 'machine names' => 'Space separated list of exportables you want to enable.', + ), + 'options' => array( + 'module' => $module_text, + 'all' => $all_text, + ), + 'examples' => array( + 'drush ctools-export-enable views_view' => 'Enable all overridden views exportable objects.', + 'drush ctools-export-enable views_view archive' => 'Enable overridden default views archive view.', + ), + ); + + $items['ctools-export-disable'] = array( + 'aliases' => array('cted'), + 'callback' => 'ctools_drush_export_op_command', + 'description' => 'Disable CTools exportables.', + 'arguments' => array( + 'table name' => 'Base table of the exportable you want to disable.', + 'machine names' => 'Space separated list of exportables you want to disable.', + ), + 'options' => array( + 'module' => $module_text, + 'all' => $all_text, + ), + 'examples' => array( + 'drush ctools-export-disable views_view' => 'Disable all overridden views exportable objects.', + 'drush ctools-export-disable views_view archive' => 'Disable overridden default views archive view.', + ), + ); + + return $items; +} + +/** + * Implementation of hook_drush_help(). + */ +function ctools_drush_help($section) { + switch ($section) { + case 'meta:ctools:title': + return dt('CTools commands'); + case 'meta:entity:summary': + return dt('CTools drush commands.'); + } +} + +/** + * Drush callback: export + */ +function ctools_drush_export($module = 'foo') { + $error = FALSE; + if (preg_match('@[^a-z_]+@', $module)) { + $error = dt('The name of the module must contain only lowercase letters and underscores') . '.'; + drush_log($error, 'error'); + return; + } + + // Filter by tables. + $tables = _ctools_drush_explode_options('tables'); + + // Check status. + $filter = drush_get_option('filter', FALSE); + if (empty($filter)) { + drush_set_option('filter', 'enabled'); + } + + // Selection. + $options = array('all' => dt('Export everything'), 'select' => dt('Make selection')); + $selection = drush_choice($options, dt('Select to proceed')); + + if (!$selection) { + return; + } + + // Present the selection screens. + if ($selection == 'select') { + $selections = _ctools_drush_selection_screen($tables); + } + else { + $info = _ctools_drush_export_info($tables, TRUE); + $selections = $info['exportables']; + } + + // Subdirectory. + $dest_exists = FALSE; + $subdir = drush_get_option('subdir', 'ctools_export'); + $dest = 'sites/all/modules/' . $subdir . '/' . $module; + + // Overwriting files can be set with 'remove' argument. + $remove = drush_get_option('remove', FALSE); + + // Check if folder exists. + if (file_exists($dest)) { + $dest_exists = TRUE; + if ($remove) { + if (drush_confirm(dt('All files except for the .info and .module files in !module will be removed. You can choose later if you want to overwrite these files as well. Are you sure you want to proceed ?', array('!module' => $module)))) { + $remove = TRUE; + drush_log(dt('Files will be removed'), 'success'); + } + else { + drush_log(dt('Export aborted.'), 'success'); + return; + } + } + } + + // Remove files (except for the .module file) if the destination folder exists. + if ($remove && $dest_exists) { + _ctools_drush_file_delete($dest); + } + + // Create new dir if needed. + if (!$dest_exists) { + if (!file_exists('sites/all/modules/' . $subdir)) { + drush_mkdir('sites/all/modules/' . $subdir); + } + } + + // Create destination directory. + drush_mkdir($dest); + + // Load bulk export module. + module_load_include('module', 'bulk_export'); + + // Create options and call Bulk export function. + // We create an array, because maybe in the future we can pass in more + // options to the export function (pre-selected modules and/or exportables). + $options = array( + 'name' => $module, + 'selections' => $selections, + ); + $files = bulk_export_export(TRUE, $options); + + $alter = array( + 'module' => $module, + 'files' => $files, + ); + // Let other drush commands alter the files. + drush_command_invoke_all_ref('drush_ctex_files_alter', $alter); + $files = $alter['files']; + + // Start writing. + if (is_array($files)) { + foreach ($files as $base_file => $data) { + $filename = $dest . '/' . $base_file; + // Extra check for .module file. + if ($base_file == ($module . '.module' || $module . '.info') && file_exists($filename)) { + if (!drush_confirm(dt('Do you want to overwrite !module_file', array('!module_file' => $base_file)))) { + drush_log(dt('Writing of !filename skipped. This is the code that was supposed to be written:', array('!filename' => $filename)), 'success'); + drush_print('---------'); + drush_print(shellColours::getColouredOutput("\n$data", 'light_green')); + drush_print('---------'); + continue; + } + } + if (file_put_contents($filename, $data)) { + drush_log(dt('Succesfully written !filename', array('!filename' => $filename)), 'success'); + } + else { + drush_log(dt('Error writing !filename', array('!filename' => $filename)), 'error'); + } + } + } + else { + drush_log(dt('No files were found to be written.'), 'error'); + } +} + +/** + * Helper function to select the exportables. By default, all exportables + * will be selected, so it will be easier to deselect them. + * + * @param $tables + */ +function _ctools_drush_selection_screen(array $tables = array()) { + $selections = $build = array(); + $files = system_rebuild_module_data(); + + $selection_number = 0; + + $info = _ctools_drush_export_info($tables, TRUE); + $exportables = $info['exportables']; + $schemas = $info['schemas']; + + $export_tables = array(); + + foreach (array_keys($exportables) as $table) { + natcasesort($exportables[$table]); + $export_tables[$table] = $files[$schemas[$table]['module']]->info['name'] . ' (' . $table . ')'; + } + + foreach ($export_tables as $table => $table_title) { + if (!empty($exportables[$table])) { + $table_count = count($exportables[$table]); + $selection_number += $table_count; + foreach ($exportables[$table] as $key => $title) { + $build[$table]['title'] = $table_title; + $build[$table]['items'][$key] = $title; + $build[$table]['count'] = $table_count; + $selections[$table][$key] = $key; + } + } + } + + drush_print(dt('Number of exportables selected: !number', array('!number' => $selection_number))); + drush_print(dt('By default all exportables are selected. Select a table to deselect exportables. Select "cancel" to start writing the files.')); + + // Let's go into a loop. + $return = FALSE; + while (!$return) { + + // Present the tables choice. + $table_rows = array(); + foreach ($build as $table => $info) { + $table_rows[$table] = $info['title'] . ' (' . $info['count'] . ')'; + } + $table_choice = drush_choice($table_rows, dt('Select a table. Select cancel to start writing files.')); + + // Bail out. + if (!$table_choice) { + drush_log(dt('Selection mode done, starting to write the files.'), 'notice'); + $return = TRUE; + return $selections; + } + + // Present the exportables choice, using the drush_choice_multiple. + $max = count($build[$table_choice]['items']); + $exportable_rows = array(); + foreach ($build[$table_choice]['items'] as $key => $title) { + $exportable_rows[$key] = $title; + } + drush_print(dt('Exportables from !table', array('!table' => $build[$table_choice]['title']))); + $multi_select = drush_choice_multiple($exportable_rows, $selections[$table_choice], dt('Select exportables.'), '!value', '!value (selected)', 0, $max); + + // Update selections. + if (is_array($multi_select)) { + $build[$table_choice]['count'] = count($multi_select); + $selections[$table_choice] = array(); + foreach ($multi_select as $key) { + $selections[$table_choice][$key] = $key; + } + } + } +} + +/** + * Delete files in a directory, keeping the .module and .info files. + * + * @param $path + * Path to directory in which to remove files. + */ +function _ctools_drush_file_delete($path) { + if (is_dir($path)) { + $files = new DirectoryIterator($path); + foreach ($files as $fileInfo) { + if (!$fileInfo->isDot() && !in_array($fileInfo->getExtension(), array('module', 'info'))) { + unlink($fileInfo->getPathname()); + } + } + } +} + +/** + * Drush callback: Export info. + * + * @params $table_names + * Each argument will be taken as a CTools exportable table name. + */ +function ctools_drush_export_info() { + // Collect array of table names from args. + $table_names = func_get_args(); + + // Get format option to allow for alternative output. + $format = drush_get_option('format', FALSE); + $tables_only = drush_get_option('tables-only', FALSE); + $filter = drush_get_option('filter', FALSE); + $export_module = drush_get_option('module', FALSE); + + $load = (bool) $filter || $export_module; + + // Get info on these tables, or all tables if none specified. + $info = _ctools_drush_export_info($table_names, $load); + $schemas = $info['schemas']; + $exportables = $info['exportables']; + + if (empty($exportables)) { + drush_log(dt('There are no exportables available.'), 'warning'); + return; + } + + // Filter by export module. + if (is_string($export_module)) { + $exportables = _ctools_drush_export_module_filter($exportables, $export_module); + } + + if (empty($exportables)) { + drush_log(dt('There are no exportables matching this criteria.'), 'notice'); + return; + } + + $exportable_counts = _ctools_drush_count_exportables($exportables); + + // Only use array keys if --tables-only option is set. + if ($tables_only) { + $exportables = array_keys($exportables); + } + + // Use format from --format option if it's present, and send to drush_format. + if ($format) { + // Create array with all exportable info and counts in one. + $output = array( + 'exportables' => $exportables, + 'count' => $exportable_counts, + ); + drush_print(drush_format($output, NULL, $format)); + } + // Build a tabular output as default. + else { + $header = $tables_only ? array() : array(dt('Module'), dt('Base table'), dt('Exportables')); + $rows = array(); + foreach ($exportables as $table => $info) { + if (is_array($info)) { + $row = array( + $schemas[$table]['module'], + $table, + // Machine name is better for this? + shellColours::getColouredOutput(implode("\n", array_keys($info)), 'light_green') . "\n", + ); + $rows[] = $row; + } + else { + $rows[] = array($info); + } + } + if (!empty($rows)) { + drush_print("\n"); + array_unshift($rows, $header); + drush_print_table($rows, TRUE, array(20, 20)); + drush_print(dt('Total exportables found: !total', array('!total' => $exportable_counts['total']))); + foreach ($exportable_counts['exportables'] as $table_name => $count) { + drush_print(dt('!table_name (!count)', array('!table_name' => $table_name, '!count' => $count)), 2); + } + drush_print("\n"); + } + } +} +/** + * Drush callback: Acts as the hub for all op commands to keep + * all arg handling etc in one place. + */ +function ctools_drush_export_op_command() { + // Get all info for the current drush command. + $command = drush_get_command(); + $op = ''; + + switch ($command['command']) { + case 'ctools-export-view': + $op = 'view'; + break; + case 'ctools-export-revert': + // Revert is same as deleting. As any objects in the db are deleted. + $op = 'delete'; + break; + case 'ctools-export-enable': + $op = 'enable'; + break; + case 'ctools-export-disable': + $op = 'disable'; + break; + } + + if (!$op) { + return; + } + + if (drush_get_option('all', FALSE)) { + $info = _ctools_drush_export_info('', TRUE); + $exportable_info = $info['exportables']; + + $all = drush_confirm(dt('Are you sure you would like to !op all exportables on the system?', + array('!op' => _ctools_drush_export_op_alias($op)))); + + if ($all && $exportable_info) { + foreach ($exportable_info as $table => $exportables) { + if (!empty($exportables)) { + ctools_drush_export_op($op, $table, $exportables); + } + } + } + } + else { + $args = func_get_args(); + // Table name should always be first arg... + $table_name = array_shift($args); + // Any additional args are assumed to be exportable names. + $object_names = $args; + + // Return any exportables based on table name, object names, options. + $exportables = _ctools_drush_export_op_command_logic($op, $table_name, $object_names); + + if ($exportables) { + ctools_drush_export_op($op, $table_name, $exportables); + } + } +} + +/** + * Iterate through exportable object names, load them, and pass each + * object to the correct op function. + * + * @param $op + * @param $table_name + * @param $exportables + * + */ +function ctools_drush_export_op($op = '', $table_name = '', $exportables = NULL) { + $objects = ctools_export_crud_load_multiple($table_name, array_keys($exportables)); + + $function = '_ctools_drush_export_' . $op; + if (function_exists($function)) { + foreach ($objects as $object) { + $function($table_name, $object); + } + } + else { + drush_log(dt('CTools CRUD function !function doesn\'t exist.', + array('!function' => $function)), 'error'); + } +} + +/** + * Helper function to abstract logic for selecting exportable types/objects + * from individual commands as they will all share this same error + * handling/arguments for returning list of exportables. + * + * @param $table_name + * @param $object_names + * + * @return + * Array of exportable objects (filtered if necessary, by name etc..) or FALSE if not. + */ +function _ctools_drush_export_op_command_logic($op = '', $table_name = NULL, array $object_names = array()) { + if (!$table_name) { + drush_log(dt('Exportable table name empty. Use the --all command if you want to perform this operation on all tables.'), 'error'); + return FALSE; + } + + // Get export info based on table name. + $info = _ctools_drush_export_info(array($table_name), TRUE); + + if (!isset($info['exportables'][$table_name])) { + drush_log(dt('Exportable table name not found.'), 'error'); + return FALSE; + } + + $exportables = &$info['exportables']; + + if (empty($object_names)) { + $all = drush_confirm(dt('No object names entered. Would you like to try and !op all exportables of type !type', + array('!op' => _ctools_drush_export_op_alias($op), '!type' => $table_name))); + if (!$all) { + drush_log(dt('Command cancelled'), 'success'); + return FALSE; + } + } + else { + // Iterate through object names and check they exist in exportables array. + // Log error and unset them if they don't. + foreach ($object_names as $object_name) { + if (!isset($exportables[$table_name][$object_name])) { + drush_log(dt('Invalid exportable: !exportable', array('!exportable' => $object_name)), 'error'); + unset($object_names[$table_name][$object_name]); + } + } + // Iterate through exportables to get just a list of selected ones. + foreach (array_keys($exportables[$table_name]) as $exportable) { + if (!in_array($exportable, $object_names)) { + unset($exportables[$table_name][$exportable]); + } + } + } + + $export_module = drush_get_option('module', FALSE); + + if (is_string($export_module)) { + $exportables = _ctools_drush_export_module_filter($exportables, $export_module); + } + + return $exportables[$table_name]; +} + +/** + * Return array of CTools exportable info based on available tables returned from + * ctools_export_get_schemas(). + * + * @param $table_names + * Array of table names to return. + * @param $load + * (bool) should ctools exportable objects be loaded for each type. + * The default behaviour will load just a list of exportable names. + * + * @return + * Nested arrays of available exportables, keyed by table name. + */ +function _ctools_drush_export_info(array $table_names = array(), $load = FALSE) { + ctools_include('export'); + // Get available schemas that declare exports. + $schemas = ctools_export_get_schemas(TRUE); + $exportables = array(); + + if (!empty($schemas)) { + // Remove types we don't want, if any. + if (!empty($table_names)) { + foreach (array_keys($schemas) as $table_name) { + if (!in_array($table_name, $table_names)) { + unset($schemas[$table_name]); + } + } + } + // Load array of available exportables for each schema. + foreach ($schemas as $table_name => $schema) { + // Load all objects. + if ($load) { + $exportables[$table_name] = ctools_export_crud_load_all($table_name); + } + // Get a list of exportable names. + else { + if (!empty($schema['export']['list callback']) && function_exists($schema['export']['list callback'])) { + $exportables[$table_name] = $schema['export']['list callback'](); + } + else { + $exportables[$table_name] = ctools_export_default_list($table_name, $schema); + } + } + } + } + + if ($load) { + $filter = drush_get_option('filter', FALSE); + $exportables = _ctools_drush_filter_exportables($exportables, $filter); + } + + return array('exportables' => $exportables, 'schemas' => $schemas); +} + +/* + * View a single object. + * + * @param $table_name + * @param $object + */ +function _ctools_drush_export_view($table_name, $object) { + $indent = drush_get_option('indent', ''); + $no_colour = drush_get_option('no-colour', FALSE); + $export = ctools_export_crud_export($table_name, $object, $indent); + if ($no_colour) { + drush_print("\n$export"); + } + else { + drush_print(shellColours::getColouredOutput("\n$export", 'light_green')); + } +} + +/* + * Revert a single object. + * + * @param $table_name + * @param $object + */ +function _ctools_drush_export_delete($table_name, $object) { + $name = _ctools_drush_get_export_name($table_name, $object); + + if (_ctools_drush_object_is_overridden($object)) { + // Remove from db. + ctools_export_crud_delete($table_name, $object); + drush_log("Reverted object: $name", 'success'); + } + else { + drush_log("Nothing to revert for: $name", 'notice'); + } +} + +/* + * Enable a single object. + * + * @param $table_name + * @param $object + */ +function _ctools_drush_export_enable($table_name, $object) { + $name = _ctools_drush_get_export_name($table_name, $object); + + if (_ctools_drush_object_is_disabled($object)) { + + // Enable object. + ctools_export_crud_enable($table_name, $object); + drush_log("Enabled object: $name", 'success'); + } + else { + drush_log("$name is already Enabled", 'notice'); + } +} + +/* + * Disable a single object. + * + * @param $table_name + * @param $object + */ +function _ctools_drush_export_disable($table_name, $object) { + $name = _ctools_drush_get_export_name($table_name, $object); + + if (!_ctools_drush_object_is_disabled($object)) { + // Disable object. + ctools_export_crud_disable($table_name, $object); + drush_log("Disabled object: $name", 'success'); + } + else { + drush_log("$name is already disabled", 'notice'); + } +} + +/** + * Filter a nested array of exportables by export module. + * + * @param $exportables array + * Passed by reference. A nested array of exportables, keyed by table name. + * @param $export_module string + * The name of the export module providing the exportable. + */ +function _ctools_drush_export_module_filter($exportables, $export_module) { + $module_list = module_list(); + + if (!isset($module_list[$export_module])) { + drush_log(dt('Invalid export module: !export_module', array('!export_module' => $export_module)), 'error'); + } + + foreach ($exportables as $table => $objects) { + foreach ($objects as $key => $object) { + if (empty($object->export_module) || ($object->export_module !== $export_module)) { + unset($exportables[$table][$key]); + } + } + } + + return array_filter($exportables); +} + +/** + * Gets the key for an exportable type. + * + * @param $table_name + * The exportable table name. + * @param $object + * The exportable object. + * + * @return string + * The key defined in the export schema data. + */ +function _ctools_drush_get_export_name($table_name, $object) { + $info = _ctools_drush_export_info(array($table_name)); + $key = $info['schemas'][$table_name]['export']['key']; + return $object->{$key}; +} + +/** + * Determine if an object is disabled. + * + * @param $object + * Loaded CTools exportable object. + * + * @return TRUE or FALSE + */ +function _ctools_drush_object_is_disabled($object) { + return (isset($object->disabled) && ($object->disabled == TRUE)) ? TRUE : FALSE; +} + +/** + * Determine if an object is enabled. + * + * @see _ctools_drush_object_is_disabled. + */ +function _ctools_drush_object_is_enabled($object) { + return (empty($object->disabled)) ? TRUE : FALSE; +} + +/** + * Determine if an object is overridden. + */ +function _ctools_drush_object_is_overridden($object) { + $status = EXPORT_IN_CODE + EXPORT_IN_DATABASE; + return ($object->export_type == $status) ? TRUE : FALSE; +} + +/** + * Determine if an object is not overridden. + */ +function _ctools_drush_object_is_not_overridden($object) { + $status = EXPORT_IN_CODE + EXPORT_IN_DATABASE; + return ($object->export_type == $status) ? FALSE : TRUE; +} + +/** + * Determine if an object is only in the db. + */ +function _ctools_drush_object_is_db_only($object) { + return ($object->export_type == EXPORT_IN_DATABASE) ? TRUE : FALSE; +} + +/** + * Determine if an object is not in the db. + */ +function _ctools_drush_object_is_not_db_only($object) { + return ($object->export_type == EXPORT_IN_DATABASE) ? FALSE : TRUE; +} + +/** + * Determine if an object is a code only default. + */ +function _ctools_drush_object_is_code_only($object) { + return ($object->export_type == EXPORT_IN_CODE) ? TRUE : FALSE; +} + +/** + * Determine if an object is not a code only default. + */ +function _ctools_drush_object_is_not_code_only($object) { + return ($object->export_type == EXPORT_IN_CODE) ? FALSE : TRUE; +} + +/** + * Return an array of count information based on exportables array. + * + * @param $exportables + * Array of exportables to count. + * + * @return + * Array of count data containing the following: + * 'total' - A total count of all exportables. + * 'exportables' - An array of exportable counts per table. + */ +function _ctools_drush_count_exportables($exportables) { + $count = array('exportables' => array()); + + foreach ($exportables as $table => $objects) { + // Add the object count for each table. + $count['exportables'][$table] = count($objects); + } + + // Once all tables have been counted, total these up. + $count['total'] = array_sum($count['exportables']); + + return $count; +} + +/** + * Filters a collection of exportables based on filters. + * + * @param $exportables + * @param $filter + */ +function _ctools_drush_filter_exportables($exportables, $filter) { + $eval = FALSE; + + if (is_string($filter)) { + switch ($filter) { + // Show enabled exportables only. + case 'enabled': + $eval = '_ctools_drush_object_is_disabled'; + break; + // Show disabled exportables only. + case 'disabled': + $eval = '_ctools_drush_object_is_enabled'; + break; + // Show overridden exportables only. + case 'overridden': + $eval = '_ctools_drush_object_is_not_overridden'; + break; + // Show database only exportables. + case 'database': + $eval = '_ctools_drush_object_is_not_db_only'; + break; + // Show code only exportables. + case 'code': + $eval = '_ctools_drush_object_is_not_code_only'; + break; + // Do nothing. + case 'all': + break; + default: + drush_log(dt('Invalid filter option. Available options are: enabled, disabled, overridden, database, and code.'), 'error'); + return; + } + + if ($eval) { + foreach ($exportables as $table => $objects) { + foreach ($objects as $key => $object) { + if ($eval($object)) { + unset($exportables[$table][$key]); + } + } + } + } + } + + return array_filter($exportables); +} + +/** + * Return an alias for an op, that will be used to show as output. + * For now, this is mainly necessary for delete => revert alias. + * + * @param $op + * The op name. Such as 'enable', 'disable', or 'delete'. + * + * @return + * The matched alias value or the original $op passed in if not found. + */ +function _ctools_drush_export_op_alias($op) { + $aliases = array( + 'delete' => 'revert', + ); + + if (isset($aliases[$op])) { + return $aliases[$op]; + } + + return $op; +} + +/** + * Convert the drush options from a csv list into an array. + * + * @param $drush_option + * The drush option name to invoke. + * + * @return + * Exploded array of options. + */ +function _ctools_drush_explode_options($drush_option) { + $options = drush_get_option($drush_option, array()); + if (!empty($options)) { + $options = explode(',', $options); + return array_map('trim', $options); + } + + return $options; +} + +/** + * Class to deal with wrapping output strings with + * colour formatting for the shell. + */ +class shellColours { + + private static $foreground_colours = array( + 'black' => '0;30', + 'dark_gray' => '1;30', + 'blue' => '0;34', + 'light_blue' => '1;34', + 'green' => '0;32', + 'light_green' => '1;32', + 'cyan' => '0;36', + 'light_cyan' => '1;36', + 'red' => '0;31', + 'light_red' => '1;31', + 'purple' => '0;35', + 'light_purple' => '1;35', + 'brown' => '0;33', + 'yellow' => '1;33', + 'light_gray' => '0;37', + 'white' => '1;37', + ); + + private static $background_colours = array( + 'black' => '40', + 'red' => '41', + 'green' => '42', + 'yellow' => '43', + 'blue' => '44', + 'magenta' => '45', + 'cyan' => '46', + 'light_gray' => '47', + ); + + private function __construct() {} + + // Returns coloured string + public static function getColouredOutput($string, $foreground_colour = NULL, $background_colour = NULL) { + $coloured_string = ""; + + // Check if given foreground colour found + if ($foreground_colour) { + $coloured_string .= "\033[" . self::$foreground_colours[$foreground_colour] . "m"; + } + // Check if given background colour found + if ($background_colour) { + $coloured_string .= "\033[" . self::$background_colours[$background_colour] . "m"; + } + + // Add string and end colouring + $coloured_string .= $string . "\033[0m"; + + return $coloured_string; + } + + // Returns all foreground colour names + public static function getForegroundColours() { + return array_keys(self::$foreground_colours); + } + + // Returns all background colour names + public static function getBackgroundColours() { + return array_keys(self::$background_colours); + } + +} // shellColours diff --git a/sites/all/modules/contrib/ctools/help/about.html b/sites/all/modules/contrib/ctools/help/about.html new file mode 100644 index 0000000..30b64c2 --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/about.html @@ -0,0 +1,29 @@ +

The Chaos Tool Suite is a series of tools for developers to make code that I've found to be very useful to Views and Panels more readily available. Certain methods of doing things, particularly with AJAX, exportable objects and a plugin system, are proving to be ideas that are useful outside of just Views and Panels. This module does not offer much directly to the end user, but instead, creates a library for other modules to use. If you are an end user and some module asked you to install the CTools suite, then this is far as you really need to go. If you're a developer and are interested in these tools, read on!

+ +

Tools provided by CTools

+ +
+
Plugins
+
The plugins tool allows a module to allow other modules (and themes!) to provide plugins which provide some kind of functionality or some kind of task. For example, in Panels there are several types of plugins: Content types (which are like blocks), layouts (which are page layouts) and styles (which can be used to style a panel). Each plugin is represented by a .inc file, and the functionality they offer can differ wildly.
+ +
Context
+
Context is the idea that the objects that are used in page generation have more value than simply creating a single piece of output. Instead, contexts can be used to create multiple pieces of content that can all be put onto the page. Additionally, contexts can be used to derive other contexts via relationships, such as determining the node author and displaying data about the new context.
+ +
AJAX Tools
+
AJAX (also known as AHAH) is a method of allowing the browser and the server to communicate without requiring a page refresh. It can be used to create complicated interactive forms, but it is somewhat difficult to integrate into Drupal's Form API. These tools make it easier to accomplish this goal. In addition, CTools provides a few other javascript helpers, such as a modal dialog, a collapsible div, a simple dropdown and dependent checkboxes.
+ +
CSS scrubbing and caching
+
Drupal comes with a fantastic array of tools to ensure HTML is safe to output but does not contain any similar tools for CSS. CTools provides a small tool to sanitize CSS, so user-input CSS code can still be safely used. It also provides a method for caching CSS for better performance.
+ +
Exportable objects
+
Views and Panels both use objects that can either be in code or in the database, and the objects can be exported into a piece of PHP code, so they can be moved from site to site or out of the database entirely. This library abstracts that functionality, so other modules can use this same concept for their data.
+ +
Form tools
+
Drupal 6's FAPI really improved over Drupal 5, and made a lot of things possible. Still, it missed a few items that were needed to make form wizards and truly dynamic AJAX forms possible. CTools includes a replacement for drupal_get_form() that has a few more options and allows the caller to examine the $form_state once the form has completed.
+ +
Form wizards
+
Finally! An easy way to have form wizards, which is any 'form' that is actually a string of forms that build up to a final conclusion. The form wizard supports a single entry point, the ability to choose whether or not the user can go forward/back/up on the form and easy callbacks to handle the difficult job of dealing with data in between forms.
+ +
Temporary object cache
+
For normal forms, all of the data needed for an object is stored in the form so that the browser handles a lot of the work. For multi-step and ajax forms, however, this is impractical, and letting the browser store data can be insecure. The object cache provides a non-volatile location to store temporary data while the form is being worked on. This is much safer than the standard Drupal caching mechanism, which is volatile, meaning it can be cleared at any time and any system using it must be capable of recreating the data that was there. This system also allows for object locking, since any object which has an item in the cache from another person can be assumed to be 'locked for editing'.
+
diff --git a/sites/all/modules/contrib/ctools/help/ajax.html b/sites/all/modules/contrib/ctools/help/ajax.html new file mode 100644 index 0000000..e69de29 diff --git a/sites/all/modules/contrib/ctools/help/collapsible-div.html b/sites/all/modules/contrib/ctools/help/collapsible-div.html new file mode 100644 index 0000000..b9b6d9c --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/collapsible-div.html @@ -0,0 +1 @@ +

To be written.

diff --git a/sites/all/modules/contrib/ctools/help/context-access.html b/sites/all/modules/contrib/ctools/help/context-access.html new file mode 100644 index 0000000..95a8d7f --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/context-access.html @@ -0,0 +1,12 @@ +

Access plugins allow context based access control to pages.

+
    'title' => Title of the plugin
+    'description' => Description of the plugin
+    'callback' => callback to see if there is access is available. params: $conf, $contexts, $account
+    'required context' => zero or more required contexts for this access plugin
+    'default' => an array of defaults or a callback giving defaults
+    'settings form' => settings form. params: &$form, &$form_state, $conf
+    settings form validate
+    settings form submit
+
+ +

Warning: your settings array will be stored in the meny system to reduce loads, so be trim.

\ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/help/context-arguments.html b/sites/all/modules/contrib/ctools/help/context-arguments.html new file mode 100644 index 0000000..5c479ae --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/context-arguments.html @@ -0,0 +1,14 @@ +

Arguments create a context from external input, which is assumed to be a string as though it came from a URL element.

+ +
'title' => title
+    'description' => Description
+    'keyword' => Default keyword for the context
+    'context' => Callback to create the context. Params: $arg = NULL, $conf = NULL, $empty = FALSE
+    'default' => either an array of default settings or a string which is a callback or null to not use.
+    'settings form' => params: $form, $form_state, $conf -- gets the whole form. Should put anything it wants to keep automatically in $form['settings']
+    'settings form validate' => params: $form, $form_state
+    'settings form submit' => params: $form, $form_state
+    'criteria form' => params: $form, &$form_state, $conf, $argument, $id -- gets the whole argument. It should only put form widgets in $form[$id]. $conf may not be properly initialized so always guard against this due to arguments being changed and handlers not being updated to match.
+    + submit + validate
+    'criteria select' => returns true if the selected criteria matches the context. params: $context, $conf
+
diff --git a/sites/all/modules/contrib/ctools/help/context-content.html b/sites/all/modules/contrib/ctools/help/context-content.html new file mode 100644 index 0000000..c1c6a35 --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/context-content.html @@ -0,0 +1,157 @@ +

The CTools pluggable content system provides various pieces of content as discrete bits of data that can be added to other applications, such as Panels or Dashboard via the UI. Whatever the content is added to stores the configuration for that individual piece of content, and provides this to the content.

+ +

Each content type plugin will be contained in a .inc file, with subsidiary files, if necessary, in or near the same directory. Each content type consists of some information and one or more subtypes, which all use the same renderer. Subtypes are considered to be instances of the type. For example, the 'views' content type would have each view in the system as a subtype. Many content types will have exactly one subtype.

+ +

Because the content and forms can be provided via ajax, the plugin also provides a list of CSS and JavaScript information that should be available on whatever page the content or forms may be AJAXed onto.

+ +

For the purposes of selecting content from the UI, each content subtype will have the following information:

+ +
    +
  • A title
  • +
  • A short description
  • +
  • A category [Do we want to add hierarchy categories? Do we want category to be more than just a string?]
  • +
  • An icon [do we want multiple icons? This becomes a hefty requirement]
  • +
+ +

Each piece of content provides one or more configuration forms, if necessary, and the system that includes the content will handle the data storage. These forms can be provided in sequence as wizards or as extra forms that can be accessed through advanced administration.

+ +

The plugin for a content type should contain:

+ +
+
title
+
For use on the content permissions screen.
+
content types
+
Either an array of content type definitions, or a callback that will return content type definitions. This callback will get the plugin definition as an argument.
+ +
content type
+
[Optional] Provide a single content type definition. This is only necessary if content types might be intensive.
+ +
render callback
+
The callback to render the content. Parameters: +
+
$subtype
+
The name of the subtype being rendered. NOT the loaded subtype data.
+ +
$conf
+
The stored configuration for the content.
+ +
$args
+
Any arguments passed.
+ +
$context
+
An array of contexts requested by the required contexts and assigned by the configuration step.
+ +
$incoming_content
+
Any 'incoming content' if this is a wrapper.
+
+
+ +
admin title
+
A callback to provide the administrative title. If it is not a function, then it will be counted as a string to use as the admin title.
+ +
admin info
+
A callback to provide administrative information about the content, to be displayed when manipulating the content. It should contain a summary of configuration.
+ +
edit form
+
Either a single form ID or an array of forms *keyed* by form ID with the value to be used as the title of the form. %title me be used as a placeholder for the administrative title if necessary. + Example: +
array(
+  'ctools_example_content_form_second' => t('Configure first form'),
+  'ctools_example_content_form_first' => t('Configure second form'),
+),
+
+The first form will always have required configuration added to it. These forms are normal FAPI forms, but you do not need to provide buttons, these will be added by the form wizard. +
+ +
add form
+
[Optional] If different from the edit forms, provide them here in the same manner. Also may be set to FALSE to not have an add form.
+ +
css
+
A file or array of CSS files that are necessary for the content.
+ +
js
+
A file or array of javascript files that are necessary for the content to be displayed.
+ +
admin css
+
A file or array of CSS files that are necessary for the forms.
+ +
admin js
+
A file or array of JavaScript files that are necessary for the forms.
+ +
extra forms
+
An array of form information to handle extra administrative forms.
+ +
no title override
+
Set to TRUE if the title cannot be overridden.
+ +
single
+
Set to TRUE if this content provides exactly one subtype.
+ +
render last
+
Set to true if for some reason this content needs to render after other content. This is primarily used for forms to ensure that render order is correct.
+
+ +

TODO: many of the above callbacks can be assumed based upon patterns: modulename + '_' + name + '_' + function. i.e, render, admin_title, admin_info, etc.

+ +

TODO: Some kind of simple access control to easily filter out content.

+ +

The subtype definition should contain:

+ +
+
title
+
The title of the subtype.
+ +
icon
+
The icon to display for the subtype.
+ +
path
+
The path for the icon if it is not in the same directory as the plugin.
+ +
description
+
The short description of the subtype, to be used when selecting it in the UI.
+ +
category
+
Either a text string for the category, or an array of the text string followed by the category weight.
+ +
required context [Optional]
+ +
Either a ctools_context_required or ctools_context_optional or array of contexts for this content. If omitted, no contexts are used.
+ +
create content access [Optional]
+ +
An optional callback to determine if a user can access this subtype. The callback will receive two arguments, the type and subtype.
+
+ +

Rendered content

+ +

Rendered content is a little more than just HTML.

+ +
+
title
+
The safe to render title of the content.
+ +
content
+
The safe to render HTML content.
+ +
links
+
An array of links associated with the content suitable for theme('links').
+ +
more
+
An optional 'more' link (destination only)
+ +
admin_links
+
Administrative links associated with the content, suitable for theme('links').
+ +
feeds
+
An array of feed icons or links associated with the content. Each member of the array is rendered HTML.
+ +
type
+
The content type.
+ +
subtype
+
The content subtype. These two may be used together as module-delta for block style rendering.
+
+ +

Todo: example

+ +

Todo after implementations are updated to new version.

diff --git a/sites/all/modules/contrib/ctools/help/context-context.html b/sites/all/modules/contrib/ctools/help/context-context.html new file mode 100644 index 0000000..2314bd5 --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/context-context.html @@ -0,0 +1,13 @@ +

Context plugin data:

+ +
+    'title' => Visible title
+    'description' => Description of context
+    'context' => Callback to create a context. Params: $empty, $data = NULL, $conf = FALSE
+    'settings form' => Callback to show a context setting form. Params: ($conf, $external = FALSE)
+    'settings form validate' => params: ($form, &$form_values, &$form_state)
+    'settings form submit' => params: 'ctools_context_node_settings_form_submit',
+    'keyword' => The default keyword to use.
+    'context name' => The unique identifier for this context for use by required context checks.
+    'no ui' => if TRUE this context cannot be selected.
+
\ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/help/context-relationships.html b/sites/all/modules/contrib/ctools/help/context-relationships.html new file mode 100644 index 0000000..cc9969e --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/context-relationships.html @@ -0,0 +1,13 @@ +

Relationship plugin data:

+ +
+    'title' => The title to display.
+    'description' => Description to display.
+    'keyword' => Default keyword for the context created by this relationship.
+    'required context' => One or more ctools_context_required/optional objects 
+      describing the context input.
+      new panels_required_context(t('Node'), 'node'),
+    'context' => The callback to create the context. Params: ($context = NULL, $conf)
+    'settings form' => Settings form. Params: $conf
+    'settings form validate' => Validate. 
+
diff --git a/sites/all/modules/contrib/ctools/help/context.html b/sites/all/modules/contrib/ctools/help/context.html new file mode 100644 index 0000000..e69de29 diff --git a/sites/all/modules/contrib/ctools/help/css.html b/sites/all/modules/contrib/ctools/help/css.html new file mode 100644 index 0000000..b9b6d9c --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/css.html @@ -0,0 +1 @@ +

To be written.

diff --git a/sites/all/modules/contrib/ctools/help/ctools.help.ini b/sites/all/modules/contrib/ctools/help/ctools.help.ini new file mode 100644 index 0000000..fcb121b --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/ctools.help.ini @@ -0,0 +1,97 @@ +[advanced help settings] +line break = TRUE + +[about] +title = About Chaos Tool Suite +weight = -100 + +[context] +title = Context tool +weight = -40 + +[context-access] +title = Context based access control plugins +parent = context + +[context-context] +title = Context plugins +parent = context + +[context-arguments] +title = Argument plugins +parent = context + +[context-relationships] +title = Relationship plugins +parent = context + +[context-content] +title = Content plugins +parent = context + +[css] +title = CSS scrubbing and caching tool + +[menu] +title = Miscellaneous menu helper tool + +[plugins] +title = Plugins and APIs tool +weight = -50 + +[plugins-api] +title = Implementing APIs +parent = plugins + +[plugins-creating] +title = Creating plugins +parent = plugins + +[plugins-implementing] +title = Implementing plugins +parent = plugins + +[export] +title = Exportable objects tool + +[export-ui] +title = Exportable objects UI creator + +[form] +title = Form tools + +[wizard] +title = Form wizard tool + +[ajax] +title = AJAX and Javascript helper tools +weight = -30 + +[modal] +title = Javascript modal tool +parent = ajax + +[collapsible-div] +title = Javascript collapsible DIV +parent = ajax + +[dropdown] +title = Javascript dropdown +parent = ajax + +[dropbutton] +title = Javascript dropbutton +parent = ajax + +[dependent] +title = Dependent checkboxes and radio buttons +parent = ajax + +[object-cache] +title = Temporary object caching + +; A bunch of this stuff we'll put in panels. + +[plugins-content] +title = Creating content type plugins +parent = panels%api diff --git a/sites/all/modules/contrib/ctools/help/dependent.html b/sites/all/modules/contrib/ctools/help/dependent.html new file mode 100644 index 0000000..b9b6d9c --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/dependent.html @@ -0,0 +1 @@ +

To be written.

diff --git a/sites/all/modules/contrib/ctools/help/dropbutton.html b/sites/all/modules/contrib/ctools/help/dropbutton.html new file mode 100644 index 0000000..b9b6d9c --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/dropbutton.html @@ -0,0 +1 @@ +

To be written.

diff --git a/sites/all/modules/contrib/ctools/help/dropdown.html b/sites/all/modules/contrib/ctools/help/dropdown.html new file mode 100644 index 0000000..b9b6d9c --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/dropdown.html @@ -0,0 +1 @@ +

To be written.

diff --git a/sites/all/modules/contrib/ctools/help/export-ui.html b/sites/all/modules/contrib/ctools/help/export-ui.html new file mode 100644 index 0000000..e6b1086 --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/export-ui.html @@ -0,0 +1,85 @@ +

Most user interfaces for exportables are very similar, so CTools includes a tool to provide the framework for the most common UI. This tool is a plugin of the 'export_ui' type. In order to create a UI for your exportable object with this tool, you first need to ensure that your module supports the plugin:

+ +
+function HOOK_ctools_plugin_directory($module, $plugin) {
+  if ($module == 'ctools' && $plugin == 'export_ui') {
+    return 'plugins/' . $plugin;
+  }
+}
+
+ +

Then, you need to create a plugin .inc file describing your UI. Most of the UI runs with sane but simple defaults, so for the very simplest UI you don't need to do very much. This is a very simple example plugin for the 'example' export type:

+ +
+$plugin = array(
+  // The name of the table as found in the schema in hook_install. This
+  // must be an exportable type with the 'export' section defined.
+  'schema' => 'example',
+
+  // The access permission to use. If not provided it will default to
+  // 'administer site configuration'
+  'access' => 'administer example',
+
+  // You can actually define large chunks of the menu system here. Nothing
+  // is required here. If you leave out the values, the prefix will default
+  // to admin/structure and the item will default to the plugin name.
+  'menu' => array(
+    'menu prefix' => 'admin/structure',
+    'menu item' => 'example',
+    // Title of the top level menu. Note this should not be translated,
+    // as the menu system will translate it.
+    'menu title' => 'Example', 
+    // Description of the top level menu, which is usually needed for
+    // menu items in an administration list. Will be translated
+    // by the menu system.
+    'menu description' => 'Administer site example objects.',
+  ),
+
+  // These are required to provide proper strings for referring to the
+  // actual type of exportable. "proper" means it will appear at the
+  // beginning of a sentence.
+  'title singular' => t('example'),
+  'title singular proper' => t('Example'),
+  'title plural' => t('examples'),
+  'title plural proper' => t('Examples'),
+
+  // This will provide you with a form for editing the properties on your
+  // exportable, with validate and submit handler.
+  //
+  // The item being edited will be in $form_state['item'].
+  //
+  // The submit handler is only responsible for moving data from
+  // $form_state['values'] to $form_state['item'].
+  //
+  // All callbacks will accept &$form and &$form_state as arguments.
+  'form' => array(
+    'settings' => 'example_ctools_export_ui_form',
+    'validate' => 'example_ctools_export_ui_form_validate',
+    'submit' => 'example_ctools_export_ui_form_submit',
+  ),
+
+);
+
+ +

For a more complete list of what you can set in your plugin, please see ctools_export_ui_defaults() in includes/export-ui.inc to see what the defaults are.

+ +

More advanced UIs

+ +

The bulk of this UI is built on an class called ctools_export_ui, which is itself stored in ctools/plugins/export_ui as the default plugin. Many UIs will have more complex needs than the defaults provide. Using OO and overriding methods can allow an implementation to use the basics and still provide more where it is needed. To utilize this, first add a 'handler' directive to your plugin .inc file:

+ +
+   'handler' => array(
+     'class' => 'ctools_export_ui_example',
+     'parent' => 'ctools_export_ui',
+   ),
+
+ +Then create your class in ctools_export_ui_example.class.php in your plugins directory: + +
+class ctools_export_ui_example extends ctools_export_ui {
+
+}
+
+ +

You can override any method found in the class (see the source file for details). In particular, there are several list methods that are good candidates for overriding if you need to provide richer data for listing, sorting or filtering. If you need multi-step add/edit forms, you can override edit_page(), add_page(), clone_page(), and import_page() to put your wizard in place of the basic editing system. For an example of how to use multi-step wizards, see the import_page() method.

diff --git a/sites/all/modules/contrib/ctools/help/export.html b/sites/all/modules/contrib/ctools/help/export.html new file mode 100644 index 0000000..ce24cad --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/export.html @@ -0,0 +1,294 @@ +

Exportable objects are objects that can live either in the database or in code, or in both. If they live in both, then the object in code is considered to be "overridden", meaning that the version in code is ignored in favor of the version in the database.

+ +

The main benefit to this is that you can move objects that are intended to be structure or feature-related into code, thus removing them entirely from the database. This is a very important part of the deployment path, since in an ideal world, the database is primarily user generated content, whereas site structure and site features should be in code. However, many many features in Drupal rely on objects being in the database and provide UIs to create them.

+ +

Using this system, you can give your objects dual life. They can be created in the UI, exported into code and put in revision control. Views and Panels both use this system heavily. Plus, any object that properly implements this system can be utilized by the Features module to be used as part of a bundle of objects that can be turned into feature modules.

+ +

Typically, exportable objects have two identifiers. One identifier is a simple serial used for database identification. It is a primary key in the database and can be used locally. It also has a name which is an easy way to uniquely identify it. This makes it much less likely that importing and exporting these objects across systems will have collisions. They still can, of course, but with good name selection, these problems can be worked around.

+ +

Making your objects exportable

+ +

To make your objects exportable, you do have to do a medium amount of work.

+ +
    +
  1. Create a chunk of code in your object's schema definition in the .install file to introduce the object to CTools' export system.
  2. +
  3. Create a load function for your object that utilizes ctools_export_load_object().
  4. +
  5. Create a save function for your object that utilizes drupal_write_record() or any method you desire.
  6. +
  7. Create an import and export mechanism from the UI.
  8. +
+ +

The export section of the schema file

+ +

Exportable objects are created by adding definition to the schema in an 'export' section. For example:

+ +
+function mymodule_schema() {
+  $schema['mymodule_myobj'] = array(
+    'description' => t('Table storing myobj definitions.'),
+    'export' => array(
+      'key' => 'name',
+      'key name' => 'Name',
+      'primary key' => 'oid',
+      'identifier' => 'myobj', // Exports will be as $myobj
+      'default hook' => 'default_mymodule_myobj',  // Function hook name.
+      'api' => array(
+        'owner' => 'mymodule',
+        'api' => 'default_mymodule_myobjs',  // Base name for api include files.
+        'minimum_version' => 1,
+        'current_version' => 1,
+      ),
+      // If the key is stored in a table that is joined in, specify it:
+      'key in table' => 'my_join_table',
+
+    ),
+
+    // If your object's data is split up across multiple tables, you can
+    // specify additional tables to join. This is very useful when working
+    // with modules like exportables.module that has a special table for
+    // translating keys to local database IDs.
+    //
+    // The joined table must have its own schema definition.
+    //
+    // If using joins, you should implement a 'delete callback' (see below)
+    // to ensure that deletes happen properly. export.inc does not do this
+    // automatically!
+    'join' => array(
+      'exportables' => array(
+        // The following parameters will be used in this way:
+        // SELECT ... FROM {mymodule_myobj} t__0 INNER JOIN {my_join_table} t__1 ON t__0.id = t__1.id AND extras
+        'table' => 'my_join_table',
+        'left_key' => 'format',
+        'right_key' => 'id',
+        // Optionally you can define a callback to add custom conditions or
+        // alter the query as necessary. The callback function takes 3 args:
+        //
+        //   myjoincallback(&$query, $schema, $join_schema);
+        //
+        // where $query is the database query object, $schema is the schema for
+        // the export base table and $join_schema is the schema for the current
+        // join table.
+        'callback' => 'myjoincallback',
+
+        // You must specify which fields will be loaded. These fields must
+        // exist in the schema definition of the joined table.
+        'load' => array(
+          'machine',
+        ),
+
+        // And finally you can define other tables to perform INNER JOINS
+        //'other_joins' => array(
+        //   'table' => ...
+        //),
+      ),
+    )
+    'fields' => array(
+      'name' => array(
+        'type' => 'varchar',
+        'length' => '255',
+        'description' => 'Unique ID for this object. Used to identify it programmatically.',
+      ),
+      'oid' => array(
+        'type' => 'serial',
+        'unsigned' => TRUE,
+        'not null' => TRUE,
+        'description' => 'Primary ID field for the table. Not used for anything except internal lookups.',
+        'no export' => TRUE, // Do not export database-only keys.
+      ),
+    // ......
+    'primary key' => array('oid'),
+    'unique keys' => array(
+      'name' => array('name'),
+    ),
+  );
+  return $schema;
+}
+
+ +
+
key
+
This is the primary key of the exportable object and should be a string as names are more portable across systems. It is possible to use numbers here, but be aware that export collisions are very likely. Defaults to 'name'.
+ +
key name
+
Human readable title of the export key. Defaults to 'Name'. Because the schema is cached, do not translate this. It must instead be translated when used.
+ +
primary key
+
A single field within the table that is to be used as the main identifier to discern whether or not the object has been written. As the schema definition's primary key value will be used by default, it is not usually necessary to define this.
+ +
object
+
The class the object should be created as, if 'object factory' is not set. If this is not set either, defaults as stdClass.
+ +
object factory
+
Function used to create the object. The function receives the schema and the loaded data as a parameters: your_factory_function($schema, $data). If this is set, 'object' has no effect since you can use your function to create whatever class you wish.
+ +
admin_title
+
A convenience field that may contain the field which represents the human readable administrative title for use in export UI. If a field "admin_title" exists, it will automatically be used.
+ +
admin_description
+
A convenience field that may contain the field which represents the human readable administrative title for use in export UI. If a field "admin_title" exists, it will automatically be used.
+ +
can disable
+
Control whether or not the exportable objects can be disabled. All this does is cause the 'disabled' field on the object to always be set appropriately, and a variable is kept to record the state. Changes made to this state must be handled by the owner of the object. Defaults to TRUE.
+ +
status
+
Exportable objects can be enabled or disabled, and this status is stored in a variable. This defines what variable that is. Defaults to: 'default_' . $table.
+ +
default hook
+
What hook to invoke to find exportable objects that are currently defined. These will all be gathered into a giant array. Defaults to 'default_' . $table.
+ +
cache defaults
+
If true, default objects will be cached so that the processing of the hook does not need to be called often. Defaults to FALSE. Recommended if you will potentially have a lot of objects in code. Not recommended if code will be the exception.
+ +
default cache bin
+
If default object caching is enabled, what cache bin to use. This defaults to the basic "cache". It is highly recommended that you use a different cache bin if possible.
+ +
identifier
+
When exporting the object, the identifier is the variable that the exported object will be placed in. Defaults to $table.
+ +
bulk export
+
Declares whether or not the exportable will be available for bulk exporting.
+ +
export type string
+
The export type string (Local, Overridden, Database) is normally stored as $item->type. Since type is a very common keyword, it's possible to specify what key to actually use.
+ +
list callback
+
Bulk export callback to provide a list of exportable objects to be chosen for bulk exporting. Defaults to $module . '_' . $table . '_list' if the function exists. If it is not, a default listing function will be provided that will make a best effort to list the titles. See ctools_export_default_list().
+ +
to hook code callback
+
Function used to generate an export for the bulk export process. This is only necessary if the export is more complicated than simply listing the fields. Defaults to $module . '_' . $table . '_to_hook_code'. + +
boolean
+
Explicitly indicate if a table field contains a boolean or not. The Schema API does not model the +difference between a tinyint and a boolean type. Boolean values are stored in tinyint fields. This may cause mismatch errors when exporting a non-boolean value from a tinyint field. Add this to a tinyint field if it contains boolean data and can be exported. Defaults to TRUE. + +
create callback
+
CRUD callback to use to create a new exportable item in memory. If not provided, the default function will be used. The single argument is a boolean used to determine if defaults should be set on the object. This object will not be written to the database by this callback.
+ +
load callback
+
CRUD callback to use to load a single item. If not provided, the default load function will be used. The callback will accept a single argument which should be an identifier of the export key.
+ +
load multiple callback
+
CRUD callback to use to load multiple items. If not provided, the default multiple load function will be used. The callback will accept an array which should be the identifiers of the export key.
+ +
load all callback
+
CRUD callback to use to load all items, usually for administrative purposes. If not provided, the default load function will be used. The callback will accept a single argument to determine if the load cache should be reset or not.
+ +
save callback
+
CRUD callback to use to save a single item. If not provided, the default save function will be used. The callback will accept a single argument which should be the complete exportable object to save.
+ +
delete callback
+
CRUD callback to use to delete a single item. If not provided, the default delete function will be used. The callback will accept a single argument which can be *either* the object or just the export key to delete. The callback MUST be able to accept either.
+ +
export callback
+
CRUD callback to use for exporting. If not provided, the default export function will be used. The callback will accept two arguments, the first is the item to export, the second is the indent to place on the export, if any.
+ +
import callback
+
CRUD callback to use for importing. If not provided, the default export function will be used. This function will accept the code as a single argument and, if the code evaluates, return an object represented by that code. In the case of failure, this will return a string with human readable errors.
+ +
status callback
+
CRUD callback to use for updating the status of an object. If the status is TRUE the object will be disabled. If the status is FALSE the object will be enabled.
+ +
api
+
The 'api' key can optionally contain some information for the plugin API definition. This means that the imports can be tied to an API name which is used to have automatic inclusion of files, and can be used to prevent dangerous objects from older versions from being loaded, causing a loss of functionality rather than site crashes or security loopholes. + +

If not present, no additional files will be loaded and the default hook will always be a simple hook that must be either part of the .module file or loaded during normal operations.

+ +

api supports these subkeys:

+ +
+
owner
+
The module that owns the API. Typically this is the name of the module that owns the schema. This will be one of the two keys used by hook_ctools_plugin_api() to determine version compatibility. Note that the name of this hook can be tailored via the use of hook_ctools_plugin_api_hook_name(). See ctools_plugin_api_get_hook() for details.
+
api
+
This is the name of the API, and will be the second parameter to the above mentioned hook. It will also be used as part of the name of the file that the hook containing default objects will be in, which comes in the form of MODULENAME.API.inc.
+
minimum_version
+
The minimum version supported. Any module reporting an API less than this will not have its default objects used. This should be updated only when API changes can cause older objects to crash or otherwise break badly.
+
current_version
+
The current version of the API. Any module reporting a required API higher than this will not have its default objects used.
+
+
+ +
+

In addition, each field can contain the following:

+
+
no export
+
Set to TRUE to prevent that field from being exported.
+ +
export callback
+
A function to override the export behavior. It will receive ($myobject, $field, $value, $indent) as arguments. By default, fields are exported through ctools_var_export().
+
+ +

Reserved keys on exportable objects

+ +

Exportable objects have several reserved keys that are used by the CTools export API. Each key can be found at $myobj->{$key} on an object loaded through ctools_export_load_object(). Implementing modules should not use these keys as they will be overwritten by the CTools export API.

+
+
api_version
+
The API version that this object implements.
+ +
disabled
+
A boolean for whether the object is disabled.
+ +
export_module
+
For objects that live in code, the module which provides the default object.
+ +
export_type
+
A bitmask representation of an object current storage. You can use this bitmask in combination with the EXPORT_IN_CODE and EXPORT_IN_DATABASE constants to test for an object's storage in your code. +
+ +
in_code_only
+
A boolean for whether the object lives only in code.
+ +
table
+
The schema API table that this object belongs to.
+ +
type
+
A string representing the storage type of this object. Can be one of the following: +
    +
  • Normal is an object that lives only in the database.
  • +
  • Overridden is an object that lives in the database and is overriding the exported configuration of a corresponding object in code.
  • +
  • Default is an object that lives only in code.
  • +
+Note: This key can be changed by setting 'export type string' to something else, to try and prevent "type" from conflicting. +
+
+ +

The load callback

+

Calling ctools_export_crud_load($table, $name) will invoke your load callback, calling ctools_export_crud_load_multiple($table, $names) will invoke your load multiple callback, and calling ctools_export_crud_load_all($table, $reset) will invoke your load all callback. The default handlers should be sufficient for most uses.

+ +

Typically, there will be three load functions. A 'single' load, to load just one object, a 'multiple' load to load multiple objects, and an 'all' load, to load all of the objects for use in administrating the objects or utilizing the objects when you need all of them. Using ctools_export_load_object() you can easily do both, as well as quite a bit in between. This example duplicates the default functionality for loading one myobj.

+ +
+/**
+ * Implements 'load callback' for myobj exportables.
+ */
+function mymodule_myobj_load($name) {
+  ctools_include('export');
+  $result = ctools_export_load_object('mymodule_myobjs', 'names', array($name));
+  if (isset($result[$name])) {
+    return $result[$name];
+  }
+}
+
+/**
+ * Implements 'load multiple callback' for myobj exportables.
+ */
+function mymodule_myobj_load_multiple(array $names) {
+  ctools_include('export')
+  $results = ctools_export_load_object('mymodule_myobjs', 'names', $names);
+  return array_filter($results);
+}
+
+ +

The save callback

+Calling ctools_export_crud_save($table, $object) will invoke your save callback. The default handlers should be sufficient for most uses. For the default save mechanism to work, you must define a 'primary key' in the 'export' section of your schema. The following example duplicates the default functionality for the myobj. + +
+/**
+* Save a single myobj.
+*/
+function mymodule_myobj_save(&$myobj) {
+  $update = (isset($myobj->oid) && is_numeric($myobj->oid)) ? array('oid') : array();
+  return drupal_write_record('myobj', $myobj, $update);
+}
+
+ +

Default hooks for your exports

+

All exportables come with a 'default' hook, which can be used to put your exportable into code. The easiest way to actually use this hook is to set up your exportable for bulk exporting, enable the bulk export module and export an object.

diff --git a/sites/all/modules/contrib/ctools/help/form.html b/sites/all/modules/contrib/ctools/help/form.html new file mode 100644 index 0000000..b9b6d9c --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/form.html @@ -0,0 +1 @@ +

To be written.

diff --git a/sites/all/modules/contrib/ctools/help/modal.html b/sites/all/modules/contrib/ctools/help/modal.html new file mode 100644 index 0000000..ea823a0 --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/modal.html @@ -0,0 +1,215 @@ +

CTools provides a simple modal that can be used as a popup to place forms. It differs from the normal modal frameworks in that it does not do its work via an iframe. This is both an advantage and a disadvantage. The iframe simply renders normal pages in a sub-browser and they can do their thing. That makes it much easier to put arbitrary pages and forms in a modal. However, the iframe is not very good at actually communicating changes to the main page, so you cannot open the modal, have it do some work, and then modify the page.

+ +

Invoking the modal

+ +

The basic form of the modal can be set up just by including the javascript and adding the proper class to a link or form that will open the modal. To include the proper javascript, simply include the library and call the add_js function:

+ +ctools_include('modal'); +ctools_modal_add_js(); + + +

You can have links and buttons bound to use the modal by adding the class ctools-use-modal. For convenience, there is a helper function to try and do this, though it's not great at doing all links so using this is optional:

+ +/** + * Render an image as a button link. This will automatically apply an AJAX class + * to the link and add the appropriate javascript to make this happen. + * + * @param $image + * The path to an image to use that will be sent to theme('image') for rendering. + * @param $dest + * The destination of the link. + * @param $alt + * The alt text of the link. + * @param $class + * Any class to apply to the link. @todo this should be a options array. + */ +function ctools_modal_image_button($image, $dest, $alt, $class = '') { + return ctools_ajax_text_button(theme('image', array('path' => $image), $dest, $alt, $class, 'ctools-use-modal'); +} + +/** + * Render text as a link. This will automatically apply an AJAX class + * to the link and add the appropriate javascript to make this happen. + * + * Note: 'html' => true so be sure any text is vetted! Chances are these kinds of buttons will + * not use user input so this is a very minor concern. + * + * @param $text + * The text to display as the link. + * @param $dest + * The destination of the link. + * @param $alt + * The alt text of the link. + * @param $class + * Any class to apply to the link. @todo this should be a options array. + */ +function ctools_modal_text_button($text, $dest, $alt, $class = '') { + return ctools_ajax_text_button($text, $dest, $alt, $class, 'ctools-use-modal'); +} + + +

Like with all CTools' AJAX functionality, the href of the link will be the destination, with any appearance of /nojs/ converted to /ajax/.

+ +

For submit buttons, however, the URL may be found a different, slightly more complex way. If you do not wish to simply submit the form to the modal, you can create a URL using hidden form fields. The ID of the item is taken and -url is appended to it to derive a class name. Then, all form elements that contain that class name are founded and their values put together to form a URL.

+ +

For example, let's say you have an 'add' button, and it has a select form item that tells your system what widget it is adding. If the id of the add button is edit-add, you would place a hidden input with the base of your URL in the form and give it a class of 'edit-add-url'. You would then add 'edit-add-url' as a class to the select widget allowing you to convert this value to the form without posting. If no URL is found, the action of the form will be used and the entire form posted to it.

+ +

Customizing the modal

+ +

If you do not wish to use the default modal, the modal can be customized by creating an array of data to define a customized modal. To do this, you add an array of info to the javascript settings to define the customizations for the modal and add an additional class to your modal link or button to tell it which modal to use.

+ +

First, you need to create a settings array. You can do this most easily with a bit of PHP:

+ +
drupal_add_js(array(
+    'my-modal-style' => array(
+      'modalSize' => array(
+        'type' => 'fixed',
+        'width' => 250,
+        'height' => 250,
+      ),
+    ),
+  ), 'setting');
+
+ +

The key to the array above (in this case, my-modal-style) is the identifier to your modal theme. You can have multiple modal themes on a page, so be sure to use an ID that will not collide with some other module's use. Using your module or theme as a prefix is a good idea.

+ +

Then, when adding the ctools-use-modal class to your link or button, also add the following class: ctools-modal-ID (in the example case, that would be ctools-modal-my-modal-style).

+ +

modalSize can be 'fixed' or 'scale'. If fixed it will be a raw pixel value; if 'scale' it will be a percentage of the screen.

+ +

You can set:

+
    +
  • modalSize: an array of data to control the sizing of the modal. It can contain: +
      +
    • type: Either fixed or scale. If fixed, the modal will always be a fixed size. If scale the modal will scale to a percentage of the browser window. Default: scale. +
    • width: If fixed the width in pixels. If scale the percentage of the screen expressed as a number less than zero. (For 80 percent, use .8, for example). Default: .8
    • +
    • height: If fixed the height in pixels. If scale the percentage of the screen expressed as a number less than zero. (For 80 percent, use .8, for example). Default: .8
    • +
    • addWidth: Any additional width to add to the modal in pixels. Only useful if the type is scale. Default: 0
    • +
    • addHeight: Any additional height to add to the modal in pixels. Only useful if the type is scale. Default: 0
    • +
    • contentRight: The number of pixels to remove from the content inside the modal to make room for scroll bar and decorations. Default: 25
    • +
    • contentBottom: The number of pixels to remove from the content inside the modal to make room for scroll bar and decorations. Default: 45
    • +
    +
  • +
  • modalTheme: The Drupal javascript themable function which controls how the modal will be rendered. This function must be in the Drupal.theme.prototype namespace. If you set this value, you must include a corresponding function in a javascript file and use drupal_add_js() to add that file. Default: CToolsModalDialog +
    +  Drupal.theme.prototype.CToolsModalDialog = function () {
    +    var html = ''
    +    html += '  <div id="ctools-modal">'
    +    html += '    <div class="ctools-modal-content">' // panels-modal-content
    +    html += '      <div class="modal-header">';
    +    html += '        <a class="close" href="#">';
    +    html +=            Drupal.CTools.Modal.currentSettings.closeText + Drupal.CTools.Modal.currentSettings.closeImage;
    +    html += '        </a>';
    +    html += '        <span id="modal-title" class="modal-title"> </span>';
    +    html += '      </div>';
    +    html += '      <div id="modal-content" class="modal-content">';
    +    html += '      </div>';
    +    html += '    </div>';
    +    html += '  </div>';
    +
    +    return html;
    +  }
    +
  • +
  • throbberTheme: The Drupal javascript themable function which controls how the modal throbber will be rendered. This function must be in the Drupal.theme.prototype namespace. If you set this value, you must include a corresponding function in a javascript file and use drupal_add_js() to add that file. Default: CToolsModalThrobber +
    +  Drupal.theme.prototype.CToolsModalThrobber = function () {
    +    var html = '';
    +    html += '  <div id="modal-throbber">';
    +    html += '    <div class="modal-throbber-wrapper">';
    +    html +=        Drupal.CTools.Modal.currentSettings.throbber;
    +    html += '    </div>';
    +    html += '  </div>';
    +
    +    return html;
    +  };
    +
    +
  • +
  • modalOptions: The options object that's sent to Drupal.CTools.Modal.modalContent. Can contain any CSS settings that will be applied to the modal backdrop, in particular settings such as opacity and background. Default: array('opacity' => .55, 'background' => '#fff');
  • +
  • animation: Controls how the modal is animated when it is first drawn. Either show, fadeIn or slideDown. Default: show
  • +
  • animationSpeed: The speed of the animation, expressed either as a word jQuery understands (slow, medium or fast) or a number of milliseconds for the animation to run. Defaults: fast
  • +
  • closeText: The text to display for the close button. Be sure to wrap this in t() for translatability. Default: t('Close window')
  • +
  • closeImage: The image to use for the close button of the modal. Default: theme('image', array('path' => ctools_image_path('icon-close-window.png'), 'alt' => t('Close window'), 'title' => t('Close window')))
  • +
  • loadingText: The text to display for the modal title during loading, along with the throbber. Be sure to wrap this in t() for translatability. Default: t('Close window')
  • +
  • throbber: The HTML to display for the throbber image. You can use this instead of CToolsModalThrobber theme if you just want to change the image but not the throbber HTML. Default: theme('image', array('path' => ctools_image_path('throbber.gif'), 'alt' => t('Loading...'), 'title' => t('Loading')))
  • +
+ +

Rendering within the modal

+

To render your data inside the modal, you need to provide a page callback in your module that responds more or less like a normal page.

+ +

In order to handle degradability, it's nice to allow your page to work both inside and outside of the modal so that users whose javascript is turned off can still use your page. There are two ways to accomplish this. First, you can embed 'nojs' as a portion of the URL and then watch to see if that turns into 'ajax' when the link is clicked. Second, if you do not wish to modify the URLs, you can check $_POST['js'] or $_POST['ctools_js'] to see if that flag was set. The URL method is the most flexible because it is easy to send the two links along completely different paths if necessary, and it is also much easier to manually test your module's output by manually visiting the nojs URL. It's actually quite difficult to do this if you have to set $_POST['js'] to test.

+ +

In your hook_menu, you can use the a CTools' AJAX convenience loader to help:

+ +
+  $items['ctools_ajax_sample/%ctools_js/login'] = array(
+      'title' => 'Login',
+      'page callback' => 'ctools_ajax_sample_login',
+      'page arguments' => array(1),
+      'access callback' => TRUE,
+      'type' => MENU_CALLBACK,
+  );
+
+ +

The first argument to the page callback will be the result of ctools_js_load() which will return TRUE if 'ajax' was the string, and FALSE if anything else (i.e, nojs) is the string. Which means you can then declare your function like this:

+ +
+function ctools_ajax_sample_login($js) {
+  if ($js) {
+    // react with the modal
+  }
+  else {
+    // react without the modal
+  }
+}
+
+ +

If your modal does not include a form, rendering the output you wish to give the user is just a matter of calling the modal renderer with your data:

+ +
+function ctools_ajax_hello_world($js) {
+  $title = t('Greetings');
+  $output = '<p>' . t('Hello world') . ''</p>';
+  if ($js) {
+    ctools_modal_render($title, $output);
+  }
+  else {
+    drupal_set_title($title);
+    return $output;
+  }
+}
+
+ +

If you need to do more than just render your modal, you can use a CTools $commands array. See the function ctools_modal_render() for more information on what you need to do here.

+ +

If you are displaying a form -- and the vast majority of modals display forms -- then you need to do just slightly more. Fortunately there is the ctools_modal_form_wrapper() function:

+ +
+  ctools_include('modal');
+  ctools_include('ajax');
+  $form_state = array(
+    'title' => t('Title of my form'),
+    'ajax' => $js,
+  );
+  $output = ctools_modal_form_wrapper('my_form', $form_state);
+  // There are some possible states after calling the form wrapper:
+  // 1) We are not using $js and the form has been executed.
+  // 2) We are using $js and the form was successfully submitted and
+  //    we need to dismiss the modal.
+  // Most other states are handled automatically unless you set flags in
+  // $form_state to avoid handling them, so we only deal with those two
+  // states.
+  if ($form_state['executed'] && $js) {
+    $commands = array();
+    $commands[] = ctools_modal_command_dismiss(t('Login Success'));
+    // In typical usage you will do something else here, such as update a
+    // div with HTML or redirect the page based upon the results of the modal
+    // form.
+    print ajax_render($commands);
+    exit;
+  }
+
+  // Otherwise, just return the output.
+  return $output;
+
+ +

You can also use CTools' form wizard inside the modal. You do not need to do much special beyond setting $form_state['modal'] = TRUE in the wizard form; it already knows how to handle the rest.

diff --git a/sites/all/modules/contrib/ctools/help/object-cache.html b/sites/all/modules/contrib/ctools/help/object-cache.html new file mode 100644 index 0000000..801a836 --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/object-cache.html @@ -0,0 +1,132 @@ +

The CTools Object Cache is a specialized cache for storing data that is non-volatile. This differs from the standard Drupal cache mechanism, which is volatile, meaning that the data can be cleared at any time and it is expected that any cached data can easily be reconstructed. In contrast, data stored in this cache is not expected to be reconstructable. It is primarily used for storing user input which is retrieved in stages, allowing for more complex user interface interactions.

+ +

The object cache consists of 3 normal functions for cache maintenance, and 2 additional functions to facilitate locking.

+ +

To use any of these functions, you must first use ctools_include:

+ +
+ctools_include('object-cache');
+
+ +
+/**
+ * Get an object from the non-volatile ctools cache.
+ *
+ * This function caches in memory as well, so that multiple calls to this
+ * will not result in multiple database reads.
+ *
+ * @param $obj
+ *   A 128 character or less string to define what kind of object is being
+ *   stored; primarily this is used to prevent collisions.
+ * @param $name
+ *   The name of the object being stored.
+ * @param $skip_cache
+ *   Skip the memory cache, meaning this must be read from the db again.
+ *
+ * @return
+ *   The data that was cached.
+ */
+function ctools_object_cache_get($obj, $name, $skip_cache = FALSE) {
+
+ +
+/**
+ * Store an object in the non-volatile ctools cache.
+ *
+ * @param $obj
+ *   A 128 character or less string to define what kind of object is being
+ *   stored; primarily this is used to prevent collisions.
+ * @param $name
+ *   The name of the object being stored.
+ * @param $cache
+ *   The object to be cached. This will be serialized prior to writing.
+ */
+function ctools_object_cache_set($obj, $name, $cache) {
+
+ +
+/**
+ * Remove an object from the non-volatile ctools cache
+ *
+ * @param $obj
+ *   A 128 character or less string to define what kind of object is being
+ *   stored; primarily this is used to prevent collisions.
+ * @param $name
+ *   The name of the object being removed.
+ */
+function ctools_object_cache_clear($obj, $name) {
+
+ +

To facilitate locking, which is the ability to prohibit updates by other users while one user has an object cached, this system provides two functions:

+ +
+/**
+ * Determine if another user has a given object cached.
+ *
+ * This is very useful for 'locking' objects so that only one user can
+ * modify them.
+ *
+ * @param $obj
+ *   A 128 character or less string to define what kind of object is being
+ *   stored; primarily this is used to prevent collisions.
+ * @param $name
+ *   The name of the object being removed.
+ *
+ * @return
+ *   An object containing the UID and updated date if found; NULL if not.
+ */
+function ctools_object_cache_test($obj, $name) {
+
+ +

The object returned by ctools_object_cache_test can be directly used to determine whether a user should be allowed to cache their own version of an object.

+ +

To allow the concept of breaking a lock, that is, clearing another users changes:

+ +
+/**
+ * Remove an object from the non-volatile ctools cache for all session IDs.
+ *
+ * This is useful for clearing a lock.
+ *
+ * @param $obj
+ *   A 128 character or less string to define what kind of object is being
+ *   stored; primarily this is used to prevent collisions.
+ * @param $name
+ *   The name of the object being removed.
+ */
+function ctools_object_cache_clear_all($obj, $name) {
+
+ +

Typical best practice is to use wrapper functions such as these:

+ +
+/**
+ * Get the cached changes to a given task handler.
+ */
+function delegator_page_get_page_cache($name) {
+  ctools_include('object-cache');
+  $cache = ctools_object_cache_get('delegator_page', $name);
+  if (!$cache) {
+    $cache = delegator_page_load($name);
+    $cache->locked = ctools_object_cache_test('delegator_page', $name);
+  }
+
+  return $cache;
+}
+
+/**
+ * Store changes to a task handler in the object cache.
+ */
+function delegator_page_set_page_cache($name, $page) {
+  ctools_include('object-cache');
+  $cache = ctools_object_cache_set('delegator_page', $name, $page);
+}
+
+/**
+ * Remove an item from the object cache.
+ */
+function delegator_page_clear_page_cache($name) {
+  ctools_include('object-cache');
+  ctools_object_cache_clear('delegator_page', $name);
+}
+
diff --git a/sites/all/modules/contrib/ctools/help/plugins-api.html b/sites/all/modules/contrib/ctools/help/plugins-api.html new file mode 100644 index 0000000..548f17b --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/plugins-api.html @@ -0,0 +1,55 @@ +

APIs are a form of plugins that are tightly associated with a module. Instead of a module providing any number of plugins, each module provides only one file for an API and this file can contain hooks that the module should invoke.

+ +

Modules support this API by implementing hook_ctools_plugin_api($module, $api). If they support the API, they return a packet of data:

+ +
+function mymodule_ctools_plugin_api($module, $api) {
+  if ($module == 'some module' && $api = 'some api') {
+    return array(
+      'version' => The minimum API version this system supports. If this API version is incompatible then the .inc file will not be loaded.
+      'path' => Where to find the file. Optional; if not specified it will be the module's directory.
+      'file' => an alternative version of the filename. If not specified it will be $module.$api.inc
+    );
+  }
+}
+
+ +

This implementation must be in the .module file.

+ +

Modules utilizing this can invole ctools_plugin_api_include() in order to ensure all modules that support the API will have their files loaded as necessary. It's usually easiest to create a small helper function like this:

+ +
+define('MYMODULE_MINIMUM_VERSION', 1);
+define('MYMODULE_VERSION', 1);
+
+function mymodule_include_api() {
+  ctools_include('plugins');
+  return ctools_plugin_api_include('mymodule', 'myapi', MYMODULE_MINIMUM_VERSION, MYMODULE_VERSION);
+}
+
+ +

Using a define will ensure your use of version numbers is consistent and easy to update when you make API changes. You can then use the usual module_invoke type commands:

+ +
+mymodule_include_api();
+module_invoke('myhook', $data);
+
+ +

If you need to pass references, this construct is standard:

+ +
+foreach (mymodule_include_api() as $module => $info) {
+  $function = $module . '_hookname';
+  // Just because they implement the API and include a file does not guarantee they implemented
+  // a hook function!
+  if (!function_exists($function)) {
+    continue;
+  }
+
+  // Typically array_merge() is used below if data is returned.
+  $result = $function($data1, $data2, $data3);
+}
+
+ +

TODO: There needs to be a way to check API version without including anything, as a module may simply +provide normal plugins and versioning could still matter.

diff --git a/sites/all/modules/contrib/ctools/help/plugins-creating.html b/sites/all/modules/contrib/ctools/help/plugins-creating.html new file mode 100644 index 0000000..2323705 --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/plugins-creating.html @@ -0,0 +1,203 @@ +There are two primary pieces to using plugins. The first is getting the data, and the second is using the data. + +

Defining a plugin

+To define that you offer a plugin that modules can implement, you first must implement hook_ctools_plugin_type() to tell the plugin system about your plugin. + +
+/**
+ * Implements hook_ctools_plugin_type() to inform CTools about the layout plugin.
+ */
+function panels_ctools_plugin_type() {
+  $plugins['layouts'] = array(
+    'load themes' => TRUE,
+  );
+
+  return $plugins;
+}
+
+ +The following information can be specified for each plugin type: +
+
cache
+
Defaults to: FALSE
+
If set to TRUE, the results of ctools_get_plugins will be cached in the 'cache' table (by default), thus preventing .inc files from being loaded. ctools_get_plugins looking for a specific plugin will always load the appropriate .inc file.
+
cache table
+
Defaults to: 'cache'
+
If 'cache' is TRUE, then this value specifies the cache table where the cached plugin information will be stored.
+
classes
+
Defaults to: array()
+
An array of class identifiers(i.e. plugin array keys) which a plugin of this type uses to provide classes to the CTools autoloader. For example, if classes is set to array('class'), then CTools will search each $plugin['class'] for a class to autoload. Depending of the plugin structure, a class identifier may be either:
+
+
- a file name:
+
the file which holds the class with the name structure as: [filename].[class].php
+
in this case the class name can be different than the class identifier
+
- the class name:
+
if the class is in the same file as the $plugin
+
the plugin .inc file can have a different name than the class identifier
+
+
defaults
+
Defaults to: array()
+
An array of defaults that should be added to each plugin; this can be used to ensure that every plugin has the basic data necessary. These defaults will not ovewrite data supplied by the plugin. This could also be a function name, in which case the callback will be used to provide defaults. NOTE, however, that the callback-based approach is deprecated as it is redundant with the 'process' callback, and as such will be removed in later versions. Consequently, you should only use the array form for maximum cross-version compatibility.
+
load themes
+
Defaults to: FALSE
+
If set to TRUE, then plugins of this type can be supplied by themes as well as modules. If this is the case, all themes that are currently enabled will provide a plugin: NOTE: Due to a slight UI bug in Drupal, it is possible for the default theme to be active but not enabled. If this is the case, that theme will NOT provide plugins, so if you are using this feature, be sure to document that issue. Also, themes set via $custom_theme do not necessarily need to be enabled, but the system has no way of knowing what those themes are, so the enabled flag is the only true method of identifying which themes can provide layouts.
+
hook
+
Defaults to: (dynamic value)
+
The name of the hook used to collect data for this plugin. Normally this is $module . '_' . $type -- but this can be changed here. If you change this, you MUST be sure to document this for your plugin implementors as it will change the format of the specially named hook. +
process
+
Defaults to: ''
+
An optional function callback to use for processing a plugin. This can be used to provide automated settings that must be calculated per-plugin instance (i.e., it is not enough to simply append an array via 'defaults'). The parameters on this callback are: callback(&$plugin, $info) where $plugin is a reference to the plugin as processed and $info is the fully processed result of hook_ctools_plugin_api_info(). +
extension
+
Defaults to: 'inc'
+
Can be used to change the extension on files containing plugins of this type. By default the extension will be "inc", though it will default to "info" if "info files" is set to true. Do not include the dot in the extension if changing it, that will be added automatically.
+
info file
+
Defaults to: FALSE
+
If set to TRUE, then the plugin will look for a .info file instead of a .inc. Internally, this will look exactly the same, though obviously a .info file cannot contain functions. This can be good for styles that may not need to contain code.
+
use hooks
+
Defaults to: TRUE*
+
Use to enable support for plugin definition hooks instead of plugin definition files. NOTE: using a central plugin definition hook is less optimal for the plugins system, and as such this will default to FALSE in later versions.
+
child plugins
+
Defaults to: FALSE
+
If set to TRUE, the plugin type can automatically have 'child plugins' meaning each plugin can actually provide multiple plugins. This is mostly used for plugins that store some of their information in the database, such as views, blocks or exportable custom versions of plugins.
+
To implement, each plugin can have a 'get child' and 'get children' callback. Both of these should be implemented for performance reasons, since it is best to avoid getting all children if necessary, but if 'get child' is not implemented, it will fall back to 'get children' if it has to.
+
Child plugins should be named parent:child, with the : being the separator, so that it knows which parent plugin to ask for the child. The 'get children' method should at least return the parent plugin as part of the list, unless it wants the parent plugin itself to not be a choosable option, which is not unheard of.
+
'get children' arguments are ($plugin, $parent) and 'get child' arguments are ($plugin, $parent, $child). +
+ +In addition, there is a 'module' and 'type' settings; these are for internal use of the plugin system and you should not change these. +

Getting the data

+To create a plugin, a module only has to execute ctools_get_plugins with the right data: + +
+  ctools_include('plugins');
+  ctools_get_plugins($module, $type, [$id])
+
+ +In the above example, $module should be your module's name and $type is the type of the plugin. It is typically best practice to provide some kind of wrapper function to make this easier. For example, Panels provides the following functions to implement the 'content_types' plugin: + +
+/**
+ * Fetch metadata on a specific content_type plugin.
+ *
+ * @param $content type
+ *   Name of a panel content type.
+ *
+ * @return
+ *   An array with information about the requested panel content type.
+ */
+function panels_get_content_type($content_type) {
+  ctools_include('context');
+  ctools_include('plugins');
+  return ctools_get_plugins('panels', 'content_types', $content_type);
+}
+
+/**
+ * Fetch metadata for all content_type plugins.
+ *
+ * @return
+ *   An array of arrays with information about all available panel content types.
+ */
+function panels_get_content_types() {
+  ctools_include('context');
+  ctools_include('plugins');
+  return ctools_get_plugins('panels', 'content_types');
+}
+
+ +

Using the data

+ +Each plugin returns a packet of data, which is added to with a few defaults. Each plugin is guaranteed to always have the following data: +
+
name
+
The name of the plugin. This is also the key in the array, of the full list of plugins, and is placed here since that is not always available.
+
module
+
The module that supplied the plugin.
+
file
+
The actual file containing the plugin.
+
path
+
The path to the file containing the plugin. This is useful for using secondary files, such as templates, css files, images, etc, that may come with a plugin.
+
+ +

Any of the above items can be overridden by the plugin itself, though the most likely one to be modified is the 'path'.

+ +

The most likely data (beyond simple printable data) for a plugin to provide is a callback. The plugin system provides a pair of functions to make it easy and consistent for these callbacks to be used. The first is ctools_plugin_get_function, which requires the full $plugin object.

+ +
+/**
+ * Get a function from a plugin, if it exists. If the plugin is not already
+ * loaded, try ctools_plugin_load_function() instead.
+ *
+ * @param $plugin
+ *   The loaded plugin type.
+ * @param $callback_name
+ *   The identifier of the function. For example, 'settings form'.
+ *
+ * @return
+ *   The actual name of the function to call, or NULL if the function
+ *   does not exist.
+ */
+function ctools_plugin_get_function($plugin, $callback_name)
+
+ +

The second does not require the full $plugin object, and will load it:

+ +
+/**
+ * Load a plugin and get a function name from it, returning success only
+ * if the function exists.
+ *
+ * @param $module
+ *   The module that owns the plugin type.
+ * @param $type
+ *   The type of plugin.
+ * @param $id
+ *   The id of the specific plugin to load.
+ * @param $callback_name
+ *   The identifier of the function. For example, 'settings form'.
+ *
+ * @return
+ *   The actual name of the function to call, or NULL if the function
+ *   does not exist.
+ */
+function ctools_plugin_load_function($module, $type, $id, $callback_name) {
+
+ +

Both of these functions will ensure any needed files are included. In fact, it allows each callback to specify alternative include files. The plugin implementation could include code like this:

+ +
+  'callback_name' => 'actual_name_of_function_here',
+
+ +

Or like this:

+
+  'callback_name' => array(
+    'file' => 'filename',
+    'path' => 'filepath', // optional, will use plugin path if absent
+    'function' => 'actual_name_of_function_here',
+  ),
+
+ +

An example, for 'plugin_example' type

+ +
+$plugin = array(
+  'name' => 'my_plugin',
+  'module' => 'my_module',
+  'example_callback' => array(
+    'file' => 'my_plugin.extrafile.inc',
+    'function' => 'my_module_my_plugin_example_callback',
+  ),
+);
+
+ +

To utilize this callback on this plugin:

+ +
+if ($function = ctools_plugin_get_function($plugin, 'example_callback')) {
+  $function($arg1, $arg2, $etc);
+}
+
+ +

Document your plugins!

+ +

Since the data provided by your plugin tends to be specific to your plugin type, you really need to document what the data returned in the hook in the .inc file will be or nobody will figure it out. Use advanced help and document it there. If every system that utilizes plugins does this, then plugin implementors will quickly learn to expect the documentation to be in the advanced help.

diff --git a/sites/all/modules/contrib/ctools/help/plugins-implementing.html b/sites/all/modules/contrib/ctools/help/plugins-implementing.html new file mode 100644 index 0000000..c95e72d --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/plugins-implementing.html @@ -0,0 +1,62 @@ +

There are two parts to implementing a plugin: telling the system where it is, and implementing one or more .inc files that contain the plugin data.

+ +

Telling the system where your plugins live

+

How a module implements plugins

+

To implement any plugins at all, you must implement a single function for all plugins: hook_ctools_plugin_directory. Every time a module loads plugins, this hook will be called to see which modules implement those plugins and in what directory those plugins will live.

+ +
+function hook_ctools_plugin_directory($module, $plugin) {
+  if ($module == 'panels' && $plugin == 'content_types') {
+    return 'plugins/content_types';
+  }
+}
+
+ +

The directory returned should be relative to your module. Another common usage is to simply return that you implement all plugins owned by a given module (or modules):

+ +
+function hook_ctools_plugin_directory($module, $plugin) {
+  if ($module == 'panels') {
+    return 'plugins/' . $plugin;
+  }
+}
+
+ +

Typically, it is recommended that all plugins be placed into the 'plugins' directory for clarity and maintainability. Inside the directory, any number of subdirectories can be used. For plugins that require extra files, such as templates, css, javascript or image files, this is highly recommended:

+
+mymodule.module
+mymodule.info
+plugins/
+    content_types/
+        my_content_type.inc
+    layouts/
+        my_layout.inc
+        my_layout.css
+        my_layout.tpl.php
+        my_layout_image.png
+
+ +

How a theme implements plugins

+

Themes can implement plugins if the plugin owner specified that it's possible in its hook_ctools_plugin_type() call. If so, it is generally exactly the same as modules, except for one important difference: themes don't get hook_ctools_plugin_directory(). Instead, themes add a line to their .info file:

+ +
+plugins[module][type] = directory
+
+ +

How to structure the .inc file

+ +

The top of the .inc file should contain an array that defines the plugin. This array is simply defined in the global namespace of the file and does not need a function. Note that previous versions of this plugin system required a specially named function. While this function will still work, its use is now discouraged, as it is annoying to name properly.

+ +

This array should look something like this:

+ +
+$plugin = array(
+  'key' => 'value',
+);
+
+ +

Several values will be filled in for you automatically, but you can override them if necessary. They include 'name', 'path', 'file' and 'module'. Additionally, the plugin owner can provide other defaults as well.

+ +

There are no required keys by the plugin system itself. The only requirements in the $plugin array will be defined by the plugin type.

+ +

After this array, if your plugin needs functions, they can be declared. Different plugin types have different needs here, so exactly what else will be needed will change from type to type.

diff --git a/sites/all/modules/contrib/ctools/help/plugins.html b/sites/all/modules/contrib/ctools/help/plugins.html new file mode 100644 index 0000000..1e64da0 --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/plugins.html @@ -0,0 +1,5 @@ +

The plugins tool allows a module to allow other modules (and themes!) to provide plugins which provide some kind of functionality or some kind of task. For example, in Panels there are several types of plugins: Content types (which are like blocks), layouts (which are page layouts) and styles (which can be used to style a panel). Each plugin is represented by a .inc file, and the functionality they offer can differ wildly.

+ +

A module which uses plugins can implement a hook describing the plugin (which is not necessary, as defaults will be filled in) and then calls a ctools function which loads either all the known plugins (used for listing/choosing) or a specific plugin (used when it's known which plugin is needed). From the perspective of the plugin system, a plugin is a packet of data, usually some printable info and a list of callbacks. It is up to the module implementing plugins to determine what that info means and what the callbacks do.

+ +

A module which implements plugins must first implement the hook_ctools_plugin_directory function, which simply tells the system which plugins are supported and what directory to look in. Each plugin will then be in a separate .inc file in the directory supplied. The .inc file will contain a specially named hook which returns the data necessary to implement the plugin.

diff --git a/sites/all/modules/contrib/ctools/help/wizard.html b/sites/all/modules/contrib/ctools/help/wizard.html new file mode 100644 index 0000000..33fc456 --- /dev/null +++ b/sites/all/modules/contrib/ctools/help/wizard.html @@ -0,0 +1,311 @@ +

Form wizards, or multi-step forms, are a process by which the user goes through or can use an arbitrary number of different forms to create a single object or perform a single task. Traditionally the multi-step form is difficult in Drupal because there is no easy place to put data in between forms. No longer! The form wizard tool allows a single entry point to easily set up a wizard of multiple forms, provide callbacks to handle data storage and updates between forms and when forms are completed. This tool pairs well with the object cache tool for storage.

+ +

The form info array

+

The wizard starts with an array of data that describes all of the forms available to the wizard and sets options for how the wizard will present and control the flow. Here is an example of the $form_info array as used in the delegator module:

+ +
+  $form_info = array(
+    'id' => 'delegator_page',
+    'path' => "admin/structure/pages/edit/$page_name/%step",
+    'show trail' => TRUE,
+    'show back' => TRUE,
+    'show return' => FALSE,
+    'next callback' => 'delegator_page_add_subtask_next',
+    'finish callback' => 'delegator_page_add_subtask_finish',
+    'return callback' => 'delegator_page_add_subtask_finish',
+    'cancel callback' => 'delegator_page_add_subtask_cancel',
+    'order' => array(
+      'basic' => t('Basic settings'),
+      'argument' => t('Argument settings'),
+      'access' => t('Access control'),
+      'menu' => t('Menu settings'),
+      'multiple' => t('Task handlers'),
+    ),
+    'forms' => array(
+      'basic' => array(
+        'form id' => 'delegator_page_form_basic'
+      ),
+      'access' => array(
+        'form id' => 'delegator_page_form_access'
+      ),
+      'menu' => array(
+        'form id' => 'delegator_page_form_menu'
+      ),
+      'argument' => array(
+        'form id' => 'delegator_page_form_argument'
+      ),
+      'multiple' => array(
+        'form id' => 'delegator_page_argument_form_multiple'
+      ),
+    ),
+  );
+
+ +

The above array starts with an id which is used to identify the wizard in various places and a path which is needed to redirect to the next step between forms. It then creates some settings which control which pieces are displayed. In this case, it displays a form trail and a 'back' button, but not the 'return' button. Then there are the wizard callbacks which allow the wizard to act appropriately when forms are submitted. Finally it contains a list of forms and their order so that it knows which forms to use and what order to use them by default. Note that the keys in the order and list of forms match; that key is called the step and is used to identify each individual step of the wizard.

+ +

Here is a full list of every item that can be in the form info array:

+ +
+
id
+
An id for wizard. This is used like a hook to automatically name callbacks, as well as a form step's form building function. It is also used in trail theming.
+ +
path
+
The path to use when redirecting between forms. %step will be replaced with the key for the form.
+ +
return path
+
When a form is complete, this is the path to go to. This is required if the 'return' button is shown and not using AJAX. It is also used for the 'Finish' button. If it is not present and needed, the cancel path will also be checked.
+ +
cancel path
+
When a form is canceled, this is the path to go to. This is required if the 'cancel' is shown and not using AJAX.
+ +
show trail
+
If set to TRUE, the form trail will be shown like a breadcrumb at the top of each form. Defaults to FALSE.
+ +
show back
+
If set to TRUE, show a back button on each form. Defaults to FALSE.
+ +
show return
+
If set to TRUE, show a return button. Defaults to FALSE.
+ +
show cancel
+
If set to TRUE, show a cancel button. Defaults to FALSE.
+ +
back text
+
Set the text of the 'back' button. Defaults to t('Back').
+ +
next text
+
Set the text of the 'next' button. Defaults to t('Continue').
+ +
return text
+
Set the text of the 'return' button. Defaults to t('Update and return').
+ +
finish text
+
Set the text of the 'finish' button. Defaults to t('Finish').
+ +
cancel text
+
Set the text of the 'cancel' button. Defaults to t('Cancel').
+ +
ajax
+
Turn on AJAX capabilities, using CTools' ajax.inc. Defaults to FALSE.
+ +
modal
+
Put the wizard in the modal tool. The modal must already be open and called from an ajax button for this to work, which is easily accomplished using functions provided by the modal tool.
+ +
ajax render
+
A callback to display the rendered form via ajax. This is not required if using the modal tool, but is required otherwise since ajax by itself does not know how to render the results. Params: &$form_state, $output.
+ +
finish callback
+
The function to call when a form is complete and the finish button has been clicked. This function should finalize all data. Params: &$form_state. +Defaults to $form_info['id']._finish if function exists. +
+ +
cancel callback
+
The function to call when a form is canceled by the user. This function should clean up any data that is cached. Params: &$form_state. +Defaults to $form_info['id']._cancel if function exists.
+ +
return callback
+
The function to call when a form is complete and the return button has been clicked. This is often the same as the finish callback. Params: &$form_state. +Defaults to $form_info['id']._return if function exists.
+ +
next callback
+
The function to call when the next button has been clicked. This function should take the submitted data and cache it for later use by the finish callback. Params: &$form_state. +Defaults to $form_info['id']._next if function exists.
+ +
order
+
An optional array of forms, keyed by the step, which represents the default order the forms will be displayed in. If not set, the forms array will control the order. Note that submit callbacks can override the order so that branching logic can be used.
+ +
forms
+
An array of form info arrays, keyed by step, describing every form available to the wizard. If order array isn't set, the wizard will use this to set the default order. Each array contains: +
+
form id
+
+ The id of the form, as used in the Drupal form system. This is also the name of the function that represents the form builder. + Defaults to $form_info['id']._.$step._form. +
+ +
include
+
The name of a file to include which contains the code for this form. This makes it easy to include the form wizard in another file or set of files. This must be the full path of the file, so be sure to use drupal_get_path() when setting this. This can also be an array of files if multiple files need to be included.
+ +
title
+
The title of the form, to be optionally set via drupal_get_title. This is required when using the modal if $form_state['title'] is not set.
+
+
+
+ +

Invoking the form wizard

+

Your module should create a page callback via hook_menu, and this callback should contain an argument for the step. The path that leads to this page callback should be the same as the 'path' set in the $form_info array.

+ +

The page callback should set up the $form_info, and figure out what the default step should be if no step is provided (note that the wizard does not do this for you; you MUST specify a step). Then invoke the form wizard:

+ +
+  $form_state = array();
+  ctools_include('wizard');
+  $output = ctools_wizard_multistep_form($form_info, $step, $form_state);
+
+ +

If using AJAX or the modal, This part is actually done! If not, you have one more small step:

+ +
+  return $output;
+
+ +

Forms and their callbacks

+

Each form within the wizard is a complete, independent form using Drupal's Form API system. It has a form builder callback as well as submit and validate callbacks and can be form altered. The primary difference between these forms and a normal Drupal form is that the submit handler should not save any data. Instead, it should make any changes to a cached object (usually placed on the $form_state) and only the _finish or _return handler should actually save any real data.

+ +

How you handle this is completely up to you. The recommended best practice is to use the CTools Object cache, and a good way to do this is to write a couple of wrapper functions around the cache that look like these example functions:

+ +
+/**
+ * Get the cached changes to a given task handler.
+ */
+function delegator_page_get_page_cache($name) {
+  ctools_include('object-cache');
+  $cache = ctools_object_cache_get('delegator_page', $name);
+  if (!$cache) {
+    $cache = delegator_page_load($name);
+    $cache->locked = ctools_object_cache_test('delegator_page', $name);
+  }
+
+  return $cache;
+}
+
+/**
+ * Store changes to a task handler in the object cache.
+ */
+function delegator_page_set_page_cache($name, $page) {
+  ctools_include('object-cache');
+  $cache = ctools_object_cache_set('delegator_page', $name, $page);
+}
+
+/**
+ * Remove an item from the object cache.
+ */
+function delegator_page_clear_page_cache($name) {
+  ctools_include('object-cache');
+  ctools_object_cache_clear('delegator_page', $name);
+}
+
+ +

Using these wrappers, when performing a get_cache operation, it defaults to loading the real object. It then checks to see if another user has this object cached using the ctools_object_cache_test() function, which automatically sets a lock (which can be used to prevent writes later on).

+ +

With this set up, the _next, _finish and _cancel callbacks are quite simple:

+ +
+/**
+ * Callback generated when the add page process is finished.
+ */
+function delegator_page_add_subtask_finish(&$form_state) {
+  $page = &$form_state['page'];
+
+  // Create a real object from the cache
+  delegator_page_save($page);
+
+  // Clear the cache
+  delegator_page_clear_page_cache($form_state['cache name']);
+}
+
+/**
+ * Callback generated when the 'next' button is clicked.
+ *
+ * All we do here is store the cache.
+ */
+function delegator_page_add_subtask_next(&$form_state) {
+  // Update the cache with changes.
+  delegator_page_set_page_cache($form_state['cache name'], $form_state['page']);
+}
+
+/**
+ * Callback generated when the 'cancel' button is clicked.
+ *
+ * All we do here is clear the cache.
+ */
+function delegator_page_add_subtask_cancel(&$form_state) {
+  // Update the cache with changes.
+  delegator_page_clear_page_cache($form_state['cache name']);
+}
+
+ +

All that's needed to tie this together is to understand how the changes made it into the cache in the first place. This happened in the various form _submit handlers, which made changes to $form_state['page'] based upon the values set in the form:

+ +
+/**
+ * Store the values from the basic settings form.
+ */
+function delegator_page_form_basic_submit($form, &$form_state) {
+  if (!isset($form_state['page']->pid) && empty($form_state['page']->import)) {
+    $form_state['page']->name = $form_state['values']['name'];
+  }
+
+  $form_state['page']->admin_title = $form_state['values']['admin_title'];
+  $form_state['page']->path = $form_state['values']['path'];
+
+  return $form;
+}
+
+ +

No database operations were made during this _submit, and that's a very important distinction about this system.

+ +

Proper handling of back button

+

When using 'show back' => TRUE the cached data should be assigned to the #default_value form property. Otherwise when the user goes back to the previous step the forms default values instead of his (cached) input is used.

+ +
+/**
+ * Form builder function for wizard.
+ */
+function wizardid_step2_form($form, &$form_state) {
+  $form_state['my data'] = my_module_get_cache($form_state['cache name']);
+  $form['example'] = array(
+    '#type' => 'radios',
+    '#title' => t('Title'),
+    '#default_value' => $form_state['my data']->example ? $form_state['my data']->example : default,
+    '#options' => array(
+      'default' => t('Default'),
+      'setting1' => t('Setting1'),
+    ),
+  );
+
+  return $form;
+}
+
+/**
+ * Submit handler to prepare needed values for storing in cache.
+ */
+function wizardid_step2_form_submit($form, &$form_state) {
+  $form_state['my data']->example = $form_state['values']['example'];
+}
+
+ +

The data is stored in the my data object on submitting. If the user goes back to this step the cached my data is used as the default form value. The function my_module_get_cache() is like the cache functions explained above.

+ +

Required fields, cancel and back buttons

+

If you have required fields in your forms, the back and cancel buttons will not work as expected since validation of the form will fail. You can add the following code to the top of your form validation to avoid this problem:

+
+/**
+ * Validation handler for step2 form
+ */
+function wizardid_step2_form_validate(&$form, &$form_state) {
+  // if the clicked button is anything but the normal flow
+  if ($form_state['clicked_button']['#next'] != $form_state['next']) {
+    drupal_get_messages('error');
+    form_set_error(NULL, '', TRUE);
+    return;
+  }
+  // you form validation goes here
+  // ...
+}
+
+ +

Wizard for anonymous users

+

If you are creating a wizard which is be used by anonymous users, you might run into some issues with drupal's caching for anonymous users. You can circumvent this by using hook_init and telling drupal to not cache your wizard pages:

+
+/**
+ * Implementation of hook init
+ */
+function mymodule_init() {
+  // if the path leads to the wizard
+  if (drupal_match_path($_GET['q'], 'path/to/your/wizard/*')) {
+    // set cache to false
+    $GLOBALS['conf']['cache'] = FALSE;   
+  }
+}
+
diff --git a/sites/all/modules/contrib/ctools/images/arrow-active.png b/sites/all/modules/contrib/ctools/images/arrow-active.png new file mode 100644 index 0000000..3bbd3c2 Binary files /dev/null and b/sites/all/modules/contrib/ctools/images/arrow-active.png differ diff --git a/sites/all/modules/contrib/ctools/images/collapsible-collapsed.png b/sites/all/modules/contrib/ctools/images/collapsible-collapsed.png new file mode 100644 index 0000000..95a214a Binary files /dev/null and b/sites/all/modules/contrib/ctools/images/collapsible-collapsed.png differ diff --git a/sites/all/modules/contrib/ctools/images/collapsible-expanded.png b/sites/all/modules/contrib/ctools/images/collapsible-expanded.png new file mode 100644 index 0000000..46f39ec Binary files /dev/null and b/sites/all/modules/contrib/ctools/images/collapsible-expanded.png differ diff --git a/sites/all/modules/contrib/ctools/images/expanded-options.png b/sites/all/modules/contrib/ctools/images/expanded-options.png new file mode 100644 index 0000000..b7b755c Binary files /dev/null and b/sites/all/modules/contrib/ctools/images/expanded-options.png differ diff --git a/sites/all/modules/contrib/ctools/images/icon-close-window.png b/sites/all/modules/contrib/ctools/images/icon-close-window.png new file mode 100644 index 0000000..5f0cf69 Binary files /dev/null and b/sites/all/modules/contrib/ctools/images/icon-close-window.png differ diff --git a/sites/all/modules/contrib/ctools/images/icon-configure.png b/sites/all/modules/contrib/ctools/images/icon-configure.png new file mode 100644 index 0000000..e23d67c Binary files /dev/null and b/sites/all/modules/contrib/ctools/images/icon-configure.png differ diff --git a/sites/all/modules/contrib/ctools/images/icon-delete.png b/sites/all/modules/contrib/ctools/images/icon-delete.png new file mode 100644 index 0000000..5f0cf69 Binary files /dev/null and b/sites/all/modules/contrib/ctools/images/icon-delete.png differ diff --git a/sites/all/modules/contrib/ctools/images/no-icon.png b/sites/all/modules/contrib/ctools/images/no-icon.png new file mode 100644 index 0000000..fa78ec1 Binary files /dev/null and b/sites/all/modules/contrib/ctools/images/no-icon.png differ diff --git a/sites/all/modules/contrib/ctools/images/status-active.gif b/sites/all/modules/contrib/ctools/images/status-active.gif new file mode 100644 index 0000000..207e95c Binary files /dev/null and b/sites/all/modules/contrib/ctools/images/status-active.gif differ diff --git a/sites/all/modules/contrib/ctools/images/throbber.gif b/sites/all/modules/contrib/ctools/images/throbber.gif new file mode 100644 index 0000000..8a084b8 Binary files /dev/null and b/sites/all/modules/contrib/ctools/images/throbber.gif differ diff --git a/sites/all/modules/contrib/ctools/includes/action-links.theme.inc b/sites/all/modules/contrib/ctools/includes/action-links.theme.inc new file mode 100644 index 0000000..3a2398a --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/action-links.theme.inc @@ -0,0 +1,33 @@ + 'links', + 'file' => 'includes/action-links.theme.inc', + ); +} + +/** + * Render a menu local actions wrapper. + * + * @param $links + * Local actions links. + * @param $attributes + * An array of attributes to append to the wrapper. + */ +function theme_ctools_menu_local_actions_wrapper($variables) { + $links = drupal_render($variables['links']); + + if (empty($links)) { + return; + } + + return ''; +} \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/includes/ajax.inc b/sites/all/modules/contrib/ctools/includes/ajax.inc new file mode 100644 index 0000000..96f5068 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/ajax.inc @@ -0,0 +1,157 @@ + $image)), $dest, $alt, $class); +} + +/** + * Render text as a link. This will automatically apply an AJAX class + * to the link and add the appropriate javascript to make this happen. + * + * Note: 'html' => true so be sure any text is vetted! Chances are these kinds of buttons will + * not use user input so this is a very minor concern. + * + * @param $text + * The text that will be displayed as the link. + * @param $dest + * The destination of the link. + * @param $alt + * The alt text of the link. + * @param $class + * Any class to apply to the link. @todo this should be a options array. + * @param $type + * A type to use, in case a different behavior should be attached. Defaults + * to ctools-use-ajax. + */ +function ctools_ajax_text_button($text, $dest, $alt, $class = '', $type = 'use-ajax') { + drupal_add_library('system', 'drupal.ajax'); + return l($text, $dest, array('html' => TRUE, 'attributes' => array('class' => array($type, $class), 'title' => $alt))); +} + +/** + * Render an icon and related text as a link. This will automatically apply an AJAX class + * to the link and add the appropriate javascript to make this happen. + * + * Note: 'html' => true so be sure any text is vetted! Chances are these kinds of buttons will + * not use user input so this is a very minor concern. + * + * @param $text + * The text that will be displayed as the link. + * @param $image + * The icon image to include in the link. + * @param $dest + * The destination of the link. + * @param $alt + * The title text of the link. + * @param $class + * Any class to apply to the link. @todo this should be a options array. + * @param $type + * A type to use, in case a different behavior should be attached. Defaults + * to ctools-use-ajax. + */ +function ctools_ajax_icon_text_button($text, $image, $dest, $alt, $class = '', $type = 'use-ajax') { + drupal_add_library('system', 'drupal.ajax'); + $rendered_image = theme('image', array('path' => $image)); + $link_content = $rendered_image . "" . $text . ""; + return l($link_content, $dest, array('html' => TRUE, 'attributes' => array('class' => array($type, $class), 'title' => $alt))); +} + +/** + * Set a single property to a value, on all matched elements. + * + * @param $selector + * The CSS selector. This can be any selector jquery uses in $(). + * @param $name + * The name or key: of the data attached to this selector. + * @param $value + * The value of the data. + */ +function ctools_ajax_command_attr($selector, $name, $value) { + ctools_add_js('ajax-responder'); + return array( + 'command' => 'attr', + 'selector' => $selector, + 'name' => $name, + 'value' => $value, + ); + } + +/** + * Force a client-side redirect. + * + * @param $url + * The url to be redirected to. This can be an absolute URL or a + * Drupal path. + * @param $delay + * A delay before applying the redirection, in milliseconds. + * @param $options + * An array of options to pass to the url() function. + */ +function ctools_ajax_command_redirect($url, $delay = 0, $options = array()) { + ctools_add_js('ajax-responder'); + return array( + 'command' => 'redirect', + 'url' => url($url, $options), + 'delay' => $delay, + ); +} + +/** + * Force a reload of the current page. + */ +function ctools_ajax_command_reload() { + ctools_add_js('ajax-responder'); + return array( + 'command' => 'reload', + ); +} + +/** + * Submit a form. + * + * This is useful for submitting a parent form after a child form has finished + * processing in a modal overlay. + * + * @param $selector + * The CSS selector to identify the form for submission. This can be any + * selector jquery uses in $(). + */ +function ctools_ajax_command_submit($selector) { + ctools_add_js('ajax-responder'); + return array( + 'command' => 'submit', + 'selector' => $selector, + ); +} + +/** + * Send an error response back via AJAX and immediately exit. + */ +function ctools_ajax_render_error($error = '') { + $commands = array(); + $commands[] = ajax_command_alert($error); + print ajax_render($commands); + exit; +} + diff --git a/sites/all/modules/contrib/ctools/includes/cache.inc b/sites/all/modules/contrib/ctools/includes/cache.inc new file mode 100644 index 0000000..3918683 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/cache.inc @@ -0,0 +1,169 @@ + TRUE, + 'ignore words' => array(), + 'separator' => '-', + 'replacements' => array(), + 'transliterate' => FALSE, + 'reduce ascii' => TRUE, + 'max length' => FALSE, + 'lower case' => FALSE, + ); + + // Allow modules to make other changes to the settings. + if (isset($settings['clean id'])) { + drupal_alter('ctools_cleanstring_' . $settings['clean id'], $settings); + } + + drupal_alter('ctools_cleanstring', $settings); + + $output = $string; + + // Do any replacements the user selected up front. + if (!empty($settings['replacements'])) { + $output = strtr($output, $settings['replacements']); + } + + // Remove slashes if instructed to do so. + if ($settings['clean slash']) { + $output = str_replace('/', '', $output); + } + + if (!empty($settings['transliterate']) && module_exists('transliteration')) { + $output = transliteration_get($output); + } + + // Reduce to the subset of ASCII96 letters and numbers + if ($settings['reduce ascii']) { + $pattern = '/[^a-zA-Z0-9\/]+/'; + $output = preg_replace($pattern, $settings['separator'], $output); + } + + // Get rid of words that are on the ignore list + if (!empty($settings['ignore words'])) { + $ignore_re = '\b' . preg_replace('/,/', '\b|\b', $settings['ignore words']) . '\b'; + + if (function_exists('mb_eregi_replace')) { + $output = mb_eregi_replace($ignore_re, '', $output); + } + else { + $output = preg_replace("/$ignore_re/i", '', $output); + } + } + + // Always replace whitespace with the separator. + $output = preg_replace('/\s+/', $settings['separator'], $output); + + // In preparation for pattern matching, + // escape the separator if and only if it is not alphanumeric. + if (isset($settings['separator'])) { + if (preg_match('/^[^' . CTOOLS_PREG_CLASS_ALNUM . ']+$/uD', $settings['separator'])) { + $seppattern = $settings['separator']; + } + else { + $seppattern = '\\' . $settings['separator']; + } + // Trim any leading or trailing separators (note the need to + $output = preg_replace("/^$seppattern+|$seppattern+$/", '', $output); + + // Replace multiple separators with a single one + $output = preg_replace("/$seppattern+/", $settings['separator'], $output); + } + + // Enforce the maximum component length + if (!empty($settings['max length'])) { + $output = ctools_cleanstring_truncate($output, $settings['max length'], $settings['separator']); + } + + if (!empty($settings['lower case'])) { + $output = drupal_strtolower($output); + } + return $output; +} + +/** + * A friendly version of truncate_utf8. + * + * @param $string + * The string to be truncated. + * @param $length + * An integer for the maximum desired length. + * @param $separator + * A string which contains the word boundary such as - or _. + * + * @return + * The string truncated below the maxlength. + */ +function ctools_cleanstring_truncate($string, $length, $separator) { + if (drupal_strlen($string) > $length) { + $string = drupal_substr($string, 0, $length + 1); // leave one more character + if ($last_break = strrpos($string, $separator)) { // space exists AND is not on position 0 + $string = substr($string, 0, $last_break); + } + else { + $string = drupal_substr($string, 0, $length); + } + } + return $string; +} diff --git a/sites/all/modules/contrib/ctools/includes/collapsible.theme.inc b/sites/all/modules/contrib/ctools/includes/collapsible.theme.inc new file mode 100644 index 0000000..f7bbbb3 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/collapsible.theme.inc @@ -0,0 +1,79 @@ + array('handle' => NULL, 'content' => NULL, 'collapsed' => FALSE), + 'file' => 'includes/collapsible.theme.inc', + ); + $items['ctools_collapsible_remembered'] = array( + 'variables' => array('id' => NULL, 'handle' => NULL, 'content' => NULL, 'collapsed' => FALSE), + 'file' => 'includes/collapsible.theme.inc', + ); +} + +/** + * Render a collapsible div. + * + * @param $handle + * Text to put in the handle/title area of the div. + * @param $content + * Text to put in the content area of the div, this is what will get + * collapsed + * @param $collapsed = FALSE + * If true, this div will start out collapsed. + */ +function theme_ctools_collapsible($vars) { + ctools_add_js('collapsible-div'); + ctools_add_css('collapsible-div'); + + $class = $vars['collapsed'] ? ' ctools-collapsed' : ''; + $output = '
'; + $output .= '
' . $vars['handle'] . '
'; + $output .= '
' . $vars['content'] . '
'; + $output .= '
'; + + return $output; +} + +/** + * Render a collapsible div whose state will be remembered. + * + * @param $id + * The CSS id of the container. This is required. + * @param $handle + * Text to put in the handle/title area of the div. + * @param $content + * Text to put in the content area of the div, this is what will get + * collapsed + * @param $collapsed = FALSE + * If true, this div will start out collapsed. + */ +function theme_ctools_collapsible_remembered($vars) { + $id = $vars['id']; + $handle = $vars['handle']; + $content = $vars['content']; + $collapsed = $vars['collapsed']; + ctools_add_js('collapsible-div'); + ctools_add_css('collapsible-div'); + + $class = $collapsed ? ' ctools-collapsed' : ''; + $output = '
'; + $output .= '
' . $handle . '
'; + $output .= '
' . $content . '
'; + $output .= '
'; + + return $output; +} + diff --git a/sites/all/modules/contrib/ctools/includes/content.inc b/sites/all/modules/contrib/ctools/includes/content.inc new file mode 100644 index 0000000..ae1c607 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/content.inc @@ -0,0 +1,853 @@ + $plugin['title'], + 'description' => $plugin['description'], + 'icon' => ctools_content_admin_icon($plugin), + 'category' => $plugin['category'], + ); + + if (isset($plugin['required context'])) { + $type['required context'] = $plugin['required context']; + } + if (isset($plugin['top level'])) { + $type['top level'] = $plugin['top level']; + } + $plugin['content types'] = array($plugin['name'] => $type); + if (!isset($plugin['single'])) { + $plugin['single'] = TRUE; + } + } + } +} + +/** + * Fetch metadata on a specific content_type plugin. + * + * @param $content type + * Name of a panel content type. + * + * @return + * An array with information about the requested panel content type. + */ +function ctools_get_content_type($content_type) { + ctools_include('context'); + ctools_include('plugins'); + return ctools_get_plugins('ctools', 'content_types', $content_type); +} + +/** + * Fetch metadata for all content_type plugins. + * + * @return + * An array of arrays with information about all available panel content types. + */ +function ctools_get_content_types() { + ctools_include('context'); + ctools_include('plugins'); + return ctools_get_plugins('ctools', 'content_types'); +} + +/** + * Get all of the individual subtypes provided by a given content type. This + * would be all of the blocks for the block type, or all of the views for + * the view type. + * + * @param $type + * The content type to load. + * + * @return + * An array of all subtypes available. + */ +function ctools_content_get_subtypes($type) { + static $cache = array(); + + $subtypes = array(); + + if (is_array($type)) { + $plugin = $type; + } + else { + $plugin = ctools_get_content_type($type); + } + + if (empty($plugin) || empty($plugin['name'])) { + return; + } + + if (isset($cache[$plugin['name']])) { + return $cache[$plugin['name']]; + } + + if (isset($plugin['content types'])) { + $function = $plugin['content types']; + if (is_array($function)) { + $subtypes = $function; + } + else if (function_exists($function)) { + // Cast to array to prevent errors from non-array returns. + $subtypes = (array) $function($plugin); + } + } + + // Walk through the subtypes and ensure minimal settings are + // retained. + foreach ($subtypes as $id => $subtype) { + // Use exact name since this is a modify by reference. + ctools_content_prepare_subtype($subtypes[$id], $plugin); + } + + $cache[$plugin['name']] = $subtypes; + + return $subtypes; +} + +/** + * Given a content type and a subtype id, return the information about that + * content subtype. + * + * @param $type + * The content type being fetched. + * @param $subtype_id + * The id of the subtype being fetched. + * + * @return + * An array of information describing the content subtype. + */ +function ctools_content_get_subtype($type, $subtype_id) { + $subtype = array(); + if (is_array($type)) { + $plugin = $type; + } + else { + $plugin = ctools_get_content_type($type); + } + + $function = ctools_plugin_get_function($plugin, 'content type'); + if ($function) { + $subtype = $function($subtype_id, $plugin); + } + else { + $subtypes = ctools_content_get_subtypes($type); + if (isset($subtypes[$subtype_id])) { + $subtype = $subtypes[$subtype_id]; + } + // If there's only 1 and we somehow have the wrong subtype ID, do not + // care. Return the proper subtype anyway. + if (empty($subtype) && !empty($plugin['single'])) { + $subtype = current($subtypes); + } + } + + if ($subtype) { + ctools_content_prepare_subtype($subtype, $plugin); + } + return $subtype; +} + +/** + * Ensure minimal required settings on a content subtype exist. + */ +function ctools_content_prepare_subtype(&$subtype, $plugin) { + foreach (array('path', 'js', 'css') as $key) { + if (!isset($subtype[$key]) && isset($plugin[$key])) { + $subtype[$key] = $plugin[$key]; + } + } + + drupal_alter('ctools_content_subtype', $subtype, $plugin); +} + +/** + * Get the content from a given content type. + * + * @param $type + * The content type. May be the name or an already loaded content type plugin. + * @param $subtype + * The name of the subtype being rendered. + * @param $conf + * The configuration for the content type. + * @param $keywords + * An array of replacement keywords that come from outside contexts. + * @param $args + * The arguments provided to the owner of the content type. Some content may + * wish to configure itself based on the arguments the panel or dashboard + * received. + * @param $context + * An array of context objects available for use. + * @param $incoming_content + * Any incoming content, if this display is a wrapper. + * + * @return + * The content as rendered by the plugin. This content should be an array + * with the following possible keys: + * - title: The safe to render title of the content. + * - title_heading: The title heading. + * - content: The safe to render HTML content. + * - links: An array of links associated with the content suitable for + * theme('links'). + * - more: An optional 'more' link (destination only) + * - admin_links: Administrative links associated with the content, suitable + * for theme('links'). + * - feeds: An array of feed icons or links associated with the content. + * Each member of the array is rendered HTML. + * - type: The content type. + * - subtype: The content subtype. These two may be used together as + * module-delta for block style rendering. + */ +function ctools_content_render($type, $subtype, $conf, $keywords = array(), $args = array(), $context = array(), $incoming_content = '') { + if (is_array($type)) { + $plugin = $type; + } + else { + $plugin = ctools_get_content_type($type); + } + + $subtype_info = ctools_content_get_subtype($plugin, $subtype); + + $function = ctools_plugin_get_function($subtype_info, 'render callback'); + if (!$function) { + $function = ctools_plugin_get_function($plugin, 'render callback'); + } + + if ($function) { + $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context); + if ($pane_context === FALSE) { + return; + } + + $content = $function($subtype, $conf, $args, $pane_context, $incoming_content); + + if (empty($content)) { + return; + } + + // Set up some defaults and other massaging on the content before we hand + // it back to the caller. + if (!isset($content->type)) { + $content->type = $plugin['name']; + } + + if (!isset($content->subtype)) { + $content->subtype = $subtype; + } + + // Override the title if configured to + if (!empty($conf['override_title'])) { + // Give previous title as an available substitution here. + $keywords['%title'] = empty($content->title) ? '' : $content->title; + $content->original_title = $keywords['%title']; + $content->title = $conf['override_title_text']; + $content->title_heading = isset($conf['override_title_heading']) ? $conf['override_title_heading'] : 'h2'; + } + + if (!empty($content->title)) { + // Perform substitutions + if (!empty($keywords) || !empty($context)) { + $content->title = ctools_context_keyword_substitute($content->title, $keywords, $context); + } + + // Sterilize the title + $content->title = filter_xss_admin($content->title); + + // If a link is specified, populate. + if (!empty($content->title_link)) { + if (!is_array($content->title_link)) { + $url = array('href' => $content->title_link); + } + else { + $url = $content->title_link; + } + // set defaults so we don't bring up notices + $url += array('href' => '', 'attributes' => array(), 'query' => array(), 'fragment' => '', 'absolute' => NULL, 'html' => TRUE); + $content->title = l($content->title, $url['href'], $url); + } + } + + return $content; + } +} + +/** + * Determine if a content type can be edited or not. + * + * Some content types simply have their content and no options. This function + * lets a UI determine if it should display an edit link or not. + */ +function ctools_content_editable($type, $subtype, $conf) { + if (empty($type['edit form']) && empty($subtype['edit form'])) { + return FALSE; + } + + $function = FALSE; + + if (!empty($subtype['check editable'])) { + $function = ctools_plugin_get_function($subtype, 'check editable'); + } + elseif (!empty($type['check editable'])) { + $function = ctools_plugin_get_function($type, 'check editable'); + } + + if ($function) { + return $function($type, $subtype, $conf); + } + + return TRUE; +} + +/** + * Get the administrative title from a given content type. + * + * @param $type + * The content type. May be the name or an already loaded content type object. + * @param $subtype + * The subtype being rendered. + * @param $conf + * The configuration for the content type. + * @param $context + * An array of context objects available for use. These may be placeholders. + */ +function ctools_content_admin_title($type, $subtype, $conf, $context = NULL) { + if (is_array($type)) { + $plugin = $type; + } + else if (is_string($type)) { + $plugin = ctools_get_content_type($type); + } + else { + return; + } + + if ($function = ctools_plugin_get_function($plugin, 'admin title')) { + $pane_context = ctools_content_select_context($plugin, $subtype, $conf, $context); + if ($pane_context === FALSE) { + if ($plugin['name'] == $subtype) { + return t('@type will not display due to missing context', array('@type' => $plugin['name'])); + } + return t('@type:@subtype will not display due to missing context', array('@type' => $plugin['name'], '@subtype' => $subtype)); + } + + return $function($subtype, $conf, $pane_context); + } + else if (isset($plugin['admin title'])) { + return $plugin['admin title']; + } + else if (isset($plugin['title'])) { + return $plugin['title']; + } +} + +/** + * Get the proper icon path to use, falling back to default icons if no icon exists. + * + * $subtype + * The loaded subtype info. + */ +function ctools_content_admin_icon($subtype) { + $icon = ''; + + if (isset($subtype['icon'])) { + $icon = $subtype['icon']; + if (!file_exists($icon)) { + $icon = $subtype['path'] . '/' . $icon; + } + } + + if (empty($icon) || !file_exists($icon)) { + $icon = ctools_image_path('no-icon.png'); + } + + return $icon; +} + +/** + * Set up the default $conf for a new instance of a content type. + */ +function ctools_content_get_defaults($plugin, $subtype) { + if (isset($plugin['defaults'])) { + $defaults = $plugin['defaults']; + } + else if (isset($subtype['defaults'])) { + $defaults = $subtype['defaults']; + } + if (isset($defaults)) { + if (is_string($defaults) && function_exists($defaults)) { + if ($return = $defaults($pane)) { + return $return; + } + } + else if (is_array($defaults)) { + return $defaults; + } + } + + return array(); +} + +/** + * Get the administrative title from a given content type. + * + * @param $type + * The content type. May be the name or an already loaded content type object. + * @param $subtype + * The subtype being rendered. + * @param $conf + * The configuration for the content type. + * @param $context + * An array of context objects available for use. These may be placeholders. + */ +function ctools_content_admin_info($type, $subtype, $conf, $context = NULL) { + if (is_array($type)) { + $plugin = $type; + } + else { + $plugin = ctools_get_content_type($type); + } + + if ($function = ctools_plugin_get_function($plugin, 'admin info')) { + $output = $function($subtype, $conf, $context); + } + + if (empty($output) || !is_object($output)) { + $output = new stdClass(); + // replace the _ with " " for a better output + $subtype = check_plain(str_replace("_", " ", $subtype)); + $output->title = $subtype; + $output->content = t('No info available.'); + } + return $output; +} + +/** + * Add the default FAPI elements to the content type configuration form + */ +function ctools_content_configure_form_defaults($form, &$form_state) { + $plugin = $form_state['plugin']; + $subtype = $form_state['subtype']; + $contexts = isset($form_state['contexts']) ? $form_state['contexts'] : NULL; + $conf = $form_state['conf']; + + $add_submit = FALSE; + if (!empty($subtype['required context']) && is_array($contexts)) { + $form['context'] = ctools_context_selector($contexts, $subtype['required context'], isset($conf['context']) ? $conf['context'] : array()); + $add_submit = TRUE; + } + + ctools_include('dependent'); + + // Unless we're not allowed to override the title on this content type, add this + // gadget to all panes. + if (empty($plugin['no title override']) && empty($subtype['no title override'])) { + $form['aligner_start'] = array( + '#markup' => '
', + ); + $form['override_title'] = array( + '#type' => 'checkbox', + '#default_value' => isset($conf['override_title']) ? $conf['override_title'] : '', + '#title' => t('Override title'), + '#id' => 'override-title-checkbox', + ); + $form['override_title_text'] = array( + '#type' => 'textfield', + '#default_value' => isset($conf['override_title_text']) ? $conf['override_title_text'] : '', + '#size' => 35, + '#id' => 'override-title-textfield', + '#dependency' => array('override-title-checkbox' => array(1)), + '#dependency_type' => 'hidden', + ); + $form['override_title_heading'] = array( + '#type' => 'select', + '#default_value' => isset($conf['override_title_heading']) ? $conf['override_title_heading'] : 'h2', + '#options' => array( + 'h1' => t('h1'), + 'h2' => t('h2'), + 'h3' => t('h3'), + 'h4' => t('h4'), + 'h5' => t('h5'), + 'h6' => t('h6'), + 'div' => t('div'), + 'span' => t('span'), + ), + '#id' => 'override-title-heading', + '#dependency' => array('override-title-checkbox' => array(1)), + '#dependency_type' => 'hidden', + ); + + $form['aligner_stop'] = array( + '#markup' => '
', + ); + if (is_array($contexts)) { + $form['override_title_markup'] = array( + '#prefix' => '
', + '#suffix' => '
', + '#markup' => t('You may use %keywords from contexts, as well as %title to contain the original title.'), + ); + } + $add_submit = TRUE; + } + + if ($add_submit) { + // '#submit' is already set up due to the wizard. + $form['#submit'][] = 'ctools_content_configure_form_defaults_submit'; + } + return $form; +} + +/** + * Submit handler to store context/title override info. + */ +function ctools_content_configure_form_defaults_submit(&$form, &$form_state) { + if (isset($form_state['values']['context'])) { + $form_state['conf']['context'] = $form_state['values']['context']; + } + if (isset($form_state['values']['override_title'])) { + $form_state['conf']['override_title'] = $form_state['values']['override_title']; + $form_state['conf']['override_title_text'] = $form_state['values']['override_title_text']; + $form_state['conf']['override_title_heading'] = $form_state['values']['override_title_heading']; + } +} + +/** + * Get the config form. + * + * The $form_info and $form_state need to be preconfigured with data you'll need + * such as whether or not you're using ajax, or the modal. $form_info will need + * your next/submit callbacks so that you can cache your data appropriately. + * + * @return + * If this function returns false, no form exists. + */ +function ctools_content_form($op, $form_info, &$form_state, $plugin, $subtype_name, $subtype, &$conf, $step = NULL) { + $form_state += array( + 'plugin' => $plugin, + 'subtype' => $subtype, + 'subtype_name' => $subtype_name, + 'conf' => &$conf, + 'op' => $op, + ); + + $form_info += array( + 'id' => 'ctools_content_form', + 'show back' => TRUE, + ); + + // Turn the forms defined in the plugin into the format the wizard needs. + if ($op == 'add') { + if (!empty($subtype['add form'])) { + _ctools_content_create_form_info($form_info, $subtype['add form'], $subtype, $subtype, $op); + } + else if (!empty($plugin['add form'])) { + _ctools_content_create_form_info($form_info, $plugin['add form'], $plugin, $subtype, $op); + } + } + + if (empty($form_info['order'])) { + // Use the edit form for the add form if add form was completely left off. + if (!empty($subtype['edit form'])) { + _ctools_content_create_form_info($form_info, $subtype['edit form'], $subtype, $subtype, $op); + } + else if (!empty($plugin['edit form'])) { + _ctools_content_create_form_info($form_info, $plugin['edit form'], $plugin, $subtype, $op); + } + } + + if (empty($form_info['order'])) { + return FALSE; + } + + ctools_include('wizard'); + return ctools_wizard_multistep_form($form_info, $step, $form_state); + +} + +function _ctools_content_create_form_info(&$form_info, $info, $plugin, $subtype, $op) { + if (is_string($info)) { + if (empty($subtype['title'])) { + $title = t('Configure'); + } + else if ($op == 'add') { + $title = t('Configure new !subtype_title', array('!subtype_title' => $subtype['title'])); + } + else { + $title = t('Configure !subtype_title', array('!subtype_title' => $subtype['title'])); + } + $form_info['order'] = array('form' => $title); + $form_info['forms'] = array( + 'form' => array( + 'title' => $title, + 'form id' => $info, + 'wrapper' => 'ctools_content_configure_form_defaults', + ), + ); + } + else if (is_array($info)) { + $form_info['order'] = array(); + $form_info['forms'] = array(); + $count = 0; + $base = 'step'; + $wrapper = NULL; + foreach ($info as $form_id => $title) { + // @todo -- docs say %title can be used to sub for the admin title. + $step = $base . ++$count; + if (empty($wrapper)) { + $wrapper = $step; + } + + if (is_array($title)) { + if (!empty($title['default'])) { + $wrapper = $step; + } + $title = $title['title']; + } + + $form_info['order'][$step] = $title; + $form_info['forms'][$step] = array( + 'title' => $title, + 'form id' => $form_id, + ); + } + if ($wrapper) { + $form_info['forms'][$wrapper]['wrapper'] = 'ctools_content_configure_form_defaults'; + } + } +} + +/** + * Get an array of all available content types that can be fed into the + * display editor for the add content list. + * + * @param $context + * If a context is provided, content that requires that context can apepar. + * @param $has_content + * Whether or not the display will have incoming content + * @param $allowed_types + * An array of allowed content types (pane types) keyed by content_type . '-' . sub_type + * @param $default_types + * A default allowed/denied status for content that isn't known about + */ +function ctools_content_get_available_types($contexts = NULL, $has_content = FALSE, $allowed_types = NULL, $default_types = NULL) { + $plugins = ctools_get_content_types(); + $available = array(); + + foreach ($plugins as $id => $plugin) { + foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) { + // exclude items that require content if we're saying we don't + // provide it. + if (!empty($subtype['requires content']) && !$has_content) { + continue; + } + + // Check to see if the content type can be used in this context. + if (!empty($subtype['required context'])) { + if (!ctools_context_match_requirements($contexts, $subtype['required context'])) { + continue; + } + } + + // Check to see if the passed-in allowed types allows this content. + if ($allowed_types) { + $key = $id . '-' . $subtype_id; + if (!isset($allowed_types[$key])) { + $allowed_types[$key] = isset($default_types[$id]) ? $default_types[$id] : $default_types['other']; + } + if (!$allowed_types[$key]) { + continue; + } + } + + // Check if the content type provides an access callback. + if (isset($subtype['create content access']) && function_exists($subtype['create content access']) && !$subtype['create content access']($plugin, $subtype)) { + continue; + } + + // If we made it through all the tests, then we can use this content. + $available[$id][$subtype_id] = $subtype; + } + } + return $available; +} + +/** + * Get an array of all content types that can be fed into the + * display editor for the add content list, regardless of + * availability. + * + */ +function ctools_content_get_all_types() { + $plugins = ctools_get_content_types(); + $available = array(); + + foreach ($plugins as $id => $plugin) { + foreach (ctools_content_get_subtypes($plugin) as $subtype_id => $subtype) { + // If we made it through all the tests, then we can use this content. + $available[$id][$subtype_id] = $subtype; + } + } + return $available; +} + +/** + * Select the context to be used for a piece of content, based upon config. + * + * @param $plugin + * The content plugin + * @param $subtype + * The subtype of the content. + * @param $conf + * The configuration array that should contain the context. + * @param $contexts + * A keyed array of available contexts. + * + * @return + * The matching contexts or NULL if none or necessary, or FALSE if + * requirements can't be met. + */ +function ctools_content_select_context($plugin, $subtype, $conf, $contexts) { + // Identify which of our possible contexts apply. + if (empty($subtype)) { + return; + } + + $subtype_info = ctools_content_get_subtype($plugin, $subtype); + if (empty($subtype_info)) { + return; + } + + if (!empty($subtype_info['all contexts']) || !empty($plugin['all contexts'])) { + return $contexts; + } + + // If the content requires a context, fetch it; if no context is returned, + // do not display the pane. + if (empty($subtype_info['required context'])) { + return; + } + + // Deal with dynamic required contexts not getting updated in the panes. + // For example, Views let you dynamically change context info. While + // we cannot be perfect, one thing we can do is if no context at all + // was asked for, and then was later added but none is selected, make + // a best guess as to what context should be used. THis is right more + // than it's wrong. + if (is_array($subtype_info['required context'])) { + if (empty($conf['context']) || count($subtype_info['required context']) != count($conf['context'])) { + foreach ($subtype_info['required context'] as $index => $required) { + if (!isset($conf['context'][$index])) { + $filtered = ctools_context_filter($contexts, $required); + if ($filtered) { + $keys = array_keys($filtered); + $conf['context'][$index] = array_shift($keys); + } + } + } + } + } + else { + if (empty($conf['context'])) { + $filtered = ctools_context_filter($contexts, $subtype_info['required context']); + if ($filtered) { + $keys = array_keys($filtered); + $conf['context'] = array_shift($keys); + } + } + } + + if (empty($conf['context'])) { + return; + } + + $context = ctools_context_select($contexts, $subtype_info['required context'], $conf['context']); + + return $context; +} + +/** + * Fetch a piece of content from the addressable content system. + * + * @param $address + * A string or an array representing the address of the content. + * @param $type + * The type of content to return. The type is dependent on what + * the content actually is. The default, 'content' means a simple + * string representing the content. However, richer systems may + * offer more options. For example, Panels might allow the + * fetching of 'display' and 'pane' objects. Page Manager + * might allow for the fetching of 'task_handler' objects + * (AKA variants). + */ +function ctools_get_addressable_content($address, $type = 'content') { + if (!is_array($address)) { + $address = explode('::', $address); + } + + if (!$address) { + return; + } + + // This removes the module from the address so the + // implementor is not responsible for that part. + $module = array_shift($address); + return module_invoke($module, 'addressable_content', $address, $type); +} diff --git a/sites/all/modules/contrib/ctools/includes/content.menu.inc b/sites/all/modules/contrib/ctools/includes/content.menu.inc new file mode 100644 index 0000000..f7f9340 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/content.menu.inc @@ -0,0 +1,179 @@ + array('access content'), + 'type' => MENU_CALLBACK, + 'file' => 'includes/content.menu.inc', + ); + $items['ctools/autocomplete/%'] = array( + 'page callback' => 'ctools_content_autocomplete_entity', + 'page arguments' => array(2), + ) + $base; +} + +/** + * Helper function for autocompletion of entity titles. + */ +function ctools_content_autocomplete_entity($entity_type, $string = '') { + if ($string != '') { + $entity_info = entity_get_info($entity_type); + + if (!module_exists('entity')) { + module_load_include('inc', 'ctools', 'includes/entity-access'); + _ctools_entity_access($entity_info, $entity_type); + } + + // We must query all ids, because if every one of the 10 don't have access + // the user may never be able to autocomplete a node title. + $preg_matches = array(); + $matches = array(); + $match = preg_match('/\[id: (\d+)\]/', $string, $preg_matches); + if (!$match) { + $match = preg_match('/^id: (\d+)/', $string, $preg_matches); + } + // If an ID match was found, use that ID rather than the whole string. + if ($match) { + $entity_id = $preg_matches[1]; + $results = _ctools_getReferencableEntities($entity_type, $entity_info, $entity_id, '=', 1); + } + else { + // We cannot find results if the entity doesn't have a label to search. + if (!isset($entity_info['entity keys']['label'])) { + drupal_json_output(array("[id: NULL]" => '' . t('Entity Type !entity_type does not support autocomplete search.', array('!entity_type' => $entity_type)) . '')); + return; + } + $results = _ctools_getReferencableEntities($entity_type, $entity_info, $string, 'LIKE', 10); + } + foreach ($results as $entity_id => $result) { + $matches[$result['label'] . " [id: $entity_id]"] = '' . check_plain($result['label']) . ''; + $matches[$result['label'] . " [id: $entity_id]"] .= isset($result['bundle']) ? ' (' . check_plain($result['bundle']) . ')' : ''; + } + + drupal_json_output($matches); + } +} + +/* + * Use well known/tested entity reference code to build our search query + * From EntityReference_SelectionHandler_Generic class + */ +function _ctools_buildQuery($entity_type, $entity_info, $match = NULL, $match_operator = 'CONTAINS') { + $base_table = $entity_info['base table']; + $label_key = $entity_info['entity keys']['label']; + $query = db_select($base_table) + ->fields($base_table, array($entity_info['entity keys']['id'])); + + if (isset($match)) { + if (isset($label_key)) { + $query->condition($base_table . '.' . $label_key, '%' . $match . '%', $match_operator); + } + // This should never happen, but double check just in case. + else { + return array(); + } + } + // Add a generic entity access tag to the query. + $query->addTag('ctools'); + + // We have to perform two checks. First check is a query alter (with tags) + // in an attempt to only return results that have access. However, this is + // not full-proof since entities many not implement hook_access query tag. + // This is why we have a second check after entity load, before we display + // the label of an entity. + if ($entity_type == 'comment') { + // Adding the 'comment_access' tag is sadly insufficient for comments: core
 + // requires us to also know about the concept of 'published' and
 + // 'unpublished'.
 + if (!user_access('administer comments')) { + $query->condition('comment.status', COMMENT_PUBLISHED); + } + + // Join to a node if the user does not have node access bypass permissions
 + // to obey node published permissions
 + if (!user_access('bypass node access')) { + $node_alias = $query->innerJoin('node', 'n', '%alias.nid = comment.nid'); + $query->condition($node_alias . '.status', NODE_PUBLISHED); + } + $query->addTag('node_access'); + } + else { + $query->addTag($entity_type . '_access'); + } + + // Add the sort option. + if (isset($label_key)) { + $query->orderBy($base_table . '.' . $label_key, 'ASC'); + } + + return $query; +} + +/** + * Private function to get referencable entities. Based on code from the + * Entity Reference module. + */ +function _ctools_getReferencableEntities($entity_type, $entity_info, $match = NULL, $match_operator = 'LIKE', $limit = 0) { + global $user; + $account = $user; + $options = array(); + // We're an entity ID, return the id + if (is_numeric($match) && $match_operator == '=') { + if ($entity = array_shift(entity_load($entity_type, array($match)))) { + if (isset($entity_info['access callback']) && function_exists($entity_info['access callback'])) { + if ($entity_info['access callback']('view', $entity, $account, $entity_type)) { + $label = entity_label($entity_type, $entity); + return array( + $match => array( + 'label' => !empty($label) ? $label : $entity->{$entity_info['entity keys']['id']}, + 'bundle' => !empty($entity_info['entity keys']['bundle']) ? check_plain($entity->{$entity_info['entity keys']['bundle']}) : NULL, + ), + ); + } + } + } + // If you don't have access, or an access callback or a valid entity, just + // Return back the Entity ID. + return array( + $match => array( + 'label' => $match, + 'bundle' => NULL, + ), + ); + } + + // We have matches, build a query to fetch the result. + if ($query = _ctools_buildQuery($entity_type, $entity_info, $match, $match_operator)) { + if ($limit > 0) { + $query->range(0, $limit); + } + + $results = $query->execute(); + + if (!empty($results)) { + foreach ($results as $record) { + $entities = entity_load($entity_type, array($record->{$entity_info['entity keys']['id']})); + $entity = array_shift($entities); + if (isset($entity_info['access callback']) && function_exists($entity_info['access callback'])) { + if ($entity_info['access callback']('view', $entity, $account, $entity_type)) { + $label = entity_label($entity_type, $entity); + $options[$record->{$entity_info['entity keys']['id']}] = array( + 'label' => !empty($label) ? $label : $entity->{$entity_info['entity keys']['id']}, + 'bundle' => !empty($entity_info['entity keys']['bundle']) ? check_plain($entity->{$entity_info['entity keys']['bundle']}) : NULL, + ); + } + } + } + } + return $options; + } + return array(); +} diff --git a/sites/all/modules/contrib/ctools/includes/content.plugin-type.inc b/sites/all/modules/contrib/ctools/includes/content.plugin-type.inc new file mode 100644 index 0000000..a0debc3 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/content.plugin-type.inc @@ -0,0 +1,17 @@ + FALSE, + 'process' => array( + 'function' => 'ctools_content_process', + 'file' => 'content.inc', + 'path' => drupal_get_path('module', 'ctools') . '/includes', + ), + ); +} \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/includes/content.theme.inc b/sites/all/modules/contrib/ctools/includes/content.theme.inc new file mode 100644 index 0000000..ae4456a --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/content.theme.inc @@ -0,0 +1,21 @@ + array( + * 0 => array( + * 'name' => 'name of access plugin', + * 'settings' => array(), // These will be set by the form + * ), + * // ... as many as needed + * ), + * 'logic' => 'AND', // or 'OR', + * ), + * @endcode + * + * To add this widget to your UI, you need to do a little bit of setup. + * + * The form will utilize two callbacks, one to get the cached version + * of the access settings, and one to store the cached version of the + * access settings. These will be used from AJAX forms, so they will + * be completely out of the context of this page load and will not have + * knowledge of anything sent to this form (the 'module' and 'argument' + * will be preserved through the URL only). + * + * The 'module' is used to determine the location of the callback. It + * does not strictly need to be a module, so that if your module defines + * multiple systems that use this callback, it can use anything within the + * module's namespace it likes. + * + * When retrieving the cache, the cache may not have already been set up; + * In order to efficiently use cache space, we want to cache the stored + * settings *only* when they have changed. Therefore, the get access cache + * callback should first look for cache, and if it finds nothing, return + * the original settings. + * + * The callbacks: + * - $module . _ctools_access_get($argument) -- get the 'access' settings + * from cache. Must return array($access, $contexts); This callback can + * perform access checking to make sure this URL is not being gamed. + * - $module . _ctools_access_set($argument, $access) -- set the 'access' + * settings in cache. + * - $module . _ctools_access_clear($argument) -- clear the cache. + * + * The ctools_object_cache is recommended for this purpose, but you can use + * any caching mechanism you like. An example: + * + * @code{ + * ctools_include('object-cache'); + * ctools_object_cache_set("$module:argument", $access); + * } + * + * To utilize this form: + * @code + * ctools_include('context-access-admin'); + * $form_state = array( + * 'access' => $access, + * 'module' => 'module name', + * 'callback argument' => 'some string', + * 'contexts' => $contexts, // an array of contexts. Optional if no contexts. + * // 'logged-in-user' will be added if not present as the access system + * // requires this context. + * ), + * $output = drupal_build_form('ctools_access_admin_form', $form_state); + * if (!empty($form_state['executed'])) { + * // save $form_state['access'] however you like. + * } + * @endcode + * + * Additionally, you may add 'no buttons' => TRUE if you wish to embed this + * form into your own, and instead call + * + * @code{ + * $form = ctools_access_admin_form($form, $form_state); + * } + * + * You'll be responsible for adding a submit button. + * + * You may use ctools_access($access, $contexts) which will return + * TRUE if access is passed or FALSE if access is not passed. + */ + +/** + * Administrative form for access control. + */ +function ctools_access_admin_form($form, &$form_state) { + ctools_include('context'); + $argument = isset($form_state['callback argument']) ? $form_state['callback argument'] : ''; + $fragment = $form_state['module']; + if ($argument) { + $fragment .= '-' . $argument; + } + + $contexts = isset($form_state['contexts']) ? $form_state['contexts'] : array(); + + $form['access_table'] = array( + '#markup' => ctools_access_admin_render_table($form_state['access'], $fragment, $contexts), + ); + + $form['add-button'] = array( + '#theme' => 'ctools_access_admin_add', + ); + // This sets up the URL for the add access modal. + $form['add-button']['add-url'] = array( + '#attributes' => array('class' => array("ctools-access-add-url")), + '#type' => 'hidden', + '#value' => url("ctools/context/ajax/access/add/$fragment", array('absolute' => TRUE)), + ); + + $plugins = ctools_get_relevant_access_plugins($contexts); + $options = array(); + foreach ($plugins as $id => $plugin) { + $options[$id] = $plugin['title']; + } + + asort($options); + + $form['add-button']['type'] = array( + // This ensures that the form item is added to the URL. + '#attributes' => array('class' => array("ctools-access-add-url")), + '#type' => 'select', + '#options' => $options, + '#required' => FALSE, + ); + + $form['add-button']['add'] = array( + '#type' => 'submit', + '#attributes' => array('class' => array('ctools-use-modal')), + '#id' => "ctools-access-add", + '#value' => t('Add'), + ); + + $form['logic'] = array( + '#type' => 'radios', + '#options' => array( + 'and' => t('All criteria must pass.'), + 'or' => t('Only one criteria must pass.'), + ), + '#default_value' => isset($form_state['access']['logic']) ? $form_state['access']['logic'] : 'and', + ); + + if (empty($form_state['no buttons'])) { + $form['buttons']['save'] = array( + '#type' => 'submit', + '#value' => t('Save'), + '#submit' => array('ctools_access_admin_form_submit'), + ); + } + + return $form; +} + +/** + * Render the table. This is used both to render it initially and to rerender + * it upon ajax response. + */ +function ctools_access_admin_render_table($access, $fragment, $contexts) { + ctools_include('ajax'); + ctools_include('modal'); + $rows = array(); + + if (empty($access['plugins'])) { + $access['plugins'] = array(); + } + + foreach ($access['plugins'] as $id => $test) { + $row = array(); + $plugin = ctools_get_access_plugin($test['name']); + $title = isset($plugin['title']) ? $plugin['title'] : t('Broken/missing access plugin %plugin', array('%plugin' => $test['name'])); + + $row[] = array('data' => $title, 'class' => array('ctools-access-title')); + + $description = ctools_access_summary($plugin, $contexts, $test); + $row[] = array('data' => $description, 'class' => array('ctools-access-description')); + + $operations = ctools_modal_image_button(ctools_image_path('icon-configure.png'), "ctools/context/ajax/access/configure/$fragment/$id", t('Configure settings for this item.')); + $operations .= ctools_ajax_image_button(ctools_image_path('icon-delete.png'), "ctools/context/ajax/access/delete/$fragment/$id", t('Remove this item.')); + + $row[] = array('data' => $operations, 'class' => array('ctools-access-operations'), 'align' => 'right'); + + $rows[] = $row; + } + + $header = array( + array('data' => t('Title'), 'class' => array('ctools-access-title')), + array('data' => t('Description'), 'class' => array('ctools-access-description')), + array('data' => '', 'class' => array('ctools-access-operations'), 'align' => 'right'), + ); + + if (empty($rows)) { + $rows[] = array(array('data' => t('No criteria selected, this test will pass.'), 'colspan' => count($header))); + } + + ctools_modal_add_js(); + return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'ctools-access-table'))); +} + +/** + * Theme the 'add' portion of the access form into a table. + */ +function theme_ctools_access_admin_add($vars) { + $rows = array(array(drupal_render_children($vars['form']))); + $output = '
'; + $output .= theme('table', array('rows' => $rows)); + $output .= '
'; + return $output; +} + +function ctools_access_admin_form_submit($form, &$form_state) { + $form_state['access']['logic'] = $form_state['values']['logic']; + + $function = $form_state['module'] . '_ctools_access_clear'; + if (function_exists($function)) { + $function($form_state['callback argument']); + } +} + +// -------------------------------------------------------------------------- +// AJAX menu entry points. + +/** + * AJAX callback to add a new access test to the list. + */ +function ctools_access_ajax_add($fragment = NULL, $name = NULL) { + ctools_include('ajax'); + ctools_include('modal'); + ctools_include('context'); + + if (empty($fragment) || empty($name)) { + ctools_ajax_render_error(); + } + + $plugin = ctools_get_access_plugin($name); + if (empty($plugin)) { + ctools_ajax_render_error(); + } + + // Separate the fragment into 'module' and 'argument' + if (strpos($fragment, '-') === FALSE) { + $module = $fragment; + $argument = NULL; + } + else { + list($module, $argument) = explode('-', $fragment, 2); + } + + $function = $module . '_ctools_access_get'; + if (!function_exists($function)) { + ctools_ajax_render_error(t('Missing callback hooks.')); + } + + list($access, $contexts) = $function($argument); + + // Make sure we have the logged in user context + if (!isset($contexts['logged-in-user'])) { + $contexts['logged-in-user'] = ctools_access_get_loggedin_context(); + } + + if (empty($access['plugins'])) { + $access['plugins'] = array(); + } + + $test = ctools_access_new_test($plugin); + + $id = $access['plugins'] ? max(array_keys($access['plugins'])) + 1 : 0; + $access['plugins'][$id] = $test; + + $form_state = array( + 'plugin' => $plugin, + 'id' => $id, + 'test' => &$access['plugins'][$id], + 'access' => &$access, + 'contexts' => $contexts, + 'title' => t('Add criteria'), + 'ajax' => TRUE, + 'modal' => TRUE, + 'modal return' => TRUE, + ); + + $output = ctools_modal_form_wrapper('ctools_access_ajax_edit_item', $form_state); + if (!isset($output[0])) { + $function = $module . '_ctools_access_set'; + if (function_exists($function)) { + $function($argument, $access); + } + + $table = ctools_access_admin_render_table($access, $fragment, $contexts); + $output = array(); + $output[] = ajax_command_replace('table#ctools-access-table', $table); + $output[] = ctools_modal_command_dismiss(); + } + + print ajax_render($output); +} + +/** + * AJAX callback to edit an access test in the list. + */ +function ctools_access_ajax_edit($fragment = NULL, $id = NULL) { + ctools_include('ajax'); + ctools_include('modal'); + ctools_include('context'); + + if (empty($fragment) || !isset($id)) { + ctools_ajax_render_error(); + } + + // Separate the fragment into 'module' and 'argument' + if (strpos($fragment, '-') === FALSE) { + $module = $fragment; + $argument = NULL; + } + else { + list($module, $argument) = explode('-', $fragment, 2); + } + + $function = $module . '_ctools_access_get'; + if (!function_exists($function)) { + ctools_ajax_render_error(t('Missing callback hooks.')); + } + + list($access, $contexts) = $function($argument); + + if (empty($access['plugins'][$id])) { + ctools_ajax_render_error(); + } + + // Make sure we have the logged in user context + if (!isset($contexts['logged-in-user'])) { + $contexts['logged-in-user'] = ctools_access_get_loggedin_context(); + } + + $plugin = ctools_get_access_plugin($access['plugins'][$id]['name']); + $form_state = array( + 'plugin' => $plugin, + 'id' => $id, + 'test' => &$access['plugins'][$id], + 'access' => &$access, + 'contexts' => $contexts, + 'title' => t('Edit criteria'), + 'ajax' => TRUE, + 'ajax' => TRUE, + 'modal' => TRUE, + 'modal return' => TRUE, + ); + + $output = ctools_modal_form_wrapper('ctools_access_ajax_edit_item', $form_state); + if (!isset($output[0])) { + $function = $module . '_ctools_access_set'; + if (function_exists($function)) { + $function($argument, $access); + } + + $table = ctools_access_admin_render_table($access, $fragment, $contexts); + $output = array(); + $output[] = ajax_command_replace('table#ctools-access-table', $table); + $output[] = ctools_modal_command_dismiss(); + } + + print ajax_render($output); +} + +/** + * Form to edit the settings of an access test. + */ +function ctools_access_ajax_edit_item($form, &$form_state) { + $test = &$form_state['test']; + $plugin = &$form_state['plugin']; + if (isset($plugin['required context'])) { + $form['context'] = ctools_context_selector($form_state['contexts'], $plugin['required context'], $test['context']); + } + $form['settings'] = array('#tree' => TRUE); + if ($function = ctools_plugin_get_function($plugin, 'settings form')) { + $form = $function($form, $form_state, $test['settings']); + } + + $form['not'] = array( + '#type' => 'checkbox', + '#title' => t('Reverse (NOT)'), + '#default_value' => !empty($test['not']), + ); + + $form['save'] = array( + '#type' => 'submit', + '#value' => t('Save'), + ); + + return $form; +} + +/** + * Validate handler for argument settings. + */ +function ctools_access_ajax_edit_item_validate($form, &$form_state) { + if ($function = ctools_plugin_get_function($form_state['plugin'], 'settings form validate')) { + $function($form, $form_state); + } +} + +/** + * Submit handler for argument settings. + */ +function ctools_access_ajax_edit_item_submit($form, &$form_state) { + if ($function = ctools_plugin_get_function($form_state['plugin'], 'settings form submit')) { + $function($form, $form_state); + } + + $form_state['test']['settings'] = $form_state['values']['settings']; + if (isset($form_state['values']['context'])) { + $form_state['test']['context'] = $form_state['values']['context']; + } + $form_state['test']['not'] = !empty($form_state['values']['not']); +} + +/** + * AJAX command to remove an access control item. + */ +function ctools_access_ajax_delete($fragment = NULL, $id = NULL) { + ctools_include('ajax'); + ctools_include('modal'); + ctools_include('context'); + + if (empty($fragment) || !isset($id)) { + ajax_render_error(); + } + + // Separate the fragment into 'module' and 'argument' + if (strpos($fragment, '-') === FALSE) { + $module = $fragment; + $argument = NULL; + } + else { + list($module, $argument) = explode('-', $fragment, 2); + } + + $function = $module . '_ctools_access_get'; + if (!function_exists($function)) { + ajax_render_error(t('Missing callback hooks.')); + } + + list($access, $contexts) = $function($argument); + + if (isset($access['plugins'][$id])) { + unset($access['plugins'][$id]); + } + + // re-cache + $function = $module . '_ctools_access_set'; + if (function_exists($function)) { + $function($argument, $access); + } + + $table = ctools_access_admin_render_table($access, $fragment, $contexts); + $output = array(); + $output[] = ajax_command_replace('table#ctools-access-table', $table); + + print ajax_render($output); +} diff --git a/sites/all/modules/contrib/ctools/includes/context-admin.inc b/sites/all/modules/contrib/ctools/includes/context-admin.inc new file mode 100644 index 0000000..821a5b3 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/context-admin.inc @@ -0,0 +1,849 @@ + array( + 'title' => t('Arguments'), + 'singular title' => t('argument'), + 'description' => '', // t("Arguments are parsed from the URL and translated into contexts that may be added to the display via the 'content' tab. These arguments are parsed in the order received, and you may use % in your URL to hold the place of an object; the rest of the arguments will come after the URL. For example, if the URL is node/%/panel and your user visits node/1/panel/foo, the first argument will be 1, and the second argument will be foo."), + 'add button' => t('Add argument'), + 'context function' => 'ctools_get_argument', + 'key' => 'arguments', // the key that data will be stored on an object, eg $panel_page + 'sortable' => TRUE, + 'settings' => 'argument_settings', + ), + 'relationship' => array( + 'title' => t('Relationships'), + 'singular title' => t('relationship'), + 'description' => '', // t('Relationships are contexts that are created from already existing contexts; the add relationship button will only appear once there is another context available. Relationships can load objects based upon how they are related to each other; for example, the author of a node, or a taxonomy term attached to a node, or the vocabulary of a taxonomy term.'), + 'add button' => t('Add relationship'), + 'context function' => 'ctools_get_relationship', + 'key' => 'relationships', + 'sortable' => FALSE, + 'settings' => 'relationship_settings', + ), + 'context' => array( + 'title' => t('Contexts'), + 'singular title' => t('context'), + 'description' => '', // t('Contexts are embedded directly into the panel; you generally must select an object in the panel. For example, you could select node 5, or the term "animals" or the user "administrator"'), + 'add button' => t('Add context'), + 'context function' => 'ctools_get_context', + 'key' => 'contexts', + 'sortable' => FALSE, + 'settings' => 'context_settings', + ), + 'requiredcontext' => array( + 'title' => t('Required contexts'), + 'singular title' => t('required context'), + 'description' => '', // t('Required contexts are passed in from some external source, such as a containing panel. If a mini panel has required contexts, it can only appear when that context is available, and therefore will not show up as a standard Drupal block.'), + 'add button' => t('Add required context'), + 'context function' => 'ctools_get_context', + 'key' => 'requiredcontexts', + 'sortable' => FALSE, + ), + ); + } + + if ($type === NULL) { + return $info; + } + + return $info[$type]; +} + + +/** + * Get the data belonging to a particular context. + */ +function ctools_context_get_plugin($type, $name) { + $info = ctools_context_info($type); + if (function_exists($info['context function'])) { + return $info['context function']($name); + } +} + +/** + * Add the argument table plus gadget plus javascript to the form. + */ +function ctools_context_add_argument_form($module, &$form, &$form_state, &$form_location, $object, $cache_key = NULL) { + if (empty($cache_key)) { + $cache_key = $object->name; + } + + $form_location = array( + '#prefix' => '
', + '#suffix' => '
', + '#theme' => 'ctools_context_item_form', + '#cache_key' => $cache_key, + '#ctools_context_type' => 'argument', + '#ctools_context_module' => $module, + ); + + $args = ctools_get_arguments(); + $choices = array(); + foreach ($args as $name => $arg) { + if (empty($arg['no ui'])) { + $choices[$name] = $arg['title']; + } + } + + asort($choices); + + if (!empty($choices) || !empty($object->arguments)) { + ctools_context_add_item_table('argument', $form_location, $choices, $object->arguments); + } +} + +function ctools_context_add_context_form($module, &$form, &$form_state, &$form_location, $object, $cache_key = NULL) { + if (empty($cache_key)) { + $cache_key = $object->name; + } + + $form_location = array( + '#prefix' => '
', + '#suffix' => '
', + '#theme' => 'ctools_context_item_form', + '#cache_key' => $cache_key, + '#ctools_context_type' => 'context', + '#ctools_context_module' => $module, + ); + + // Store the order the choices are in so javascript can manipulate it. + $form_location['markup'] = array( + '#markup' => ' ', + ); + + $choices = array(); + foreach (ctools_get_contexts() as $name => $arg) { + if (empty($arg['no ui'])) { + $choices[$name] = $arg['title']; + } + } + + asort($choices); + + if (!empty($choices) || !empty($object->contexts)) { + ctools_context_add_item_table('context', $form_location, $choices, $object->contexts); + } + +} + +function ctools_context_add_required_context_form($module, &$form, &$form_state, &$form_location, $object, $cache_key = NULL) { + if (empty($cache_key)) { + $cache_key = $object->name; + } + + $form_location = array( + '#prefix' => '
', + '#suffix' => '
', + '#theme' => 'ctools_context_item_form', + '#cache_key' => $cache_key, + '#ctools_context_type' => 'requiredcontext', + '#ctools_context_module' => $module, + ); + + // Store the order the choices are in so javascript can manipulate it. + $form_location['markup'] = array( + '#value' => ' ', + ); + + $choices = array(); + foreach (ctools_get_contexts() as $name => $arg) { + if (empty($arg['no required context ui'])) { + $choices[$name] = $arg['title']; + } + } + + asort($choices); + + if (!empty($choices) || !empty($object->contexts)) { + ctools_context_add_item_table('requiredcontext', $form_location, $choices, $object->requiredcontexts); + } +} + +function ctools_context_add_relationship_form($module, &$form, &$form_state, &$form_location, $object, $cache_key = NULL) { + if (empty($cache_key)) { + $cache_key = $object->name; + } + + $form_location = array( + '#prefix' => '
', + '#suffix' => '
', + '#theme' => 'ctools_context_item_form', + '#cache_key' => $cache_key, + '#ctools_context_type' => 'relationship', + '#ctools_context_module' => $module, + ); + + // Store the order the choices are in so javascript can manipulate it. + $form_location['markup'] = array( + '#value' => ' ', + ); + + $base_contexts = isset($object->base_contexts) ? $object->base_contexts : array(); + $available_relationships = ctools_context_get_relevant_relationships(ctools_context_load_contexts($object, TRUE, $base_contexts)); + + ctools_context_add_item_table('relationship', $form_location, $available_relationships, $object->relationships); +} + +/** + * Include all context administrative include files, css, javascript. + */ +function ctools_context_admin_includes() { + ctools_include('context'); + ctools_include('modal'); + ctools_include('ajax'); + ctools_include('object-cache'); + ctools_modal_add_js(); + ctools_modal_add_plugin_js(ctools_get_contexts()); + ctools_modal_add_plugin_js(ctools_get_relationships()); +} + +/** + * Add the context table to the page. + */ +function ctools_context_add_item_table($type, &$form, $available_contexts, $items) { + $form[$type] = array( + '#tree' => TRUE, + ); + + $module = $form['#ctools_context_module']; + $cache_key = $form['#cache_key']; + + if (isset($items) && is_array($items)) { + foreach ($items as $position => $context) { + ctools_context_add_item_to_form($module, $type, $cache_key, $form[$type][$position], $position, $context); + } + } + + $type_info = ctools_context_info($type); + $form['description'] = array( + '#prefix' => '
', + '#suffix' => '
', + '#markup' => $type_info['description'], + ); + + ctools_context_add_item_table_buttons($type, $module, $form, $available_contexts); +} + +function ctools_context_add_item_table_buttons($type, $module, &$form, $available_contexts) { + drupal_add_library('system', 'drupal.ajax'); + $form['buttons'] = array( + '#tree' => TRUE, + ); + + if (!empty($available_contexts)) { + $type_info = ctools_context_info($type); + + $module = $form['#ctools_context_module']; + $cache_key = $form['#cache_key']; + + // The URL for this ajax button + $form['buttons'][$type]['add-url'] = array( + '#attributes' => array('class' => array("ctools-$type-add-url")), + '#type' => 'hidden', + '#value' => url("ctools/context/ajax/add/$module/$type/$cache_key", array('absolute' => TRUE)), + ); + + asort($available_contexts); + // This also will be in the URL. + $form['buttons'][$type]['item'] = array( + '#attributes' => array('class' => array("ctools-$type-add-url")), + '#type' => 'select', + '#options' => $available_contexts, + '#required' => FALSE, + ); + + $form['buttons'][$type]['add'] = array( + '#type' => 'submit', + '#attributes' => array('class' => array('ctools-use-modal')), + '#id' => "ctools-$type-add", + '#value' => $type_info['add button'], + ); + } +} + +/** + * Add a row to the form. Used both in the main form and by + * the ajax to add an item. + */ +function ctools_context_add_item_to_form($module, $type, $cache_key, &$form, $position, $item) { + // This is the single function way to load any plugin by variable type. + $info = ctools_context_get_plugin($type, $item['name']); + $form['title'] = array( + '#markup' => check_plain($item['identifier']), + ); + + // Relationships not sortable. + $type_info = ctools_context_info($type); + + if (!empty($type_info['sortable'])) { + $form['position'] = array( + '#type' => 'weight', + '#default_value' => $position, + '#attributes' => array('class' => array('drag-position')), + ); + } + + $form['remove'] = array( + '#markup' => ctools_ajax_image_button(ctools_image_path('icon-delete.png'), "ctools/context/ajax/delete/$module/$type/$cache_key/$position", t('Remove this item.')), + ); + + $form['settings'] = array( + '#markup' => ctools_modal_image_button(ctools_image_path('icon-configure.png'), "ctools/context/ajax/configure/$module/$type/$cache_key/$position", t('Configure settings for this item.')), + ); +} + + +// --------------------------------------------------------------------------- +// AJAX forms and stuff. + +/** + * Ajax entry point to add an context + */ +function ctools_context_ajax_item_add($mechanism = NULL, $type = NULL, $cache_key = NULL, $name = NULL, $step = NULL) { + ctools_include('ajax'); + ctools_include('modal'); + ctools_include('context'); + ctools_include('cache'); + ctools_include('plugins-admin'); + + if (!$name) { + return ctools_ajax_render_error(); + } + + // Load stored object from cache. + if (!($object = ctools_cache_get($mechanism, $cache_key))) { + ctools_ajax_render_error(t('Invalid object name.')); + } + + // Get info about what we're adding, i.e, relationship, context, argument, etc. + $plugin_definition = ctools_context_get_plugin($type, $name); + if (empty($plugin_definition)) { + ctools_ajax_render_error(t('Invalid context type')); + } + + // Set up the $conf array for this plugin + if (empty($step) || empty($object->temporary)) { + // Create the basis for our new context. + $conf = ctools_context_get_defaults($plugin_definition, $object, $type); + $object->temporary = &$conf; + } + else { + $conf = &$object->temporary; + } + + // Load the contexts that may be used. + $base_contexts = isset($object->base_contexts) ? $object->base_contexts : array(); + $contexts = ctools_context_load_contexts($object, TRUE, $base_contexts); + + $type_info = ctools_context_info($type); + $form_state = array( + 'ajax' => TRUE, + 'modal' => TRUE, + 'modal return' => TRUE, + 'object' => &$object, + 'conf' => &$conf, + 'plugin' => $plugin_definition, + 'type' => $type, + 'contexts' => $contexts, + 'title' => t('Add @type "@context"', array('@type' => $type_info['singular title'], '@context' => $plugin_definition['title'])), + 'type info' => $type_info, + 'op' => 'add', + 'step' => $step, + ); + + $form_info = array( + 'path' => "ctools/context/ajax/add/$mechanism/$type/$cache_key/$name/%step", + 'show cancel' => TRUE, + 'default form' => 'ctools_edit_context_form_defaults', + 'auto cache' => TRUE, + 'cache mechanism' => $mechanism, + 'cache key' => $cache_key, + // This is stating what the cache will be referred to in $form_state + 'cache location' => 'object', + ); + + if ($type == 'requiredcontext') { + $form_info += array( + 'add form name' => 'required context add form', + 'edit form name' => 'required context edit form', + ); + } + + $output = ctools_plugin_configure_form($form_info, $form_state); + + if (!empty($form_state['cancel'])) { + $output = array(ctools_modal_command_dismiss()); + } + else if (!empty($form_state['complete'])) { + // Successful submit -- move temporary data to location. + + // Create a reference to the place our context lives. Since this is fairly + // generic, this is the easiest way to get right to the place of the + // object without knowing precisely what data we're poking at. + $ref = &$object->{$type_info['key']}; + + // Figure out the position for our new context. + $position = empty($ref) ? 0 : max(array_keys($ref)) + 1; + + $conf['id'] = ctools_context_next_id($ref, $name); + $ref[$position] = $conf; + + if (isset($object->temporary)) { + unset($object->temporary); + } + + ctools_cache_operation($mechanism, $cache_key, 'finalize', $object); + + // Very irritating way to update the form for our contexts. + $arg_form_state = form_state_defaults() + array( + 'values' => array(), + 'process_input' => FALSE, + 'complete form' => array(), + ); + + $rel_form_state = $arg_form_state; + + $arg_form = array( + '#post' => array(), + '#programmed' => FALSE, + '#tree' => FALSE, + '#parents' => array(), + '#array_parents' => array(), + ); + + // Build a chunk of the form to merge into the displayed form + $arg_form[$type] = array( + '#tree' => TRUE, + ); + $arg_form[$type][$position] = array( + '#tree' => TRUE, + ); + + ctools_context_add_item_to_form($mechanism, $type, $cache_key, $arg_form[$type][$position], $position, $ref[$position]); + $arg_form = form_builder('ctools_context_form', $arg_form, $arg_form_state); + + // Build the relationships table so we can ajax it in. + // This is an additional thing that goes in here. + $rel_form = array( + '#theme' => 'ctools_context_item_form', + '#cache_key' => $cache_key, + '#ctools_context_type' => 'relationship', + '#ctools_context_module' => $mechanism, + '#only_buttons' => TRUE, + '#post' => array(), + '#programmed' => FALSE, + '#tree' => FALSE, + '#parents' => array(), + '#array_parents' => array(), + ); + + $rel_form['relationship'] = array( + '#tree' => TRUE, + ); + + // Allow an object to set some 'base' contexts that come from elsewhere. + $rel_contexts = isset($object->base_contexts) ? $object->base_contexts : array(); + $all_contexts = ctools_context_load_contexts($object, TRUE, $rel_contexts); + $available_relationships = ctools_context_get_relevant_relationships($all_contexts); + + $output = array(); + if (!empty($available_relationships)) { + ctools_context_add_item_table_buttons('relationship', $mechanism, $rel_form, $available_relationships); + $rel_form = form_builder('dummy_form_id', $rel_form, $rel_form_state); + $output[] = ajax_command_replace('div#ctools-relationships-table div.buttons', drupal_render($rel_form)); + } + + $theme_vars = array(); + $theme_vars['type'] = $type; + $theme_vars['form'] = $arg_form[$type][$position]; + $theme_vars['position'] = $position; + $theme_vars['count'] = $position; + $text = theme('ctools_context_item_row', $theme_vars); + $output[] = ajax_command_append('#' . $type . '-table tbody', $text); + $output[] = ajax_command_changed('#' . $type . '-row-' . $position, '.title'); + $output[] = ctools_modal_command_dismiss(); + } + else { + $output = ctools_modal_form_render($form_state, $output); + } + print ajax_render($output); + exit; +} + +/** + * Ajax entry point to edit an item + */ +function ctools_context_ajax_item_edit($mechanism = NULL, $type = NULL, $cache_key = NULL, $position = NULL, $step = NULL) { + ctools_include('ajax'); + ctools_include('modal'); + ctools_include('context'); + ctools_include('cache'); + ctools_include('plugins-admin'); + + if (!isset($position)) { + return ctools_ajax_render_error(); + } + + // Load stored object from cache. + if (!($object = ctools_cache_get($mechanism, $cache_key))) { + ctools_ajax_render_error(t('Invalid object name.')); + } + + $type_info = ctools_context_info($type); + + // Create a reference to the place our context lives. Since this is fairly + // generic, this is the easiest way to get right to the place of the + // object without knowing precisely what data we're poking at. + $ref = &$object->{$type_info['key']}; + + if (empty($step) || empty($object->temporary)) { + // Create the basis for our new context. + $conf = $object->{$type_info['key']}[$position]; + $object->temporary = &$conf; + } + else { + $conf = &$object->temporary; + } + + $name = $ref[$position]['name']; + if (empty($name)) { + ctools_ajax_render_error(); + } + + // load the plugin definition + $plugin_definition = ctools_context_get_plugin($type, $name); + if (empty($plugin_definition)) { + ctools_ajax_render_error(t('Invalid context type')); + } + + // Load the contexts + $base_contexts = isset($object->base_contexts) ? $object->base_contexts : array(); + $contexts = ctools_context_load_contexts($object, TRUE, $base_contexts); + + $form_state = array( + 'ajax' => TRUE, + 'modal' => TRUE, + 'modal return' => TRUE, + 'object' => &$object, + 'conf' => &$conf, + 'position' => $position, + 'plugin' => $plugin_definition, + 'type' => $type, + 'contexts' => $contexts, + 'title' => t('Edit @type "@context"', array('@type' => $type_info['singular title'], '@context' => $plugin_definition['title'])), + 'type info' => $type_info, + 'op' => 'add', + 'step' => $step, + ); + + $form_info = array( + 'path' => "ctools/context/ajax/configure/$mechanism/$type/$cache_key/$position/%step", + 'show cancel' => TRUE, + 'default form' => 'ctools_edit_context_form_defaults', + 'auto cache' => TRUE, + 'cache mechanism' => $mechanism, + 'cache key' => $cache_key, + // This is stating what the cache will be referred to in $form_state + 'cache location' => 'object', + ); + + if ($type == 'requiredcontext') { + $form_info += array( + 'add form name' => 'required context add form', + 'edit form name' => 'required context edit form', + ); + } + + $output = ctools_plugin_configure_form($form_info, $form_state); + + if (!empty($form_state['cancel'])) { + $output = array(ctools_modal_command_dismiss()); + } + else if (!empty($form_state['complete'])) { + // successful submit + $ref[$position] = $conf; + if (isset($object->temporary)) { + unset($object->temporary); + } + + ctools_cache_operation($mechanism, $cache_key, 'finalize', $object); + + $output = array(); + $output[] = ctools_modal_command_dismiss(); + + $arg_form_state = form_state_defaults() + array( + 'values' => array(), + 'process_input' => FALSE, + 'complete form' => array(), + ); + + $arg_form = array( + '#post' => array(), + '#parents' => array(), + '#array_parents' => array(), + '#programmed' => FALSE, + '#tree' => FALSE, + ); + + // Build a chunk of the form to merge into the displayed form + $arg_form[$type] = array( + '#tree' => TRUE, + ); + $arg_form[$type][$position] = array( + '#tree' => TRUE, + ); + + ctools_context_add_item_to_form($mechanism, $type, $cache_key, $arg_form[$type][$position], $position, $ref[$position]); + $arg_form = form_builder('ctools_context_form', $arg_form, $arg_form_state); + + $theme_vars = array(); + $theme_vars['type'] = $type; + $theme_vars['form'] = $arg_form[$type][$position]; + $theme_vars['position'] = $position; + $theme_vars['count'] = $position; + $output[] = ajax_command_replace('#' . $type . '-row-' . $position, theme('ctools_context_item_row', $theme_vars)); + $output[] = ajax_command_changed('#' . $type . '-row-' . $position, '.title'); + } + else { + $output = ctools_modal_form_render($form_state, $output); + } + print ajax_render($output); + exit; +} + +/** + * Get the defaults for a new instance of a context plugin. + * + * @param $plugin_definition + * The metadata definition of the plugin from ctools_get_plugins(). + * @param $object + * The object the context plugin will be added to. + * @param $type + * The type of context plugin. i.e, context, requiredcontext, relationship + */ +function ctools_context_get_defaults($plugin_definition, $object, $type) { + // Fetch the potential id of the plugin so we can append + // title and keyword information for new ones. + $type_info = ctools_context_info($type); + $id = ctools_context_next_id($object->{$type_info['key']}, $plugin_definition['name']); + + $conf = array( + 'identifier' => $plugin_definition['title'] . ($id > 1 ? ' ' . $id : ''), + 'keyword' => ctools_get_keyword($object, $plugin_definition['keyword']), + 'name' => $plugin_definition['name'], + ); + + if (isset($plugin_definition['defaults'])) { + $defaults = $plugin_definition['defaults']; + } + else if (isset($subtype['defaults'])) { + $defaults = $subtype['defaults']; + } + + if (isset($defaults)) { + if (is_string($defaults) && function_exists($defaults)) { + if ($settings = $defaults($plugin_definition)) { + $conf += $settings; + } + } + else if (is_array($defaults)) { + $conf += $defaults; + } + } + + return $conf; +} + +/** + * Form wrapper for the edit context form. + * + * @todo: We should uncombine these. + */ +function ctools_edit_context_form_defaults($form, &$form_state) { + // Basic values required to orient ourselves + $object = $form_state['object']; + $plugin_definition = $form_state['plugin']; + $type_info = $form_state['type info']; + $contexts = $form_state['contexts']; + $conf = $form_state['conf']; + + if ($type_info['key'] == 'arguments' && !isset($conf['default'])) { + $conf['default'] = 'ignore'; + $conf['title'] = ''; + } + + $form['description'] = array( + '#prefix' => '
', + '#suffix' => '
', + '#markup' => check_plain($plugin_definition['description']), + ); + + if ($type_info['key'] == 'relationships') { + $form['context'] = ctools_context_selector($contexts, $plugin_definition['required context'], isset($conf['context']) ? $conf['context'] : ''); + } + if ($type_info['key'] == 'arguments') { + $form['default'] = array( + '#type' => 'select', + '#title' => t('Default'), + '#options' => array( + 'ignore' => t('Ignore it; content that requires this context will not be available.'), + '404' => t('Display page not found or display nothing at all.'), + ), + '#default_value' => $conf['default'], + '#description' => t('If the argument is missing or is not valid, select how this should behave.'), + ); + + $form['title'] = array( + '#type' => 'textfield', + '#title' => t('Title'), + '#default_value' => $conf['title'], + '#description' => t('Enter a title to use when this argument is present. You may use %KEYWORD substitution, where the keyword is specified below.'), + ); + } + + $form['identifier'] = array( + '#type' => 'textfield', + '#title' => t('Identifier'), + '#description' => t('Enter a name to identify this !type on administrative screens.', array('!type' => t('context'))), + '#default_value' => $conf['identifier'], + ); + + $form['keyword'] = array( + '#type' => 'textfield', + '#title' => t('Keyword'), + '#description' => t('Enter a keyword to use for substitution in titles.'), + '#default_value' => $conf['keyword'], + ); + + if ($type_info['key'] == 'requiredcontexts') { + $form['optional'] = array( + '#type' => 'checkbox', + '#title' => t('Context is optional'), + '#default_value' => !empty($form_state['conf']['optional']), + '#description' => t('This context need not be present for the component to function.'), + ); + } + + $form['#submit'][] = 'ctools_edit_context_form_defaults_submit'; + + return $form; +} + +/** + * Submit handler to store context identifier and keyword info. + */ +function ctools_edit_context_form_defaults_submit(&$form, &$form_state) { + if ($form_state['type info']['key'] == 'relationships') { + $form_state['conf']['context'] = $form_state['values']['context']; + } + if ($form_state['type info']['key'] == 'arguments') { + $form_state['conf']['default'] = $form_state['values']['default']; + $form_state['conf']['title'] = $form_state['values']['title']; + } + if ($form_state['type info']['key'] == 'requiredcontexts') { + $form_state['conf']['optional'] = $form_state['values']['optional']; + } + + $form_state['conf']['identifier'] = $form_state['values']['identifier']; + $form_state['conf']['keyword'] = $form_state['values']['keyword']; +} + +/** + * Ajax entry point to edit an item + */ +function ctools_context_ajax_item_delete($mechanism = NULL, $type = NULL, $cache_key = NULL, $position = NULL) { + ctools_include('ajax'); + ctools_include('context'); + ctools_include('cache'); + + if (!isset($position)) { + return ctools_ajax_render_error(); + } + + // Load stored object from cache. + if (!($object = ctools_cache_get($mechanism, $cache_key))) { + ctools_ajax_render_error(t('Invalid object name.')); + } + + $type_info = ctools_context_info($type); + + // Create a reference to the place our context lives. Since this is fairly + // generic, this is the easiest way to get right to the place of the + // object without knowing precisely what data we're poking at. + $ref = &$object->{$type_info['key']}; + + if (!array_key_exists($position, $ref)) { + ctools_ajax_render_error(t('Unable to delete missing item!')); + } + + unset($ref[$position]); + ctools_cache_operation($mechanism, $cache_key, 'finalize', $object); + + $output = array(); + $output[] = ajax_command_replace('#' . $type . '-row-' . $position, ''); + $output[] = ajax_command_restripe("#$type-table"); + print ajax_render($output); + exit; +} + +// --- End of contexts + +function ctools_save_context($type, &$ref, $form_values) { + $type_info = ctools_context_info($type); + + // Organize arguments + $new = array(); + $order = array(); + + foreach ($ref as $id => $context) { + $position = $form_values[$type][$id]['position']; + $order[$position] = $id; + } + + ksort($order); + foreach ($order as $id) { + $new[] = $ref[$id]; + } + $ref = $new; +} + +function ctools_get_keyword($page, $word) { + // Create a complete set of keywords + $keywords = array(); + foreach (array('arguments', 'relationships', 'contexts', 'requiredcontexts') as $type) { + if (!empty($page->$type) && is_array($page->$type)) { + foreach ($page->$type as $info) { + $keywords[$info['keyword']] = TRUE; + } + } + } + + $keyword = $word; + $count = 1; + while (!empty($keywords[$keyword])) { + $keyword = $word . '_' . ++$count; + } + return $keyword; +} + diff --git a/sites/all/modules/contrib/ctools/includes/context-task-handler.inc b/sites/all/modules/contrib/ctools/includes/context-task-handler.inc new file mode 100644 index 0000000..21ceea5 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/context-task-handler.inc @@ -0,0 +1,540 @@ + $handler) { + $plugin = page_manager_get_task_handler($handler->handler); + // First, see if the handler has a tester. + $function = ctools_plugin_get_function($plugin, 'test'); + if ($function) { + $test = $function($handler, $contexts, $args); + if ($test) { + return $id; + } + } + else { + // If not, if it's a 'context' type handler, use the default tester. + if ($plugin['handler type'] == 'context') { + $test = ctools_context_handler_default_test($handler, $contexts, $args); + if ($test) { + return $id; + } + } + } + } + + return FALSE; +} + +/** + * Default test function to see if a task handler should be rendered. + * + * This tests against the standard selection criteria that most task + * handlers should be implementing. + */ +function ctools_context_handler_default_test($handler, $base_contexts, $args) { + ctools_include('context'); + // Add my contexts + $contexts = ctools_context_handler_get_handler_contexts($base_contexts, $handler); + + // Test. + return ctools_context_handler_select($handler, $contexts); +} + +/** + * Render a task handler. + */ +function ctools_context_handler_render_handler($task, $subtask, $handler, $contexts, $args, $page = TRUE) { + $function = page_manager_get_renderer($handler); + if (!$function) { + return NULL; + } + + if ($page) { + if ($subtask) { + $task_name = page_manager_make_task_name($task['name'], $subtask['name']); + } + else { + $task_name = $task['name']; + } + + page_manager_get_current_page(array( + 'name' => $task_name, + 'task' => $task, + 'subtask' => $subtask, + 'contexts' => $contexts, + 'arguments' => $args, + 'handler' => $handler, + )); + } + + $info = $function($handler, $contexts, $args); + if (!$info) { + return NULL; + } + + $context = array( + 'args' => $args, + 'contexts' => $contexts, + 'task' => $task, + 'subtask' => $subtask, + 'handler' => $handler + ); + drupal_alter('ctools_render', $info, $page, $context); + + // If we don't own the page, let the caller deal with rendering. + if (!$page) { + return $info; + } + + if (!empty($info['response code']) && $info['response code'] != 200) { + switch ($info['response code']) { + case 403: + return MENU_ACCESS_DENIED; + case 404: + return MENU_NOT_FOUND; + case 410: + drupal_add_http_header('Status', '410 Gone'); + drupal_exit(); + break; + case 301: + case 302: + case 303: + case 304: + case 305: + case 307: + $info += array( + 'query' => array(), + 'fragment' => '', + ); + $options = array( + 'query' => $info['query'], + 'fragment' => $info['fragment'], + ); + drupal_goto($info['destination'], $options, $info['response code']); + // @todo -- should other response codes be supported here? + } + } + + $plugin = page_manager_get_task_handler($handler->handler); + + if (module_exists('contextual') && user_access('access contextual links') && isset($handler->task)) { + // Provide a contextual link to edit this, if we can: + $callback = isset($plugin['contextual link']) ? $plugin['contextual link'] : 'ctools_task_handler_default_contextual_link'; + if ($callback && function_exists($callback)) { + $links = $callback($handler, $plugin, $contexts, $args); + } + + if (!empty($links) && is_array($links)) { + $build = array( + '#theme_wrappers' => array('container'), + '#attributes' => array('class' => array('contextual-links-region')), + ); + + if (!is_array($info['content'])) { + $build['content']['#markup'] = $info['content']; + } + else { + $build['content'] = $info['content']; + } + + $build['contextual_links'] = array( + '#prefix' => '', + '#theme' => 'links__contextual', + '#links' => $links, + '#attributes' => array('class' => array('contextual-links')), + '#attached' => array( + 'library' => array(array('contextual', 'contextual-links')), + ), + ); + $info['content'] = $build; + } + } + + foreach (ctools_context_handler_get_task_arguments($task, $subtask) as $id => $argument) { + $plugin = ctools_get_argument($argument['name']); + $cid = ctools_context_id($argument, 'argument'); + if (!empty($contexts[$cid]) && ($function = ctools_plugin_get_function($plugin, 'breadcrumb'))) { + $function($argument['settings'], $contexts[$cid]); + } + } + + if (isset($info['title'])) { + drupal_set_title($info['title'], PASS_THROUGH); + } + + // Only directly output if $page was set to true. + if (!empty($info['no_blocks'])) { + ctools_set_no_blocks(FALSE); + } + return $info['content']; +} + +/** + * Default function to provide contextual link for a task as defined by the handler. + * + * This provides a simple link to th main content operation and is suitable + * for most normal handlers. Setting 'contextual link' to a function overrides + * this and setting it to FALSE will prevent a contextual link from appearing. + */ +function ctools_task_handler_default_contextual_link($handler, $plugin, $contexts, $args) { + if (!user_access('administer page manager')) { + return; + } + + $task = page_manager_get_task($handler->task); + + $title = !empty($task['tab title']) ? $task['tab title'] : t('Edit @type', array('@type' => $plugin['title'])); + $trail = array(); + if (!empty($plugin['tab operation'])) { + if (is_array($plugin['tab operation'])) { + $trail = $plugin['tab operation']; + } + else if (function_exists($plugin['tab operation'])) { + $trail = $plugin['tab operation']($handler, $contexts, $args); + } + } + $path = page_manager_edit_url(page_manager_make_task_name($handler->task, $handler->subtask), $trail); + + $links = array(array( + 'href' => $path, + 'title' => $title, + 'query' => drupal_get_destination(), + )); + + return $links; +} + +/** + * Called to execute actions that should happen before a handler is rendered. + */ +function ctools_context_handler_pre_render($handler, $contexts, $args) { } + +/** + * Compare arguments to contexts for selection purposes. + * + * @param $handler + * The handler in question. + * @param $contexts + * The context objects provided by the task. + * + * @return + * TRUE if these contexts match the selection rules. NULL or FALSE + * otherwise. + */ +function ctools_context_handler_select($handler, $contexts) { + if (empty($handler->conf['access'])) { + return TRUE; + } + + ctools_include('context'); + return ctools_access($handler->conf['access'], $contexts); +} + +/** + * Get the array of summary strings for the arguments. + * + * These summary strings are used to communicate to the user what + * arguments the task handlers are selecting. + * + * @param $task + * The loaded task plugin. + * @param $subtask + * The subtask id. + * @param $handler + * The handler to be checked. + */ +function ctools_context_handler_summary($task, $subtask, $handler) { + if (empty($handler->conf['access']['plugins'])) { + return array(); + } + + ctools_include('context'); + $strings = array(); + $contexts = ctools_context_handler_get_all_contexts($task, $subtask, $handler); + + foreach ($handler->conf['access']['plugins'] as $test) { + $plugin = ctools_get_access_plugin($test['name']); + if ($string = ctools_access_summary($plugin, $contexts, $test)) { + $strings[] = $string; + } + } + + return $strings; +} + +// -------------------------------------------------------------------------- +// Tasks and Task handlers can both have their own sources of contexts. +// Sometimes we need all of these contexts at once (when editing +// the task handler, for example) but sometimes we need them separately +// (when a task has contexts loaded and is trying out the task handlers, +// for example). Therefore there are two paths we can take to getting contexts. + +/** + * Load the contexts for a task, using arguments. + * + * This creates the base array of contexts, loaded from arguments, suitable + * for use in rendering. + */ +function ctools_context_handler_get_task_contexts($task, $subtask, $args) { + $contexts = ctools_context_handler_get_base_contexts($task, $subtask); + $arguments = ctools_context_handler_get_task_arguments($task, $subtask); + ctools_context_get_context_from_arguments($arguments, $contexts, $args); + + return $contexts; +} + +/** + * Load the contexts for a task handler. + * + * This expands a base set of contexts passed in from a task with the + * contexts defined on the task handler. The contexts from the task + * must already have been loaded. + */ +function ctools_context_handler_get_handler_contexts($contexts, $handler) { + $object = ctools_context_handler_get_handler_object($handler); + return ctools_context_load_contexts($object, FALSE, $contexts); +} + +/** + * Load the contexts for a task and task handler together. + * + * This pulls the arguments from a task and everything else from a task + * handler and loads them as a group. Since there is no data, this loads + * the contexts as placeholders. + */ +function ctools_context_handler_get_all_contexts($task, $subtask, $handler) { + $contexts = array(); + + $object = ctools_context_handler_get_task_object($task, $subtask, $handler); + $contexts = ctools_context_load_contexts($object, TRUE, $contexts); + ctools_context_handler_set_access_restrictions($task, $subtask, $handler, $contexts); + return $contexts; +} + +/** + * Create an object suitable for use with the context system that kind of + * expects things in a certain, kind of clunky format. + */ +function ctools_context_handler_get_handler_object($handler) { + $object = new stdClass; + $object->name = $handler->name; + $object->contexts = isset($handler->conf['contexts']) ? $handler->conf['contexts'] : array(); + $object->relationships = isset($handler->conf['relationships']) ? $handler->conf['relationships'] : array(); + + return $object; +} + +/** + * Create an object suitable for use with the context system that kind of + * expects things in a certain, kind of clunky format. This one adds in + * arguments from the task. + */ +function ctools_context_handler_get_task_object($task, $subtask, $handler) { + $object = new stdClass; + $object->name = !empty($handler->name) ? $handler->name : 'temp'; + $object->base_contexts = ctools_context_handler_get_base_contexts($task, $subtask, TRUE); + $object->arguments = ctools_context_handler_get_task_arguments($task, $subtask); + $object->contexts = isset($handler->conf['contexts']) ? $handler->conf['contexts'] : array(); + $object->relationships = isset($handler->conf['relationships']) ? $handler->conf['relationships'] : array(); + + return $object; +} + +/** + * Get base contexts from a task, if it has any. + * + * Tasks can get their contexts either from base contexts or arguments; base + * contexts extract their information from the environment. + */ +function ctools_context_handler_get_base_contexts($task, $subtask, $placeholders = FALSE) { + if ($function = ctools_plugin_get_function($task, 'get base contexts')) { + return $function($task, $subtask, $placeholders); + } + + return array(); +} + +/** + * Get the arguments from a task that are used to load contexts. + */ +function ctools_context_handler_get_task_arguments($task, $subtask) { + if ($function = ctools_plugin_get_function($task, 'get arguments')) { + return $function($task, $subtask); + } + + return array(); +} + +/** + * Set any access restrictions on the contexts for a handler. + * + * Both the task and the handler could add restrictions to the contexts + * based upon the access control. These restrictions might be useful + * to limit what kind of content appears in the add content dialog; + * for example, if we have an access item that limits a node context + * to only 'story' and 'page' types, there is no need for content that + * only applies to the 'poll' type to appear. + */ +function ctools_context_handler_set_access_restrictions($task, $subtask, $handler, &$contexts) { + // First, for the task: + if ($function = ctools_plugin_get_function($task, 'access restrictions')) { + $function($task, $subtask, $contexts); + } + + // Then for the handler: + if (isset($handler->conf['access'])) { + ctools_access_add_restrictions($handler->conf['access'], $contexts); + } +} + +/** + * Form to choose context based selection rules for a task handler. + * + * The configuration will be assumed to go simply in $handler->conf and + * will be keyed by the argument ID. + */ +function ctools_context_handler_edit_criteria($form, &$form_state) { + if (!isset($form_state['handler']->conf['access'])) { + $form_state['handler']->conf['access'] = array(); + } + + ctools_include('context'); + ctools_include('modal'); + ctools_include('ajax'); + ctools_modal_add_plugin_js(ctools_get_access_plugins()); + ctools_include('context-access-admin'); + $form_state['module'] = (isset($form_state['module'])) ? $form_state['module'] : 'page_manager_task_handler'; + // Encode a bunch of info into the argument so we can get our cache later + $form_state['callback argument'] = $form_state['task_name'] . '*' . $form_state['handler']->name; + $form_state['access'] = $form_state['handler']->conf['access']; + $form_state['no buttons'] = TRUE; + $form_state['contexts'] = ctools_context_handler_get_all_contexts($form_state['task'], $form_state['subtask'], $form_state['handler']); + + $form['markup'] = array( + '#markup' => '
' . + t('If there is more than one variant on a page, when the page is visited each variant is given an opportunity to be displayed. Starting from the first variant and working to the last, each one tests to see if its selection rules will pass. The first variant that meets its criteria (as specified below) will be used.') . + '
', + ); + $form = ctools_access_admin_form($form, $form_state); + return $form; +} + +/** + * Submit handler for rules selection + */ +function ctools_context_handler_edit_criteria_submit(&$form, &$form_state) { + $form_state['handler']->conf['access']['logic'] = $form_state['values']['logic']; +} + +/** + * Edit contexts that go with this panel. + */ +function ctools_context_handler_edit_context($form, &$form_state) { + ctools_include('context-admin'); + ctools_context_admin_includes(); + + $handler = $form_state['handler']; + $page = $form_state['page']; + $cache_name = $handler->name ? $handler->name : 'temp'; + if (isset($page->context_cache[$cache_name])) { + $cache = $page->context_cache[$cache_name]; + } + else { + $cache = ctools_context_handler_get_task_object($form_state['task'], $form_state['subtask'], $form_state['handler']); + $form_state['page']->context_cache[$cache_name] = $cache; + } + + $form['right'] = array( + '#prefix' => '
', + '#suffix' => '
', + ); + + $form['left'] = array( + '#prefix' => '
', + '#suffix' => '
', + ); + + $module = 'page_manager_context::' . $page->task_name; + ctools_context_add_context_form($module, $form, $form_state, $form['right']['contexts_table'], $cache); + ctools_context_add_relationship_form($module, $form, $form_state, $form['right']['relationships_table'], $cache); + + $theme_vars = array(); + $theme_vars['object'] = $cache; + $theme_vars['header'] = t('Summary of contexts'); + $form['left']['summary'] = array( + '#prefix' => '
', + '#suffix' => '
', + '#markup' => theme('ctools_context_list', $theme_vars), + ); + + $form_state['context_object'] = &$cache; + return $form; +} + +/** + * Process submission of the context edit form. + */ +function ctools_context_handler_edit_context_submit(&$form, &$form_state) { + $handler = &$form_state['handler']; + + $cache_name = $handler->name ? $handler->name : 'temp'; + + $handler->conf['contexts'] = $form_state['context_object']->contexts; + $handler->conf['relationships'] = $form_state['context_object']->relationships; + if (isset($form_state['page']->context_cache[$cache_name])) { + unset($form_state['page']->context_cache[$cache_name]); + } +} + diff --git a/sites/all/modules/contrib/ctools/includes/context.inc b/sites/all/modules/contrib/ctools/includes/context.inc new file mode 100644 index 0000000..1f9c1e4 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/context.inc @@ -0,0 +1,1602 @@ +type = $type; + $this->data = $data; + $this->title = t('Unknown context'); + } + + function is_type($type) { + if ($type == 'any' || $this->type == 'any') { + return TRUE; + } + + $a = is_array($type) ? $type : array($type); + $b = is_array($this->type) ? $this->type : array($this->type); + return (bool) array_intersect($a, $b); + } + + function get_argument() { + return $this->argument; + } + + function get_original_argument() { + if (!is_null($this->original_argument)) { + return $this->original_argument; + } + return $this->argument; + } + + function get_keyword() { + return $this->keyword; + } + + function get_identifier() { + return $this->identifier; + } + + function get_title() { + return $this->title; + } + + function get_page_title() { + return $this->page_title; + } +} + +/** + * Used to create a method of comparing if a list of contexts + * match a required context type. + */ +class ctools_context_required { + var $keywords = ''; + + /** + * If set, the title will be used in the selector to identify + * the context. This is very useful when multiple contexts + * are required to inform the user will be used for what. + */ + var $title = NULL; + + /** + * Test to see if this context is required. + */ + var $required = TRUE; + + /** + * If TRUE, skip the check in ctools_context_required::select() + * for contexts whose names may have changed. + */ + var $skip_name_check = FALSE; + + /** + * + * @param $title + * The first parameter should be the 'title' of the context for use + * in UYI selectors when multiple contexts qualify. + * @param ... + * One or more keywords to use for matching which contexts are allowed. + */ + function ctools_context_required($title) { + $args = func_get_args(); + $this->title = array_shift($args); + + // If we have a boolean value at the end for $skip_name_check, store it + if (is_bool(end($args))) { + $this->skip_name_check = array_pop($args); + } + + // If we were given restrictions at the end, store them. + if (count($args) > 1 && is_array(end($args))) { + $this->restrictions = array_pop($args); + } + + if (count($args) == 1) { + $args = array_shift($args); + } + $this->keywords = $args; + } + + function filter($contexts) { + $result = array(); + + // See which of these contexts are valid + foreach ((array) $contexts as $cid => $context) { + if ($context->is_type($this->keywords)) { + // Compare to see if our contexts were met. + if (!empty($this->restrictions) && !empty($context->restrictions)) { + foreach ($this->restrictions as $key => $values) { + // If we have a restriction, the context must either not have that + // restriction listed, which means we simply don't know what it is, + // or there must be an intersection of the restricted values on + // both sides. + if (!is_array($values)) { + $values = array($values); + } + if (!empty($context->restrictions[$key]) && !array_intersect($values, $context->restrictions[$key])) { + continue 2; + } + } + } + $result[$cid] = $context; + } + } + + return $result; + } + + function select($contexts, $context) { + if (!is_array($contexts)) { + if (is_object($contexts) && $contexts instanceof ctools_context) { + $contexts = array($contexts->id => $contexts); + } + else { + $contexts = array($contexts); + } + } + + // If we had requested a $context but that $context doesn't exist + // in our context list, there is a good chance that what happened + // is our context IDs changed. See if there's another context + // that satisfies our requirements. + if (!$this->skip_name_check && !empty($context) && !isset($contexts[$context])) { + $choices = $this->filter($contexts); + + // If we got a hit, take the first one that matches. + if ($choices) { + $keys = array_keys($choices); + $context = reset($keys); + } + } + + if (empty($context) || empty($contexts[$context])) { + return FALSE; + } + return $contexts[$context]; + } +} + +/** + * Used to compare to see if a list of contexts match an optional context. This + * can produce empty contexts to use as placeholders. + */ +class ctools_context_optional extends ctools_context_required { + var $required = FALSE; + function ctools_context_optional() { + $args = func_get_args(); + call_user_func_array(array($this, 'ctools_context_required'), $args); + } + + /** + * Add the 'empty' context which is possible for optional + */ + function add_empty(&$contexts) { + $context = new ctools_context('any'); + $context->title = t('No context'); + $context->identifier = t('No context'); + $contexts['empty'] = $context; + } + + function filter($contexts) { + $this->add_empty($contexts); + return parent::filter($contexts); + } + + function select($contexts, $context) { + $this->add_empty($contexts); + if (empty($context)) { + return $contexts['empty']; + } + + $result = parent::select($contexts, $context); + + // Don't flip out if it can't find the context; this is optional, put + // in an empty. + if ($result == FALSE) { + $result = $contexts['empty']; + } + return $result; + } +} + +/** + * Return a keyed array of context that match the given 'required context' + * filters. + * + * Functions or systems that require contexts of a particular type provide a + * ctools_context_required or ctools_context_optional object. This function + * examines that object and an array of contexts to determine which contexts + * match the filter. + * + * Since multiple contexts can be required, this function will accept either + * an array of all required contexts, or just a single required context object. + * + * @param $contexts + * A keyed array of all available contexts. + * @param $required + * A ctools_context_required or ctools_context_optional object, or an array + * of such objects. + * + * @return + * A keyed array of contexts that match the filter. + */ +function ctools_context_filter($contexts, $required) { + if (is_array($required)) { + $result = array(); + foreach ($required as $r) { + $result = array_merge($result, _ctools_context_filter($contexts, $r)); + } + return $result; + } + + return _ctools_context_filter($contexts, $required); +} + +function _ctools_context_filter($contexts, $required) { + $result = array(); + + if (is_object($required)) { + $result = $required->filter($contexts); + } + + return $result; +} + +/** + * Create a select box to choose possible contexts. + * + * This only creates a selector if there is actually a choice; if there + * is only one possible context, that one is silently assigned. + * + * If an array of required contexts is provided, one selector will be + * provided for each context. + * + * @param $contexts + * A keyed array of all available contexts. + * @param $required + * The required context object or array of objects. + * + * @return + * A form element, or NULL if there are no contexts that satisfy the + * requirements. + */ +function ctools_context_selector($contexts, $required, $default) { + if (is_array($required)) { + $result = array('#tree' => TRUE); + $count = 1; + foreach ($required as $id => $r) { + $result[] = _ctools_context_selector($contexts, $r, isset($default[$id]) ? $default[$id] : '', $count++); + } + return $result; + } + + return _ctools_context_selector($contexts, $required, $default); +} + +function _ctools_context_selector($contexts, $required, $default, $num = 0) { + $filtered = ctools_context_filter($contexts, $required); + $count = count($filtered); + + $form = array(); + + if ($count >= 1) { + // If there's more than one to choose from, create a select widget. + foreach ($filtered as $cid => $context) { + $options[$cid] = $context->get_identifier(); + } + if (!empty($required->title)) { + $title = $required->title; + } + else { + $title = $num ? t('Context %count', array('%count' => $num)) : t('Context'); + } + + $form = array( + '#type' => 'select', + '#options' => $options, + '#title' => $title, + '#default_value' => $default, + ); + } + + return $form; +} + +/** + * Are there enough contexts for a plugin? + * + * Some plugins can have a 'required contexts' item which can either + * be a context requirement object or an array of them. When contexts + * are required, items that do not have enough contexts should not + * appear. This tests an item to see if it has enough contexts + * to actually appear. + * + * @param $contexts + * A keyed array of all available contexts. + * @param $required + * The required context object or array of objects. + * + * @return + * TRUE if there are enough contexts, FALSE if there are not. + */ +function ctools_context_match_requirements($contexts, $required) { + if (!is_array($required)) { + $required = array($required); + } + + // Get the keys to avoid bugs in PHP 5.0.8 with keys and loops. + // And use it to remove optional contexts. + $keys = array_keys($required); + foreach ($keys as $key) { + if (empty($required[$key]->required)) { + unset($required[$key]); + } + } + + $count = count($required); + return (count(ctools_context_filter($contexts, $required)) >= $count); +} + +/** + * Create a select box to choose possible contexts. + * + * This only creates a selector if there is actually a choice; if there + * is only one possible context, that one is silently assigned. + * + * If an array of required contexts is provided, one selector will be + * provided for each context. + * + * @param $contexts + * A keyed array of all available contexts. + * @param $required + * The required context object or array of objects. + * + * @return + * A form element, or NULL if there are no contexts that satisfy the + * requirements. + */ +function ctools_context_converter_selector($contexts, $required, $default) { + if (is_array($required)) { + $result = array('#tree' => TRUE); + $count = 1; + foreach ($required as $id => $r) { + $result[] = _ctools_context_converter_selector($contexts, $r, isset($default[$id]) ? $default[$id] : '', $count++); + } + return $result; + } + + return _ctools_context_converter_selector($contexts, $required, $default); +} + +function _ctools_context_converter_selector($contexts, $required, $default, $num = 0) { + $filtered = ctools_context_filter($contexts, $required); + $count = count($filtered); + + $form = array(); + + if ($count > 1) { + // If there's more than one to choose from, create a select widget. + $options = array(); + foreach ($filtered as $cid => $context) { + if ($context->type == 'any') { + $options[''] = t('No context'); + continue; + } + $key = $context->get_identifier(); + if ($converters = ctools_context_get_converters($cid . '.', $context)) { + $options[$key] = $converters; + } + } + if (empty($options)) { + return array( + '#type' => 'value', + '#value' => 'any', + ); + } + if (!empty($required->title)) { + $title = $required->title; + } + else { + $title = $num ? t('Context %count', array('%count' => $num)) : t('Context'); + } + + return array( + '#type' => 'select', + '#options' => $options, + '#title' => $title, + '#description' => t('Please choose which context and how you would like it converted.'), + '#default_value' => $default, + ); + } +} + +/** + * Get a list of converters available for a given context. + */ +function ctools_context_get_converters($cid, $context) { + if (empty($context->plugin)) { + return array(); + } + + return _ctools_context_get_converters($cid, $context->plugin); +} + +/** + * Get a list of converters available for a given context. + */ +function _ctools_context_get_converters($id, $plugin_name) { + $plugin = ctools_get_context($plugin_name); + if (empty($plugin['convert list'])) { + return array(); + } + + $converters = array(); + if (is_array($plugin['convert list'])) { + $converters = $plugin['convert list']; + } + else if ($function = ctools_plugin_get_function($plugin, 'convert list')) { + $converters = (array) $function($plugin); + } + + foreach (module_implements('ctools_context_convert_list_alter') as $module) { + $function = $module . '_ctools_context_convert_list_alter'; + $function($plugin, $converters); + } + + // Now, change them all to include the plugin: + $return = array(); + foreach ($converters as $key => $title) { + $return[$id . $key] = $title; + } + + natcasesort($return); + return $return; +} + +/** + * Get a list of all contexts + converters available. + */ +function ctools_context_get_all_converters() { + $contexts = ctools_get_contexts(); + $converters = array(); + foreach ($contexts as $name => $context) { + if (empty($context['no required context ui'])) { + $context_converters = _ctools_context_get_converters($name . '.', $name); + if ($context_converters) { + $converters[$context['title']] = $context_converters; + } + } + } + + return $converters; +} + +/** + * Let the context convert an argument based upon the converter that was given. + * + * @param $context + * The context object + * @param $converter + * The converter to use, which should be a string provided by the converter list. + * @param $converter_options + * A n array of options to pass on to the generation function. For contexts + * that use token module, of particular use is 'sanitize' => FALSE which can + * get raw tokens. This should ONLY be used in values that will later be + * treated as unsafe user input since these values are by themselves unsafe. + * It is particularly useful to get raw values from Field API. + */ +function ctools_context_convert_context($context, $converter, $converter_options = array()) { + // Contexts without plugins might be optional placeholders. + if (empty($context->plugin)) { + return; + } + + $value = $context->argument; + $plugin = ctools_get_context($context->plugin); + if ($function = ctools_plugin_get_function($plugin, 'convert')) { + $value = $function($context, $converter, $converter_options); + } + + foreach (module_implements('ctools_context_converter_alter') as $module) { + $function = $module . '_ctools_context_converter_alter'; + $function($context, $converter, $value, $converter_options); + } + + return $value; +} + +/** + * Choose a context or contexts based upon the selection made via + * ctools_context_filter. + * + * @param $contexts + * A keyed array of all available contexts + * @param $required + * The required context object provided by the plugin + * @param $context + * The selection made using ctools_context_selector + */ +function ctools_context_select($contexts, $required, $context) { + if (is_array($required)) { + $result = array(); + foreach ($required as $id => $r) { + if (empty($required[$id])) { + continue; + } + + if (($result[] = _ctools_context_select($contexts, $r, $context[$id])) === FALSE) { + return FALSE; + } + } + return $result; + } + + return _ctools_context_select($contexts, $required, $context); +} + +function _ctools_context_select($contexts, $required, $context) { + if (!is_object($required)) { + return FALSE; + } + + return $required->select($contexts, $context); +} + +/** + * Create a new context object. + * + * @param $type + * The type of context to create; this loads a plugin. + * @param $data + * The data to put into the context. + * @param $empty + * Whether or not this context is specifically empty. + * @param $conf + * A configuration structure if this context was created via UI. + * + * @return + * A $context or NULL if one could not be created. + */ +function ctools_context_create($type, $data = NULL, $conf = FALSE) { + ctools_include('plugins'); + $plugin = ctools_get_context($type); + + if ($function = ctools_plugin_get_function($plugin, 'context')) { + return $function(FALSE, $data, $conf, $plugin); + } +} + +/** + * Create an empty context object. + * + * Empty context objects are primarily used as placeholders in the UI where + * the actual contents of a context object may not be known. It may have + * additional text embedded to give the user clues as to how the context + * is used. + * + * @param $type + * The type of context to create; this loads a plugin. + * + * @return + * A $context or NULL if one could not be created. + */ +function ctools_context_create_empty($type) { + $plugin = ctools_get_context($type); + if ($function = ctools_plugin_get_function($plugin, 'context')) { + $context = $function(TRUE, NULL, FALSE, $plugin); + if (is_object($context)) { + $context->empty = TRUE; + } + + return $context; + } +} + +/** + * Perform keyword and context substitutions. + */ +function ctools_context_keyword_substitute($string, $keywords, $contexts, $converter_options = array()) { + // Ensure a default keyword exists: + $keywords['%%'] = '%'; + + // Match contexts to the base keywords: + $context_keywords = array(); + foreach ($contexts as $context) { + if (isset($context->keyword)) { + $context_keywords[$context->keyword] = $context; + } + } + + // Look for context matches we we only have to convert known matches. + $matches = array(); + if (preg_match_all('/%(%|[a-zA-Z0-9_-]+(?:\:[a-zA-Z0-9_-]+)*)/us', $string, $matches)) { + foreach ($matches[1] as $keyword) { + // Ignore anything it finds with %%. + if ($keyword[0] == '%') { + continue; + } + + // If the keyword is already set by something passed in, don't try to + // overwrite it. + if (!empty($keywords['%' . $keyword])) { + continue; + } + + // Figure out our keyword and converter, if specified. + if (strpos($keyword, ':')) { + list($context, $converter) = explode(':', $keyword, 2); + } + else { + $context = $keyword; + if (isset($context_keywords[$keyword])) { + $plugin = ctools_get_context($context_keywords[$context]->plugin); + + // Fall back to a default converter, if specified. + if ($plugin && !empty($plugin['convert default'])) { + $converter = $plugin['convert default']; + } + } + } + + if (empty($context_keywords[$context]) || !empty($context_keywords[$context]->empty)) { + $keywords['%' . $keyword] = ''; + } + else if (!empty($converter)) { + $keywords['%' . $keyword] = ctools_context_convert_context($context_keywords[$context], $converter, $converter_options); + } + else { + $keywords['%' . $keyword] = $context_keywords[$keyword]->title; + } + } + } + return strtr($string, $keywords); +} + +/** + * Determine a unique context ID for a context + * + * Often contexts of many different types will be placed into a list. This + * ensures that even though contexts of multiple types may share IDs, they + * are unique in the final list. + */ +function ctools_context_id($context, $type = 'context') { + if (!$context['id']) { + $context['id'] = 1; + } + + return $type . '_' . $context['name'] . '_' . $context['id']; +} + +/** + * Get the next id available given a list of already existing objects. + * + * This finds the next id available for the named object. + * + * @param $objects + * A list of context descriptor objects, i.e, arguments, relationships, contexts, etc. + * @param $name + * The name being used. + */ +function ctools_context_next_id($objects, $name) { + $id = 0; + // Figure out which instance of this argument we're creating + if (!$objects) { + return $id + 1; + } + + foreach ($objects as $object) { + if (isset($object['name']) && $object['name'] == $name) { + if ($object['id'] > $id) { + $id = $object['id']; + } + } + } + + return $id + 1; +} + + +// --------------------------------------------------------------------------- +// Functions related to contexts from arguments. + +/** + * Fetch metadata on a specific argument plugin. + * + * @param $argument + * Name of an argument plugin. + * + * @return + * An array with information about the requested argument plugin. + */ +function ctools_get_argument($argument) { + ctools_include('plugins'); + return ctools_get_plugins('ctools', 'arguments', $argument); +} + +/** + * Fetch metadata for all argument plugins. + * + * @return + * An array of arrays with information about all available argument plugins. + */ +function ctools_get_arguments() { + ctools_include('plugins'); + return ctools_get_plugins('ctools', 'arguments'); +} + +/** + * Get a context from an argument. + * + * @param $argument + * The configuration of an argument. It must contain the following data: + * - name: The name of the argument plugin being used. + * - argument_settings: The configuration based upon the plugin forms. + * - identifier: The human readable identifier for this argument, usually + * defined by the UI. + * - keyword: The keyword used for this argument for substitutions. + * + * @param $arg + * The actual argument received. This is expected to be a string from a URL but + * this does not have to be the only source of arguments. + * @param $empty + * If true, the $arg will not be used to load the context. Instead, an empty + * placeholder context will be loaded. + * + * @return + * A context object if one can be loaded. + */ +function ctools_context_get_context_from_argument($argument, $arg, $empty = FALSE) { + ctools_include('plugins'); + if (empty($argument['name'])) { + return; + } + + if ($function = ctools_plugin_load_function('ctools', 'arguments', $argument['name'], 'context')) { + // Backward compatibility: Merge old style settings into new style: + if (!empty($argument['settings'])) { + $argument += $argument['settings']; + unset($argument['settings']); + } + + $context = $function($arg, $argument, $empty); + + if (is_object($context)) { + $context->identifier = $argument['identifier']; + $context->page_title = isset($argument['title']) ? $argument['title'] : ''; + $context->keyword = $argument['keyword']; + $context->id = ctools_context_id($argument, 'argument'); + $context->original_argument = $arg; + + if (!empty($context->empty)) { + $context->placeholder = array( + 'type' => 'argument', + 'conf' => $argument, + ); + } + } + return $context; + } +} + +/** + * Retrieve a list of empty contexts for all arguments. + */ +function ctools_context_get_placeholders_from_argument($arguments) { + $contexts = array(); + foreach ($arguments as $argument) { + $context = ctools_context_get_context_from_argument($argument, NULL, TRUE); + if ($context) { + $contexts[ctools_context_id($argument, 'argument')] = $context; + } + } + return $contexts; +} + +/** + * Load the contexts for a given list of arguments. + * + * @param $arguments + * The array of argument definitions. + * @param &$contexts + * The array of existing contexts. New contexts will be added to this array. + * @param $args + * The arguments to load. + * + * @return + * FALSE if an argument wants to 404. + */ +function ctools_context_get_context_from_arguments($arguments, &$contexts, $args) { + foreach ($arguments as $argument) { + // pull the argument off the list. + $arg = array_shift($args); + $id = ctools_context_id($argument, 'argument'); + + // For % arguments embedded in the URL, our context is already loaded. + // There is no need to go and load it again. + if (empty($contexts[$id])) { + if ($context = ctools_context_get_context_from_argument($argument, $arg)) { + $contexts[$id] = $context; + } + } + else { + $context = $contexts[$id]; + } + + if ((empty($context) || empty($context->data)) && !empty($argument['default']) && $argument['default'] == '404') { + return FALSE; + } + } + return TRUE; +} + +// --------------------------------------------------------------------------- +// Functions related to contexts from relationships. + +/** + * Fetch metadata on a specific relationship plugin. + * + * @param $content type + * Name of a panel content type. + * + * @return + * An array with information about the requested relationship. + */ +function ctools_get_relationship($relationship) { + ctools_include('plugins'); + return ctools_get_plugins('ctools', 'relationships', $relationship); +} + +/** + * Fetch metadata for all relationship plugins. + * + * @return + * An array of arrays with information about all available relationships. + */ +function ctools_get_relationships() { + ctools_include('plugins'); + return ctools_get_plugins('ctools', 'relationships'); +} + +/** + * + * @param $relationship + * The configuration of a relationship. It must contain the following data: + * - name: The name of the relationship plugin being used. + * - relationship_settings: The configuration based upon the plugin forms. + * - identifier: The human readable identifier for this relationship, usually + * defined by the UI. + * - keyword: The keyword used for this relationship for substitutions. + * + * @param $source_context + * The context this relationship is based upon. + * + * @param $placeholders + * If TRUE, placeholders are acceptable. + * + * @return + * A context object if one can be loaded. + */ +function ctools_context_get_context_from_relationship($relationship, $source_context, $placeholders = FALSE) { + ctools_include('plugins'); + if ($function = ctools_plugin_load_function('ctools', 'relationships', $relationship['name'], 'context')) { + // Backward compatibility: Merge old style settings into new style: + if (!empty($relationship['relationship_settings'])) { + $relationship += $relationship['relationship_settings']; + unset($relationship['relationship_settings']); + } + + $context = $function($source_context, $relationship, $placeholders); + if ($context) { + $context->identifier = $relationship['identifier']; + $context->page_title = isset($relationship['title']) ? $relationship['title'] : ''; + $context->keyword = $relationship['keyword']; + if (!empty($context->empty)) { + $context->placeholder = array( + 'type' => 'relationship', + 'conf' => $relationship, + ); + } + return $context; + } + } +} + +/** + * Fetch all relevant relationships. + * + * Relevant relationships are any relationship that can be created based upon + * the list of existing contexts. For example, the 'node author' relationship + * is relevant if there is a 'node' context, but makes no sense if there is + * not one. + * + * @param $contexts + * An array of contexts used to figure out which relationships are relevant. + * + * @return + * An array of relationship keys that are relevant for the given set of + * contexts. + */ +function ctools_context_get_relevant_relationships($contexts) { + $relevant = array(); + $relationships = ctools_get_relationships(); + + // Go through each relationship + foreach ($relationships as $rid => $relationship) { + // For each relationship, see if there is a context that satisfies it. + if (empty($relationship['no ui']) && ctools_context_filter($contexts, $relationship['required context'])) { + $relevant[$rid] = $relationship['title']; + } + } + + return $relevant; +} + +/** + * Fetch all active relationships + * + * @param $relationships + * An keyed array of relationship data including: + * - name: name of relationship + * - context: context id relationship belongs to. This will be used to + * identify which context in the $contexts array to use to create the + * relationship context. + * + * @param $contexts + * A keyed array of contexts used to figure out which relationships + * are relevant. New contexts will be added to this. + * + * @param $placeholders + * If TRUE, placeholders are acceptable. + */ +function ctools_context_get_context_from_relationships($relationships, &$contexts, $placeholders = FALSE) { + $return = array(); + + foreach ($relationships as $rdata) { + if (!isset($rdata['context'])) { + continue; + } + + if (is_array($rdata['context'])) { + $rcontexts = array(); + foreach ($rdata['context'] as $cid) { + if (empty($contexts[$cid])) { + continue 2; + } + $rcontexts[] = $contexts[$cid]; + } + } + else { + if (empty($contexts[$rdata['context']])) { + continue; + } + $rcontexts = $contexts[$rdata['context']]; + } + + $cid = ctools_context_id($rdata, 'relationship'); + if ($context = ctools_context_get_context_from_relationship($rdata, $rcontexts)) { + $contexts[$cid] = $context; + } + } +} + +// --------------------------------------------------------------------------- +// Functions related to loading contexts from simple context definitions. + +/** + * Fetch metadata on a specific context plugin. + * + * @param $context + * Name of a context. + * + * @return + * An array with information about the requested panel context. + */ +function ctools_get_context($context) { + static $gate = array(); + ctools_include('plugins'); + $plugin = ctools_get_plugins('ctools', 'contexts', $context); + if (empty($gate['context']) && !empty($plugin['superceded by'])) { + // This gate prevents infinite loops. + $gate[$context] = TRUE; + $new_plugin = ctools_get_plugins('ctools', 'contexts', $plugin['superceded by']); + $gate[$context] = FALSE; + + // If a new plugin was returned, return it. Otherwise fall through and + // return the original we fetched. + if ($new_plugin) { + return $new_plugin; + } + } + + return $plugin; +} + +/** + * Fetch metadata for all context plugins. + * + * @return + * An array of arrays with information about all available panel contexts. + */ +function ctools_get_contexts() { + ctools_include('plugins'); + return ctools_get_plugins('ctools', 'contexts'); +} + +/** + * + * @param $context + * The configuration of a context. It must contain the following data: + * - name: The name of the context plugin being used. + * - context_settings: The configuration based upon the plugin forms. + * - identifier: The human readable identifier for this context, usually + * defined by the UI. + * - keyword: The keyword used for this context for substitutions. + * @param $type + * This is either 'context' which indicates the context will be loaded + * from data in the settings, or 'required_context' which means the + * context must be acquired from an external source. This is the method + * used to pass pure contexts from one system to another. + * + * @return + * A context object if one can be loaded. + */ +function ctools_context_get_context_from_context($context, $type = 'context', $argument = NULL) { + ctools_include('plugins'); + $plugin = ctools_get_context($context['name']); + if ($function = ctools_plugin_get_function($plugin, 'context')) { + // Backward compatibility: Merge old style settings into new style: + if (!empty($context['context_settings'])) { + $context += $context['context_settings']; + unset($context['context_settings']); + } + + if (isset($argument) && isset($plugin['placeholder name'])) { + $context[$plugin['placeholder name']] = $argument; + } + + $return = $function($type == 'requiredcontext', $context, TRUE, $plugin); + if ($return) { + $return->identifier = $context['identifier']; + $return->page_title = isset($context['title']) ? $context['title'] : ''; + $return->keyword = $context['keyword']; + + if (!empty($context->empty)) { + $context->placeholder = array( + 'type' => 'context', + 'conf' => $context, + ); + } + + return $return; + } + } +} + +/** + * Retrieve a list of base contexts based upon a simple 'contexts' definition. + * + * For required contexts this will always retrieve placeholders. + * + * @param $contexts + * The list of contexts defined in the UI. + * @param $type + * Either 'context' or 'requiredcontext', which indicates whether the contexts + * are loaded from internal data or copied from an external source. + * @param $placeholders + * If true, placeholders are acceptable. + */ +function ctools_context_get_context_from_contexts($contexts, $type = 'context', $placeholders = FALSE) { + $return = array(); + foreach ($contexts as $context) { + $ctext = ctools_context_get_context_from_context($context, $type); + if ($ctext) { + if ($placeholders) { + $ctext->placeholder = TRUE; + } + $return[ctools_context_id($context, $type)] = $ctext; + } + } + return $return; +} + +/** + * Match up external contexts to our required contexts. + * + * This function is used to create a list of contexts with proper + * IDs based upon a list of required contexts. + * + * These contexts passed in should match the numeric positions of the + * required contexts. The caller must ensure this has already happened + * correctly as this function will not detect errors here. + * + * @param $required + * A list of required contexts as defined by the UI. + * @param $contexts + * A list of matching contexts as passed in from the calling system. + */ +function ctools_context_match_required_contexts($required, $contexts) { + $return = array(); + if (!is_array($required)) { + return $return; + } + + foreach ($required as $r) { + $context = clone(array_shift($contexts)); + $context->identifier = $r['identifier']; + $context->page_title = isset($r['title']) ? $r['title'] : ''; + $context->keyword = $r['keyword']; + $return[ctools_context_id($r, 'requiredcontext')] = $context; + } + + return $return; +} + +/** + * Load a full array of contexts for an object. + * + * Not all of the types need to be supported by this object. + * + * This function is not used to load contexts from external data, but may + * be used to load internal contexts and relationships. Otherwise it can also + * be used to generate a full set of placeholders for UI purposes. + * + * @param $object + * An object that contains some or all of the following variables: + * + * - requiredcontexts: A list of UI configured contexts that are required + * from an external source. Since these require external data, they will + * only be added if $placeholders is set to TRUE, and empty contexts will + * be created. + * - arguments: A list of UI configured arguments that will create contexts. + * Since these require external data, they will only be added if $placeholders + * is set to TRUE. + * - contexts: A list of UI configured contexts that have no external source, + * and are essentially hardcoded. For example, these might configure a + * particular node or a particular taxonomy term. + * - relationships: A list of UI configured contexts to be derived from other + * contexts that already exist from other sources. For example, these might + * be used to get a user object from a node via the node author relationship. + * @param $placeholders + * If TRUE, this will generate placeholder objects for types this function + * cannot load. + * @param $contexts + * An array of pre-existing contexts that will be part of the return value. + */ +function ctools_context_load_contexts($object, $placeholders = TRUE, $contexts = array()) { + if (!empty($object->base_contexts)) { + $contexts += $object->base_contexts; + } + + if ($placeholders) { + // This will load empty contexts as placeholders for arguments that come + // from external sources. If this isn't set, it's assumed these context + // will already have been matched up and loaded. + if (!empty($object->requiredcontexts) && is_array($object->requiredcontexts)) { + $contexts += ctools_context_get_context_from_contexts($object->requiredcontexts, 'requiredcontext', $placeholders); + } + + if (!empty($object->arguments) && is_array($object->arguments)) { + $contexts += ctools_context_get_placeholders_from_argument($object->arguments); + } + } + + if (!empty($object->contexts) && is_array($object->contexts)) { + $contexts += ctools_context_get_context_from_contexts($object->contexts, 'context', $placeholders); + } + + // add contexts from relationships + if (!empty($object->relationships) && is_array($object->relationships)) { + ctools_context_get_context_from_relationships($object->relationships, $contexts, $placeholders); + } + + return $contexts; +} + +/** + * Return the first context with a form id from a list of contexts. + * + * This function is used to figure out which contexts represents 'the form' + * from a list of contexts. Only one contexts can actually be 'the form' for + * a given page, since the @code{
} tag can not be embedded within + * itself. + */ +function ctools_context_get_form($contexts) { + if (!empty($contexts)) { + foreach ($contexts as $id => $context) { + // if a form shows its id as being a 'required context' that means the + // the context is external to this display and does not count. + if (!empty($context->form_id) && substr($id, 0, 15) != 'requiredcontext') { + return $context; + } + } + } +} + +/** + * Replace placeholders with real contexts using data extracted from a form + * for the purposes of previews. + * + * @param $contexts + * All of the contexts, including the placeholders. + * @param $arguments + * The arguments. These will be acquired from $form_state['values'] and the + * keys must match the context IDs. + * + * @return + * A new $contexts array containing the replaced contexts. Not all contexts + * may be replaced if, for example, an argument was unable to be converted + * into a context. + */ +function ctools_context_replace_placeholders($contexts, $arguments) { + foreach ($contexts as $cid => $context) { + if (empty($context->empty)) { + continue; + } + + $new_context = NULL; + switch ($context->placeholder['type']) { + case 'relationship': + $relationship = $context->placeholder['conf']; + if (isset($contexts[$relationship['context']])) { + $new_context = ctools_context_get_context_from_relationship($relationship, $contexts[$relationship['context']]); + } + break; + case 'argument': + if (isset($arguments[$cid]) && $arguments[$cid] !== '') { + $argument = $context->placeholder['conf']; + $new_context = ctools_context_get_context_from_argument($argument, $arguments[$cid]); + } + break; + case 'context': + if (!empty($arguments[$cid])) { + $context_info = $context->placeholder['conf']; + $new_context = ctools_context_get_context_from_context($context_info, 'requiredcontext', $arguments[$cid]); + } + break; + } + + if ($new_context && empty($new_context->empty)) { + $contexts[$cid] = $new_context; + } + } + + return $contexts; +} + +/** + * Provide a form array for getting data to replace placeholder contexts + * with real data. + */ +function ctools_context_replace_form(&$form, $contexts) { + foreach ($contexts as $cid => $context) { + if (empty($context->empty)) { + continue; + } + + // Get plugin info from the context which should have been set when the + // empty context was created. + $info = NULL; + $plugin = NULL; + $settings = NULL; + switch ($context->placeholder['type']) { + case 'argument': + $info = $context->placeholder['conf']; + $plugin = ctools_get_argument($info['name']); + break; + + case 'context': + $info = $context->placeholder['conf']; + $plugin = ctools_get_context($info['name']); + break; + } + + // Ask the plugin where the form is. + if ($plugin && isset($plugin['placeholder form'])) { + if (is_array($plugin['placeholder form'])) { + $form[$cid] = $plugin['placeholder form']; + } + else if (function_exists($plugin['placeholder form'])) { + $widget = $plugin['placeholder form']($info); + if ($widget) { + $form[$cid] = $widget; + } + } + + if (!empty($form[$cid])) { + $form[$cid]['#title'] = t('@identifier (@keyword)', array('@keyword' => '%' . $context->keyword, '@identifier' => $context->identifier)); + } + } + } +} + +// --------------------------------------------------------------------------- +// Functions related to loading access control plugins + +/** + * Fetch metadata on a specific access control plugin. + * + * @param $name + * Name of a plugin. + * + * @return + * An array with information about the requested access control plugin. + */ +function ctools_get_access_plugin($name) { + ctools_include('plugins'); + return ctools_get_plugins('ctools', 'access', $name); +} + +/** + * Fetch metadata for all access control plugins. + * + * @return + * An array of arrays with information about all available access control plugins. + */ +function ctools_get_access_plugins() { + ctools_include('plugins'); + return ctools_get_plugins('ctools', 'access'); +} + +/** + * Fetch a list of access plugins that are available for a given list of + * contexts. + * + * if 'logged-in-user' is not in the list of contexts, it will be added as + * this is required. + */ +function ctools_get_relevant_access_plugins($contexts) { + if (!isset($contexts['logged-in-user'])) { + $contexts['logged-in-user'] = ctools_access_get_loggedin_context(); + } + + $all_plugins = ctools_get_access_plugins(); + $plugins = array(); + foreach ($all_plugins as $id => $plugin) { + if (!empty($plugin['required context']) && !ctools_context_match_requirements($contexts, $plugin['required context'])) { + continue; + } + $plugins[$id] = $plugin; + } + + return $plugins; +} + +/** + * Create a context for the logged in user. + */ +function ctools_access_get_loggedin_context() { + $context = ctools_context_create('entity:user', array('type' => 'current'), TRUE); + $context->identifier = t('Logged in user'); + $context->keyword = 'viewer'; + $context->id = 0; + + return $context; +} + +/** + * Get a summary of an access plugin's settings. + */ +function ctools_access_summary($plugin, $contexts, $test) { + if (!isset($contexts['logged-in-user'])) { + $contexts['logged-in-user'] = ctools_access_get_loggedin_context(); + } + + $description = ''; + if ($function = ctools_plugin_get_function($plugin, 'summary')) { + $required_context = isset($plugin['required context']) ? $plugin['required context'] : array(); + $context = isset($test['context']) ? $test['context'] : array(); + $description = $function($test['settings'], ctools_context_select($contexts, $required_context, $context), $plugin); + } + + if (!empty($test['not'])) { + $description = "NOT ($description)"; + } + + return $description; +} + +/** + * Get a summary of a group of access plugin's settings. + */ +function ctools_access_group_summary($access, $contexts) { + if (empty($access['plugins'])) { + return; + } + + $descriptions = array(); + foreach ($access['plugins'] as $id => $test) { + $plugin = ctools_get_access_plugin($test['name']); + $descriptions[] = ctools_access_summary($plugin, $contexts, $test); + } + + $separator = (isset($access['logic']) && $access['logic'] == 'and') ? t(', and ') : t(', or '); + return implode($separator, $descriptions); +} + +/** + * Determine if the current user has access via plugin. + * + * @param $settings + * An array of settings theoretically set by the user. + * @param $contexts + * An array of zero or more contexts that may be used to determine if + * the user has access. + * + * @return + * TRUE if access is granted, false if otherwise. + */ +function ctools_access($settings, $contexts = array()) { + if (empty($settings['plugins'])) { + return TRUE; + } + + if (!isset($settings['logic'])) { + $settings['logic'] = 'and'; + } + + if (!isset($contexts['logged-in-user'])) { + $contexts['logged-in-user'] = ctools_access_get_loggedin_context(); + } + + foreach ($settings['plugins'] as $test) { + $pass = FALSE; + $plugin = ctools_get_access_plugin($test['name']); + if ($plugin && $function = ctools_plugin_get_function($plugin, 'callback')) { + // Do we need just some contexts or all of them? + if (!empty($plugin['all contexts'])) { + $test_contexts = $contexts; + } + else { + $required_context = isset($plugin['required context']) ? $plugin['required context'] : array(); + $context = isset($test['context']) ? $test['context'] : array(); + $test_contexts = ctools_context_select($contexts, $required_context, $context); + } + + $pass = $function($test['settings'], $test_contexts, $plugin); + if (!empty($test['not'])) { + $pass = !$pass; + } + } + + if ($pass && $settings['logic'] == 'or') { + // Pass if 'or' and this rule passed. + return TRUE; + } + else if (!$pass && $settings['logic'] == 'and') { + // Fail if 'and' and this rule failed. + return FALSE; + } + } + + // Return TRUE if logic was and, meaning all rules passed. + // Return FALSE if logic was or, meaning no rule passed. + return $settings['logic'] == 'and'; +} + +/** + * Create default settings for a new access plugin. + * + * @param $plugin + * The access plugin being used. + * + * @return + * A default configured test that should be placed in $access['plugins']; + */ +function ctools_access_new_test($plugin) { + $test = array( + 'name' => $plugin['name'], + 'settings' => array(), + ); + + // Set up required context defaults. + if (isset($plugin['required context'])) { + if (is_object($plugin['required context'])) { + $test['context'] = ''; + } + else { + $test['context'] = array(); + foreach ($plugin['required context'] as $required) { + $test['context'][] = ''; + } + } + } + + + $default = NULL; + if (isset($plugin['default'])) { + $default = $plugin['default']; + } + elseif (isset($plugin['defaults'])) { + $default = $plugin['defaults']; + } + + // Setup plugin defaults. + if (isset($default)) { + if (is_array($default)) { + $test['settings'] = $default; + } + else if (function_exists($default)) { + $test['settings'] = $default(); + } + else { + $test['settings'] = array(); + } + } + + return $test; +} + +/** + * Apply restrictions to contexts based upon the access control configured. + * + * These restrictions allow the UI to not show content that may not + * be relevant to all types of a particular context. + */ +function ctools_access_add_restrictions($settings, $contexts) { + if (empty($settings['plugins'])) { + return; + } + + if (!isset($settings['logic'])) { + $settings['logic'] = 'and'; + } + + // We're not going to try to figure out restrictions on the or. + if ($settings['logic'] == 'or' && count($settings['plugins']) > 1) { + return; + } + + foreach ($settings['plugins'] as $test) { + $plugin = ctools_get_access_plugin($test['name']); + if ($plugin && $function = ctools_plugin_get_function($plugin, 'restrictions')) { + $required_context = isset($plugin['required context']) ? $plugin['required context'] : array(); + $context = isset($test['context']) ? $test['context'] : array(); + $contexts = ctools_context_select($contexts, $required_context, $context); + if ($contexts !== FALSE) { + $function($test['settings'], $contexts); + } + } + } +} diff --git a/sites/all/modules/contrib/ctools/includes/context.menu.inc b/sites/all/modules/contrib/ctools/includes/context.menu.inc new file mode 100644 index 0000000..ee227cb --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/context.menu.inc @@ -0,0 +1,40 @@ + array('access content'), + 'type' => MENU_CALLBACK, + 'file' => 'includes/context-admin.inc', + 'theme callback' => 'ajax_base_page_theme', + ); + $items['ctools/context/ajax/add'] = array( + 'page callback' => 'ctools_context_ajax_item_add', + ) + $base; + $items['ctools/context/ajax/configure'] = array( + 'page callback' => 'ctools_context_ajax_item_edit', + ) + $base; + $items['ctools/context/ajax/delete'] = array( + 'page callback' => 'ctools_context_ajax_item_delete', + ) + $base; + + // For the access system + $base['file'] = 'includes/context-access-admin.inc'; + $items['ctools/context/ajax/access/add'] = array( + 'page callback' => 'ctools_access_ajax_add', + ) + $base; + $items['ctools/context/ajax/access/configure'] = array( + 'page callback' => 'ctools_access_ajax_edit', + ) + $base; + $items['ctools/context/ajax/access/delete'] = array( + 'page callback' => 'ctools_access_ajax_delete', + ) + $base; + +} diff --git a/sites/all/modules/contrib/ctools/includes/context.plugin-type.inc b/sites/all/modules/contrib/ctools/includes/context.plugin-type.inc new file mode 100644 index 0000000..866def2 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/context.plugin-type.inc @@ -0,0 +1,24 @@ + TRUE, + ); + $items['arguments'] = array( + 'child plugins' => TRUE, + ); + $items['relationships'] = array( + 'child plugins' => TRUE, + ); + $items['access'] = array( + 'child plugins' => TRUE, + ); +} diff --git a/sites/all/modules/contrib/ctools/includes/context.theme.inc b/sites/all/modules/contrib/ctools/includes/context.theme.inc new file mode 100644 index 0000000..8f660b8 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/context.theme.inc @@ -0,0 +1,344 @@ + array('object' => NULL), + 'file' => 'includes/context.theme.inc', + ); + $theme['ctools_context_list_no_table'] = array( + 'variables' => array('object' => NULL), + 'file' => 'includes/context.theme.inc', + ); + $theme['ctools_context_item_form'] = array( + 'render element' => 'form', +// 'variables' => array('form' => NULL), + 'file' => 'includes/context.theme.inc', + ); + $theme['ctools_context_item_row'] = array( + 'variables' => array('type' => NULL, 'form' => NULL, 'position' => NULL, 'count' => NULL, 'with_tr' => TRUE), + 'file' => 'includes/context.theme.inc', + ); + + // For the access plugin + $theme['ctools_access_admin_add'] = array( + 'render element' => 'form', + 'file' => 'includes/context-access-admin.inc', + ); +} + +/** + * Theme the form item for the context entry. + */ +function theme_ctools_context_item_row($vars) { + $type = $vars['type']; + $form = $vars['form']; + $position = $vars['position']; + $count = $vars['count']; + $with_tr = $vars['with_tr']; + $output = ' ' . render($form['title']) . ''; + if (!empty($form['position'])) { + $output .= ' ' . render($form['position']) . ''; + } + $output .= '' . render($form['settings']); + $output .= render($form['remove']) . ''; + + if ($with_tr) { + $output = '' . $output . ''; + } + return $output; +} + +/** + * Display the context item. + */ +function theme_ctools_context_item_form($vars) { + $form = $vars['form']; + + $output = ''; + $type = $form['#ctools_context_type']; + $module = $form['#ctools_context_module']; + $cache_key = $form['#cache_key']; + + $type_info = ctools_context_info($type); + + if (!empty($form[$type]) && empty($form['#only_buttons'])) { + $count = 0; + $rows = ''; + foreach (array_keys($form[$type]) as $id) { + if (!is_numeric($id)) { + continue; + } + $theme_vars = array(); + $theme_vars['type'] = $type; + $theme_vars['form'] = $form[$type][$id]; + $theme_vars['position'] = $id; + $theme_vars['count'] = $count++; + $rows .= theme('ctools_context_item_row', $theme_vars); + } + + $output .= ''; + $output .= ''; + $output .= ''; + $output .= ''; + if (!empty($type_info['sortable']) && $count) { + $output .= ''; + } + $output .= ''; + $output .= ''; + $output .= ''; + $output .= ''; + + $output .= $rows; + + $output .= ''; + $output .= '
' . $type_info['title'] . '' . t('Weight') . '' . t('Operation') . '
'; + } + + if (!empty($form['buttons'])) { + // Display the add context item. + $row = array(); + $row[] = array('data' => render($form['buttons'][$type]['item']), 'class' => array('title')); + $row[] = array('data' => render($form['buttons'][$type]['add']), 'class' => array('add'), 'width' => "60%"); + $output .= '
'; + $output .= render($form['buttons'][$type]); + $theme_vars = array(); + $theme_vars['header'] = array(); + $theme_vars['rows'] = array($row); + $theme_vars['attributes'] = array('id' => $type . '-add-table'); + $output .= theme('table', $theme_vars); + $output .= '
'; + } + if (!empty($form['description'])) { + $output .= render($form['description']); + } + + if (!empty($type_info['sortable'])) { + drupal_add_tabledrag($type . '-table', 'order', 'sibling', 'drag-position'); + } + + return $output; +} + +/** + * Create a visible list of all the contexts available on an object. + * Assumes arguments, relationships and context objects. + * + * Contexts must be preloaded. + */ +function theme_ctools_context_list($vars) { + $object = $vars['object']; + $header = $vars['header']; + $description = (!empty($vars['description'])) ? $vars['description'] : NULL; + $titles = array(); + $output = ''; + $count = 1; + + $contexts = ctools_context_load_contexts($object); + + // Describe 'built in' contexts. + if (!empty($object->base_contexts)) { + foreach ($object->base_contexts as $id => $context) { + $output .= ''; + $output .= '' . t('Built in context') . ''; + $desc = check_plain($context->identifier); + if (isset($context->keyword)) { + $desc .= '
' . t('Keyword: %@keyword', array('@keyword' => $context->keyword)); + foreach (ctools_context_get_converters('%' . $context->keyword . ':', $context) as $keyword => $title) { + $desc .= '
' . t('@keyword --> @title', array('@keyword' => $keyword, '@title' => $title)); + } + $desc .= '
'; + + } + if (isset($context->description)) { + $desc .= '
' . filter_xss_admin($context->description) . '
'; + } + $output .= '' . $desc . ''; + $output .= ''; + $titles[$id] = $context->identifier; + } + } + + // First, make a list of arguments. Arguments are pretty simple. + if (!empty($object->arguments)) { + foreach ($object->arguments as $argument) { + $output .= ''; + $output .= '' . t('Argument @count', array('@count' => $count)) . ''; + $desc = check_plain($argument['identifier']); + if (isset($argument['keyword'])) { + $desc .= '
' . t('Keyword: %@keyword', array('@keyword' => $argument['keyword'])); + if (isset($contexts[ctools_context_id($argument, 'argument')])) { + foreach (ctools_context_get_converters('%' . $argument['keyword'] . ':', $contexts[ctools_context_id($argument, 'argument')]) as $keyword => $title) { + $desc .= '
' . t('@keyword --> @title', array('@keyword' => $keyword, '@title' => $title)); + } + } + $desc .= '
'; + } + $output .= '' . $desc . ''; + $output .= ''; + $titles[ctools_context_id($argument, 'argument')] = $argument['identifier']; + $count++; + } + } + + $count = 1; + // Then, make a nice list of contexts. + if (!empty($object->contexts)) { + foreach ($object->contexts as $context) { + $output .= ''; + $output .= '' . t('Context @count', array('@count' => $count)) . ''; + $desc = check_plain($context['identifier']); + if (isset($context['keyword'])) { + $desc .= '
' . t('Keyword: %@keyword', array('@keyword' => $context['keyword'])); + foreach (ctools_context_get_converters('%' . $context['keyword'] . ':', $contexts[ctools_context_id($context, 'context')]) as $keyword => $title) { + $desc .= '
' . t('@keyword --> @title', array('@keyword' => $keyword, '@title' => $title)); + } + $desc .= '
'; + } + $output .= '' . $desc . ''; + $output .= ''; + $titles[ctools_context_id($context)] = $context['identifier']; + $count++; + } + } + + // And relationships + if (!empty($object->relationships)) { + foreach ($object->relationships as $relationship) { + $output .= ''; + if (is_array($relationship['context'])) { + $rtitles = array(); + foreach ($relationship['context'] as $cid) { + $rtitles[$cid] = $titles[$cid]; + } + $title = implode(' + ', $rtitles); + } + else { + $title = $titles[$relationship['context']]; + } + $output .= '' . t('From "@title"', array('@title' => $title)) . ''; + $desc = check_plain($relationship['identifier']); + if (isset($relationship['keyword'])) { + $desc .= '
' . t('Keyword: %@keyword', array('@keyword' => $relationship['keyword'])); + foreach (ctools_context_get_converters('%' . $relationship['keyword'] . ':', $contexts[ctools_context_id($relationship, 'relationship')]) as $keyword => $title) { + $desc .= '
' . t('@keyword --> @title', array('@keyword' => $keyword, '@title' => $title)); + } + $desc .= '
'; + } + $output .= '' . $desc . ''; + $output .= ''; + $titles[ctools_context_id($relationship, 'relationship')] = $relationship['identifier']; + $count++; + } + } + + $head = ''; + if ($header) { + if ($description) { + $header .= '
' . $description . '
'; + } + $head .= ''; + $head .= '' . $header . ''; + $head .= ''; + } + + return $output ? "$head$output
\n" : "$head
\n"; +} + +/** + * ctools_context_list() but not in a table format because tabledrag + * won't let us have tables within tables and still drag. + */ +function theme_ctools_context_list_no_table($vars) { + $object = $vars['object']; + ctools_add_css('context'); + $titles = array(); + $output = ''; + $count = 1; + // Describe 'built in' contexts. + if (!empty($object->base_contexts)) { + foreach ($object->base_contexts as $id => $context) { + $output .= '
'; + $output .= '
' . t('Built in context') . '
'; + $desc = check_plain($context->identifier); + if (isset($context->keyword)) { + $desc .= '
' . t('Keyword: %@keyword', array('@keyword' => $context->keyword)) . '
'; + } + if (isset($context->description)) { + $desc .= '
' . filter_xss_admin($context->description) . '
'; + } + $output .= '
' . $desc . '
'; + $output .= '
'; + $titles[$id] = $context->identifier; + $count++; + } + } + + // First, make a list of arguments. Arguments are pretty simple. + if (!empty($object->arguments)) { + foreach ($object->arguments as $argument) { + $output .= '
'; + $output .= '
' . t('Argument @count', array('@count' => $count)) . '
'; + $desc = check_plain($argument['identifier']); + if (isset($argument['keyword'])) { + $desc .= '
' . t('Keyword: %@keyword', array('@keyword' => $argument['keyword'])) . '
'; + } + $output .= '
' . $desc . '
'; + $output .= '
'; + $titles[ctools_context_id($argument, 'argument')] = $argument['identifier']; + $count++; + } + } + $count = 1; + // Then, make a nice list of contexts. + if (!empty($object->contexts)) { + foreach ($object->contexts as $context) { + $output .= '
'; + $output .= '
' . t('Context @count', array('@count' => $count)) . '
'; + $desc = check_plain($context['identifier']); + if (isset($context['keyword'])) { + $desc .= '
' . t('Keyword: %@keyword', array('@keyword' => $context['keyword'])) . '
'; + } + $output .= '
' . $desc . '
'; + $output .= '
'; + $titles[ctools_context_id($context)] = $context['identifier']; + $count++; + } + } + // And relationships + if (!empty($object->relationships)) { + foreach ($object->relationships as $relationship) { + $output .= '
'; + if (is_array($relationship['context'])) { + $rtitles = array(); + foreach ($relationship['context'] as $cid) { + $rtitles[$cid] = $titles[$cid]; + } + $title = implode(' + ', $rtitles); + } + else { + $title = $titles[$relationship['context']]; + } + + $output .= '
' . t('From "@title"', array('@title' => $title)) . '
'; + $desc = check_plain($relationship['identifier']); + if (isset($relationship['keyword'])) { + $desc .= '
' . t('Keyword: %@keyword', array('@keyword' => $relationship['keyword'])) . '
'; + } + $output .= '
' . $desc . '
'; + $output .= '
'; + $titles[ctools_context_id($relationship, 'relationship')] = $relationship['identifier']; + $count++; + } + } + + return $output; +} + diff --git a/sites/all/modules/contrib/ctools/includes/css-cache.inc b/sites/all/modules/contrib/ctools/includes/css-cache.inc new file mode 100644 index 0000000..d88160b --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/css-cache.inc @@ -0,0 +1,52 @@ + $id))->fetchField(); + if ($filename && file_exists($filename)) { + file_unmanaged_delete($filename); + } + // Remove any previous records. + db_delete('ctools_css_cache') + ->condition('cid', $id) + ->execute(); + + $filename = ctools_css_cache($css, $filter); + + db_merge('ctools_css_cache') + ->key(array('cid' => $id)) + ->fields(array( + 'filename' => $filename, + 'css' => $css, + 'filter' => intval($filter), + )) + ->execute(); + + return $filename; +} + +/** + * Retrieve a filename associated with an id of previously cached CSS. + * + * This will ensure the file still exists and, if not, create it. + */ +function ctools_css_retrieve($id) { + $cache = db_query('SELECT * FROM {ctools_css_cache} WHERE cid = :cid', array(':cid' => $id))->fetchObject(); + if (!$cache) { + return; + } + + if (!file_exists($cache->filename)) { + $filename = ctools_css_cache($cache->css, $cache->filter); + if ($filename != $cache->filename) { + db_update('ctools_css_cache') + ->fields(array('filename' => $filename)) + ->condition('cid', $id) + ->execute(); + $cache->filename = $filename; + } + } + + return $cache->filename; +} + +/** + * Remove stored CSS and any associated file. + */ +function ctools_css_clear($id) { + $cache = db_query('SELECT * FROM {ctools_css_cache} WHERE cid = :cid', array(':cid' => $id))->fetchObject(); + if (!$cache) { + return; + } + + if (file_exists($cache->filename)) { + file_unmanaged_delete($cache->filename); + // If we remove an existing file, there may be cached pages that refer + // to it. We must get rid of them: FIXME same format in D7? + cache_clear_all(); + } + + db_delete('ctools_css_cache') + ->condition('cid', $id) + ->execute(); +} + +/** + * Write a chunk of CSS to a temporary cache file and return the file name. + * + * This function optionally filters the CSS (always compressed, if so) and + * generates a unique filename based upon md5. It returns that filename that + * can be used with drupal_add_css(). Note that as a cache file, technically + * this file is volatile so it should be checked before it is used to ensure + * that it exists. + * + * You can use file_exists() to test for the file and file_delete() to remove + * it if it needs to be cleared. + * + * @param $css + * A chunk of well-formed CSS text to cache. + * @param $filter + * If TRUE the css will be filtered. If FALSE the text will be cached + * as-is. + * + * @return $filename + * The filename the CSS will be cached in. + */ +function ctools_css_cache($css, $filter = TRUE) { + if ($filter) { + $css = ctools_css_filter($css); + } + + // Create the css/ within the files folder. + $path = 'public://ctools/css'; + if (!file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { +// if (!file_prepare_directory($path, FILE_CREATE_DIRECTORY)) { + drupal_set_message(t('Unable to create CTools CSS cache directory. Check the permissions on your files directory.'), 'error'); + return; + } + + // @todo Is this slow? Does it matter if it is? + $filename = $path . '/' . md5($css) . '.css'; + + // Generally md5 is considered unique enough to sign file downloads. + // So this replaces already existing files based on the assumption that two + // files with the same hash are identical content wise. + // If we rename, the cache folder can potentially fill up with thousands of + // files with the same content. + $filename = file_unmanaged_save_data($css, $filename, FILE_EXISTS_REPLACE); + + return $filename; +} + +/** + * Filter a chunk of CSS text. + * + * This function disassembles the CSS into a raw format that makes it easier + * for our tool to work, then runs it through the filter and reassembles it. + * If you find that you want the raw data for some reason or another, you + * can use the disassemble/assemble functions yourself. + * + * @param $css + * The CSS text to filter. + * @param $compressed + * If true, generate compressed output; if false, generate pretty output. + * Defaults to TRUE. + */ +function ctools_css_filter($css, $compressed = TRUE) { + $css_data = ctools_css_disassemble($css); + + // Note: By using this function yourself you can control the allowed + // properties and values list. + $filtered = ctools_css_filter_css_data($css_data); + + return $compressed ? ctools_css_compress($filtered) : ctools_css_assemble($filtered); +} + +/** + * Re-assemble a css string and format it nicely. + * + * @param array $css_data + * An array of css data, as produced by @see ctools_css_disassemble() + * disassembler and the @see ctools_css_filter_css_data() filter. + * + * @return string $css + * css optimized for human viewing. + */ +function ctools_css_assemble($css_data) { + // Initialize the output. + $css = ''; + // Iterate through all the statements. + foreach ($css_data as $selector_str => $declaration) { + // Add the selectors, separating them with commas and line feeds. + $css .= strpos($selector_str, ',') === FALSE ? $selector_str : str_replace(", ", ",\n", $selector_str); + // Add the opening curly brace. + $css .= " {\n"; + // Iterate through all the declarations. + foreach ($declaration as $property => $value) { + $css .= " " . $property . ": " . $value . ";\n"; + } + // Add the closing curly brace. + $css .= "}\n\n"; + } + // Return the output. + return $css; +} + +/** + * Compress css data (filter it first!) to optimize for use on view. + * + * @param array $css_data + * An array of css data, as produced by @see ctools_css_disassemble() + * disassembler and the @see ctools_css_filter_css_data() filter. + * + * @return string $css + * css optimized for use. + */ +function ctools_css_compress($css_data) { + // Initialize the output. + $css = ''; + // Iterate through all the statements. + foreach ($css_data as $selector_str => $declaration) { + if (empty($declaration)) { + // Skip this statement if filtering removed all parts of the declaration. + continue; + } + // Add the selectors, separating them with commas. + $css .= $selector_str; + // And, the opening curly brace. + $css .= "{"; + // Iterate through all the statement properties. + foreach ($declaration as $property => $value) { + $css .= $property . ':' . $value . ';'; + } + // Add the closing curly brace. + $css .= "}"; + } + // Return the output. + return $css; +} + +/** + * Disassemble the css string. + * + * Strip the css of irrelevant characters, invalid/malformed selectors and + * declarations, and otherwise prepare it for processing. + * + * @param string $css + * A string containing the css to be disassembled. + * + * @return array $disassembled_css + * An array of disassembled, slightly cleaned-up/formatted css statements. + */ +function ctools_css_disassemble($css) { + $disassembled_css = array(); + // Remove comments. + $css = preg_replace("/\/\*(.*)?\*\//Usi", "", $css); + // Split out each statement. Match either a right curly brace or a semi-colon + // that precedes a left curly brace with no right curly brace separating them. + $statements = preg_split('/}|;(?=[^}]*{)/', $css); + + // If we have any statements, parse them. + if (!empty($statements)) { + // Iterate through all of the statements. + foreach ($statements as $statement) { + // Get the selector(s) and declaration. + if (empty($statement) || !strpos($statement, '{')) { + continue; + } + + list($selector_str, $declaration) = explode('{', $statement); + + // If the selector exists, then disassemble it, check it, and regenerate + // the selector string. + $selector_str = empty($selector_str) ? FALSE : _ctools_css_disassemble_selector($selector_str); + if (empty($selector_str)) { + // No valid selectors. Bomb out and start the next item. + continue; + } + + // Disassemble the declaration, check it and tuck it into an array. + if (!isset($disassembled_css[$selector_str])) { + $disassembled_css[$selector_str] = array(); + } + $disassembled_css[$selector_str] += _ctools_css_disassemble_declaration($declaration); + } + } + return $disassembled_css; +} + +function _ctools_css_disassemble_selector($selector_str) { + // Get all selectors individually. + $selectors = explode(",", trim($selector_str)); + // Iterate through all the selectors, sanity check them and return if they + // pass. Note that this handles 0, 1, or more valid selectors gracefully. + foreach ($selectors as $key => $selector) { + // Replace un-needed characters and do a little cleanup. + $selector = preg_replace("/[\n|\t|\\|\s]+/", ' ', trim($selector)); + // Make sure this is still a real selector after cleanup. + if (!empty($selector)) { + $selectors[$key] = $selector; + } + else { + // Selector is no good, so we scrap it. + unset($selectors[$key]); + } + } + // Check for malformed selectors; if found, we skip this declaration. + if (empty($selectors)) { + return FALSE; + } + return implode(', ', $selectors); +} + +function _ctools_css_disassemble_declaration($declaration) { + $formatted_statement = array(); + $propval_pairs = explode(";", $declaration); + // Make sure we actually have some properties to work with. + if (!empty($propval_pairs)) { + // Iterate through the remains and parse them. + foreach ($propval_pairs as $key => $propval_pair) { + // Check that we have a ':', otherwise it's an invalid pair. + if (strpos($propval_pair, ':') === FALSE) { + continue; + } + // Clean up the current property-value pair. + $propval_pair = preg_replace("/[\n|\t|\\|\s]+/", ' ', trim($propval_pair)); + // Explode the remaining fragements some more, but clean them up first. + list($property, $value) = explode(':', $propval_pair, 2); + // If the property survived, toss it onto the stack. + if (!empty($property)) { + $formatted_statement[trim($property)] = trim($value); + } + } + } + return $formatted_statement; +} + +/** + * Run disassembled $css through the filter. + * + * @param $css + * CSS code disassembled by ctools_dss_disassemble(). + * @param $allowed_properties + * A list of properties that are allowed by the filter. If empty + * ctools_css_filter_default_allowed_properties() will provide the + * list. + * @param $allowed_values + * A list of values that are allowed by the filter. If empty + * ctools_css_filter_default_allowed_values() will provide the + * list. + * + * @return + * An array of disassembled, filtered CSS. + */ +function ctools_css_filter_css_data($css, $allowed_properties = array(), $allowed_values = array(), $allowed_values_regex = '', $disallowed_values_regex = '') { +//function ctools_css_filter_css_data($css, &$filtered = NULL, $allowed_properties = array(), $allowed_values = array(), $allowed_values_regex = '', $disallowed_values_regex = '') { + // Retrieve the default list of allowed properties if none is provided. + $allowed_properties = !empty($allowed_properties) ? $allowed_properties : ctools_css_filter_default_allowed_properties(); + // Retrieve the default list of allowed values if none is provided. + $allowed_values = !empty($allowed_values) ? $allowed_values : ctools_css_filter_default_allowed_values(); + // Define allowed values regex if none is provided. + $allowed_values_regex = !empty($allowed_values_regex) ? $allowed_values_regex : '/(#[0-9a-f]+|rgb\(\d+%?,\d*%?,?\d*%?\)?|\d{0,2}\.?\d{0,2}(cm|em|ex|in|mm|pc|pt|px|%|,|\))?)/'; + // Define disallowed url() value contents, if none is provided. + // $disallowed_values_regex = !empty($disallowed_values_regex) ? $disallowed_values_regex : '/[url|expression]\s*\(\s*[^\s)]+?\s*\)\s*/'; + $disallowed_values_regex = !empty($disallowed_values_regex) ? $disallowed_values_regex : '/(url|expression)/'; + + foreach ($css as $selector_str => $declaration) { + foreach ($declaration as $property => $value) { + if (!in_array($property, $allowed_properties)) { + // $filtered['properties'][$selector_str][$property] = $value; + unset($css[$selector_str][$property]); + continue; + } + $value = str_replace('!important', '', $value); + if (preg_match($disallowed_values_regex, $value) || !(in_array($value, $allowed_values) || preg_match($allowed_values_regex, $value))) { + // $filtered['values'][$selector_str][$property] = $value; + unset($css[$selector_str][$property]); + continue; + } + } + } + return $css; +} + +/** + * Provide a deafult list of allowed properties by the filter. + */ +function ctools_css_filter_default_allowed_properties() { + return array( + 'azimuth', + 'background', + 'background-color', + 'background-image', + 'background-repeat', + 'background-attachment', + 'background-position', + 'border', + 'border-top-width', + 'border-right-width', + 'border-bottom-width', + 'border-left-width', + 'border-width', + 'border-top-color', + 'border-right-color', + 'border-bottom-color', + 'border-left-color', + 'border-color', + 'border-top-style', + 'border-right-style', + 'border-bottom-style', + 'border-left-style', + 'border-style', + 'border-top', + 'border-right', + 'border-bottom', + 'border-left', + 'clear', + 'color', + 'cursor', + 'direction', + 'display', + 'elevation', + 'float', + 'font', + 'font-family', + 'font-size', + 'font-style', + 'font-variant', + 'font-weight', + 'height', + 'letter-spacing', + 'line-height', + 'margin', + 'margin-top', + 'margin-right', + 'margin-bottom', + 'margin-left', + 'overflow', + 'padding', + 'padding-top', + 'padding-right', + 'padding-bottom', + 'padding-left', + 'pause', + 'pause-after', + 'pause-before', + 'pitch', + 'pitch-range', + 'richness', + 'speak', + 'speak-header', + 'speak-numeral', + 'speak-punctuation', + 'speech-rate', + 'stress', + 'text-align', + 'text-decoration', + 'text-indent', + 'text-transform', + 'unicode-bidi', + 'vertical-align', + 'voice-family', + 'volume', + 'white-space', + 'width', + 'fill', + 'fill-opacity', + 'fill-rule', + 'stroke', + 'stroke-width', + 'stroke-linecap', + 'stroke-linejoin', + 'stroke-opacity', + ); +} + +/** + * Provide a default list of allowed values by the filter. + */ +function ctools_css_filter_default_allowed_values() { + return array( + 'auto', + 'aqua', + 'black', + 'block', + 'blue', + 'bold', + 'both', + 'bottom', + 'brown', + 'capitalize', + 'center', + 'collapse', + 'dashed', + 'dotted', + 'fuchsia', + 'gray', + 'green', + 'italic', + 'inherit', + 'left', + 'lime', + 'lowercase', + 'maroon', + 'medium', + 'navy', + 'normal', + 'nowrap', + 'olive', + 'pointer', + 'purple', + 'red', + 'right', + 'solid', + 'silver', + 'teal', + 'top', + 'transparent', + 'underline', + 'uppercase', + 'white', + 'yellow', + ); +} + +/** + * Delegated implementation of hook_flush_caches() + */ +function ctools_css_flush_caches() { + // Remove all generated files. + // @see http://drupal.org/node/573292 + // file_unmanaged_delete_recursive('public://render'); + $filedir = file_default_scheme() . '://ctools/css'; + if (drupal_realpath($filedir) && file_exists($filedir)) { + // We use the @ because it's possible that files created by the webserver + // cannot be deleted while using drush to clear the cache. We don't really + // care that much about that, to be honest, so we use the @ to suppress + // the error message. + @file_unmanaged_delete_recursive($filedir); + } + + db_delete('ctools_css_cache')->execute(); +} diff --git a/sites/all/modules/contrib/ctools/includes/dependent.inc b/sites/all/modules/contrib/ctools/includes/dependent.inc new file mode 100644 index 0000000..74de919 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/dependent.inc @@ -0,0 +1,181 @@ + TRUE to the + * fieldset works around that. + * + * For radios, because they are selected a little bit differently, instead of + * using the CSS id, use: radio:NAME where NAME is the #name of the property. + * This can be quickly found by looking at the HTML of the generated form, but + * it is usually derived from the array which contains the item. For example, + * $form['menu']['type'] would have a name of menu[type]. This name is the same + * field that is used to determine where in $form_state['values'] you will find + * the value of the form. + * + * The item that is dependent on, should be set to #tree = TRUE. + * + * Usage: + * + * First, ensure this tool is loaded: + * @code { ctools_include('dependent'); } + * + * On any form item, add + * - @code '#dependency' => array('id-of-form-without-the-#' => array(list, of, values, that, make, this, gadget, visible)), @endcode + * + * A fuller example, that hides the menu title when no menu is selected: + * @code + *function ctools_dependent_example() { + * $form = array(); + * $form['menu'] = array( + * '#type' => 'fieldset', + * '#title' => t('Menu settings'), + * '#tree' => TRUE, + * ); + * $form['menu']['type'] = array( + * '#title' => t('Menu type'), + * '#type' => 'radios', + * '#options' => array( + * 'none' => t('No menu entry'), + * 'normal' => t('Normal menu entry'), + * 'tab' => t('Menu tab'), + * 'default tab' => t('Default menu tab'), + * ), + * '#default_value' => 'none', + * ); + * + * $form['menu']['title'] = array( + * '#title' => t('Title'), + * '#type' => 'textfield', + * '#default_value' => '', + * '#description' => t('If set to normal or tab, enter the text to use for the menu item.'), + * '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab')), + * ); + * + * return system_settings_form($form); + *} + * @endcode + * + * An example for hiding checkboxes using #prefix and #suffix: + * @code + *function ctools_dependent_example_checkbox() { + * $form = array(); + * $form['object'] = array( + * '#type' => 'fieldset', + * '#title' => t('Select object type'), + * '#tree' => TRUE, + * ); + * $form['object']['type'] = array( + * '#title' => t('Object type'), + * '#type' => 'radios', + * '#options' => array( + * 'view' => t('View'), + * 'node' => t('Node'), + * 'field' => t('Field'), + * 'term' => t('Term'), + * ), + * '#default_value' => 'view', + * ); + * + * $form['object']['elements'] = array( + * '#title' => t('Select the elements to load from the node.'), + * '#type' => 'checkboxes', + * '#prefix' => '
', + * '#suffix' => '
', + * '#dependency' => array('radio:menu[type]' => array('node')), + * '#options' => array( + * 'body' => t('Body'), + * 'fields' => t('Fields'), + * 'taxonomy' => t('Taxonomy'), + * ), + * '#default_value' => array('body', 'fields'), + * ); + * + * return system_settings_form($form); + *} + * @endcode + * + * Deprecated: + * + * You no longer use ctools_dependent_process(), and it should be removed + * completely. + * + * If you have a form element which isn't listed in ctools_dependent_element_info_alter + * you have to add [#pre_render'][] => 'ctools_dependent_pre_render' to your form. + */ + +/** + * Process callback to add dependency to form items. + * + */ +function ctools_dependent_process($element, &$form_state, &$form) { + return $element; +} + +function ctools_dependent_pre_render($element) { + // Preprocess only items with #dependency set. + if (isset($element['#dependency'])) { + if (!isset($element['#dependency_count'])) { + $element['#dependency_count'] = 1; + } + if (!isset($element['#dependency_type'])) { + $element['#dependency_type'] = 'hide'; + } + + $js = array( + 'values' => $element['#dependency'], + 'num' => $element['#dependency_count'], + 'type' => $element['#dependency_type'], + ); + + // Add a additional wrapper id around fieldsets, textareas to support depedency on it. + if (in_array($element['#type'], array('textarea', 'fieldset', 'text_format'))) { + $element['#theme_wrappers'][] = 'container'; + $element['#attributes']['id'] = $element['#id'] . '-wrapper'; + } + + // Text formats need to unset the dependency on the textarea + // or it gets applied twice. + if ($element['#type'] == 'text_format') { + unset($element['value']['#dependency']); + } + + $element['#attached']['js'][] = ctools_attach_js('dependent'); + $options['CTools']['dependent'][$element['#id']] = $js; + $element['#attached']['js'][] = array('type' => 'setting', 'data' => $options); + + } + + return $element; +} + +/** + * CTools alters the element_info to be able to add #process functions to + * every major form element to make it much more handy to use #dependency, + * because you don't have to add #process. + */ +function ctools_dependent_element_info_alter(&$type) { + $form_elements = array('checkbox', 'checkboxes', 'date', 'fieldset', 'item', 'machine_name', 'markup', 'radio', 'radios', 'select', 'textarea', 'textfield', 'text_format'); + foreach ($form_elements as $element) { + $type[$element]['#pre_render'][] = 'ctools_dependent_pre_render'; + } +} diff --git a/sites/all/modules/contrib/ctools/includes/dropbutton.theme.inc b/sites/all/modules/contrib/ctools/includes/dropbutton.theme.inc new file mode 100644 index 0000000..fcdd5a3 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/dropbutton.theme.inc @@ -0,0 +1,143 @@ + .5 seconds, or can be immediately closed by + * clicking the twisty again. The code is smart enough that if the mouse + * moves away and then back within the .5 second window, it will not + * re-close. + * + * Multiple dropbuttons can be placed per page. + * + * If only one link is passed to the theme function, the function will render + * a ctools-button with no twisty. The twisty is only rendered when 2 or more + * links are provided. The wrapping element will be classed with both + * ctools-button and ctools-dropbutton when a dropbutton is rendered. + * + * If the user does not have javascript enabled, the link will not appear, + * and instead by default the list of links will appear as a normal inline + * list. + * + * The menu is minimally styled by default, and to make it look different + * will require your own CSS. You can apply your own class to the + * dropbutton to help style it. + * + * The twisty is rendered as a border on a widthless and heightless element. + * There is no image for the twisty. + * The color of the twisty is the color of the link by default. To adjust the + * size of the twisty, adjust the border widths on .ctools-twisty. The adjust + * the color of the twisty, assign a new color to the .ctools-button class or + * assign a color to .ctools-twisty. You shouldn't need to adjust border-color. + * + * Use the theme() function to render dropbutton e.g. + * theme('links__ctools_dropbutton', array()) where array contains a renderable + * array of links. + */ + +/** + * Delegated implementation of hook_theme() + */ +function ctools_dropbutton_theme(&$items) { + $items['links__ctools_dropbutton'] = array( + 'variables' => array('title' => NULL, 'links' => NULL, 'image' => FALSE, 'class' => NULL), + 'file' => 'includes/dropbutton.theme.inc', + ); +} + +/** + * Create a dropbutton menu. + * + * @param $title + * The text to place in the clickable area to activate the dropbutton. This + * text is indented to -9999px by default. + * @param $links + * A list of links to provide within the dropbutton, suitable for use + * in via Drupal's theme('links'). + * @param $image + * If true, the dropbutton link is an image and will not get extra decorations + * that a text dropbutton link will. + * @param $class + * An optional class to add to the dropbutton's container div to allow you + * to style a single dropbutton however you like without interfering with + * other dropbuttons. + */ +function theme_links__ctools_dropbutton($vars) { + // Check to see if the number of links is greater than 1; + // otherwise just treat this like a button. + if (!empty($vars['links'])) { + $is_drop_button = (count($vars['links']) > 1); + + // Add needed files + if ($is_drop_button) { + ctools_add_js('dropbutton'); + ctools_add_css('dropbutton'); + } + ctools_add_css('button'); + + // Provide a unique identifier for every button on the page. + static $id = 0; + $id++; + + // Wrapping div + $class = 'ctools-no-js'; + $class .= ($is_drop_button) ? ' ctools-dropbutton' : ''; + $class .= ' ctools-button'; + if (!empty($vars['class'])) { + $class .= ($vars['class']) ? (' ' . implode(' ', $vars['class'])) : ''; + } + + $output = ''; + + $output .= '
'; + + // Add a twisty if this is a dropbutton + if ($is_drop_button) { + $vars['title'] = ($vars['title'] ? check_plain($vars['title']) : t('open')); + + $output .= ''; // ctools-link + } + + // The button content + $output .= '
'; + + // Check for attributes. theme_links expects an array(). + $vars['attributes'] = (!empty($vars['attributes'])) ? $vars['attributes'] : array(); + + // Remove the inline and links classes from links if they exist. + // These classes are added and styled by Drupal core and mess up the default + // styling of any link list. + if (!empty($vars['attributes']['class'])) { + $classes = $vars['attributes']['class']; + foreach ($classes as $key => $class) { + if ($class === 'inline' || $class === 'links') { + unset($vars['attributes']['class'][$key]); + } + } + } + + // Call theme_links to render the list of links. + $output .= theme_links(array('links' => $vars['links'], 'attributes' => $vars['attributes'], 'heading' => '')); + $output .= '
'; // ctools-content + $output .= '
'; // ctools-dropbutton + return $output; + } + else { + return ''; + } +} + diff --git a/sites/all/modules/contrib/ctools/includes/dropdown.theme.inc b/sites/all/modules/contrib/ctools/includes/dropdown.theme.inc new file mode 100644 index 0000000..7e748f5 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/dropdown.theme.inc @@ -0,0 +1,90 @@ + .5 seconds, or can be immediately closed by + * clicking the link again. The code is smart enough that if the mouse + * moves away and then back within the .5 second window, it will not + * re-close. + * + * Multiple dropdowns can be placed per page. + * + * If the user does not have javascript enabled, the link will not appear, + * and instead by default the list of links will appear as a normal inline + * list. + * + * The menu is heavily styled by default, and to make it look different + * will require a little bit of CSS. You can apply your own class to the + * dropdown to help ensure that your CSS can override the dropdown's CSS. + * + * In particular, the text, link, background and border colors may need to + * be changed. Please see dropdown.css for information about the existing + * styling. + */ + +/** + * Delegated implementation of hook_theme() + */ +function ctools_dropdown_theme(&$items) { + $items['ctools_dropdown'] = array( + 'variables' => array('title' => NULL, 'links' => NULL, 'image' => FALSE, 'class' => ''), + 'file' => 'includes/dropdown.theme.inc', + ); +} + +/** + * Create a dropdown menu. + * + * @param $title + * The text to place in the clickable area to activate the dropdown. + * @param $links + * A list of links to provide within the dropdown, suitable for use + * in via Drupal's theme('links'). + * @param $image + * If true, the dropdown link is an image and will not get extra decorations + * that a text dropdown link will. + * @param $class + * An optional class to add to the dropdown's container div to allow you + * to style a single dropdown however you like without interfering with + * other dropdowns. + */ +function theme_ctools_dropdown($vars) { + // Provide a unique identifier for every dropdown on the page. + static $id = 0; + $id++; + + $class = 'ctools-dropdown-no-js ctools-dropdown' . ($vars['class'] ? (' ' . $vars['class']) : ''); + + ctools_add_js('dropdown'); + ctools_add_css('dropdown'); + + $output = ''; + + $output .= '
'; + $output .= ''; // wrapper + $output .= '
'; + $output .= '
'; + $output .= theme_links(array('links' => $vars['links'], 'attributes' => array(), 'heading' => '')); + $output .= '
'; // container + $output .= '
'; // container wrapper + $output .= '
'; // dropdown + return $output; +} + diff --git a/sites/all/modules/contrib/ctools/includes/entity-access.inc b/sites/all/modules/contrib/ctools/includes/entity-access.inc new file mode 100644 index 0000000..972cf13 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/entity-access.inc @@ -0,0 +1,150 @@ +nid); + if ($node->vid !== $default_revision->vid) { + return _node_revision_access($node, $op, $account); + } + else { + return node_access($op, $node, $account); + } + } + // No node is provided. Check for access to all nodes. + if (user_access('bypass node access', $account)) { + return TRUE; + } + if (!user_access('access content', $account)) { + return FALSE; + } + if ($op == 'view' && node_access_view_all_nodes($account)) { + return TRUE; + } + return FALSE; +} + +/** + * Access callback for the user entity. + */ +function ctools_metadata_user_access($op, $entity = NULL, $account = NULL, $entity_type) { + $account = isset($account) ? $account : $GLOBALS['user']; + // Grant access to the users own user account and to the anonymous one. + if (isset($entity) && $op != 'delete' && (($entity->uid == $account->uid && $entity->uid) || (!$entity->uid && $op == 'view'))) { + return TRUE; + } + if (user_access('administer users', $account) || user_access('access user profiles', $account) && $op == 'view' && $entity->status) { + return TRUE; + } + return FALSE; +} + +/** + * Access callback for the comment entity. + */ +function ctools_metadata_comment_access($op, $entity = NULL, $account = NULL) { + // When determining access to a comment, if comment has an associated node, + // the user must be able to view the node in order to access the comment. + if (isset($entity->nid)) { + if (!node_access('view', node_load($entity->nid), $account)) { + return FALSE; + } + } + + // Comment administrators are allowed to perform all operations on all + // comments. + if (user_access('administer comments', $account)) { + return TRUE; + } + + // Unpublished comments can never be accessed by non-admins. + if (isset($entity->status) && $entity->status == COMMENT_NOT_PUBLISHED) { + return FALSE; + } + + if (user_access('access comments', $account) && $op == 'view') { + return TRUE; + } + return FALSE; +} + +/** + * Access callback for the taxonomy entities. + */ +function ctools_metadata_taxonomy_access($op, $entity = NULL, $account = NULL, $entity_type) { + if ($entity_type == 'taxonomy_vocabulary') { + return user_access('administer taxonomy', $account); + } + if (user_access('administer taxonomy', $account) || user_access('access content', $account) && $op == 'view') { + return TRUE; + } + return FALSE; +} diff --git a/sites/all/modules/contrib/ctools/includes/export-ui.inc b/sites/all/modules/contrib/ctools/includes/export-ui.inc new file mode 100644 index 0000000..16e57d6 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/export-ui.inc @@ -0,0 +1,475 @@ + TRUE, + 'title' => $plugin['name'], + 'export' => array(), + 'allowed operations' => array(), + 'menu' => array(), + 'redirect' => array(), + 'form' => array(), + 'strings' => array(), + 'list' => NULL, + 'access' => 'administer site configuration', + ); + + // Provide CRUD access defaults based on the base 'access' setting: + $plugin += array( + 'create access' => $plugin['access'], + 'delete access' => $plugin['access'], + ); + + if (empty($plugin['has menu'])) { + return; + } + + // The following keys are required and the plugin cannot be processed + // without them. + $keys = array( + 'title singular', + 'title plural', + 'title singular proper', + 'title plural proper', + 'schema', + ); + + foreach ($keys as $key) { + if (empty($plugin[$key])) { + drupal_set_message(t('The plugin definition of @plugin is missing the %key key.', array('%key' => $key, '@plugin' => $plugin['name'])), 'error'); + } + } + + // If we're on the modules page and building a menu, there is a design flaw + // in Drupal core that causes modules to be installed but the schema does + // not become available until AFTER menu rebuild. This helps smooth that + // out. This is a HACK but it should work: + $schema = ctools_export_get_schema($plugin['schema']); + + if (empty($schema)) { + // If we're updating the schema may not have been read yet, so don't report this error in that case. + if (!defined('MAINTENANCE_MODE')) { + drupal_set_message(t('The plugin definition of @plugin cannot locate schema %schema.', array('%schema' => $plugin['schema'], '@plugin' => $plugin['name'])), 'error'); + } + return; + } + + if (empty($schema['export'])) { + drupal_set_message(t('The plugin definition of @plugin uses %schema, but it has no export section.', array('%schema' => $plugin['schema'], '@plugin' => $plugin['name'])), 'error'); + return; + } + $plugin['export'] += $schema['export']; + + $plugin['export'] += array( + // Add the identifier key from the schema so we don't have to call + // ctools_export_get_schema() just for that. + 'key' => $schema['export']['key'], + ); + + // Add some default fields that appear often in exports + // If these use different keys they can easily be specified in the + // $plugin. + + if (empty($plugin['export']['admin_title']) && !empty($schema['fields']['admin_title'])) { + $plugin['export']['admin_title'] = 'admin_title'; + } + if (empty($plugin['export']['admin_description']) && !empty($schema['fields']['admin_description'])) { + $plugin['export']['admin_description'] = 'admin_description'; + } + + // Define allowed operations, and the name of the operations. + $plugin['allowed operations'] += array( + 'edit' => array('title' => t('Edit')), + 'enable' => array('title' => t('Enable'), 'ajax' => TRUE, 'token' => TRUE), + 'disable' => array('title' => t('Disable'), 'ajax' => TRUE, 'token' => TRUE), + 'revert' => array('title' => t('Revert')), + 'delete' => array('title' => t('Delete')), + 'clone' => array('title' => t('Clone')), + 'import' => array('title' => t('Import')), + 'export' => array('title' => t('Export')), + ); + + $plugin['menu'] += array( + 'menu item' => str_replace(' ', '-', $plugin['name']), + 'menu prefix' => 'admin/structure', + 'menu title' => $plugin['title'], + 'menu description' => '', + ); + $base_path = ctools_export_ui_plugin_base_path($plugin); + $prefix_count = count(explode('/', $plugin['menu']['menu prefix'])); + + $plugin['menu'] += array( + // Default menu items that should be declared. + 'items' => array(), + ); + + $plugin['menu']['items'] += array( + 'list callback' => array(), + 'list' => array(), + 'add' => array(), + 'edit callback' => array(), + 'edit' => array(), + ); + + $plugin['menu']['items']['list callback'] += array( + 'path' => '', + // Menu items are translated by the menu system. + // TODO: We need more flexibility in title. The title of the admin page + // is not necessarily the title of the object, plus we need + // plural, singular, proper, not proper, etc. + 'title' => $plugin['menu']['menu title'], + 'description' => $plugin['menu']['menu description'], + 'page callback' => 'ctools_export_ui_switcher_page', + 'page arguments' => array($plugin['name'], 'list'), + 'access callback' => 'ctools_export_ui_task_access', + 'access arguments' => array($plugin['name'], 'list'), + 'type' => MENU_NORMAL_ITEM, + ); + + $plugin['menu']['items']['list'] += array( + 'path' => 'list', + 'title' => 'List', + 'page callback' => 'ctools_export_ui_switcher_page', + 'page arguments' => array($plugin['name'], 'list'), + 'access callback' => 'ctools_export_ui_task_access', + 'access arguments' => array($plugin['name'], 'list'), + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'weight' => -10, + ); + + $plugin['menu']['items']['add'] += array( + 'path' => 'add', + 'title' => 'Add', + 'page callback' => 'ctools_export_ui_switcher_page', + 'page arguments' => array($plugin['name'], 'add'), + 'access callback' => 'ctools_export_ui_task_access', + 'access arguments' => array($plugin['name'], 'add'), + 'type' => MENU_LOCAL_ACTION, + ); + + $plugin['menu']['items']['edit callback'] += array( + 'path' => 'list/%ctools_export_ui', + 'page callback' => 'ctools_export_ui_switcher_page', + 'page arguments' => array($plugin['name'], 'edit', $prefix_count + 2), + 'load arguments' => array($plugin['name']), + 'access callback' => 'ctools_export_ui_task_access', + 'access arguments' => array($plugin['name'], 'edit', $prefix_count + 2), + 'type' => MENU_CALLBACK, + ); + + $plugin['menu']['items']['edit'] += array( + 'path' => 'list/%ctools_export_ui/edit', + 'title' => 'Edit', + 'page callback' => 'ctools_export_ui_switcher_page', + 'page arguments' => array($plugin['name'], 'edit', $prefix_count + 2), + 'load arguments' => array($plugin['name']), + 'access callback' => 'ctools_export_ui_task_access', + 'access arguments' => array($plugin['name'], 'edit', $prefix_count + 2), + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'weight' => -10, + ); + + if ($plugin['allowed operations']['import']) { + $plugin['menu']['items'] += array('import' => array()); + $plugin['menu']['items']['import'] += array( + 'path' => 'import', + 'title' => 'Import', + 'page callback' => 'ctools_export_ui_switcher_page', + 'page arguments' => array($plugin['name'], 'import'), + 'access callback' => 'ctools_export_ui_task_access', + 'access arguments' => array($plugin['name'], 'import'), + 'type' => MENU_LOCAL_ACTION, + ); + } + + if ($plugin['allowed operations']['export']) { + $plugin['menu']['items'] += array('export' => array()); + $plugin['menu']['items']['export'] += array( + 'path' => 'list/%ctools_export_ui/export', + 'title' => 'Export', + 'page callback' => 'ctools_export_ui_switcher_page', + 'page arguments' => array($plugin['name'], 'export', $prefix_count + 2), + 'load arguments' => array($plugin['name']), + 'access callback' => 'ctools_export_ui_task_access', + 'access arguments' => array($plugin['name'], 'export', $prefix_count + 2), + 'type' => MENU_LOCAL_TASK, + ); + } + + if ($plugin['allowed operations']['revert']) { + $plugin['menu']['items'] += array('revert' => array()); + $plugin['menu']['items']['revert'] += array( + 'path' => 'list/%ctools_export_ui/revert', + 'title' => 'Revert', + 'page callback' => 'ctools_export_ui_switcher_page', + // Note: Yes, 'delete' op is correct. + 'page arguments' => array($plugin['name'], 'delete', $prefix_count + 2), + 'load arguments' => array($plugin['name']), + 'access callback' => 'ctools_export_ui_task_access', + 'access arguments' => array($plugin['name'], 'revert', $prefix_count + 2), + 'type' => MENU_CALLBACK, + ); + } + + if ($plugin['allowed operations']['delete']) { + $plugin['menu']['items'] += array('delete' => array()); + $plugin['menu']['items']['delete'] += array( + 'path' => 'list/%ctools_export_ui/delete', + 'title' => 'Delete', + 'page callback' => 'ctools_export_ui_switcher_page', + 'page arguments' => array($plugin['name'], 'delete', $prefix_count + 2), + 'load arguments' => array($plugin['name']), + 'access callback' => 'ctools_export_ui_task_access', + 'access arguments' => array($plugin['name'], 'delete', $prefix_count + 2), + 'type' => MENU_CALLBACK, + ); + } + + if ($plugin['allowed operations']['clone']) { + $plugin['menu']['items'] += array('clone' => array()); + $plugin['menu']['items']['clone'] += array( + 'path' => 'list/%ctools_export_ui/clone', + 'title' => 'Clone', + 'page callback' => 'ctools_export_ui_switcher_page', + 'page arguments' => array($plugin['name'], 'clone', $prefix_count + 2), + 'load arguments' => array($plugin['name']), + 'access callback' => 'ctools_export_ui_task_access', + 'access arguments' => array($plugin['name'], 'clone', $prefix_count + 2), + 'type' => MENU_CALLBACK, + ); + } + + if ($plugin['allowed operations']['enable']) { + $plugin['menu']['items'] += array('enable' => array()); + $plugin['menu']['items']['enable'] += array( + 'path' => 'list/%ctools_export_ui/enable', + 'title' => 'Enable', + 'page callback' => 'ctools_export_ui_switcher_page', + 'page arguments' => array($plugin['name'], 'enable', $prefix_count + 2), + 'load arguments' => array($plugin['name']), + 'access callback' => 'ctools_export_ui_task_access', + 'access arguments' => array($plugin['name'], 'enable', $prefix_count + 2), + 'type' => MENU_CALLBACK, + ); + } + + if ($plugin['allowed operations']['disable']) { + $plugin['menu']['items'] += array('disable' => array()); + $plugin['menu']['items']['disable'] += array( + 'path' => 'list/%ctools_export_ui/disable', + 'title' => 'Disable', + 'page callback' => 'ctools_export_ui_switcher_page', + 'page arguments' => array($plugin['name'], 'disable', $prefix_count + 2), + 'load arguments' => array($plugin['name']), + 'access callback' => 'ctools_export_ui_task_access', + 'access arguments' => array($plugin['name'], 'disable', $prefix_count + 2), + 'type' => MENU_CALLBACK, + ); + } + + // Define some redirects that should happen after edit/add/clone/delete operations. + $plugin['redirect'] += array( + 'add' => $base_path, + 'clone' => $base_path, + 'edit' => $base_path, + 'delete' => $base_path, + 'revert' => $base_path, + 'import' => $base_path, + ); + + // Define form elements. + $plugin['form'] += array( + 'settings' => function_exists($plugin['name'] . '_form') ? $plugin['name'] . '_form' : '', + 'validate' => function_exists($plugin['name'] . '_form_validate') ? $plugin['name'] . '_form_validate' : '', + 'submit' => function_exists($plugin['name'] . '_form_submit') ? $plugin['name'] . '_form_submit' : '', + ); + + // Define strings. + + // For all strings, %title may be filled in at a later time via str_replace + // since we do not know the title now. + $plugin['strings'] += array( + 'title' => array(), + 'confirmation' => array(), + 'help' => array(), + 'message' => array(), + ); + + // Strings used in drupal_set_title(). + $plugin['strings']['title'] += array( + 'add' => t('Add a new @plugin', array('@plugin' => $plugin['title singular'])), + // The "%title" will be replaced in ctools_export_ui_form(), as in this + // stage we dont have the specific exportable object. + 'edit' => t('Edit @plugin %title', array('@plugin' => $plugin['title singular'])), + 'clone' => t('Clone @plugin %title', array('@plugin' => $plugin['title singular'])), + + 'import' => t('Import @plugin', array('@plugin' => $plugin['title singular'])), + 'export' => t('Export @plugin %title', array('@plugin' => $plugin['title singular'])), + ); + + // Strings used in confirmation pages. + $plugin['strings']['confirmation'] += array( + 'revert' => array(), + 'delete' => array(), + 'add' => array(), + 'edit' => array(), + ); + + $plugin['strings']['confirmation']['revert'] += array( + 'question' => t('Are you sure you want to revert %title?'), + 'information' => t('This action will permanently remove any customizations made to this item.'), + 'success' => t('The item has been reverted.'), + ); + + $plugin['strings']['confirmation']['delete'] += array( + 'question' => t('Are you sure you want to delete %title?'), + 'information' => t('This action will permanently remove this item from your database..'), + 'success' => t('The item has been deleted.'), + ); + + $plugin['strings']['confirmation']['add'] += array( + 'success' => t('%title has been created.'), + 'fail' => t('%title could not be created.'), + ); + + $plugin['strings']['confirmation']['edit'] += array( + 'success' => t('%title has been updated.'), + 'fail' => t('%title could not be updated.'), + ); + + // Strings used in $forms. + $plugin['strings']['help'] += array( + 'import' => t('You can import an exported definition by pasting the exported object code into the field below.'), + ); + + // Strings used in drupal_set_message(). + $plugin['strings']['message'] += array( + 'enable' => t('@plugin %title was enabled.', array('@plugin' => $plugin['title singular proper'])), + 'disable' => t('@plugin %title was disabled.', array('@plugin' => $plugin['title singular proper'])), + 'no items' => t('There are no @titles to display.', array('@titles' => $plugin['title plural'])), + ); +} + +/** + * Get the class to handle creating a list of exportable items. + * + * If a plugin does not define a lister class at all, then the default + * lister class will be used. + * + * @return + * Either the lister class or FALSE if one could not be had. + */ +function ctools_export_ui_get_handler($plugin) { + $cache = &drupal_static(__FUNCTION__, array()); + if (empty($cache[$plugin['name']])) { + // If a list class is not specified by the plugin, fall back to the + // default ctools_export_ui plugin instead. + if (empty($plugin['handler'])) { + $default = ctools_get_export_ui('ctools_export_ui'); + $class = ctools_plugin_get_class($default, 'handler'); + } + else { + $class = ctools_plugin_get_class($plugin, 'handler'); + } + + if ($class) { + $cache[$plugin['name']] = new $class(); + $cache[$plugin['name']]->init($plugin); + } + } + return !empty($cache[$plugin['name']]) ? $cache[$plugin['name']] : FALSE; +} + +/** + * Get the base path from a plugin. + * + * @param $plugin + * The plugin. + * + * @return + * The menu path to the plugin's list. + */ +function ctools_export_ui_plugin_base_path($plugin) { + return $plugin['menu']['menu prefix'] . '/' . $plugin['menu']['menu item']; +} + +/** + * Get the path to a specific menu item from a plugin. + * + * @param $plugin + * The plugin name. + * @param $item_id + * The id in the menu items from the plugin. + * @param $export_key + * The export key of the item being edited, if it exists. + * @return + * The menu path to the plugin's list. + */ +function ctools_export_ui_plugin_menu_path($plugin, $item_id, $export_key = NULL) { + $path = $plugin['menu']['items'][$item_id]['path']; + if ($export_key) { + $path = str_replace('%ctools_export_ui', $export_key, $path); + } + return ctools_export_ui_plugin_base_path($plugin) . '/' . $path; +} + +/** + * Helper function to include CTools plugins and get an export-ui exportable. + * + * @param $plugin_name + * The plugin that should be laoded. + */ +function ctools_get_export_ui($plugin_name) { + ctools_include('plugins'); + return ctools_get_plugins('ctools', 'export_ui', $plugin_name); + +} + +/** + * Helper function to include CTools plugins and get all export-ui exportables. + */ +function ctools_get_export_uis() { + ctools_include('plugins'); + return ctools_get_plugins('ctools', 'export_ui'); +} + +/** + * Main page callback to manipulate exportables. + * + * This simply loads the object defined in the plugin and hands it off to + * a method based upon the name of the operation in use. This can easily + * be used to add more ops. + */ +function ctools_export_ui_switcher_page($plugin_name, $op) { + $args = func_get_args(); + $js = !empty($_REQUEST['js']); + + // Load the $plugin information + $plugin = ctools_get_export_ui($plugin_name); + $handler = ctools_export_ui_get_handler($plugin); + + if ($handler) { + $method = $op . '_page'; + if (method_exists($handler, $method)) { + // replace the first two arguments: + $args[0] = $js; + $args[1] = $_POST; + return call_user_func_array(array($handler, $method), $args); + } + } + else { + return t('Configuration error. No handler found.'); + } +} diff --git a/sites/all/modules/contrib/ctools/includes/export-ui.menu.inc b/sites/all/modules/contrib/ctools/includes/export-ui.menu.inc new file mode 100644 index 0000000..d27bf15 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/export-ui.menu.inc @@ -0,0 +1,24 @@ +hook_menu($items); + } + } + } +} diff --git a/sites/all/modules/contrib/ctools/includes/export-ui.plugin-type.inc b/sites/all/modules/contrib/ctools/includes/export-ui.plugin-type.inc new file mode 100644 index 0000000..f1a7510 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/export-ui.plugin-type.inc @@ -0,0 +1,20 @@ + array( + 'function' => 'ctools_export_ui_process', + 'file' => 'export-ui.inc', + 'path' => drupal_get_path('module', 'ctools') . '/includes', + ), + 'classes' => array('handler'), + ); +} \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/includes/export.inc b/sites/all/modules/contrib/ctools/includes/export.inc new file mode 100644 index 0000000..0b85c2e --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/export.inc @@ -0,0 +1,1267 @@ +export_type & EXPORT_IN_DATABASE) { + // Existing record. + $update = array($key); + } + else { + // New record. + $update = array(); + $object->export_type = EXPORT_IN_DATABASE; + } + return drupal_write_record($table, $object, $update); + } +} + +/** + * Delete a single exportable object. + * + * This only deletes from the database, which means that if an item is in + * code, then this is actually a revert. + * + * @param $table + * The name of the table to use to retrieve $schema values. This table + * must have an 'export' section containing data or this function + * will fail. + * @param $object + * The fully populated object to delete, or the export key. + */ +function ctools_export_crud_delete($table, $object) { + $schema = ctools_export_get_schema($table); + $export = $schema['export']; + + if (!empty($export['delete callback']) && function_exists($export['delete callback'])) { + return $export['delete callback']($object); + } + else { + // If we were sent an object, get the export key from it. Otherwise + // assume we were sent the export key. + $value = is_object($object) ? $object->{$export['key']} : $object; + db_delete($table) + ->condition($export['key'], $value) + ->execute(); + } +} + +/** + * Get the exported code of a single exportable object. + * + * @param $table + * The name of the table to use to retrieve $schema values. This table + * must have an 'export' section containing data or this function + * will fail. + * @param $object + * The fully populated object to delete, or the export key. + * @param $indent + * Any indentation to apply to the code, in case this object is embedded + * into another, for example. + * + * @return + * A string containing the executable export of the object. + */ +function ctools_export_crud_export($table, $object, $indent = '') { + $schema = ctools_export_get_schema($table); + $export = $schema['export']; + + if (!empty($export['export callback']) && function_exists($export['export callback'])) { + return $export['export callback']($object, $indent); + } + else { + return ctools_export_object($table, $object, $indent); + } +} + +/** + * Turn exported code into an object. + * + * Note: If the code is poorly formed, this could crash and there is no + * way to prevent this. + * + * @param $table + * The name of the table to use to retrieve $schema values. This table + * must have an 'export' section containing data or this function + * will fail. + * @param $code + * The code to eval to create the object. + * + * @return + * An object created from the export. This object will NOT have been saved + * to the database. In the case of failure, a string containing all errors + * that the system was able to determine. + */ +function ctools_export_crud_import($table, $code) { + $schema = ctools_export_get_schema($table); + $export = $schema['export']; + + if (!empty($export['import callback']) && function_exists($export['import callback'])) { + return $export['import callback']($code); + } + else { + ob_start(); + eval($code); + ob_end_clean(); + + if (empty(${$export['identifier']})) { + $errors = ob_get_contents(); + if (empty($errors)) { + $errors = t('No item found.'); + } + return $errors; + } + + $item = ${$export['identifier']}; + + // Set these defaults just the same way that ctools_export_new_object sets + // them. + $item->export_type = NULL; + $item->{$export['export type string']} = t('Local'); + + return $item; + } +} + +/** + * Change the status of a certain object. + * + * @param $table + * The name of the table to use to enable a certain object. This table + * must have an 'export' section containing data or this function + * will fail. + * @param $object + * The fully populated object to enable, or the machine readable name. + * @param $status + * The status, in this case, is whether or not it is 'disabled'. + */ +function ctools_export_crud_set_status($table, $object, $status) { + $schema = ctools_export_get_schema($table); + $export = $schema['export']; + + if (!empty($export['status callback']) && function_exists($export['status callback'])) { + $export['status callback']($object, $status); + } + else { + if (is_object($object)) { + ctools_export_set_object_status($object, $status); + } + else { + ctools_export_set_status($table, $object, $status); + } + } + +} + + +/** + * Enable a certain object. + * + * @param $table + * The name of the table to use to enable a certain object. This table + * must have an 'export' section containing data or this function + * will fail. + * @param $object + * The fully populated object to enable, or the machine readable name. + */ +function ctools_export_crud_enable($table, $object) { + return ctools_export_crud_set_status($table, $object, FALSE); +} + +/** + * Disable a certain object. + * + * @param $table + * The name of the table to use to disable a certain object. This table + * must have an 'export' section containing data or this function + * will fail. + * @param $object + * The fully populated object to disable, or the machine readable name. + */ +function ctools_export_crud_disable($table, $object) { + return ctools_export_crud_set_status($table, $object, TRUE); +} + +/** + * @} + */ + +/** + * Load some number of exportable objects. + * + * This function will cache the objects, load subsidiary objects if necessary, + * check default objects in code and properly set them up. It will cache + * the results so that multiple calls to load the same objects + * will not cause problems. + * + * It attempts to reduce, as much as possible, the number of queries + * involved. + * + * @param $table + * The name of the table to be loaded from. Data is expected to be in the + * schema to make all this work. + * @param $type + * A string to notify the loader what the argument is + * - all: load all items. This is the default. $args is unused. + * - names: $args will be an array of specific named objects to load. + * - conditions: $args will be a keyed array of conditions. The conditions + * must be in the schema for this table or errors will result. + * @param $args + * An array of arguments whose actual use is defined by the $type argument. + */ +function ctools_export_load_object($table, $type = 'all', $args = array()) { + $cache = &drupal_static(__FUNCTION__); + $cache_table_exists = &drupal_static(__FUNCTION__ . '_table_exists', array()); + $cached_database = &drupal_static('ctools_export_load_object_all'); + + if (!array_key_exists($table, $cache_table_exists)) { + $cache_table_exists[$table] = db_table_exists($table); + } + + $schema = ctools_export_get_schema($table); + if (empty($schema) || !$cache_table_exists[$table]) { + return array(); + } + + $export = $schema['export']; + + if (!isset($cache[$table])) { + $cache[$table] = array(); + } + + // If fetching all and cached all, we've done so and we are finished. + if ($type == 'all' && !empty($cached_database[$table])) { + return $cache[$table]; + } + + $return = array(); + + // Don't load anything we've already cached. + if ($type == 'names' && !empty($args)) { + foreach ($args as $id => $name) { + if (isset($cache[$table][$name])) { + $return[$name] = $cache[$table][$name]; + unset($args[$id]); + } + } + + // If nothing left to load, return the result. + if (empty($args)) { + return $return; + } + } + + // Build the query + $query = db_select($table, 't__0')->fields('t__0'); + $alias_count = 1; + if (!empty($schema['join'])) { + foreach ($schema['join'] as $join_key => $join) { + if ($join_schema = drupal_get_schema($join['table'])) { + $query->join($join['table'], 't__' . $alias_count, 't__0.' . $join['left_key'] . ' = ' . 't__' . $alias_count . '.' . $join['right_key']); + $query->fields('t__' . $alias_count); + $alias_count++; + + // Allow joining tables to alter the query through a callback. + if (isset($join['callback']) && function_exists($join['callback'])) { + $join['callback']($query, $schema, $join_schema); + } + } + } + } + + $conditions = array(); + $query_args = array(); + + // If they passed in names, add them to the query. + if ($type == 'names') { + $query->condition($export['key'], $args, 'IN'); + } + else if ($type == 'conditions') { + foreach ($args as $key => $value) { + if (isset($schema['fields'][$key])) { + $query->condition($key, $value); + } + } + } + + $result = $query->execute(); + + $status = variable_get($export['status'], array()); + // Unpack the results of the query onto objects and cache them. + foreach ($result as $data) { + if (isset($schema['export']['object factory']) && function_exists($schema['export']['object factory'])) { + $object = $schema['export']['object factory']($schema, $data); + } + else { + $object = _ctools_export_unpack_object($schema, $data, $export['object']); + } + $object->table = $table; + $object->{$export['export type string']} = t('Normal'); + $object->export_type = EXPORT_IN_DATABASE; + // Determine if default object is enabled or disabled. + if (isset($status[$object->{$export['key']}])) { + $object->disabled = $status[$object->{$export['key']}]; + } + + $cache[$table][$object->{$export['key']}] = $object; + if ($type == 'conditions') { + $return[$object->{$export['key']}] = $object; + } + } + + // Load subrecords. + if (isset($export['subrecords callback']) && function_exists($export['subrecords callback'])) { + $export['subrecords callback']($cache[$table]); + } + + if ($type == 'names' && !empty($args) && !empty($export['cache defaults'])) { + $defaults = _ctools_export_get_some_defaults($table, $export, $args); + } + else { + $defaults = _ctools_export_get_defaults($table, $export); + } + + if ($defaults) { + foreach ($defaults as $object) { + if ($type == 'conditions') { + // if this does not match all of our conditions, skip it. + foreach ($args as $key => $value) { + if (!isset($object->$key)) { + continue 2; + } + if (is_array($value)) { + if (!in_array($object->$key, $value)) { + continue 2; + } + } + else if ($object->$key != $value) { + continue 2; + } + } + } + else if ($type == 'names') { + if (!in_array($object->{$export['key']}, $args)) { + continue; + } + } + + // Determine if default object is enabled or disabled. + if (isset($status[$object->{$export['key']}])) { + $object->disabled = $status[$object->{$export['key']}]; + } + + if (!empty($cache[$table][$object->{$export['key']}])) { + $cache[$table][$object->{$export['key']}]->{$export['export type string']} = t('Overridden'); + $cache[$table][$object->{$export['key']}]->export_type |= EXPORT_IN_CODE; + $cache[$table][$object->{$export['key']}]->export_module = isset($object->export_module) ? $object->export_module : NULL; + if ($type == 'conditions') { + $return[$object->{$export['key']}] = $cache[$table][$object->{$export['key']}]; + } + } + else { + $object->{$export['export type string']} = t('Default'); + $object->export_type = EXPORT_IN_CODE; + $object->in_code_only = TRUE; + $object->table = $table; + + $cache[$table][$object->{$export['key']}] = $object; + if ($type == 'conditions') { + $return[$object->{$export['key']}] = $object; + } + } + } + } + + // If fetching all, we've done so and we are finished. + if ($type == 'all') { + $cached_database[$table] = TRUE; + return $cache[$table]; + } + + if ($type == 'names') { + foreach ($args as $name) { + if (isset($cache[$table][$name])) { + $return[$name] = $cache[$table][$name]; + } + } + } + + // For conditions, + return $return; +} + +/** + * Reset all static caches in ctools_export_load_object() or static caches for + * a given table in ctools_export_load_object(). + * + * @param $table + * String that is the name of a table. If not defined, all static caches in + * ctools_export_load_object() will be reset. + */ +function ctools_export_load_object_reset($table = NULL) { + // Reset plugin cache to make sure new include files are picked up. + ctools_include('plugins'); + ctools_get_plugins_reset(); + if (empty($table)) { + drupal_static_reset('ctools_export_load_object'); + drupal_static_reset('ctools_export_load_object_all'); + drupal_static_reset('_ctools_export_get_defaults'); + } + else { + $cache = &drupal_static('ctools_export_load_object'); + $cached_database = &drupal_static('ctools_export_load_object_all'); + $cached_defaults = &drupal_static('_ctools_export_get_defaults'); + unset($cache[$table]); + unset($cached_database[$table]); + unset($cached_defaults[$table]); + } +} + +/** + * Get the default version of an object, if it exists. + * + * This function doesn't care if an object is in the database or not and + * does not check. This means that export_type could appear to be incorrect, + * because a version could exist in the database. However, it's not + * incorrect for this function as it is *only* used for the default + * in code version. + */ +function ctools_get_default_object($table, $name) { + $schema = ctools_export_get_schema($table); + $export = $schema['export']; + + if (!$export['default hook']) { + return; + } + + // Try to load individually from cache if this cache is enabled. + if (!empty($export['cache defaults'])) { + $defaults = _ctools_export_get_some_defaults($table, $export, array($name)); + } + else { + $defaults = _ctools_export_get_defaults($table, $export); + } + + $status = variable_get($export['status'], array()); + + if (!isset($defaults[$name])) { + return; + } + + $object = $defaults[$name]; + + // Determine if default object is enabled or disabled. + if (isset($status[$object->{$export['key']}])) { + $object->disabled = $status[$object->{$export['key']}]; + } + + $object->{$export['export type string']} = t('Default'); + $object->export_type = EXPORT_IN_CODE; + $object->in_code_only = TRUE; + + return $object; +} + +/** + * Call the hook to get all default objects of the given type from the + * export. If configured properly, this could include loading up an API + * to get default objects. + */ +function _ctools_export_get_defaults($table, $export) { + $cache = &drupal_static(__FUNCTION__, array()); + + // If defaults may be cached, first see if we can load from cache. + if (!isset($cache[$table]) && !empty($export['cache defaults'])) { + $cache[$table] = _ctools_export_get_defaults_from_cache($table, $export); + } + + if (!isset($cache[$table])) { + // If we're caching, attempt to get a lock. We will wait a short time + // on the lock, but not too long, because it's better to just rebuild + // and throw away results than wait too long on a lock. + if (!empty($export['cache defaults'])) { + for ($counter = 0; !($lock = lock_acquire('ctools_export:' . $table)) && $counter > 5; $counter++) { + lock_wait('ctools_export:' . $table, 1); + ++$counter; + } + } + + $cache[$table] = array(); + + if ($export['default hook']) { + if (!empty($export['api'])) { + ctools_include('plugins'); + $info = ctools_plugin_api_include($export['api']['owner'], $export['api']['api'], + $export['api']['minimum_version'], $export['api']['current_version']); + $modules = array_keys($info); + } + else { + $modules = module_implements($export['default hook']); + } + + foreach ($modules as $module) { + $function = $module . '_' . $export['default hook']; + if (function_exists($function)) { + foreach ((array) $function($export) as $name => $object) { + // Record the module that provides this exportable. + $object->export_module = $module; + + if (empty($export['api'])) { + $cache[$table][$name] = $object; + } + else { + // If version checking is enabled, ensure that the object can be used. + if (isset($object->api_version) && + version_compare($object->api_version, $export['api']['minimum_version']) >= 0 && + version_compare($object->api_version, $export['api']['current_version']) <= 0) { + $cache[$table][$name] = $object; + } + } + } + } + } + + drupal_alter($export['default hook'], $cache[$table]); + + // If we acquired a lock earlier, cache the results and release the + // lock. + if (!empty($lock)) { + // Cache the index. + $index = array_keys($cache[$table]); + cache_set('ctools_export_index:' . $table, $index, $export['default cache bin']); + + // Cache each object. + foreach ($cache[$table] as $name => $object) { + cache_set('ctools_export:' . $table . ':' . $name, $object, $export['default cache bin']); + } + lock_release('ctools_export:' . $table); + } + } + } + + return $cache[$table]; +} + +/** + * Attempt to load default objects from cache. + * + * We can be instructed to cache default objects by the schema. If so + * we cache them as an index which is a list of all default objects, and + * then each default object is cached individually. + * + * @return Either an array of cached objects, or NULL indicating a cache + * rebuild is necessary. + */ +function _ctools_export_get_defaults_from_cache($table, $export) { + $data = cache_get('ctools_export_index:' . $table, $export['default cache bin']); + if (!$data || !is_array($data->data)) { + return; + } + + // This is the perfectly valid case where there are no default objects, + // and we have cached this state. + if (empty($data->data)) { + return array(); + } + + $keys = array(); + foreach ($data->data as $name) { + $keys[] = 'ctools_export:' . $table . ':' . $name; + } + + $data = cache_get_multiple($keys, $export['default cache bin']); + + // If any of our indexed keys missed, then we have a fail and we need to + // rebuild. + if (!empty($keys)) { + return; + } + + // Now, translate the returned cache objects to actual objects. + $cache = array(); + foreach ($data as $cached_object) { + $cache[$cached_object->data->{$export['key']}] = $cached_object->data; + } + + return $cache; +} + +/** + * Get a limited number of default objects. + * + * This attempts to load the objects directly from cache. If it cannot, + * the cache is rebuilt. This does not disturb the general get defaults + * from cache object. + * + * This function should ONLY be called if default caching is enabled. + * It does not check, it is assumed the caller has already done so. + */ +function _ctools_export_get_some_defaults($table, $export, $names) { + foreach ($names as $name) { + $keys[] = 'ctools_export:' . $table . ':' . $name; + } + + $data = cache_get_multiple($keys, $export['default cache bin']); + + // Cache hits remove the $key from $keys by reference. Cache + // misses do not. A cache miss indicates we may have to rebuild. + if (!empty($keys)) { + return _ctools_export_get_defaults($table, $export); + } + + // Now, translate the returned cache objects to actual objects. + $cache = array(); + foreach ($data as $cached_object) { + $cache[$cached_object->data->{$export['key']}] = $cached_object->data; + } + + return $cache; +} + +/** + * Unpack data loaded from the database onto an object. + * + * @param $schema + * The schema from drupal_get_schema(). + * @param $data + * The data as loaded from the database. + * @param $object + * If an object, data will be unpacked onto it. If a string + * an object of that type will be created. + */ +function _ctools_export_unpack_object($schema, $data, $object = 'stdClass') { + if (is_string($object)) { + if (class_exists($object)) { + $object = new $object; + } + else { + $object = new stdClass; + } + } + + // Go through our schema and build correlations. + foreach ($schema['fields'] as $field => $info) { + if (isset($data->$field)) { + $object->$field = empty($info['serialize']) ? $data->$field : unserialize($data->$field); + } + else { + $object->$field = NULL; + } + } + + if (isset($schema['join'])) { + foreach ($schema['join'] as $join_key => $join) { + $join_schema = ctools_export_get_schema($join['table']); + if (!empty($join['load'])) { + foreach ($join['load'] as $field) { + $info = $join_schema['fields'][$field]; + $object->$field = empty($info['serialize']) ? $data->$field : unserialize($data->$field); + } + } + } + } + + return $object; +} + +/** + * Unpack data loaded from the database onto an object. + * + * @param $table + * The name of the table this object represents. + * @param $data + * The data as loaded from the database. + */ +function ctools_export_unpack_object($table, $data) { + $schema = ctools_export_get_schema($table); + return _ctools_export_unpack_object($schema, $data, $schema['export']['object']); +} + +/** + * Export a field. + * + * This is a replacement for var_export(), allowing us to more nicely + * format exports. It will recurse down into arrays and will try to + * properly export bools when it can, though PHP has a hard time with + * this since they often end up as strings or ints. + */ +function ctools_var_export($var, $prefix = '') { + if (is_array($var)) { + if (empty($var)) { + $output = 'array()'; + } + else { + $output = "array(\n"; + foreach ($var as $key => $value) { + $output .= $prefix . " " . ctools_var_export($key) . " => " . ctools_var_export($value, $prefix . ' ') . ",\n"; + } + $output .= $prefix . ')'; + } + } + else if (is_object($var) && get_class($var) === 'stdClass') { + // var_export() will export stdClass objects using an undefined + // magic method __set_state() leaving the export broken. This + // workaround avoids this by casting the object as an array for + // export and casting it back to an object when evaluated. + $output = '(object) ' . ctools_var_export((array) $var, $prefix); + } + else if (is_bool($var)) { + $output = $var ? 'TRUE' : 'FALSE'; + } + else { + $output = var_export($var, TRUE); + } + + return $output; +} + +/** + * Export an object into code. + */ +function ctools_export_object($table, $object, $indent = '', $identifier = NULL, $additions = array(), $additions2 = array()) { + $schema = ctools_export_get_schema($table); + if (!isset($identifier)) { + $identifier = $schema['export']['identifier']; + } + + $output = $indent . '$' . $identifier . ' = new ' . get_class($object) . "();\n"; + + if ($schema['export']['can disable']) { + $output .= $indent . '$' . $identifier . '->disabled = FALSE; /* Edit this to true to make a default ' . $identifier . ' disabled initially */' . "\n"; + } + if (!empty($schema['export']['api']['current_version'])) { + $output .= $indent . '$' . $identifier . '->api_version = ' . $schema['export']['api']['current_version'] . ";\n"; + } + + // Put top additions here: + foreach ($additions as $field => $value) { + $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n"; + } + + $fields = $schema['fields']; + if (!empty($schema['join'])) { + foreach ($schema['join'] as $join) { + if (!empty($join['load'])) { + foreach ($join['load'] as $join_field) { + $fields[$join_field] = $join['fields'][$join_field]; + } + } + } + } + + // Go through our schema and joined tables and build correlations. + foreach ($fields as $field => $info) { + if (!empty($info['no export'])) { + continue; + } + if (!isset($object->$field)) { + if (isset($info['default'])) { + $object->$field = $info['default']; + } + else { + $object->$field = ''; + } + } + + // Note: This is the *field* export callback, not the table one! + if (!empty($info['export callback']) && function_exists($info['export callback'])) { + $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . $info['export callback']($object, $field, $object->$field, $indent) . ";\n"; + } + else { + $value = $object->$field; + if ($info['type'] == 'int') { + if (isset($info['size']) && $info['size'] == 'tiny') { + $info['boolean'] = (!isset($info['boolean'])) ? $schema['export']['boolean'] : $info['boolean']; + $value = ($info['boolean']) ? (bool) $value : (int) $value; + } + else { + $value = (int) $value; + } + } + + $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n"; + } + } + + // And bottom additions here + foreach ($additions2 as $field => $value) { + $output .= $indent . '$' . $identifier . '->' . $field . ' = ' . ctools_var_export($value, $indent) . ";\n"; + } + + return $output; +} + +/** + * Get the schema for a given table. + * + * This looks for data the export subsystem needs and applies defaults so + * that it's easily available. + */ +function ctools_export_get_schema($table) { + static $drupal_static_fast; + if (!isset($drupal_static_fast)) { + $drupal_static_fast['cache'] = &drupal_static(__FUNCTION__); + } + $cache = &$drupal_static_fast['cache']; + + if (empty($cache[$table])) { + $schema = drupal_get_schema($table); + + // If our schema isn't loaded, it's possible we're in a state where it + // simply hasn't been cached. If we've been asked, let's force the + // issue. + if (!$schema || empty($schema['export'])) { + // force a schema reset: + $schema = drupal_get_schema($table, TRUE); + } + + if (!isset($schema['export'])) { + return array(); + } + + if (empty($schema['module'])) { + return array(); + } + + // Add some defaults + $schema['export'] += array( + 'key' => 'name', + 'key name' => 'Name', + 'object' => 'stdClass', + 'status' => 'default_' . $table, + 'default hook' => 'default_' . $table, + 'can disable' => TRUE, + 'identifier' => $table, + 'primary key' => !empty($schema['primary key']) ? $schema['primary key'][0] : '', + 'bulk export' => TRUE, + 'list callback' => "$schema[module]_{$table}_list", + 'to hook code callback' => "$schema[module]_{$table}_to_hook_code", + 'cache defaults' => FALSE, + 'default cache bin' => 'cache', + 'export type string' => 'type', + 'boolean' => TRUE, + ); + + // If the export definition doesn't have the "primary key" then the CRUD + // save callback won't work. + if (empty($schema['export']['primary key']) && user_access('administer site configuration')) { + drupal_set_message(t('The export definition of @table is missing the "primary key" property.', array('@table' => $table)), 'error'); + } + + // Notes: + // The following callbacks may be defined to override default behavior + // when using CRUD functions: + // + // create callback + // load callback + // load multiple callback + // load all callback + // save callback + // delete callback + // export callback + // import callback + // + // See the appropriate ctools_export_crud function for details on what + // arguments these callbacks should accept. Please do not call these + // directly, always use the ctools_export_crud_* wrappers to ensure + // that default implementations are honored. + $cache[$table] = $schema; + } + + return $cache[$table]; +} + +/** + * Gets the schemas for all tables with ctools object metadata. + */ +function ctools_export_get_schemas($for_export = FALSE) { + $export_tables = &drupal_static(__FUNCTION__); + if (is_null($export_tables)) { + $export_tables = array(); + $schemas = drupal_get_schema(); + foreach ($schemas as $table => $schema) { + if (!isset($schema['export'])) { + unset($schemas[$table]); + continue; + } + $export_tables[$table] = ctools_export_get_schema($table); + } + } + return $for_export ? array_filter($export_tables, '_ctools_export_filter_export_tables') : $export_tables; +} + +function _ctools_export_filter_export_tables($schema) { + return !empty($schema['export']['bulk export']); +} + +function ctools_export_get_schemas_by_module($modules = array(), $for_export = FALSE) { + $export_tables = array(); + $list = ctools_export_get_schemas($for_export); + foreach ($list as $table => $schema) { + $export_tables[$schema['module']][$table] = $schema; + } + return empty($modules) ? $export_tables : array_keys($export_tables, $modules); +} + +/** + * Set the status of a default $object as a variable. + * + * The status, in this case, is whether or not it is 'disabled'. + * This function does not check to make sure $object actually + * exists. + */ +function ctools_export_set_status($table, $name, $new_status = TRUE) { + $schema = ctools_export_get_schema($table); + $status = variable_get($schema['export']['status'], array()); + + $status[$name] = $new_status; + variable_set($schema['export']['status'], $status); +} + +/** + * Set the status of a default $object as a variable. + * + * This is more efficient than ctools_export_set_status because it + * will actually unset the variable entirely if it's not necessary, + * this saving a bit of space. + */ +function ctools_export_set_object_status($object, $new_status = TRUE) { + $table = $object->table; + $schema = ctools_export_get_schema($table); + $export = $schema['export']; + $status = variable_get($export['status'], array()); + + // Compare + if (!$new_status && $object->export_type & EXPORT_IN_DATABASE) { + unset($status[$object->{$export['key']}]); + } + else { + $status[$object->{$export['key']}] = $new_status; + } + + variable_set($export['status'], $status); +} + +/** + * Provide a form for displaying an export. + * + * This is a simple form that should be invoked like this: + * @code + * $output = drupal_get_form('ctools_export_form', $code, $object_title); + * @endcode + */ +function ctools_export_form($form, &$form_state, $code, $title = '') { + $lines = substr_count($code, "\n"); + $form['code'] = array( + '#type' => 'textarea', + '#title' => $title, + '#default_value' => $code, + '#rows' => $lines, + ); + + return $form; +} + +/** + * Create a new object based upon schema values. + * + * Because 'default' has ambiguous meaning on some fields, we will actually + * use 'object default' to fill in default values if default is not set + * That's a little safer to use as it won't cause weird database default + * situations. + */ +function ctools_export_new_object($table, $set_defaults = TRUE) { + $schema = ctools_export_get_schema($table); + $export = $schema['export']; + + $object = new $export['object']; + foreach ($schema['fields'] as $field => $info) { + if (isset($info['object default'])) { + $object->$field = $info['object default']; + } + else if (isset($info['default'])) { + $object->$field = $info['default']; + } + else { + $object->$field = NULL; + } + } + + if ($set_defaults) { + // Set some defaults so this data always exists. + // We don't set the export_type property here, as this object is not saved + // yet. We do give it NULL so we don't generate notices trying to read it. + $object->export_type = NULL; + $object->{$export['export type string']} = t('Local'); + } + return $object; +} + +/** + * Convert a group of objects to code based upon input and return this as a larger + * export. + */ +function ctools_export_to_hook_code(&$code, $table, $names = array(), $name = 'foo') { + $schema = ctools_export_get_schema($table); + $export = $schema['export']; + // Use the schema-specified function for generating hook code, if one exists + if (function_exists($export['to hook code callback'])) { + $output = $export['to hook code callback']($names, $name); + } + // Otherwise, the following code generates basic hook code + else { + $output = ctools_export_default_to_hook_code($schema, $table, $names, $name); + } + + if (!empty($output)) { + if (isset($export['api'])) { + if (isset($code[$export['api']['owner']][$export['api']['api']]['version'])) { + $code[$export['api']['owner']][$export['api']['api']]['version'] = max($code[$export['api']['owner']][$export['api']['api']]['version'], $export['api']['minimum_version']); + } + else { + $code[$export['api']['owner']][$export['api']['api']]['version'] = $export['api']['minimum_version']; + $code[$export['api']['owner']][$export['api']['api']]['code'] = ''; + } + $code[$export['api']['owner']][$export['api']['api']]['code'] .= $output; + } + else { + if (empty($code['general'])) { + $code['general'] = ''; + } + $code['general'] .= $output; + } + } +} + +/** + * Default function to export objects to code. + * + * Note that if your module provides a 'to hook code callback' then it will + * receive only $names and $name as arguments. Your module is presumed to + * already know the rest. + */ +function ctools_export_default_to_hook_code($schema, $table, $names, $name) { + $export = $schema['export']; + $output = ''; + $objects = ctools_export_crud_load_multiple($table, $names); + if ($objects) { + $output = "/**\n"; + $output .= " * Implements hook_{$export['default hook']}().\n"; + $output .= " */\n"; + $output .= "function " . $name . "_{$export['default hook']}() {\n"; + $output .= " \${$export['identifier']}s = array();\n\n"; + foreach ($objects as $object) { + $output .= ctools_export_crud_export($table, $object, ' '); + $output .= " \${$export['identifier']}s['" . check_plain($object->$export['key']) . "'] = \${$export['identifier']};\n\n"; + } + $output .= " return \${$export['identifier']}s;\n"; + $output .= "}\n"; + } + + return $output; +} +/** + * Default function for listing bulk exportable objects. + */ +function ctools_export_default_list($table, $schema) { + $list = array(); + + $items = ctools_export_crud_load_all($table); + $export_key = $schema['export']['key']; + foreach ($items as $item) { + // Try a couple of possible obvious title keys: + $keys = array('admin_title', 'title'); + if (isset($schema['export']['admin_title'])) { + array_unshift($keys, $schema['export']['admin_title']); + } + + $string = ''; + foreach ($keys as $key) { + if (!empty($item->$key)) { + $string = $item->$key . " (" . $item->$export_key . ")"; + break; + } + } + + if (empty($string)) { + $string = $item->$export_key; + } + $list[$item->$export_key] = check_plain($string); + } + return $list; +} diff --git a/sites/all/modules/contrib/ctools/includes/fields.inc b/sites/all/modules/contrib/ctools/includes/fields.inc new file mode 100644 index 0000000..f379f5e --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/fields.inc @@ -0,0 +1,357 @@ + $field_name, + 'entity_type' => 'ctools', + 'bundle' => 'ctools', + + // Use the default field settings for settings and widget. + 'settings' => field_info_instance_settings($field['type']), + 'widget' => array( + 'type' => $field_type['default_widget'], + 'settings' => array(), + ), + + // Build a dummy display mode. + 'display' => array( + $view_mode => array( + 'type' => $formatter, + 'settings' => $formatter_settings, + ), + ), + + // Set the other fields to their default values. + // @see _field_write_instance(). + 'required' => FALSE, + 'label' => $field_name, + 'description' => '', + 'deleted' => 0, + ); +} + +/** + * Helper function for calling hook_field_formatter_settings_form() without needing to load an instance of the field. + * + * @param $field + * A fully loaded field. + * @param $formatter_type + * The formatter key selected from the options provided by field_ui_formatter_options(). + * @param $form + * The full form from the function that's calling this function. + * @param $form_state + * The full form_state from the function that's calling this function. + * @param $view_mode + * We're passing a view mode from this function to the fake instance we're creating. Defaults to ctools, but we expect developers to actually name this something meaningful. + */ +function ctools_fields_get_field_formatter_settings_form($field, $formatter_type, &$form, $form_state, $view_mode = 'ctools') { + $conf = $form_state['conf']; + $formatter = field_info_formatter_types($formatter_type); + if (isset($formatter['settings'])) { + $conf['formatter_settings'] += $formatter['settings']; + } + $function = $formatter['module'] . '_field_formatter_settings_form'; + if (function_exists($function)) { + $instance = ctools_fields_fake_field_instance($field['field_name'], $view_mode, $formatter_type, $conf['formatter_settings']); + $settings_form = $function($field, $instance, $view_mode, $form, $form_state); + if ($settings_form) { + // Allow other modules to alter the formatter settings form. + $context = array( + 'module' => $formatter['module'], + 'formatter' => $formatter, + 'field' => $field, + 'instance' => $instance, + 'view_mode' => $view_mode, + 'form' => $form, + 'form_state' => $form_state, + ); + drupal_alter('field_formatter_settings_form', $settings_form, $context); + + $settings_form['#tree'] = TRUE; + $form['ctools_field_list']['#value'][] = $field; + $form += $settings_form; + } + } + + if (isset($field['cardinality']) && $field['cardinality'] != 1) { + list($prefix, $suffix) = explode('@count', t('Skip the first @count item(s)')); + $form['delta_offset'] = array( + '#type' => 'textfield', + '#size' => 5, + '#field_prefix' => $prefix, + '#field_suffix' => $suffix, + '#default_value' => isset($conf['delta_offset']) ? $conf['delta_offset'] : 0, + ); + + list($prefix, $suffix) = explode('@count', t('Then display at most @count item(s)')); + $form['delta_limit'] = array( + '#type' => 'textfield', + '#size' => 5, + '#field_prefix' => $prefix, + '#field_suffix' => $suffix, + '#description' => t('Enter 0 to display all items.'), + '#default_value' => isset($conf['delta_limit']) ? $conf['delta_limit'] : 0, + ); + + $form['delta_reversed'] = array( + '#title' => t('Display in reverse order'), + '#type' => 'checkbox', + '#default_value' => !empty($conf['delta_reversed']), + '#description' => t('(start from last values)'), + ); + } +} + +/** + * Helper function for generating all the formatter information associated with + * any fields. + * Especially useful for determining the fields that will be added to form that + * executes hook_field_formatter_settings_form(). + * + * @param $fields + * An array of fully loaded fields. + */ +function ctools_fields_get_field_formatter_info($fields) { + $info = array(); + $field_info = module_invoke_all('field_formatter_info'); + foreach ($fields as $field) { + foreach ($field_info as $format_name => $formatter_info) { + if (in_array($field['type'], $formatter_info['field types'])) { + $info += array($format_name => $formatter_info); + } + } + } + drupal_alter('field_formatter_info', $info); + return $info; +} + +/** + * Returns the label of a certain field. + * + * Cribbed from Views. + */ +function ctools_field_label($field_name) { + $label_counter = array(); + // Count the amount of instances per label per field. + $instances = field_info_instances(); + foreach ($instances as $entity_type) { + foreach ($entity_type as $bundle) { + if (isset($bundle[$field_name])) { + $label_counter[$bundle[$field_name]['label']] = isset($label_counter[$bundle[$field_name]['label']]) ? ++$label_counter[$bundle[$field_name]['label']] : 1; + } + } + } + if (empty($label_counter)) { + return $field_name; + } + // Sort the field lables by it most used label and return the most used one. + arsort($label_counter); + $label_counter = array_keys($label_counter); + return $label_counter[0]; +} + +/** + * Replacement for core _field_invoke() to invoke on a single field. + * + * Core only allows invoking field hooks via a private function for all fields + * on an entire entity. However, we very often need to invoke our hooks on + * a single field as we take things apart and only use little bits. + * + * @param $field_name + * Either a field instance object or the name of the field. + * If the 'field' key is populated it will be used as the field + * settings. + * @param $op + * Possible operations include: + * - form + * - validate + * - presave + * - insert + * - update + * - delete + * - delete revision + * - view + * - prepare translation + * @param $entity_type + * The type of $entity; e.g. 'node' or 'user'. + * @param $entity + * The fully formed $entity_type entity. + * @param $a + * - The $form in the 'form' operation. + * - The value of $view_mode in the 'view' operation. + * - Otherwise NULL. + * @param $b + * - The $form_state in the 'submit' operation. + * - Otherwise NULL. + * @param $options + * An associative array of additional options, with the following keys: + * - 'field_name': The name of the field whose operation should be + * invoked. By default, the operation is invoked on all the fields + * in the entity's bundle. NOTE: This option is not compatible with + * the 'deleted' option; the 'field_id' option should be used + * instead. + * - 'field_id': The id of the field whose operation should be + * invoked. By default, the operation is invoked on all the fields + * in the entity's' bundles. + * - 'default': A boolean value, specifying which implementation of + * the operation should be invoked. + * - if FALSE (default), the field types implementation of the operation + * will be invoked (hook_field_[op]) + * - If TRUE, the default field implementation of the field operation + * will be invoked (field_default_[op]) + * Internal use only. Do not explicitely set to TRUE, but use + * _field_invoke_default() instead. + * - 'deleted': If TRUE, the function will operate on deleted fields + * as well as non-deleted fields. If unset or FALSE, only + * non-deleted fields are operated on. + * - 'language': A language code or an array of language codes keyed by field + * name. It will be used to narrow down to a single value the available + * languages to act on. + * + * @see _field_invoke() + */ +function ctools_field_invoke_field($field_name, $op, $entity_type, $entity, &$a = NULL, &$b = NULL, $options = array()) { + if (is_array($field_name)) { + $instance = $field_name; + $field = empty($field_name['field']) ? field_info_field($instance['field_name']) : $field_name['field']; + $field_name = $instance['field_name']; + } + else { + list(, , $bundle) = entity_extract_ids($entity_type, $entity); + $instance = field_info_instance($entity_type, $field_name, $bundle); + } + + if (empty($instance)) { + return; + } + + // Merge default options. + $default_options = array( + 'default' => FALSE, + 'deleted' => FALSE, + 'language' => NULL, + ); + $options += $default_options; + + $return = array(); + + // Everything from here is unmodified code from _field_invoke() formerly + // inside a foreach loop over the instances. + $function = $options['default'] ? 'field_default_' . $op : $field['module'] . '_field_' . $op; + if (function_exists($function)) { + // Determine the list of languages to iterate on. + $available_languages = field_available_languages($entity_type, $field); + $languages = _field_language_suggestion($available_languages, $options['language'], $field_name); + + foreach ($languages as $langcode) { + $items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array(); + $result = $function($entity_type, $entity, $field, $instance, $langcode, $items, $a, $b); + if (isset($result)) { + // For hooks with array results, we merge results together. + // For hooks with scalar results, we collect results in an array. + if (is_array($result)) { + $return = array_merge($return, $result); + } + else { + $return[] = $result; + } + } + + // Populate $items back in the field values, but avoid replacing missing + // fields with an empty array (those are not equivalent on update). + if ($items !== array() || isset($entity->{$field_name}[$langcode])) { + $entity->{$field_name}[$langcode] = $items; + } + } + } + + return $return; +} + +/** + * Replacement for core _field_invoke_default() to invoke on a single field. + * + * @see ctools_field_invoke_field() + * @see _field_invoke_default() + */ +function ctools_field_invoke_field_default($field_name, $op, $entity_type, $entity, &$a = NULL, &$b = NULL, $options = array()) { + $options['default'] = TRUE; + return ctools_field_invoke_field($field_name, $op, $entity_type, $entity, $a, $b, $options); +} + +/** + * Returns a list of field definitions of a specified type. + * + * @param string $field_type + * A field type name; e.g. 'text' or 'date'. + * + * @return array + * An array of field definitions of the specified type, keyed by field name. + */ +function ctools_fields_get_fields_by_type($field_type) { + $fields = array(); + foreach (field_info_fields() as $field_name => $field_info) { + if ($field_info['type'] == $field_type) { + $fields[$field_name] = $field_info; + } + } + return $fields; +} + +/** + * Derive the foreign keys that a field provides. + * + * @param $field_name + * The name of the field. + * + * @return + * An array of foreign keys according to Schema API. + */ +function ctools_field_foreign_keys($field_name) { + $foreign_keys = &drupal_static(__FUNCTION__, array()); + if (!isset($foreign_keys[$field_name])) { + $foreign_keys[$field_name] = array(); + $field = field_info_field($field_name); + + if (!empty($field['foreign keys'])) { + $foreign_keys[$field_name] = $field['foreign keys']; + } + else { + // try to fetch foreign keys from schema, as not everything + // stores foreign keys properly in the field info. + $module = $field['module']; + + module_load_install($module); + $schema = module_invoke($module, 'field_schema', $field); + if (!empty($schema['foreign keys'])) { + $foreign_keys[$field_name] = $schema['foreign keys']; + } + } + } + + return $foreign_keys[$field_name]; +} diff --git a/sites/all/modules/contrib/ctools/includes/jump-menu.inc b/sites/all/modules/contrib/ctools/includes/jump-menu.inc new file mode 100644 index 0000000..51f4598 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/jump-menu.inc @@ -0,0 +1,150 @@ + t('Go'), + 'choose' => t('- Choose -'), + 'inline' => TRUE, + 'hide' => TRUE, + ); + + $form['#attached']['js'][] = ctools_attach_js('jump-menu'); + + if (!empty($options['choose'])) { + $select = array('' => $options['choose']) + $select; + } + + $form['jump'] = array( + '#type' => 'select', + '#options' => $select, + '#attributes' => array( + 'class' => array('ctools-jump-menu-select'), + ), + ); + + if (!empty($options['title'])) { + $form['jump']['#title'] = $options['title']; + } + + if (!empty($options['description'])) { + $form['jump']['#description'] = $options['description']; + } + + if (!empty($options['default_value'])) { + $form['jump']['#default_value'] = $options['default_value']; + } + + if (isset($options['image'])) { + $form['go'] = array( + '#type' => 'image_button', + '#src' => $options['image'], + '#submit' => array('ctools_jump_menu_submit'), + '#attributes' => array( + 'class' => array('ctools-jump-menu-button'), + ), + ); + } + else { + $form['go'] = array( + '#type' => 'submit', + '#value' => $options['button'], + '#submit' => array('ctools_jump_menu_submit'), + '#attributes' => array( + 'class' => array('ctools-jump-menu-button'), + ), + ); + } + + if ($options['inline']) { + $form['jump']['#prefix'] = '
'; + $form['go']['#suffix'] = '
'; + } + + if ($options['hide']) { + $form['jump']['#attributes']['class'][] = 'ctools-jump-menu-change'; + $form['go']['#attributes']['class'][] = 'ctools-jump-menu-hide'; + } + + return $form; +} + +/** + * Submit handler for the jump menu. + * + * This is normally only invoked upon submit without javascript enabled. + */ +function ctools_jump_menu_submit($form, &$form_state) { + if ($form_state['values']['jump'] === '') { + // We have nothing to do when the user has not selected any value. + return; + } + + // If the path we are redirecting to contains the string :: then treat the + // the string after the double colon as the path to redirect to. + // This allows duplicate paths to be used in jump menus for multiple options. + $redirect_array = explode("::", $form_state['values']['jump']); + + if(isset($redirect_array[1]) && !empty($redirect_array[1])){ + $redirect = $redirect_array[1]; + } + else { + $redirect = $form_state['values']['jump']; + } + + // If the path we are redirecting to starts with the base path (for example, + // "/somepath/node/1"), we need to strip the base path off before passing it + // to $form_state['redirect']. + $base_path = base_path(); + if (strpos($redirect, $base_path) === 0) { + $redirect = substr($redirect, strlen($base_path)); + } + + // Parse the URL so that query strings and fragments are preserved in the + // redirect. + $redirect = drupal_parse_url($redirect); + $redirect['path'] = urldecode($redirect['path']); + $form_state['redirect'] = array($redirect['path'], $redirect); +} diff --git a/sites/all/modules/contrib/ctools/includes/language.inc b/sites/all/modules/contrib/ctools/includes/language.inc new file mode 100644 index 0000000..9a7850b --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/language.inc @@ -0,0 +1,44 @@ + names in current language, localized + * 'native' => native names + * @param $all + * Boolean to return all languages or only enabled ones + * + * @see locale_language_list + */ +function ctools_language_list($field = 'name', $all = FALSE) { + if ($all) { + $languages = language_list(); + } + else { + $languages = language_list('enabled'); + $languages = $languages[1]; + } + $list = array(); + foreach ($languages as $language) { + $list[$language->language] = ($field == 'name') ? t($language->name) : $language->$field; + } + return $list; +} + +/** + * Returns an array of language names similar to ctools_language_list() except + * that additional choices have been added for ease of use. + */ + +function ctools_language_list_all() { + $languages = array( + '***CURRENT_LANGUAGE***' => t("Current user's language"), + '***DEFAULT_LANGUAGE***' => t("Default site language"), + LANGUAGE_NONE => t('Language neutral'), + ); + $languages = array_merge($languages, ctools_language_list()); + return $languages; +} \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/includes/math-expr.inc b/sites/all/modules/contrib/ctools/includes/math-expr.inc new file mode 100644 index 0000000..eeb184d --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/math-expr.inc @@ -0,0 +1,388 @@ + + +================================================================================ + +NAME + ctools_math_expr - safely evaluate math expressions + +SYNOPSIS + include('ctools_math_expr.class.php'); + $m = new ctools_math_expr; + // basic evaluation: + $result = $m->evaluate('2+2'); + // supports: order of operation; parentheses; negation; built-in functions + $result = $m->evaluate('-8(5/2)^2*(1-sqrt(4))-8'); + // create your own variables + $m->evaluate('a = e^(ln(pi))'); + // or functions + $m->evaluate('f(x,y) = x^2 + y^2 - 2x*y + 1'); + // and then use them + $result = $m->evaluate('3*f(42,a)'); + +DESCRIPTION + Use the ctools_math_expr class when you want to evaluate mathematical expressions + from untrusted sources. You can define your own variables and functions, + which are stored in the object. Try it, it's fun! + +METHODS + $m->evalute($expr) + Evaluates the expression and returns the result. If an error occurs, + prints a warning and returns false. If $expr is a function assignment, + returns true on success. + + $m->e($expr) + A synonym for $m->evaluate(). + + $m->vars() + Returns an associative array of all user-defined variables and values. + + $m->funcs() + Returns an array of all user-defined functions. + +PARAMETERS + $m->suppress_errors + Set to true to turn off warnings when evaluating expressions + + $m->last_error + If the last evaluation failed, contains a string describing the error. + (Useful when suppress_errors is on). + +AUTHOR INFORMATION + Copyright 2005, Miles Kaufmann. + +LICENSE + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are + met: + + 1 Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. The name of the author may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +*/ + +class ctools_math_expr { + var $suppress_errors = false; + var $last_error = null; + + var $v = array('e'=>2.71,'pi'=>3.14); // variables (and constants) + var $f = array(); // user-defined functions + var $vb = array('e', 'pi'); // constants + var $fb = array( // built-in functions + 'sin','sinh','arcsin','asin','arcsinh','asinh', + 'cos','cosh','arccos','acos','arccosh','acosh', + 'tan','tanh','arctan','atan','arctanh','atanh', + 'pow', 'exp', + 'sqrt','abs','ln','log', + 'time', 'ceil', 'floor', 'min', 'max', 'round'); + + function ctools_math_expr() { + // make the variables a little more accurate + $this->v['pi'] = pi(); + $this->v['e'] = exp(1); + drupal_alter('ctools_math_expression_functions', $this->fb); + } + + function e($expr) { + return $this->evaluate($expr); + } + + function evaluate($expr) { + $this->last_error = null; + $expr = trim($expr); + if (substr($expr, -1, 1) == ';') $expr = substr($expr, 0, strlen($expr)-1); // strip semicolons at the end + //=============== + // is it a variable assignment? + if (preg_match('/^\s*([a-z]\w*)\s*=\s*(.+)$/', $expr, $matches)) { + if (in_array($matches[1], $this->vb)) { // make sure we're not assigning to a constant + return $this->trigger("cannot assign to constant '$matches[1]'"); + } + if (($tmp = $this->pfx($this->nfx($matches[2]))) === false) return false; // get the result and make sure it's good + $this->v[$matches[1]] = $tmp; // if so, stick it in the variable array + return $this->v[$matches[1]]; // and return the resulting value + //=============== + // is it a function assignment? + } elseif (preg_match('/^\s*([a-z]\w*)\s*\(\s*([a-z]\w*(?:\s*,\s*[a-z]\w*)*)\s*\)\s*=\s*(.+)$/', $expr, $matches)) { + $fnn = $matches[1]; // get the function name + if (in_array($matches[1], $this->fb)) { // make sure it isn't built in + return $this->trigger("cannot redefine built-in function '$matches[1]()'"); + } + $args = explode(",", preg_replace("/\s+/", "", $matches[2])); // get the arguments + if (($stack = $this->nfx($matches[3])) === false) return false; // see if it can be converted to postfix + for ($i = 0; $iv)) { + $stack[$i] = $this->v[$token]; + } else { + return $this->trigger("undefined variable '$token' in function definition"); + } + } + } + $this->f[$fnn] = array('args'=>$args, 'func'=>$stack); + return true; + //=============== + } else { + return $this->pfx($this->nfx($expr)); // straight up evaluation, woo + } + } + + function vars() { + $output = $this->v; + unset($output['pi']); + unset($output['e']); + return $output; + } + + function funcs() { + $output = array(); + foreach ($this->f as $fnn=>$dat) + $output[] = $fnn . '(' . implode(',', $dat['args']) . ')'; + return $output; + } + + //===================== HERE BE INTERNAL METHODS ====================\\ + + // Convert infix to postfix notation + function nfx($expr) { + + $index = 0; + $stack = new ctools_math_expr_stack; + $output = array(); // postfix form of expression, to be passed to pfx() + $expr = trim(strtolower($expr)); + + $ops = array('+', '-', '*', '/', '^', '_'); + $ops_r = array('+'=>0,'-'=>0,'*'=>0,'/'=>0,'^'=>1); // right-associative operator? + $ops_p = array('+'=>0,'-'=>0,'*'=>1,'/'=>1,'_'=>1,'^'=>2); // operator precedence + + $expecting_op = false; // we use this in syntax-checking the expression + // and determining when a - is a negation + + if (preg_match("/[^\w\s+*^\/()\.,-]/", $expr, $matches)) { // make sure the characters are all good + return $this->trigger("illegal character '{$matches[0]}'"); + } + + while(1) { // 1 Infinite Loop ;) + $op = substr($expr, $index, 1); // get the first character at the current index + // find out if we're currently at the beginning of a number/variable/function/parenthesis/operand + $ex = preg_match('/^([a-z]\w*\(?|\d+(?:\.\d*)?|\.\d+|\()/', substr($expr, $index), $match); + //=============== + if ($op == '-' and !$expecting_op) { // is it a negation instead of a minus? + $stack->push('_'); // put a negation on the stack + $index++; + } elseif ($op == '_') { // we have to explicitly deny this, because it's legal on the stack + return $this->trigger("illegal character '_'"); // but not in the input expression + //=============== + } elseif ((in_array($op, $ops) or $ex) and $expecting_op) { // are we putting an operator on the stack? + if ($ex) { // are we expecting an operator but have a number/variable/function/opening parethesis? + $op = '*'; $index--; // it's an implicit multiplication + } + // heart of the algorithm: + while($stack->count > 0 and ($o2 = $stack->last()) and in_array($o2, $ops) and ($ops_r[$op] ? $ops_p[$op] < $ops_p[$o2] : $ops_p[$op] <= $ops_p[$o2])) { + $output[] = $stack->pop(); // pop stuff off the stack into the output + } + // many thanks: http://en.wikipedia.org/wiki/Reverse_Polish_notation#The_algorithm_in_detail + $stack->push($op); // finally put OUR operator onto the stack + $index++; + $expecting_op = false; + //=============== + } elseif ($op == ')' and $expecting_op) { // ready to close a parenthesis? + while (($o2 = $stack->pop()) != '(') { // pop off the stack back to the last ( + if (is_null($o2)) return $this->trigger("unexpected ')'"); + else $output[] = $o2; + } + if (preg_match("/^([a-z]\w*)\($/", $stack->last(2), $matches)) { // did we just close a function? + $fnn = $matches[1]; // get the function name + $arg_count = $stack->pop(); // see how many arguments there were (cleverly stored on the stack, thank you) + $output[] = $stack->pop(); // pop the function and push onto the output + if (in_array($fnn, $this->fb)) { // check the argument count + if($arg_count > 1) + return $this->trigger("too many arguments ($arg_count given, 1 expected)"); + } elseif (array_key_exists($fnn, $this->f)) { + if ($arg_count != count($this->f[$fnn]['args'])) + return $this->trigger("wrong number of arguments ($arg_count given, " . count($this->f[$fnn]['args']) . " expected)"); + } else { // did we somehow push a non-function on the stack? this should never happen + return $this->trigger("internal error"); + } + } + $index++; + //=============== + } elseif ($op == ',' and $expecting_op) { // did we just finish a function argument? + while (($o2 = $stack->pop()) != '(') { + if (is_null($o2)) return $this->trigger("unexpected ','"); // oops, never had a ( + else $output[] = $o2; // pop the argument expression stuff and push onto the output + } + // make sure there was a function + if (!preg_match("/^([a-z]\w*)\($/", $stack->last(2), $matches)) + return $this->trigger("unexpected ','"); + $stack->push($stack->pop()+1); // increment the argument count + $stack->push('('); // put the ( back on, we'll need to pop back to it again + $index++; + $expecting_op = false; + //=============== + } elseif ($op == '(' and !$expecting_op) { + $stack->push('('); // that was easy + $index++; + $allow_neg = true; + //=============== + } elseif ($ex and !$expecting_op) { // do we now have a function/variable/number? + $expecting_op = true; + $val = $match[1]; + if (preg_match("/^([a-z]\w*)\($/", $val, $matches)) { // may be func, or variable w/ implicit multiplication against parentheses... + if (in_array($matches[1], $this->fb) or array_key_exists($matches[1], $this->f)) { // it's a func + $stack->push($val); + $stack->push(1); + $stack->push('('); + $expecting_op = false; + } else { // it's a var w/ implicit multiplication + $val = $matches[1]; + $output[] = $val; + } + } else { // it's a plain old var or num + $output[] = $val; + } + $index += strlen($val); + //=============== + } elseif ($op == ')') { // miscellaneous error checking + return $this->trigger("unexpected ')'"); + } elseif (in_array($op, $ops) and !$expecting_op) { + return $this->trigger("unexpected operator '$op'"); + } else { // I don't even want to know what you did to get here + return $this->trigger("an unexpected error occurred"); + } + if ($index == strlen($expr)) { + if (in_array($op, $ops)) { // did we end with an operator? bad. + return $this->trigger("operator '$op' lacks operand"); + } else { + break; + } + } + while (substr($expr, $index, 1) == ' ') { // step the index past whitespace (pretty much turns whitespace + $index++; // into implicit multiplication if no operator is there) + } + + } + while (!is_null($op = $stack->pop())) { // pop everything off the stack and push onto output + if ($op == '(') return $this->trigger("expecting ')'"); // if there are (s on the stack, ()s were unbalanced + $output[] = $op; + } + return $output; + } + + // evaluate postfix notation + function pfx($tokens, $vars = array()) { + + if ($tokens == false) return false; + + $stack = new ctools_math_expr_stack; + + foreach ($tokens as $token) { // nice and easy + // if the token is a binary operator, pop two values off the stack, do the operation, and push the result back on + if (in_array($token, array('+', '-', '*', '/', '^'))) { + if (is_null($op2 = $stack->pop())) return $this->trigger("internal error"); + if (is_null($op1 = $stack->pop())) return $this->trigger("internal error"); + switch ($token) { + case '+': + $stack->push($op1+$op2); break; + case '-': + $stack->push($op1-$op2); break; + case '*': + $stack->push($op1*$op2); break; + case '/': + if ($op2 == 0) return $this->trigger("division by zero"); + $stack->push($op1/$op2); break; + case '^': + $stack->push(pow($op1, $op2)); break; + } + // if the token is a unary operator, pop one value off the stack, do the operation, and push it back on + } elseif ($token == "_") { + $stack->push(-1*$stack->pop()); + // if the token is a function, pop arguments off the stack, hand them to the function, and push the result back on + } elseif (preg_match("/^([a-z]\w*)\($/", $token, $matches)) { // it's a function! + $fnn = $matches[1]; + if (in_array($fnn, $this->fb)) { // built-in function: + if (is_null($op1 = $stack->pop())) return $this->trigger("internal error"); + $fnn = preg_replace("/^arc/", "a", $fnn); // for the 'arc' trig synonyms + if ($fnn == 'ln') $fnn = 'log'; + eval('$stack->push(' . $fnn . '($op1));'); // perfectly safe eval() + } elseif (array_key_exists($fnn, $this->f)) { // user function + // get args + $args = array(); + for ($i = count($this->f[$fnn]['args'])-1; $i >= 0; $i--) { + if (is_null($args[$this->f[$fnn]['args'][$i]] = $stack->pop())) return $this->trigger("internal error"); + } + $stack->push($this->pfx($this->f[$fnn]['func'], $args)); // yay... recursion!!!! + } + // if the token is a number or variable, push it on the stack + } else { + if (is_numeric($token)) { + $stack->push($token); + } elseif (array_key_exists($token, $this->v)) { + $stack->push($this->v[$token]); + } elseif (array_key_exists($token, $vars)) { + $stack->push($vars[$token]); + } else { + return $this->trigger("undefined variable '$token'"); + } + } + } + // when we're out of tokens, the stack should have a single element, the final result + if ($stack->count != 1) return $this->trigger("internal error"); + return $stack->pop(); + } + + // trigger an error, but nicely, if need be + function trigger($msg) { + $this->last_error = $msg; + if (!$this->suppress_errors) trigger_error($msg, E_USER_WARNING); + return false; + } +} + +// for internal use +class ctools_math_expr_stack { + + var $stack = array(); + var $count = 0; + + function push($val) { + $this->stack[$this->count] = $val; + $this->count++; + } + + function pop() { + if ($this->count > 0) { + $this->count--; + return $this->stack[$this->count]; + } + return null; + } + + function last($n=1) { + return !empty($this->stack[$this->count-$n]) ? $this->stack[$this->count-$n] : NULL; + } +} + diff --git a/sites/all/modules/contrib/ctools/includes/menu.inc b/sites/all/modules/contrib/ctools/includes/menu.inc new file mode 100644 index 0000000..e725ea4 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/menu.inc @@ -0,0 +1,98 @@ + 'menu_local_action', + '#link' => $addition, + ); + } + else { + $data['tabs'][0]['output'][] = array( + '#theme' => 'menu_local_task', + '#link' => $addition, + '#active' => (!$has_active && $root_path === $addition['href']), + ); + } + } + if (!empty($data['tabs'][0]['output'])) { + uasort($data['tabs'][0]['output'], 'ctools_menu_sort'); + $data['tabs'][0]['count'] = count($data['tabs'][0]['output']); + } + + if (!empty($data['actions']['output'])) { + uasort($data['actions']['output'], 'ctools_menu_sort'); + $data['actions']['count'] = count($data['actions']['output']); + } + } +} diff --git a/sites/all/modules/contrib/ctools/includes/modal.inc b/sites/all/modules/contrib/ctools/includes/modal.inc new file mode 100644 index 0000000..fc99015 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/modal.inc @@ -0,0 +1,262 @@ + array( + 'loadingText' => t('Loading...'), + 'closeText' => t('Close Window'), + 'closeImage' => theme('image', array( + 'path' => ctools_image_path('icon-close-window.png'), + 'title' => t('Close window'), + 'alt' => t('Close window'), + )), + 'throbber' => theme('image', array( + 'path' => ctools_image_path('throbber.gif'), + 'title' => t('Loading...'), + 'alt' => t('Loading'), + )), + ), + ); + + drupal_add_js($settings, 'setting'); + drupal_add_library('system', 'jquery.form'); + drupal_add_library('system', 'drupal.progress'); + drupal_add_library('system', 'drupal.ajax'); + drupal_add_library('system', 'ui'); + ctools_add_js('modal'); + + ctools_add_css('modal'); + $done = TRUE; +} + +/** + * @todo this is deprecated + */ +function ctools_modal_add_plugin_js($plugins) { + $css = array(); + $js = array(drupal_get_path('module', 'ctools') . '/js/dependent.js' => TRUE); + foreach ($plugins as $subtype) { + if (isset($subtype['js'])) { + foreach ($subtype['js'] as $file) { + if (file_exists($file)) { + $js[$file] = TRUE; + } + else if (file(exists($subtype['path'] . '/' . $file))) { + $js[$subtype['path'] . '/' . $file] = TRUE; + } + } + } + if (isset($subtype['css'])) { + foreach ($subtype['css'] as $file) { + if (file_exists($file)) { + $css[$file] = TRUE; + } + else if (file(exists($subtype['path'] . '/' . $file))) { + $css[$subtype['path'] . '/' . $file] = TRUE; + } + } + } + } + + foreach (array_keys($js) as $file) { + drupal_add_js($file); + } + foreach (array_keys($css) as $file) { + drupal_add_css($file); + } +} + +/** + * Place HTML within the modal. + * + * @param $title + * The title of the modal. + * @param $html + * The html to place within the modal. + */ +function ctools_modal_command_display($title, $html) { + if (is_array($html)) { + $html = drupal_render($html); + } + + return array( + 'command' => 'modal_display', + 'title' => $title, + 'output' => $html, + ); +} + +/** + * Dismiss the modal. + */ +function ctools_modal_command_dismiss() { + return array( + 'command' => 'modal_dismiss', + ); +} + +/** + * Display loading screen in the modal + */ +function ctools_modal_command_loading() { + return array( + 'command' => 'modal_loading', + ); +} + +/** + * Render an image as a button link. This will automatically apply an AJAX class + * to the link and add the appropriate javascript to make this happen. + * + * @param $image + * The path to an image to use that will be sent to theme('image') for rendering. + * @param $dest + * The destination of the link. + * @param $alt + * The alt text of the link. + * @param $class + * Any class to apply to the link. @todo this should be a options array. + */ +function ctools_modal_image_button($image, $dest, $alt, $class = '') { + return ctools_ajax_text_button(theme('image', array('path' => $image)), $dest, $alt, $class, 'ctools-use-modal'); +} + +/** + * Render text as a link. This will automatically apply an AJAX class + * to the link and add the appropriate javascript to make this happen. + * + * Note: 'html' => true so be sure any text is vetted! Chances are these kinds of buttons will + * not use user input so this is a very minor concern. + * + * @param $text + * The text that will be displayed as the link. + * @param $dest + * The destination of the link. + * @param $alt + * The alt text of the link. + * @param $class + * Any class to apply to the link. @todo this should be a options array. + */ +function ctools_modal_text_button($text, $dest, $alt, $class = '') { + return ctools_ajax_text_button($text, $dest, $alt, $class, 'ctools-use-modal'); +} + +/** + * Wrap a form so that we can use it properly with AJAX. Essentially if the + * form wishes to render, it automatically does that, otherwise it returns + * the render array so we can see submission results. + + * @param array $form + * An associative array containing the structure of the form. + * @param array $form_state + * An associative array containing the current state of the form. + * If the 'reset_html_ids' key is set to TRUE, it will prevent HTML IDs in + * forms from being incremented. + * + * @return mixed + * The output of the form, if it was rendered. If $form_state['ajax'] + * is set, this will use ctools_modal_form_render so it will be + * a $command object suitable for ajax_render already. + * + * If the form was not rendered, the raw render array will be returned. + * + * If ajax is set the form will never be redirected. + */ +function ctools_modal_form_wrapper($form_id, &$form_state) { + // Since this will run again on form rebuild while still in the modal, prevent + // form IDs from being incremented. + // @todo https://drupal.org/node/1305882 + if (!empty($form_state['reset_html_ids']) && !empty($_POST['ajax_html_ids'])) { + unset($_POST['ajax_html_ids']); + } + + // This won't override settings already in. + $form_state += array( + 're_render' => FALSE, + 'no_redirect' => !empty($form_state['ajax']), + ); + + $output = drupal_build_form($form_id, $form_state); + if (!empty($form_state['ajax']) && (!$form_state['executed'] || $form_state['rebuild'])) { + return ctools_modal_form_render($form_state, $output); + } + + return $output; +} + +/** + * Render a form into an AJAX display. + */ +function ctools_modal_form_render($form_state, $output) { + if (is_array($output)) { + $output = drupal_render($output); + } + + $title = empty($form_state['title']) ? drupal_get_title() : $form_state['title']; + + // If there are messages for the form, render them. + if ($messages = theme('status_messages')) { + $output = $messages . $output; + } + + $commands = array(); + // If the form has not yet been rendered, render it. + $commands[] = ctools_modal_command_display($title, $output); + return $commands; +} + +/** + * Perform a simple modal render and immediately exit. + * + * This is primarily used for error displays, since usually modals will + * contain forms. + */ +function ctools_modal_render($title, $output) { + $commands = array(); + $commands[] = ctools_modal_command_display($title, $output); + print ajax_render($commands); +} diff --git a/sites/all/modules/contrib/ctools/includes/object-cache.cron.inc b/sites/all/modules/contrib/ctools/includes/object-cache.cron.inc new file mode 100644 index 0000000..99f2276 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/object-cache.cron.inc @@ -0,0 +1,16 @@ + $sid, ':object' => $obj, ':name' => $name)) + ->fetchObject(); + if ($data) { + $cache[$key] = unserialize($data->data); + } + } + return isset($cache[$key]) ? $cache[$key] : NULL; +} + +/** + * Store an object in the non-volatile ctools cache. + * + * @param $obj + * A 128 character or less string to define what kind of object is being + * stored; primarily this is used to prevent collisions. + * @param $name + * The name of the object being stored. + * @param $cache + * The object to be cached. This will be serialized prior to writing. + * @param $sid + * The session id, allowing someone to use Session API or their own solution; + * defaults to session_id(). + */ +function ctools_object_cache_set($obj, $name, $cache, $sid = NULL) { + // Store the CTools session id in the user session to force a + // session for anonymous users in Drupal 7 and Drupal 6 Pressflow. + // see http://drupal.org/node/562374, http://drupal.org/node/861778 + if (empty($GLOBALS['user']->uid) && empty($_SESSION['ctools_session_id'])) { + $_SESSION['ctools_hold_session'] = TRUE; + } + + ctools_object_cache_clear($obj, $name, $sid); + + if (!$sid) { + $sid = session_id(); + } + + db_insert('ctools_object_cache') + ->fields(array( + 'sid' => $sid, + 'obj' => $obj, + 'name' => $name, + 'data' => serialize($cache), + 'updated' => REQUEST_TIME, + )) + ->execute(); +} + +/** + * Remove an object from the non-volatile ctools cache + * + * @param $obj + * A 128 character or less string to define what kind of object is being + * stored; primarily this is used to prevent collisions. + * @param $name + * The name of the object being removed. + * @param $sid + * The session id, allowing someone to use Session API or their own solution; + * defaults to session_id(). + */ +function ctools_object_cache_clear($obj, $name, $sid = NULL) { + + if (!$sid) { + $sid = session_id(); + } + + db_delete('ctools_object_cache') + ->condition('sid', $sid) + ->condition('obj', $obj) + ->condition('name', $name) + ->execute(); + // Ensure the static cache is emptied of this obj:name set. + drupal_static_reset('ctools_object_cache_get'); +} + + +/** + * Determine if another user has a given object cached. + * + * This is very useful for 'locking' objects so that only one user can + * modify them. + * + * @param $obj + * A 128 character or less string to define what kind of object is being + * stored; primarily this is used to prevent collisions. + * @param $name + * The name of the object being removed. + * @param $sid + * The session id, allowing someone to use Session API or their own solution; + * defaults to session_id(). + * + * @return + * An object containing the UID and updated date if found; NULL if not. + */ +function ctools_object_cache_test($obj, $name, $sid = NULL) { + + if (!$sid) { + $sid = session_id(); + } + + return db_query('SELECT s.uid, c.updated FROM {ctools_object_cache} c INNER JOIN {sessions} s ON c.sid = s.sid WHERE s.sid <> :session_id AND c.obj = :obj AND c.name = :name ORDER BY c.updated ASC', array(':session_id' => $sid, ':obj' => $obj, ':name' => $name)) + ->fetchObject(); +} + +/** + * Get the cache status of a group of objects. + * + * This is useful for displaying lock status when listing a number of objects + * an an administration UI. + * + * @param $obj + * A 128 character or less string to define what kind of object is being + * stored; primarily this is used to prevent collisions. + * @param $names + * An array of names of objects + * + * @return + * An array of objects containing the UID and updated date for each name found. + */ +function ctools_object_cache_test_objects($obj, $names) { + return db_query("SELECT c.name, s.uid, c.updated FROM {ctools_object_cache} c INNER JOIN {sessions} s ON c.sid = s.sid WHERE c.obj = :obj AND c.name IN (:names) ORDER BY c.updated ASC", array(':obj' => $obj, ':names' => $names)) + ->fetchAllAssoc('name'); +} + +/** + * Remove an object from the non-volatile ctools cache for all session IDs. + * + * This is useful for clearing a lock. + * + * @param $obj + * A 128 character or less string to define what kind of object is being + * stored; primarily this is used to prevent collisions. + * @param $name + * The name of the object being removed. + */ +function ctools_object_cache_clear_all($obj, $name) { + db_delete('ctools_object_cache') + ->condition('obj', $obj) + ->condition('name', $name) + ->execute(); + // Ensure the static cache is emptied of this obj:name set. + $cache = &drupal_static('ctools_object_cache_get', array()); + unset($cache["$obj:$name"]); +} + +/** + * Remove all objects in the object cache that are older than the + * specified age. + * + * @param $age + * The minimum age of objects to remove, in seconds. For example, 86400 is + * one day. Defaults to 7 days. + */ +function ctools_object_cache_clean($age = NULL) { + if (empty($age)) { + $age = 86400 * 7; // 7 days + } + db_delete('ctools_object_cache') + ->condition('updated', REQUEST_TIME - $age, '<') + ->execute(); +} diff --git a/sites/all/modules/contrib/ctools/includes/page-wizard.inc b/sites/all/modules/contrib/ctools/includes/page-wizard.inc new file mode 100644 index 0000000..a211361 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/page-wizard.inc @@ -0,0 +1,194 @@ +plugin = $plugin; + if ($function = ctools_plugin_get_function($plugin, 'default cache')) { + $function($cache); + } + + return $cache; +} + +/** + * Store changes to a task handler in the object cache. + */ +function page_manager_set_wizard_cache($cache) { + ctools_include('object-cache'); + ctools_object_cache_set('page_manager_page_wizard', $cache->plugin['name'], $cache); +} + +/** + * Remove an item from the object cache. + */ +function page_manager_clear_wizard_cache($name) { + ctools_include('object-cache'); + ctools_object_cache_clear('page_manager_page_wizard', $name); +} + +/** + * Menu callback for the page wizard. + */ +function page_manager_page_wizard($name, $step = NULL) { + $plugin = page_manager_get_page_wizard($name); + if (!$plugin) { + return MENU_NOT_FOUND; + } + + // Check for simple access string on plugin. + if (!empty($plugin['access']) && !user_access($plugin['access'])) { + return MENU_ACCESS_DENIED; + } + + // Check for possibly more complex access callback on plugin. + if ($function = ctools_plugin_get_function($plugin, 'access callback') && !$function($plugin)) { + return MENU_ACCESS_DENIED; + } + + // Create a basic wizard.in form info array and merge it with the + // plugin's. + $form_info = array( + 'id' => 'page_manager_page_wizard', + 'show trail' => TRUE, + 'show back' => TRUE, + 'show return' => FALSE, + 'show cancel' => FALSE, + 'next callback' => 'page_manager_page_wizard_next', + 'finish callback' => 'page_manager_page_wizard_finish', + + 'path' => "admin/structure/pages/wizard/$name/%step", + ); + + $form_info = array_merge_recursive($form_info, $plugin['form info']); + + // If step is unset, go with the basic step. + if (!isset($step)) { + $step = current(array_keys($form_info['order'])); + $cache = page_manager_make_wizard_cache($plugin); + } + else { + $cache = page_manager_get_wizard_cache($plugin); + } + + ctools_include('wizard'); + $form_state = array( + 'plugin' => $plugin, + 'wizard cache' => $cache, + 'type' => 'edit', + 'rerender' => TRUE, + 'step' => $step, + ); + + if (isset($plugin['page title'])) { + drupal_set_title($plugin['page title']); + } + + if ($function = ctools_plugin_get_function($form_state['plugin'], 'start')) { + $function($form_info, $step, $form_state); + } + + $output = ctools_wizard_multistep_form($form_info, $step, $form_state); + return $output; +} + +/** + * Callback generated when the add page process is finished. + */ +function page_manager_page_wizard_finish(&$form_state) { + if ($function = ctools_plugin_get_function($form_state['plugin'], 'finish')) { + $function($form_state); + } + + page_manager_clear_wizard_cache($form_state['wizard cache']->plugin['name']); +} + +/** + * Callback generated when the 'next' button is clicked. + * + * All we do here is store the cache. + */ +function page_manager_page_wizard_next(&$form_state) { + if ($function = ctools_plugin_get_function($form_state['plugin'], 'next')) { + $function($form_state); + } + + page_manager_set_wizard_cache($form_state['wizard cache']); +} + +/** + * Provide a simple administrative list of all wizards. + * + * This is called as a page callback, but can also be used by any module + * that wants to get a list of wizards for its type. + */ +function page_manager_page_wizard_list($type = NULL) { + $plugins = page_manager_get_page_wizards(); + if (empty($plugins)) { + return '

' . t('There are no wizards available at this time.') . '

'; + } + + uasort($plugins, 'ctools_plugin_sort'); + + $output = '
'; + foreach ($plugins as $id => $plugin) { + if (!$type || (isset($plugin['type']) && $plugin['type'] == $type)) { + $output .= '
' . l($plugin['title'], 'admin/structure/pages/wizard/' . $id) . '
'; + $output .= '
' . $plugin['description'] . '
'; + } + } + $output .= '
'; + + return $output; +} diff --git a/sites/all/modules/contrib/ctools/includes/page-wizard.menu.inc b/sites/all/modules/contrib/ctools/includes/page-wizard.menu.inc new file mode 100644 index 0000000..7ed932e --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/page-wizard.menu.inc @@ -0,0 +1,32 @@ + array('use page manager'), + 'file' => 'includes/page-wizard.inc', + 'type' => MENU_CALLBACK, + ); + + $items['admin/structure/pages/wizard'] = array( + 'title' => 'Wizards', + 'page callback' => 'page_manager_page_wizard_list', + 'page arguments' => array(4), + 'weight' => -5, + 'type' => MENU_LOCAL_TASK, + ) + $base; + + $items['admin/structure/pages/wizard/%'] = array( + 'title' => 'Wizard', + 'page callback' => 'page_manager_page_wizard', + 'page arguments' => array(4), + ) + $base; +} diff --git a/sites/all/modules/contrib/ctools/includes/plugins-admin.inc b/sites/all/modules/contrib/ctools/includes/plugins-admin.inc new file mode 100644 index 0000000..d4ead0a --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/plugins-admin.inc @@ -0,0 +1,208 @@ + TRUE and + * 'modal return' => TRUE should make this system work well in the modal. + * + * In addition to standard wizard fields, this supports one extra field: + * - 'default form': A callback to a 'wrapper' that will be applied to either + * the first or a marked form. This is useful for adding global features that + * are applicable to all instances of a plugin, such as identifiers, or + * contexts, or the like. + * + * @param array &$form_state + * This is a standard form state array. This system imposes some requirements + * on what will be in the form state: + * + * - 'plugin': The plugin definition being edited. + * - 'conf': The configuration that is being edited, presumed to be an array. + * Ultimately at the end, this is where the modified config will be + * found. + * - 'op': The operation, either 'add' or 'edit'. This is used to derive form + * names and can also be used to derive default values from the plugin. + * - 'step': The current step. May be null if it is the 'first' step, but + * generally needs to be defined in the path. + * + * @param string $default_form + * An optional default form that can be added. + * + * @return + * If this function returns false, no form exists. + */ +function ctools_plugin_configure_form($form_info, &$form_state) { + // Turn the forms defined in the plugin into the format the wizard needs. + _ctools_plugin_configure_create_form_info($form_info, $form_state['plugin'], $form_state['op']); + + if (empty($form_info['order'])) { + return FALSE; + } + + ctools_include('wizard'); + return ctools_wizard_multistep_form($form_info, $form_state['step'], $form_state); +} + +function _ctools_plugin_configure_create_form_info(&$form_info, $plugin_definition, $op) { + // Provide a few extra defaults. + $form_info += array( + 'id' => 'ctools_plugin_configure_form', + 'show back' => TRUE, + ); + + $add_form = isset($form_info['add form name']) ? $form_info['add form name'] : 'add form'; + $edit_form = isset($form_info['edit form name']) ? $form_info['edit form name'] : 'edit form'; + + // Figure out what the forms should actually be. Since they can be specified + // in a couple of different ways (in order to support simple declarations for + // the minimal forms but more complex declarations for powerful wizards). + if ($op == 'add') { + if (!empty($plugin_definition[$add_form])) { + $info = $plugin_definition[$add_form]; + } + } + if (!isset($info) || $op == 'edit') { + // Use the edit form for the add form if add form was completely left off. + if (!empty($plugin_definition[$edit_form])) { + $info = $plugin_definition[$edit_form]; + } + } + + // If there is a default form wrapper, but no form is supplied, + // use the wrapper as the form. + if (empty($info) && !empty($form_info['default form'])) { + $info = $form_info['default form']; + } + + // @todo we may want to make these titles more settable? + if (is_string($info)) { + if (empty($plugin_definition['title'])) { + $title = t('Configure'); + } + else if ($op == 'add') { + $title = t('Configure new !plugin_title', array('!plugin_title' => $plugin_definition['title'])); + } + else { + $title = t('Configure !plugin_title', array('!plugin_title' => $plugin_definition['title'])); + } + if (empty($form_info['order'])) { + $form_info['order'] = array(); + } + $form_info['order']['form'] = $title; + + if (empty($form_info['forms'])) { + $form_info['forms'] = array(); + } + $form_info['forms']['form'] = array( + 'title' => $title, + 'form id' => $info, + ); + + // Add the default form if one is specified. + if (!empty($form_info['default form']) && $form_info['forms']['form']['form id'] != $form_info['default form']) { + $form_info['forms']['form']['wrapper'] = $form_info['default form']; + } + + // If no submit is supplied, supply the default submit which will do the + // most obvious task. + if (!function_exists($form_info['forms']['form']['form id'] . '_submit')) { + // Store the original wrapper so we can chain it. + if (!empty($form_info['forms']['form']['wrapper'])) { + $form_info['forms']['form']['original wrapper'] = $form_info['forms']['form']['wrapper']; + } + $form_info['forms']['form']['wrapper'] = 'ctools_plugins_default_form_wrapper'; + } + } + else if (is_array($info)) { + if (empty($form_info['order'])) { + $form_info['order'] = array(); + } + if (empty($form_info['forms'])) { + $form_info['forms'] = array(); + } + $count = 0; + $base = 'step'; + $wrapper = NULL; + foreach ($info as $form_id => $title) { + $step = $base . ++$count; + if (empty($wrapper)) { + $wrapper = $step; + } + + if (is_array($title)) { + if (!empty($title['default'])) { + $wrapper = $step; + } + $title = $title['title']; + } + + $form_info['order'][$step] = $title; + $form_info['forms'][$step] = array( + 'title' => $title, + 'form id' => $form_id, + ); + } + if ($wrapper && !empty($form_info['default form'])) { + $form_info['forms'][$wrapper]['wrapper'] = $form_info['default form']; + } + } +} + +/** + * A wrapper to provide a default submit so that plugins don't have to duplicate + * a whole bunch of code to do what most of them want to do anyway. + */ +function ctools_plugins_default_form_wrapper($form, &$form_state) { + $form_info = &$form_state['form_info']; + $info = $form_info['forms'][$form_state['step']]; + + if (isset($info['original wrapper']) && function_exists($info['original wrapper'])) { + $form = $info['original wrapper']($form, $form_state); + } + + if (isset($form['buttons']['next'])) { + if (empty($form['buttons']['next']['#submit'])) { + $form['buttons']['next']['#submit'] = $form['#submit']; + } + $form['buttons']['next']['#submit'][] = 'ctools_plugins_default_form_wrapper_submit'; + } + if (isset($form['buttons']['return'])) { + if (empty($form['buttons']['return']['#submit'])) { + $form['buttons']['return']['#submit'] = $form['#submit']; + } + $form['buttons']['return']['#submit'][] = 'ctools_plugins_default_form_wrapper_submit'; + } + return $form; +} + +/** + * Provide a default storage mechanism. + */ +function ctools_plugins_default_form_wrapper_submit(&$form, &$form_state) { + foreach (array_keys($form_state['plugin']['defaults']) as $key) { + if (isset($form_state['values'][$key])) { + $form_state['conf'][$key] = $form_state['values'][$key]; + } + } +} diff --git a/sites/all/modules/contrib/ctools/includes/plugins.inc b/sites/all/modules/contrib/ctools/includes/plugins.inc new file mode 100644 index 0000000..79a6087 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/plugins.inc @@ -0,0 +1,917 @@ +=') + && version_compare($version, $current_version, '<='))) { + if (!isset($info['path'])) { + $info['path'] = drupal_get_path('module', $module); + } + $cache[$owner][$api][$module] = $info; + } + } + + // And allow themes to implement these as well. + $themes = _ctools_list_themes(); + foreach ($themes as $name => $theme) { + if (!empty($theme->info['api'][$owner][$api])) { + $info = $theme->info['api'][$owner][$api]; + if (!isset($info['version'])) { + continue; + } + + // Only process if version is between minimum and current, inclusive. + if (version_compare($info['version'], $minimum_version, '>=') && version_compare($info['version'], $current_version, '<=')) { + if (!isset($info['path'])) { + $info['path'] = ''; + } + // Because themes can't easily specify full path, we add it here + // even though we do not for modules: + $info['path'] = drupal_get_path('theme', $name) . '/' . $info['path']; + $cache[$owner][$api][$name] = $info; + } + } + } + + // Allow other modules to hook in. + drupal_alter($hook, $cache[$owner][$api], $owner, $api); + } + + return $cache[$owner][$api]; +} + +/** + * Load a group of API files. + * + * This will ask each module if they support the given API, and if they do + * it will load the specified file name. The API and the file name + * coincide by design. + * + * @param $owner + * The name of the module that controls the API. + * @param $api + * The name of the api. The api name forms the file name: + * $module.$api.inc, though this can be overridden by the module's response. + * @param $minimum_version + * The lowest version API that is compatible with this one. If a module + * reports its API as older than this, its files will not be loaded. This + * should never change during operation. + * @param $current_version + * The current version of the api. If a module reports its minimum API as + * higher than this, its files will not be loaded. This should never change + * during operation. + * + * @return + * The API information, in case you need it. + */ +function ctools_plugin_api_include($owner, $api, $minimum_version, $current_version) { + static $already_done = array(); + + $info = ctools_plugin_api_info($owner, $api, $minimum_version, $current_version); + foreach ($info as $module => $plugin_info) { + if (!isset($already_done[$owner][$api][$module])) { + if (isset($plugin_info["$api file"])) { + $file = $plugin_info["$api file"]; + } + else if (isset($plugin_info['file'])) { + $file = $plugin_info['file']; + } + else { + $file = "$module.$api.inc"; + } + + if (file_exists(DRUPAL_ROOT . "/$plugin_info[path]/$file")) { + require_once DRUPAL_ROOT . "/$plugin_info[path]/$file"; + } + else if (file_exists(DRUPAL_ROOT . "/$file")) { + require_once DRUPAL_ROOT . "/$file"; + } + $already_done[$owner][$api][$module] = TRUE; + } + } + + return $info; +} + +/** + * Find out what hook to use to determine if modules support an API. + * + * By default, most APIs will use hook_ctools_plugin_api, but some modules + * want sole ownership. This technique lets modules define what hook + * to use. + */ +function ctools_plugin_api_get_hook($owner, $api) { + // Allow modules to use their own hook for this. The only easy way to do + // this right now is with a magically named function. + if (function_exists($function = $owner . '_' . $api . '_hook_name')) { + $hook = $function(); + } + else if (function_exists($function = $owner . '_ctools_plugin_api_hook_name')) { + $hook = $function(); + } + + // Do this last so that if the $function above failed to return, we have a + // sane default. + if (empty($hook)) { + $hook = 'ctools_plugin_api'; + } + + return $hook; +} + +/** + * Fetch a group of plugins by name. + * + * @param $module + * The name of the module that utilizes this plugin system. It will be + * used to call hook_ctools_plugin_$plugin() to get more data about the plugin. + * @param $type + * The type identifier of the plugin. + * @param $id + * If specified, return only information about plugin with this identifier. + * The system will do its utmost to load only plugins with this id. + * + * @return + * An array of information arrays about the plugins received. The contents + * of the array are specific to the plugin. + */ +function ctools_get_plugins($module, $type, $id = NULL) { + // Store local caches of plugins and plugin info so we don't have to do full + // lookups every time. + static $drupal_static_fast; + if (!isset($drupal_static_fast)) { + $drupal_static_fast['plugins'] = &drupal_static('ctools_plugins', array()); + } + $plugins = &$drupal_static_fast['plugins']; + + $info = ctools_plugin_get_plugin_type_info(); + + // Bail out noisily if an invalid module/type combination is requested. + if (!isset($info[$module][$type])) { + watchdog('ctools', 'Invalid plugin module/type combination requested: module @module and type @type', array('@module' => $module, '@type' => $type), WATCHDOG_ERROR); + return array(); + } + + // Make sure our plugins array is populated. + if (!isset($plugins[$module][$type])) { + $plugins[$module][$type] = array(); + } + + // Attempt to shortcut this whole piece of code if we already have + // the requested plugin: + if ($id && array_key_exists($id, $plugins[$module][$type])) { + return $plugins[$module][$type][$id]; + } + + // Store the status of plugin loading. If a module plugin type pair is true, + // then it is fully loaded and no searching or setup needs to be done. + $setup = &drupal_static('ctools_plugin_setup', array()); + + // We assume we don't need to build a cache. + $build_cache = FALSE; + + // If the plugin info says this can be cached, check cache first. + if ($info[$module][$type]['cache'] && empty($setup[$module][$type])) { + $cache = cache_get("plugins:$module:$type", $info[$module][$type]['cache table']); + + if (!empty($cache->data)) { + // Cache load succeeded so use the cached plugin list. + $plugins[$module][$type] = $cache->data; + // Set $setup to true so we know things where loaded. + $setup[$module][$type] = TRUE; + } + else { + // Cache load failed so store that we need to build and write the cache. + $build_cache = TRUE; + } + } + + // Always load all hooks if we need them. Note we only need them now if the + // plugin asks for them. We can assume that if we have plugins we've already + // called the global hook. + if (!empty($info[$module][$type]['use hooks']) && empty($plugins[$module][$type])) { + $plugins[$module][$type] = ctools_plugin_load_hooks($info[$module][$type]); + } + + // Then see if we should load all files. We only do this if we + // want a list of all plugins or there was a cache miss. + if (empty($setup[$module][$type]) && ($build_cache || !$id)) { + $setup[$module][$type] = TRUE; + $plugins[$module][$type] = array_merge($plugins[$module][$type], ctools_plugin_load_includes($info[$module][$type])); + // If the plugin can have child plugins, and we're loading all plugins, + // go through the list of plugins we have and find child plugins. + if (!$id && !empty($info[$module][$type]['child plugins'])) { + // If a plugin supports children, go through each plugin and ask. + $temp = array(); + foreach ($plugins[$module][$type] as $name => $plugin) { + // The strpos ensures that we don't try to find children for plugins + // that are already children. + if (!empty($plugin['get children']) && function_exists($plugin['get children']) && strpos($name, ':') === FALSE) { + $temp = array_merge($plugin['get children']($plugin, $name), $temp); + } + else { + $temp[$name] = $plugin; + } + } + $plugins[$module][$type] = $temp; + } + } + + + // If we were told earlier that this is cacheable and the cache was + // empty, give something back. + if ($build_cache) { + cache_set("plugins:$module:$type", $plugins[$module][$type], $info[$module][$type]['cache table']); + } + + // If no id was requested, we are finished here: + if (!$id) { + // Use array_filter because looking for unknown plugins could cause NULL + // entries to appear in the list later. + return array_filter($plugins[$module][$type]); + } + + // Check to see if we need to look for the file + if (!array_key_exists($id, $plugins[$module][$type])) { + // If we can have child plugins, check to see if the plugin name is in the + // format of parent:child and break it up if it is. + if (!empty($info[$module][$type]['child plugins']) && strpos($id, ':') !== FALSE) { + list($parent, $child) = explode(':', $id, 2); + } + else { + $parent = $id; + } + + if (!array_key_exists($parent, $plugins[$module][$type])) { + $result = ctools_plugin_load_includes($info[$module][$type], $parent); + // Set to either what was returned or NULL. + $plugins[$module][$type][$parent] = isset($result[$parent]) ? $result[$parent] : NULL; + } + + // If we are looking for a child, and have the parent, ask the parent for the child. + if (!empty($child) && !empty($plugins[$module][$type][$parent]) && function_exists($plugins[$module][$type][$parent]['get child'])) { + $plugins[$module][$type][$id] = $plugins[$module][$type][$parent]['get child']($plugins[$module][$type][$parent], $parent, $child); + } + } + + // At this point we should either have the plugin, or a NULL. + return $plugins[$module][$type][$id]; +} + +/** + * Return the full list of plugin type info for all plugin types registered in + * the current system. + * + * This function manages its own cache getting/setting, and should always be + * used as the way to initially populate the list of plugin types. Make sure you + * call this function to properly populate the ctools_plugin_type_info static + * variable. + * + * @return array + * A multilevel array of plugin type info, the outer array keyed on module + * name and each inner array keyed on plugin type name. + */ +function ctools_plugin_get_plugin_type_info($flush = FALSE) { + static $drupal_static_fast; + if (!isset($drupal_static_fast)) { + $drupal_static_fast['info_loaded'] = &drupal_static('ctools_plugin_type_info_loaded', FALSE); + $drupal_static_fast['all_type_info'] = &drupal_static('ctools_plugin_type_info', array()); + } + $info_loaded = &$drupal_static_fast['info_loaded']; + $all_type_info = &$drupal_static_fast['all_type_info']; + + // Only trigger info loading once. + if ($info_loaded && !$flush) { + return $all_type_info; + } + $info_loaded = TRUE; + + $cache = cache_get('ctools_plugin_type_info'); + if (!empty($cache->data) && !$flush) { + // Plugin type info cache is warm, use it. + $all_type_info = $cache->data; + } + else { + // Cache expired, refill it. + foreach (module_implements('ctools_plugin_type') as $module) { + $module_infos = array(); + $function = $module . '_ctools_plugin_type'; + $module_infos = $function(); + + foreach ($module_infos as $plugin_type_name => $plugin_type_info) { + // Apply defaults. Array addition will not overwrite pre-existing keys. + $plugin_type_info += array( + 'module' => $module, + 'type' => $plugin_type_name, + 'cache' => FALSE, + 'cache table' => 'cache', + 'classes' => array(), + 'use hooks' => FALSE, + 'defaults' => array(), + 'process' => '', + 'alterable' => TRUE, + 'extension' => 'inc', + 'info file' => FALSE, + 'hook' => $module . '_' . $plugin_type_name, + 'load themes' => FALSE, + ); + $all_type_info[$module][$plugin_type_name] = $plugin_type_info; + } + } + cache_set('ctools_plugin_type_info', $all_type_info); + } + + return $all_type_info; +} + +/** + * Reset all static caches that affect the result of ctools_get_plugins(). + */ +function ctools_get_plugins_reset() { + drupal_static_reset('ctools_plugins'); + drupal_static_reset('ctools_plugin_setup'); + drupal_static_reset('ctools_plugin_load_includes'); + drupal_static_reset('ctools_plugin_api_info'); +} + +/** + * Load plugins from a directory. + * + * @param $info + * The plugin info as returned by ctools_plugin_get_info() + * @param $file + * The file to load if we're looking for just one particular plugin. + * + * @return + * An array of information created for this plugin. + */ +function ctools_plugin_load_includes($info, $filename = NULL) { + // Keep a static array so we don't hit file_scan_directory more than necessary. + $all_files = &drupal_static(__FUNCTION__, array()); + + // store static of plugin arrays for reference because they can't be reincluded. + static $plugin_arrays = array(); + + if (!isset($all_files[$info['module']][$info['type']])) { + $cache = cache_get("ctools_plugin_files:$info[module]:$info[type]"); + if ($cache) { + $all_files[$info['module']][$info['type']] = $cache->data; + } + // Do not attempt any file scan even if the cached entry was empty. + // A NULL entry here would mean the plugin just does not exists, and we + // cannot afford to run file scan on production sites normal run. + elseif (!isset($all_files[$info['module']][$info['type']])) { + $all_files[$info['module']][$info['type']] = array(); + // Load all our plugins. + $directories = ctools_plugin_get_directories($info); + $extension = (empty($info['info file']) || ($info['extension'] != 'inc')) ? $info['extension'] : 'info'; + + foreach ($directories as $module => $path) { + $all_files[$info['module']][$info['type']][$module] = file_scan_directory($path, '/\.' . $extension . '$/', array('key' => 'name')); + } + + cache_set("ctools_plugin_files:$info[module]:$info[type]", $all_files[$info['module']][$info['type']]); + } + } + $file_list = $all_files[$info['module']][$info['type']]; + $plugins = array(); + + // Iterate through all the plugin .inc files, load them and process the hook + // that should now be available. + foreach (array_filter($file_list) as $module => $files) { + if ($filename) { + $files = isset($files[$filename]) ? array($filename => $files[$filename]) : array(); + } + foreach ($files as $file) { + if (!empty($info['info file'])) { + // Parse a .info file + $result = ctools_plugin_process_info($info, $module, $file); + } + else { + // Parse a hook. + $plugin = NULL; // ensure that we don't have something leftover from earlier. + + if (isset($plugin_arrays[$file->uri])) { + $identifier = $plugin_arrays[$file->uri]; + } + else { + + require_once DRUPAL_ROOT . '/' . $file->uri; + // .inc files have a special format for the hook identifier. + // For example, 'foo.inc' in the module 'mogul' using the plugin + // whose hook is named 'borg_type' should have a function named (deep breath) + // mogul_foo_borg_type() + + // If, however, the .inc file set the quasi-global $plugin array, we + // can use that and not even call a function. Set the $identifier + // appropriately and ctools_plugin_process() will handle it. + if (isset($plugin)) { + $plugin_arrays[$file->uri] = $plugin; + $identifier = $plugin; + } + else { + $identifier = $module . '_' . $file->name; + } + } + + $result = ctools_plugin_process($info, $module, $identifier, dirname($file->uri), basename($file->uri), $file->name); + } + if (is_array($result)) { + $plugins = array_merge($plugins, $result); + } + } + } + return $plugins; +} + +/** + * Get a list of directories to search for plugins of the given type. + * + * This utilizes hook_ctools_plugin_directory() to determine a complete list of + * directories. Only modules that implement this hook and return a string + * value will have their directories included. + * + * @param $info + * The $info array for the plugin as returned by ctools_plugin_get_info(). + * + * @return array $directories + * An array of directories to search. + */ +function ctools_plugin_get_directories($info) { + $directories = array(); + + foreach (module_implements('ctools_plugin_directory') as $module) { + $function = $module . '_ctools_plugin_directory'; + $result = $function($info['module'], $info['type']); + if ($result && is_string($result)) { + $directories[$module] = drupal_get_path('module', $module) . '/' . $result; + } + } + + if (!empty($info['load themes'])) { + $themes = _ctools_list_themes(); + foreach ($themes as $name => $theme) { + if (!empty($theme->info['plugins'][$info['module']][$info['type']])) { + $directories[$name] = drupal_get_path('theme', $name) . '/' . $theme->info['plugins'][$info['module']][$info['type']]; + } + } + } + return $directories; +} + +/** + * Helper function to build a ctools-friendly list of themes capable of + * providing plugins. + * + * @return array $themes + * A list of themes that can act as plugin providers, sorted parent-first with + * the active theme placed last. + */ +function _ctools_list_themes() { + static $themes; + if (is_null($themes)) { + $current = variable_get('theme_default', FALSE); + $themes = $active = array(); + $all_themes = list_themes(); + foreach ($all_themes as $name => $theme) { + // Only search from active themes + if (empty($theme->status) && $theme->name != $current) { + continue; + } + $active[$name] = $theme; + // Prior to drupal 6.14, $theme->base_themes does not exist. Build it. + if (!isset($theme->base_themes) && !empty($theme->base_theme)) { + $active[$name]->base_themes = ctools_find_base_themes($all_themes, $name); + } + } + + // Construct a parent-first list of all themes + foreach ($active as $name => $theme) { + $base_themes = isset($theme->base_themes) ? $theme->base_themes : array(); + $themes = array_merge($themes, $base_themes, array($name => $theme->info['name'])); + } + // Put the actual theme info objects into the array + foreach (array_keys($themes) as $name) { + if (isset($all_themes[$name])) { + $themes[$name] = $all_themes[$name]; + } + } + + // Make sure the current default theme always gets the last word + if ($current_key = array_search($current, array_keys($themes))) { + $themes += array_splice($themes, $current_key, 1); + } + } + return $themes; +} + + +/** + * Find all the base themes for the specified theme. + * + * Themes can inherit templates and function implementations from earlier themes. + * + * NOTE: this is a verbatim copy of system_find_base_themes(), which was not + * implemented until 6.14. It is included here only as a fallback for outdated + * versions of drupal core. + * + * @param $themes + * An array of available themes. + * @param $key + * The name of the theme whose base we are looking for. + * @param $used_keys + * A recursion parameter preventing endless loops. + * @return + * Returns an array of all of the theme's ancestors; the first element's value + * will be NULL if an error occurred. + */ +function ctools_find_base_themes($themes, $key, $used_keys = array()) { + $base_key = $themes[$key]->info['base theme']; + // Does the base theme exist? + if (!isset($themes[$base_key])) { + return array($base_key => NULL); + } + + $current_base_theme = array($base_key => $themes[$base_key]->info['name']); + + // Is the base theme itself a child of another theme? + if (isset($themes[$base_key]->info['base theme'])) { + // Do we already know the base themes of this theme? + if (isset($themes[$base_key]->base_themes)) { + return $themes[$base_key]->base_themes + $current_base_theme; + } + // Prevent loops. + if (!empty($used_keys[$base_key])) { + return array($base_key => NULL); + } + $used_keys[$base_key] = TRUE; + return ctools_find_base_themes($themes, $base_key, $used_keys) + $current_base_theme; + } + // If we get here, then this is our parent theme. + return $current_base_theme; +} + + +/** + * Load plugin info for the provided hook; this is handled separately from + * plugins from files. + * + * @param $info + * The info array about the plugin as created by ctools_plugin_get_info() + * + * @return + * An array of info supplied by any hook implementations. + */ +function ctools_plugin_load_hooks($info) { + $hooks = array(); + foreach (module_implements($info['hook']) as $module) { + $result = ctools_plugin_process($info, $module, $module, drupal_get_path('module', $module)); + if (is_array($result)) { + $hooks = array_merge($hooks, $result); + } + } + return $hooks; +} + +/** + * Process a single hook implementation of a ctools plugin. + * + * @param $info + * The $info array about the plugin as returned by ctools_plugin_get_info() + * @param $module + * The module that implements the plugin being processed. + * @param $identifier + * The plugin identifier, which is used to create the name of the hook + * function being called. + * @param $path + * The path where files utilized by this plugin will be found. + * @param $file + * The file that was loaded for this plugin, if it exists. + * @param $base + * The base plugin name to use. If a file was loaded for the plugin, this + * is the plugin to assume must be present. This is used to automatically + * translate the array to make the syntax more friendly to plugin + * implementors. + */ +function ctools_plugin_process($info, $module, $identifier, $path, $file = NULL, $base = NULL) { + if (is_array($identifier)) { + $result = $identifier; + } + else { + $function = $identifier . '_' . $info['hook']; + if (!function_exists($function)) { + return NULL; + } + $result = $function($info); + if (!isset($result) || !is_array($result)) { + return NULL; + } + } + + // Automatically convert to the proper format that lets plugin implementations + // not nest arrays as deeply as they used to, but still support the older + // format where they do: + if ($base && (!isset($result[$base]) || !is_array($result[$base]))) { + $result = array($base => $result); + } + + return _ctools_process_data($result, $info, $module, $path, $file); +} + +/** + * Fill in default values and run hooks for data loaded for one or + * more plugins. + */ +function _ctools_process_data($result, $plugin_type_info, $module, $path, $file) { + // Fill in global defaults. + foreach ($result as $name => $plugin) { + $result[$name] += array( + 'module' => $module, + 'name' => $name, + 'path' => $path, + 'file' => $file, + 'plugin module' => $plugin_type_info['module'], + 'plugin type' => $plugin_type_info['type'], + ); + + // Fill in plugin-specific defaults, if they exist. + if (!empty($plugin_type_info['defaults'])) { + if (is_array($plugin_type_info['defaults'])) { + $result[$name] += $plugin_type_info['defaults']; + } + } + + // Allow the plugin to be altered before processing. + if (!empty($plugin_type_info['alterable']) && $plugin_type_info['alterable']) { + drupal_alter('ctools_plugin_pre', $result[$name], $plugin_type_info); + } + + // Allow the plugin owner to do additional processing. + if (!empty($plugin_type_info['process']) && $function = ctools_plugin_get_function($plugin_type_info, 'process')) { + $function($result[$name], $plugin_type_info); + } + + // Allow the plugin to be altered after processing. + if (!empty($plugin_type_info['alterable']) && $plugin_type_info['alterable']) { + drupal_alter('ctools_plugin_post', $result[$name], $plugin_type_info); + } + } + return $result; +} + + +/** + * Process an info file for plugin information, rather than a hook. + */ +function ctools_plugin_process_info($info, $module, $file) { + $result = drupal_parse_info_file($file->uri); + if ($result) { + $result = array($file->name => $result); + return _ctools_process_data($result, $info, $module, dirname($file->uri), basename($file->uri)); + } +} + +/** + * Ask a module for info about a particular plugin type. + */ +function ctools_plugin_get_info($module, $type) { + $all_info = ctools_plugin_get_plugin_type_info(); + return isset($all_info[$module][$type]) ? $all_info[$module][$type] : array(); +} + +/** + * Get a function from a plugin, if it exists. If the plugin is not already + * loaded, try ctools_plugin_load_function() instead. + * + * @param $plugin_definition + * The loaded plugin type. + * @param $function_name + * The identifier of the function. For example, 'settings form'. + * + * @return + * The actual name of the function to call, or NULL if the function + * does not exist. + */ +function ctools_plugin_get_function($plugin_definition, $function_name) { + // If cached the .inc file may not have been loaded. require_once is quite safe + // and fast so it's okay to keep calling it. + if (isset($plugin_definition['file'])) { + // Plugins that are loaded from info files have the info file as + // $plugin['file']. Don't try to run those. + $info = ctools_plugin_get_info($plugin_definition['plugin module'], $plugin_definition['plugin type']); + if (empty($info['info file'])) { + require_once DRUPAL_ROOT . '/' . $plugin_definition['path'] . '/' . $plugin_definition['file']; + } + } + + if (!isset($plugin_definition[$function_name])) { + return; + } + + if (is_array($plugin_definition[$function_name]) && isset($plugin_definition[$function_name]['function'])) { + $function = $plugin_definition[$function_name]['function']; + if (isset($plugin_definition[$function_name]['file'])) { + $file = $plugin_definition[$function_name]['file']; + if (isset($plugin_definition[$function_name]['path'])) { + $file = $plugin_definition[$function_name]['path'] . '/' . $file; + } + require_once DRUPAL_ROOT . '/' . $file; + } + } + else { + $function = $plugin_definition[$function_name]; + } + + if (function_exists($function)) { + return $function; + } +} + +/** + * Load a plugin and get a function name from it, returning success only + * if the function exists. + * + * @param $module + * The module that owns the plugin type. + * @param $type + * The type of plugin. + * @param $id + * The id of the specific plugin to load. + * @param $function_name + * The identifier of the function. For example, 'settings form'. + * + * @return + * The actual name of the function to call, or NULL if the function + * does not exist. + */ +function ctools_plugin_load_function($module, $type, $id, $function_name) { + $plugin = ctools_get_plugins($module, $type, $id); + return ctools_plugin_get_function($plugin, $function_name); +} + +/** + * Get a class from a plugin, if it exists. If the plugin is not already + * loaded, try ctools_plugin_load_class() instead. + * + * @param $plugin_definition + * The loaded plugin type. + * @param $class_name + * The identifier of the class. For example, 'handler'. + * + * @return + * The actual name of the class to call, or NULL if the class does not exist. + */ +function ctools_plugin_get_class($plugin_definition, $class_name) { + // If cached the .inc file may not have been loaded. require_once is quite safe + // and fast so it's okay to keep calling it. + if (isset($plugin_definition['file'])) { + // Plugins that are loaded from info files have the info file as + // $plugin['file']. Don't try to run those. + $info = ctools_plugin_get_info($plugin_definition['plugin module'], $plugin_definition['plugin type']); + if (empty($info['info file'])) { + require_once DRUPAL_ROOT . '/' . $plugin_definition['path'] . '/' . $plugin_definition['file']; + } + } + + $return = FALSE; + if (!isset($plugin_definition[$class_name])) { + return; + } + else if (is_string($plugin_definition[$class_name])) { + // Plugin uses the string form shorthand. + $return = $plugin_definition[$class_name]; + } + else if (isset($plugin_definition[$class_name]['class'])) { + // Plugin uses the verbose array form. + $return = $plugin_definition[$class_name]['class']; + } + // @todo consider adding an else {watchdog(...)} here + + return ($return && class_exists($return)) ? $return : NULL; +} + +/** + * Load a plugin and get a class name from it, returning success only if the + * class exists. + * + * @param $module + * The module that owns the plugin type. + * @param $type + * The type of plugin. + * @param $id + * The id of the specific plugin to load. + * @param $class_name + * The identifier of the class. For example, 'handler'. + * + * @return + * The actual name of the class to call, or NULL if the class does not exist. + */ +function ctools_plugin_load_class($module, $type, $id, $class_name) { + $plugin = ctools_get_plugins($module, $type, $id); + return ctools_plugin_get_class($plugin, $class_name); +} + +/** + * Sort callback for sorting plugins naturally. + * + * Sort first by weight, then by title. + */ +function ctools_plugin_sort($a, $b) { + if (is_object($a)) { + $a = (array) $a; + } + if (is_object($b)) { + $b = (array) $b; + } + + if (empty($a['weight'])) { + $a['weight'] = 0; + } + + if (empty($b['weight'])) { + $b['weight'] = 0; + } + + if ($a['weight'] == $b['weight']) { + return strnatcmp(strtolower($a['title']), strtolower($b['title'])); + } + return ($a['weight'] < $b['weight']) ? -1 : 1; +} diff --git a/sites/all/modules/contrib/ctools/includes/registry.inc b/sites/all/modules/contrib/ctools/includes/registry.inc new file mode 100644 index 0000000..9d4328e --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/registry.inc @@ -0,0 +1,77 @@ +name] = $module_object; + } + + $all_type_info = ctools_plugin_get_plugin_type_info(TRUE); + foreach ($all_type_info as $module => $plugin_types) { + foreach ($plugin_types as $plugin_type_name => $plugin_type_info) { + if (empty($plugin_type_info['classes'])) { + // This plugin type does not use classes, so skip it. + continue; + } + + // Retrieve the full list of plugins of this type. + $plugins = ctools_get_plugins($module, $plugin_type_name); + foreach ($plugins as $plugin_name => $plugin_definition) { + foreach ($plugin_type_info['classes'] as $class_key) { + if (empty($plugin_definition[$class_key])) { + // Plugin doesn't provide a class for this class key, so skip it. + continue; + } + + if (is_string($plugin_definition[$class_key])) { + // Plugin definition uses the shorthand for defining a class name + // and location; look for the containing file by following naming + // convention. + $path = $plugin_definition['path'] . '/' . $plugin_definition[$class_key] . '.class.php'; + } + else { + // Plugin uses the verbose definition to indicate where its class + // files are. + $class = $plugin_definition[$class_key]['class']; + // Use the filename if it's explicitly set, else follow the naming + // conventions. + $filename = isset($plugin_definition[$class_key]['file']) ? $plugin_definition[$class_key]['file'] : $class . '.class.php'; + $base_path = isset($plugin_definition[$class_key]['path']) ? $plugin_definition[$class_key]['path'] : $plugin_definition['path']; + $path = "$base_path/$filename"; + } + + if (file_exists($path)) { + // If the file exists, add it to the files for registry building. + $files[$path] = array('module' => $plugin_definition['module'], 'weight' => $modules[$plugin_definition['module']]->weight); + } + else { + // Else, watchdog that we've got some erroneous plugin info. + $args = array( + '@plugin' => $plugin_definition['name'], + '@owner' => $module, + '@type' => $plugin_type_name, + '@file' => $path, + '@class' => $class_key, + ); + watchdog('ctools', 'Plugin @plugin of plugin type @owner:@type points to nonexistent file @file for class handler @class.', $args); + } + } + } + } + } +} diff --git a/sites/all/modules/contrib/ctools/includes/stylizer.inc b/sites/all/modules/contrib/ctools/includes/stylizer.inc new file mode 100644 index 0000000..5bc8450 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/stylizer.inc @@ -0,0 +1,1654 @@ + theme('image', array('path' => $file)), 'title' => $title)); +} + +/** + * Theme the style icon image + */ +function theme_ctools_style_icon($vars) { + $image = $vars['image']; + ctools_add_css('stylizer'); + ctools_add_js('stylizer'); + $output = '
'; + $output .= $vars['image']; + if ($vars['title']) { + $output .= '
' . $vars['title'] . '
'; + } + $output .= '
'; + return $output; +} + +/** + * Add the necessary CSS for a stylizer plugin to the page. + * + * This will check to see if the images directory and the cached CSS + * exists and, if not, will regenerate everything needed. + */ +function ctools_stylizer_add_css($plugin, $settings) { + if (!file_exists(ctools_stylizer_get_image_path($plugin, $settings, FALSE))) { + ctools_stylizer_build_style($plugin, $settings, TRUE); + return; + } + + ctools_include('css'); + $filename = ctools_css_retrieve(ctools_stylizer_get_css_id($plugin, $settings)); + if (!$filename) { + ctools_stylizer_build_style($plugin, $settings, TRUE); + } + else { + drupal_add_css($filename); + } +} + +/** + * Build the files for a stylizer given the proper settings. + */ +function ctools_stylizer_build_style($plugin, $settings, $add_css = FALSE) { + $path = ctools_stylizer_get_image_path($plugin, $settings); + if (!$path) { + return; + } + + $replacements = array(); + + // Set up palette conversions + foreach ($settings['palette'] as $key => $color) { + $replacements['%' . $key ] = $color; + } + + // Process image actions: + if (!empty($plugin['actions'])) { + $processor = new ctools_stylizer_image_processor; + $processor->execute($path, $plugin, $settings); + +// @todo -- there needs to be an easier way to get at this. +// dsm($processor->message_log); + // Add filenames to our conversions. + } + + // Convert and write the CSS file. + $css = file_get_contents($plugin['path'] . '/' . $plugin['css']); + + // Replace %style keyword with our generated class name. + // @todo We need one more unique identifier I think. + $class = ctools_stylizer_get_css_class($plugin, $settings); + $replacements['%style'] = '.' . $class; + + if (!empty($processor) && !empty($processor->paths)) { + foreach ($processor->paths as $file => $image) { + $replacements[$file] = file_create_url($image); + } + } + + if (!empty($plugin['build']) && function_exists($plugin['build'])) { + $plugin['build']($plugin, $settings, $css, $replacements); + } + + $css = strtr($css, $replacements); + ctools_include('css'); + $filename = ctools_css_store(ctools_stylizer_get_css_id($plugin, $settings), $css, FALSE); + + if ($add_css) { + drupal_add_css($filename); + } +} + +/** + * Clean up no longer used files. + * + * To prevent excess clutter in the files directory, this should be called + * whenever a style is going out of use. When being deleted, but also when + * the palette is being changed. + */ +function ctools_stylizer_cleanup_style($plugin, $settings) { + ctools_include('css'); + $path = ctools_stylizer_get_image_path($plugin, $settings, FALSE); + if ($path) { + ctools_stylizer_recursive_delete($path); + } + + ctools_css_clear(ctools_stylizer_get_css_id($plugin, $settings)); +} + +/** + * Recursively delete all files and folders in the specified filepath, then + * delete the containing folder. + * + * Note that this only deletes visible files with write permission. + * + * @param string $path + * A filepath relative to file_directory_path. + */ +function ctools_stylizer_recursive_delete($path) { + if (empty($path)) { + return; + } + + $listing = $path . '/*'; + + foreach (glob($listing) as $file) { + if (is_file($file) === TRUE) { + @unlink($file); + } + elseif (is_dir($file) === TRUE) { + ctools_stylizer_recursive_delete($file); + } + } + + @rmdir($path); +} + +/** + * Get a safe name for the settings. + * + * This uses an md5 of the palette if the name is temporary so + * that multiple temporary styles on the same page can coexist + * safely. + */ +function ctools_stylizer_get_settings_name($settings) { + if ($settings['name'] != '_temporary') { + return $settings['name']; + } + + return $settings['name'] . '-' . md5(serialize($settings['palette'])); +} + +/** + * Get the path where images will be stored for a given style plugin and settings. + * + * This function will make sure the path exists. + */ +function ctools_stylizer_get_image_path($plugin, $settings, $check = TRUE) { + $path = 'public://ctools/style/' . $settings['name'] . '/' . md5(serialize($settings['palette'])); + if (!file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { + drupal_set_message(t('Unable to create CTools styles cache directory @path. Check the permissions on your files directory.', array('@path' => $path)), 'error'); + return; + } + + return $path; +} + +/** + * Get the id used to cache CSS for a given style plugin and settings. + */ +function ctools_stylizer_get_css_id($plugin, $settings) { + return 'ctools-stylizer:' . $settings['name'] . ':' . md5(serialize($settings['palette'])); +} + +/** + * Get the class to use for a stylizer plugin. + */ +function ctools_stylizer_get_css_class($plugin, $settings) { + ctools_include('cleanstring'); + return ctools_cleanstring($plugin['name'] . '-' . ctools_stylizer_get_settings_name($settings)); +} + +class ctools_stylizer_image_processor { + var $workspace = NULL; + var $name = NULL; + + var $workspaces = array(); + + var $message_log = array(); + var $error_log = array(); + + function execute($path, $plugin, $settings) { + $this->path = $path; + $this->plugin = $plugin; + $this->settings = $settings; + $this->palette = $settings['palette']; + + if (is_string($plugin['actions']) && function_exists($plugin['actions'])) { + $actions = $plugin['actions']($plugin, $settings); + } + else if (is_array($plugin['actions'])) { + $actions = $plugin['actions']; + } + + if (!empty($actions) && is_array($actions)) { + foreach ($plugin['actions'] as $action) { + $command = 'command_' . array_shift($action); + if (method_exists($this, $command)) { + call_user_func_array(array($this, $command), $action); + } + } + } + + // Clean up buffers. + foreach ($this->workspaces as $name => $workspace) { + imagedestroy($this->workspaces[$name]); + } + } + + function log($message, $type = 'normal') { + $this->message_log[] = $message; + if ($type == 'error') { + $this->error_log[] = $message; + } + } + + function set_current_workspace($workspace) { + $this->log("Set current workspace: $workspace"); + $this->workspace = &$this->workspaces[$workspace]; + $this->name = $workspace; + } + + /** + * Create a new workspace. + */ + function command_new($name, $width, $height) { + $this->log("New workspace: $name ($width x $height)"); + // Clean up if there was already a workspace there. + if (isset($this->workspaces[$name])) { + imagedestroy($this->workspaces[$name]); + } + + $this->workspaces[$name] = imagecreatetruecolor($width, $height); + $this->set_current_workspace($name); + + // Make sure the new workspace has a transparent color. + + // Turn off transparency blending (temporarily) + imagealphablending($this->workspace, FALSE); + + // Create a new transparent color for image + $color = imagecolorallocatealpha($this->workspace, 0, 0, 0, 127); + + // Completely fill the background of the new image with allocated color. + imagefill($this->workspace, 0, 0, $color); + + // Restore transparency blending + imagesavealpha($this->workspace, TRUE); + + } + + /** + * Create a new workspace a file. + * + * This will make the new workspace the current workspace. + */ + function command_load($name, $file) { + $this->log("New workspace: $name (from $file)"); + if (!file_exists($file)) { + // Try it relative to the plugin + $file = $this->plugin['path'] . '/' . $file; + if (!file_exists($file)) { + $this->log("Unable to open $file"); + return; + } + } + + // Clean up if there was already a workspace there. + if (isset($this->workspaces[$name])) { + imagedestroy($this->workspaces[$name]); + } + + $this->workspaces[$name] = imagecreatefrompng($file); + $this->set_current_workspace($name); + } + + /** + * Create a new workspace using the properties of an existing workspace + */ + function command_new_from($name, $workspace) { + $this->log("New workspace: $name from existing $workspace"); + if (empty($this->workspaces[$workspace])) { + $this->log("Workspace $name does not exist.", 'error'); + return; + } + + // Clean up if there was already a workspace there. + if (isset($this->workspaces[$name])) { + imagedestroy($this->workspaces[$name]); + } + + $this->workspaces[$name] = $this->new_image($this->workspace[$workspace]); + $this->set_current_workspace($name); + } + + /** + * Set the current workspace. + */ + function command_workspace($name) { + $this->log("Set workspace: $name"); + if (empty($this->workspaces[$name])) { + $this->log("Workspace $name does not exist.", 'error'); + return; + } + $this->set_current_workspace($name); + } + + /** + * Copy the contents of one workspace into the current workspace. + */ + function command_merge_from($workspace, $x = 0, $y = 0) { + $this->log("Merge from: $workspace ($x, $y)"); + if (empty($this->workspaces[$workspace])) { + $this->log("Workspace $name does not exist.", 'error'); + return; + } + + $this->merge($this->workspaces[$workspace], $this->workspace, $x, $y); + } + + function command_merge_to($workspace, $x = 0, $y = 0) { + $this->log("Merge to: $workspace ($x, $y)"); + if (empty($this->workspaces[$workspace])) { + $this->log("Workspace $name does not exist.", 'error'); + return; + } + + $this->merge($this->workspace, $this->workspaces[$workspace], $x, $y); + $this->set_current_workspace($workspace); + } + + /** + * Blend an image into the current workspace. + */ + function command_merge_from_file($file, $x = 0, $y = 0) { + $this->log("Merge from file: $file ($x, $y)"); + if (!file_exists($file)) { + // Try it relative to the plugin + $file = $this->plugin['path'] . '/' . $file; + if (!file_exists($file)) { + $this->log("Unable to open $file"); + return; + } + } + + $source = imagecreatefrompng($file); + + $this->merge($source, $this->workspace, $x, $y); + + imagedestroy($source); + } + + function command_fill($color, $x, $y, $width, $height) { + $this->log("Fill: $color ($x, $y, $width, $height)"); + imagefilledrectangle($this->workspace, $x, $y, $x + $width, $y + $height, _color_gd($this->workspace, $this->palette[$color])); + } + + function command_gradient($from, $to, $x, $y, $width, $height, $direction = 'down') { + $this->log("Gradient: $from to $to ($x, $y, $width, $height) $direction"); + + if ($direction == 'down') { + for ($i = 0; $i < $height; ++$i) { + $color = _color_blend($this->workspace, $this->palette[$from], $this->palette[$to], $i / ($height - 1)); + imagefilledrectangle($this->workspace, $x, $y + $i, $x + $width, $y + $i + 1, $color); + } + } + else { + for ($i = 0; $i < $width; ++$i) { + $color = _color_blend($this->workspace, $this->palette[$from], $this->palette[$to], $i / ($width - 1)); + imagefilledrectangle($this->workspace, $x + $i, $y, $x + $i + 1, $y + $height, $color); + } + } + } + + /** + * Colorize the current workspace with the given location. + * + * This uses simple color blending to colorize the image. + * + * @todo it is possible that this colorize could allow different methods for + * determining how to blend colors? + */ + function command_colorize($color, $x = NULL, $y = NULL, $width = NULL, $height = NULL) { + if (!isset($x)) { + $whole_image = TRUE; + $x = $y = 0; + $width = imagesx($this->workspace); + $height = imagesy($this->workspace); + } + $this->log("Colorize: $color ($x, $y, $width, $height)"); + + $c = _color_unpack($this->palette[$color]); + + imagealphablending($this->workspace, FALSE); + imagesavealpha($this->workspace, TRUE); + + // If PHP 5 use the nice imagefilter which is faster. + if (!empty($whole_image) && version_compare(phpversion(), '5.2.5', '>=') && function_exists('imagefilter')) { + imagefilter($this->workspace, IMG_FILTER_COLORIZE, $c[0], $c[1], $c[2]); + } + else { + // Otherwise we can do it the brute force way. + for ($j = 0; $j < $height; $j++) { + for ($i = 0; $i < $width; $i++) { + $current = imagecolorsforindex($this->workspace, imagecolorat($this->workspace, $i, $j)); + $new_index = imagecolorallocatealpha($this->workspace, $c[0], $c[1], $c[2], $current['alpha']); + imagesetpixel($this->workspace, $i, $j, $new_index); + } + } + } + } + + /** + * Colorize the current workspace with the given location. + * + * This uses a color replacement algorithm that retains luminosity but + * turns replaces all color with the specified color. + */ + function command_hue($color, $x = NULL, $y = NULL, $width = NULL, $height = NULL) { + if (!isset($x)) { + $whole_image = TRUE; + $x = $y = 0; + $width = imagesx($this->workspace); + $height = imagesy($this->workspace); + } + $this->log("Hue: $color ($x, $y, $width, $height)"); + + list($red, $green, $blue) = _color_unpack($this->palette[$color]); + + // We will create a monochromatic palette based on the input color + // which will go from black to white. + + // Input color luminosity: this is equivalent to the position of the + // input color in the monochromatic palette + $luminosity_input = round(255 * ($red + $green + $blue) / 765); // 765 = 255 * 3 + + // We fill the palette entry with the input color at itscorresponding position + $palette[$luminosity_input]['red'] = $red; + $palette[$luminosity_input]['green'] = $green; + $palette[$luminosity_input]['blue'] = $blue; + + // Now we complete the palette, first we'll do it to the black, and then to + // the white. + + // From input to black + $steps_to_black = $luminosity_input; + + // The step size for each component + if ($steps_to_black) { + $step_size_red = $red / $steps_to_black; + $step_size_green = $green / $steps_to_black; + $step_size_blue = $blue / $steps_to_black; + + for ($i = $steps_to_black; $i >= 0; $i--) { + $palette[$steps_to_black-$i]['red'] = $red - round($step_size_red * $i); + $palette[$steps_to_black-$i]['green'] = $green - round($step_size_green * $i); + $palette[$steps_to_black-$i]['blue'] = $blue - round($step_size_blue * $i); + } + } + + // From input to white + $steps_to_white = 255 - $luminosity_input; + + if ($steps_to_white) { + $step_size_red = (255 - $red) / $steps_to_white; + $step_size_green = (255 - $green) / $steps_to_white; + $step_size_blue = (255 - $blue) / $steps_to_white; + } + else { + $step_size_red=$step_size_green=$step_size_blue=0; + } + + // The step size for each component + for ($i = ($luminosity_input + 1); $i <= 255; $i++) { + $palette[$i]['red'] = $red + round($step_size_red * ($i - $luminosity_input)); + $palette[$i]['green'] = $green + round($step_size_green * ($i - $luminosity_input)); + $palette[$i]['blue']= $blue + round($step_size_blue * ($i - $luminosity_input)); + } + + // Go over the specified area of the image and update the colors. + for ($j = $x; $j < $height; $j++) { + for ($i = $y; $i < $width; $i++) { + $color = imagecolorsforindex($this->workspace, imagecolorat($this->workspace, $i, $j)); + $luminosity = round(255 * ($color['red'] + $color['green'] + $color['blue']) / 765); + $new_color = imagecolorallocatealpha($this->workspace, $palette[$luminosity]['red'], $palette[$luminosity]['green'], $palette[$luminosity]['blue'], $color['alpha']); + imagesetpixel($this->workspace, $i, $j, $new_color); + } + } + } + + /** + * Take a slice out of the current workspace and save it as an image. + */ + function command_slice($file, $x = NULL, $y = NULL, $width = NULL, $height = NULL) { + if (!isset($x)) { + $x = $y = 0; + $width = imagesx($this->workspace); + $height = imagesy($this->workspace); + } + + $this->log("Slice: $file ($x, $y, $width, $height)"); + + $base = basename($file); + $image = $this->path . '/' . $base; + + $slice = $this->new_image($this->workspace, $width, $height); + imagecopy($slice, $this->workspace, 0, 0, $x, $y, $width, $height); + + // Make sure alphas are saved: + imagealphablending($slice, FALSE); + imagesavealpha($slice, TRUE); + + // Save image. + $temp_name = drupal_tempnam('temporary://', 'file'); + + imagepng($slice, drupal_realpath($temp_name)); + file_unmanaged_move($temp_name, $image); + imagedestroy($slice); + + // Set standard file permissions for webserver-generated files + @chmod(realpath($image), 0664); + + $this->paths[$file] = $image; + } + + /** + * Prepare a new image for being copied or worked on, preserving transparency. + */ + function &new_image(&$source, $width = NULL, $height = NULL) { + if (!isset($width)) { + $width = imagesx($source); + } + + if (!isset($height)) { + $height = imagesy($source); + } + + $target = imagecreatetruecolor($width, $height); + imagealphablending($target, FALSE); + imagesavealpha($target, TRUE); + + $transparency_index = imagecolortransparent($source); + + // If we have a specific transparent color + if ($transparency_index >= 0) { + // Get the original image's transparent color's RGB values + $transparent_color = imagecolorsforindex($source, $transparency_index); + + // Allocate the same color in the new image resource + $transparency_index = imagecolorallocate($target, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']); + + // Completely fill the background of the new image with allocated color. + imagefill($target, 0, 0, $transparency_index); + + // Set the background color for new image to transparent + imagecolortransparent($target, $transparency_index); + } + // Always make a transparent background color for PNGs that don't have one allocated already + else { + // Create a new transparent color for image + $color = imagecolorallocatealpha($target, 0, 0, 0, 127); + + // Completely fill the background of the new image with allocated color. + imagefill($target, 0, 0, $color); + } + + return $target; + } + + /** + * Merge two images together, preserving alpha transparency. + */ + function merge(&$from, &$to, $x, $y) { + // Blend over template. + $width = imagesx($from); + $height = imagesy($from); + + // Re-enable alpha blending to make sure transparency merges. + imagealphablending($to, TRUE); + imagecopy($to, $from, $x, $y, 0, 0, $width, $height); + imagealphablending($to, FALSE); + } +} + +/** + * Get the cached changes to a given task handler. + */ +function ctools_stylizer_get_settings_cache($name) { + ctools_include('object-cache'); + return ctools_object_cache_get('ctools_stylizer_settings', $name); +} + +/** + * Store changes to a task handler in the object cache. + */ +function ctools_stylizer_set_settings_cache($name, $settings) { + ctools_include('object-cache'); + ctools_object_cache_set('ctools_stylizer_settings', $name, $settings); +} + +/** + * Remove an item from the object cache. + */ +function ctools_stylizer_clear_settings_cache($name) { + ctools_include('object-cache'); + ctools_object_cache_clear('ctools_stylizer_settings', $name); +} + +/** + * Add a new style of the specified type. + */ +function ctools_stylizer_edit_style(&$info, $js, $step = NULL) { + $name = '::new'; + $form_info = array( + 'id' => 'ctools_stylizer_edit_style', + 'path' => $info['path'], + 'show trail' => TRUE, + 'show back' => TRUE, + 'show return' => FALSE, + 'next callback' => 'ctools_stylizer_edit_style_next', + 'finish callback' => 'ctools_stylizer_edit_style_finish', + 'return callback' => 'ctools_stylizer_edit_style_finish', + 'cancel callback' => 'ctools_stylizer_edit_style_cancel', + 'forms' => array( + 'choose' => array( + 'form id' => 'ctools_stylizer_edit_style_form_choose', + ), + ), + ); + + if (empty($info['settings'])) { + $form_info['order'] = array( + 'choose' => t('Select base style'), + ); + if (empty($step)) { + $step = 'choose'; + } + + if ($step != 'choose') { + $cache = ctools_stylizer_get_settings_cache($name); + if (!$cache) { + $output = t('Missing settings cache.'); + if ($js) { + return ctools_modal_form_render($form_state, $output); + } + else { + return $output; + } + } + + if (!empty($cache['owner settings'])) { + $info['owner settings'] = $cache['owner settings']; + } + $settings = $cache['settings']; + } + else { + $settings = array( + 'name' => '_temporary', + 'style_base' => NULL, + 'palette' => array(), + ); + ctools_stylizer_clear_settings_cache($name); + } + $op = 'add'; + } + else { + $cache = ctools_stylizer_get_settings_cache($info['settings']['name']); + + if (!empty($cache)) { + if (!empty($cache['owner settings'])) { + $info['owner settings'] = $cache['owner settings']; + } + $settings = $cache['settings']; + } + else { + $settings = $info['settings']; + } + $op = 'edit'; + } + + if (!empty($info['op'])) { + // Allow this to override. Necessary to allow cloning properly. + $op = $info['op']; + } + + $plugin = NULL; + if (!empty($settings['style_base'])) { + $plugin = ctools_get_style_base($settings['style_base']); + $info['type'] = $plugin['type']; + ctools_stylizer_add_plugin_forms($form_info, $plugin, $op); + } + else { + // This is here so the 'finish' button does not show up, and because + // we don't have the selected style we don't know what the next form(s) + // will be. + $form_info['order']['next'] = t('Configure style'); + } + + if (count($form_info['order']) < 2 || $step == 'choose') { + $form_info['show trail'] = FALSE; + } + + $form_state = array( + 'module' => $info['module'], + 'type' => $info['type'], + 'owner info' => &$info, + 'base_style_plugin' => $plugin, + 'name' => $name, + 'step' => $step, + 'settings' => $settings, + 'ajax' => $js, + 'op' => $op, + ); + + if (!empty($info['modal'])) { + $form_state['modal'] = TRUE; + $form_state['title'] = $info['modal']; + $form_state['modal return'] = TRUE; + } + + ctools_include('wizard'); + $output = ctools_wizard_multistep_form($form_info, $step, $form_state); + + if (!empty($form_state['complete'])) { + $info['complete'] = TRUE; + $info['settings'] = $form_state['settings']; + } + + if ($js && !$output && !empty($form_state['clicked_button']['#next'])) { + // We have to do a separate redirect here because the formula that adds + // stuff to the wizard after being chosen hasn't happened. The wizard + // tried to go to the next step which did not exist. + return ctools_stylizer_edit_style($info, $js, $form_state['clicked_button']['#next']); + } + + if ($js) { + return ctools_modal_form_render($form_state, $output); + } + else { + return $output; + } +} + +/** + * Add wizard forms specific to a style base plugin. + * + * The plugin can store forms either as a simple 'edit form' + * => 'form callback' or if it needs the more complicated wizard + * functionality, it can set 'forms' and 'order' with values suitable + * for the wizard $form_info array. + * + * @param &$form_info + * The form info to modify. + * @param $plugin + * The plugin to use. + * @param $op + * Either 'add' or 'edit' so we can get the right forms. + */ +function ctools_stylizer_add_plugin_forms(&$form_info, $plugin, $op) { + if (empty($plugin['forms'])) { + if ($op == 'add' && isset($plugin['add form'])) { + $id = $plugin['add form']; + } + else if (isset($plugin['edit form'])) { + $id = $plugin['edit form']; + } + else { + $id = 'ctools_stylizer_edit_style_form_default'; + } + + $form_info['forms']['settings'] = array( + 'form id' => $id, + ); + $form_info['order']['settings'] = t('Settings'); + } + else { + $form_info['forms'] += $plugin['forms']; + $form_info['order'] += $plugin['order']; + } +} + +/** + * Callback generated when the add style process is finished. + */ +function ctools_stylizer_edit_style_finish(&$form_state) { + $form_state['complete'] = TRUE; + ctools_stylizer_clear_settings_cache($form_state['name']); + + if (isset($form_state['settings']['old_settings'])) { + unset($form_state['settings']['old_settings']); + } +} + +/** + * Callback generated when the 'next' button is clicked. + */ +function ctools_stylizer_edit_style_next(&$form_state) { + $form_state['form_info']['path'] = str_replace('%name', $form_state['name'], $form_state['form_info']['path']); + $form_state['redirect'] = ctools_wizard_get_path($form_state['form_info'], $form_state['clicked_button']['#next']); + + // Update the cache with changes. + $cache = array('settings' => $form_state['settings']); + if (!empty($form_state['owner info']['owner settings'])) { + $cache['owner settings'] = $form_state['owner info']['owner settings']; + } + ctools_stylizer_set_settings_cache($form_state['name'], $cache); +} + +/** + * Callback generated when the 'cancel' button is clicked. + * + * We might have some temporary data lying around. We must remove it. + */ +function ctools_stylizer_edit_style_cancel(&$form_state) { + if (!empty($form_state['name'])) { + ctools_stylizer_clear_settings_cache($form_state['name']); + } +} + +/** + * Choose which plugin to use to create a new style. + */ +function ctools_stylizer_edit_style_form_choose($form, &$form_state) { + $plugins = ctools_get_style_bases(); + $options = array(); + + $categories = array(); + foreach ($plugins as $name => $plugin) { + if ($form_state['module'] == $plugin['module'] && $form_state['type'] == $plugin['type']) { + $categories[$plugin['category']] = $plugin['category']; + $unsorted_options[$plugin['category']][$name] = ctools_stylizer_print_style_icon($plugin, TRUE); + } + } + + asort($categories); + + foreach ($categories as $category) { + $options[$category] = $unsorted_options[$category]; + } + + $form['style_base'] = array( + '#prefix' => '
', + '#suffix' => '
', + ); + + ctools_include('cleanstring'); + foreach ($options as $category => $radios) { + $cat = ctools_cleanstring($category); + $form['style_base'][$cat] = array( + '#prefix' => '
', + '#suffix' => '
', + ); + + foreach ($radios as $key => $choice) { + // Generate the parents as the autogenerator does, so we will have a + // unique id for each radio button. + $form['style_base'][$cat][$key] = array( + '#type' => 'radio', + '#title' => $choice, + '#parents' => array('style_base'), + '#id' => drupal_clean_css_identifier('edit-style-base-' . $key), + '#return_value' => check_plain($key), + ); + } + } + + return $form; +} + +function ctools_stylizer_edit_style_form_choose_submit($form, &$form_state) { + $form_state['settings']['style_base'] = $form_state['values']['style_base']; + + // The 'next' form will show up as 'next' but that's not accurate now that + // we have a style. Figure out what next really is and update. + $plugin = ctools_get_style_base($form_state['settings']['style_base']); + if (empty($plugin['forms'])) { + $form_state['clicked_button']['#next'] = 'settings'; + } + else { + $forms = array_keys($form_info['forms']); + $form_state['clicked_button']['#next'] = array_shift($forms); + } + + // Fill in the defaults for the settings. + if (!empty($plugin['defaults'])) { + // @todo allow a callback + $form_state['settings'] += $plugin['defaults']; + } + + return $form; +} + +/** + * The default stylizer style editing form. + * + * Even when not using this, styles should call through to this form in + * their own edit forms. + */ +function ctools_stylizer_edit_style_form_default($form, &$form_state) { + ctools_add_js('stylizer'); + ctools_add_css('stylizer'); + drupal_add_library('system', 'farbtastic'); + + $plugin = &$form_state['base_style_plugin']; + $settings = &$form_state['settings']; + + $form['top box'] = array( + '#prefix' => '
', + '#suffix' => '
', + ); + $form['top box']['left'] = array( + '#prefix' => '
', + '#suffix' => '
', + ); + $form['top box']['preview'] = array( + // We have a copy of the $form_state on $form because form theme functions + // do not get $form_state. + '#theme' => 'ctools_stylizer_preview_form', + '#form_state' => &$form_state, + ); + + $form['top box']['preview']['submit'] = array( + '#type' => 'submit', + '#value' => t('Preview'), + ); + + if (!empty($plugin['palette'])) { + $form['top box']['color'] = array( + '#type' => 'fieldset', + '#title' => t('Color scheme'), + '#attributes' => array('id' => 'ctools_stylizer_color_scheme_form', 'class' => array('ctools-stylizer-color-edit')), + '#theme' => 'ctools_stylizer_color_scheme_form', + ); + + $form['top box']['color']['palette']['#tree'] = TRUE; + + foreach ($plugin['palette'] as $key => $color) { + if (empty($settings['palette'][$key])) { + $settings['palette'][$key] = $color['default_value']; + } + + $form['top box']['color']['palette'][$key] = array( + '#type' => 'textfield', + '#title' => $color['label'], + '#default_value' => $settings['palette'][$key], + '#size' => 8, + ); + } + } + + if (!empty($plugin['settings form']) && function_exists($plugin['settings form'])) { + $plugin['settings form']($form, $form_state); + } + + if (!empty($form_state['owner info']['owner form']) && function_exists($form_state['owner info']['owner form'])) { + $form_state['owner info']['owner form']($form, $form_state); + } + + return $form; +} + +/** + * Theme the stylizer color scheme form. + */ +function theme_ctools_stylizer_color_scheme_form($vars) { + $form = &$vars['form']; + $output = ''; + + // Wrapper + $output .= '
'; + + // Color schemes +// $output .= drupal_render($form['scheme']); + + // Palette + $output .= '
'; + foreach (element_children($form['palette']) as $name) { + $output .= render($form['palette'][$name]); + } + $output .= '
'; // palette + + $output .= '
'; // color form + + return $output; +} + +/** + * Theme the stylizer preview form. + */ +function theme_ctools_stylizer_preview_form($vars) { + $form = &$vars['form']; + + $plugin = $form['#form_state']['base_style_plugin']; + $settings = $form['#form_state']['settings']; + + if (!empty($form['#form_state']['settings']['old_settings'])) { + ctools_stylizer_cleanup_style($plugin, $form['#form_state']['settings']['old_settings']); + } + $preview = ''; + if (!empty($plugin['preview'])) { + $preview = $plugin['preview']; + } + else { + $base_types = ctools_get_style_base_types(); + if (!empty($base_types[$plugin['module']][$plugin['type']]['preview'])) { + $preview = $base_types[$plugin['module']][$plugin['type']]['preview']; + } + } + + if (!empty($preview) && function_exists($preview)) { + $output = '
' . t('Preview') . ''; + $output .= $preview($plugin, $settings); + $output .= drupal_render_children($form); + $output .= '
'; + + return $output; + } +} + +function ctools_stylizer_edit_style_form_default_validate($form, &$form_state) { + if (!empty($form_state['owner info']['owner form validate']) && function_exists($form_state['owner info']['owner form validate'])) { + $form_state['owner info']['owner form validate']($form, $form_state); + } + + if (!empty($form_state['base_style_plugin']['settings form validate']) && function_exists($form_state['base_style_plugin']['settings form validate'])) { + $form_state['base_style_plugin']['settings form validate']($form, $form_state); + } +} + +function ctools_stylizer_edit_style_form_default_submit($form, &$form_state) { + // Store old settings for the purposes of cleaning up. + $form_state['settings']['old_settings'] = $form_state['settings']; + $form_state['settings']['palette'] = $form_state['values']['palette']; + + if (!empty($form_state['owner info']['owner form submit']) && function_exists($form_state['owner info']['owner form submit'])) { + $form_state['owner info']['owner form submit']($form, $form_state); + } + + if (!empty($form_state['base_style_plugin']['settings form submit']) && function_exists($form_state['base_style_plugin']['settings form submit'])) { + $form_state['base_style_plugin']['settings form submit']($form, $form_state); + } + + if ($form_state['clicked_button']['#value'] == t('Preview')) { + $form_state['rerender'] = TRUE; + // Update the cache with changes. + if (!empty($form_state['name'])) { + $cache = array('settings' => $form_state['settings']); + if (!empty($form_state['owner info']['owner settings'])) { + $cache['owner settings'] = $form_state['owner info']['owner settings']; + } + ctools_stylizer_set_settings_cache($form_state['name'], $cache); + } + } +} + +// -------------------------------------------------------------------------- +// CSS forms and tools that plugins can use. + +/** + * Font selector form + */ +function ctools_stylizer_font_selector_form(&$form, &$form_state, $label, $settings) { + // Family + $form['#prefix'] = '
'; + $form['#type'] = 'fieldset'; + $form['#title'] = $label; + $form['#suffix'] = '
'; + $form['#tree'] = TRUE; + + $form['font'] = array( + '#title' => t('Font family'), + '#type' => 'select', + '#default_value' => isset($settings['font']) ? $settings['font'] : '', + '#options' => array( + '' => '', + 'Arial, Helvetica, sans-serif' => t('Arial, Helvetica, sans-serif'), + 'Times New Roman, Times, serif' => t('Times New Roman, Times, serif'), + 'Courier New, Courier, monospace' => t('Courier New, Courier, monospace'), + 'Georgia, Times New Roman, Times, serif' => t('Georgia, Times New Roman, Times, serif'), + 'Verdana, Arial, Helvetica, sans-serif' => t('Verdana, Arial, Helvetica, sans-serif'), + 'Geneva, Arial, Helvetica, sans-serif' => t('Geneva, Arial, Helvetica, sans-serif'), + 'Trebuchet MS, Trebuchet, Verdana, sans-serif' => t('Trebuchet MS, Trebuchet, Verdana, sans-serif'), + ), + ); + + // size + $form['size'] = array( + '#title' => t('Size'), + '#type' => 'select', + '#default_value' => isset($settings['size']) ? $settings['size'] : '', + '#options' => array( + '' => '', + 'xx-small' => t('XX-Small'), + 'x-small' => t('X-Small'), + 'small' => t('Small'), + 'medium' => t('Medium'), + 'large' => t('Large'), + 'x-large' => t('X-Large'), + 'xx-large' => t('XX-Large'), + ), + ); + + // letter spacing + $form['letter_spacing'] = array( + '#title' => t('Letter spacing'), + '#type' => 'select', + '#default_value' => isset($settings['letter_spacing']) ? $settings['letter_spacing'] : '', + '#options' => array( + '' => '', + "-10px" => '10px', + "-9px" => '9px', + "-8px" => '8px', + "-7px" => '7px', + "-6px" => '6px', + "-5px" => '5px', + "-4px" => '4px', + "-3px" => '3px', + "-2px" => '2px', + "-1px" => '1px', + "0" => '0', + "1px" => '1px', + "2px" => '2px', + "3px" => '3px', + "4px" => '4px', + "5px" => '5px', + "6px" => '6px', + "7px" => '7px', + "8px" => '8px', + "9px" => '9px', + "10px" => '10px', + "11px" => '11px', + "12px" => '12px', + "13px" => '13px', + "14px" => '14px', + "15px" => '15px', + "16px" => '16px', + "17px" => '17px', + "18px" => '18px', + "19px" => '19px', + "20px" => '20px', + "21px" => '21px', + "22px" => '22px', + "23px" => '23px', + "24px" => '24px', + "25px" => '25px', + "26px" => '26px', + "27px" => '27px', + "28px" => '28px', + "29px" => '29px', + "30px" => '30px', + "31px" => '31px', + "32px" => '32px', + "33px" => '33px', + "34px" => '34px', + "35px" => '35px', + "36px" => '36px', + "37px" => '37px', + "38px" => '38px', + "39px" => '39px', + "40px" => '40px', + "41px" => '41px', + "42px" => '42px', + "43px" => '43px', + "44px" => '44px', + "45px" => '45px', + "46px" => '46px', + "47px" => '47px', + "48px" => '48px', + "49px" => '49px', + "50px" => '50px', + ), + ); + + // word space + $form['word_spacing'] = array( + '#title' => t('Word spacing'), + '#type' => 'select', + '#default_value' => isset($settings['word_spacing']) ? $settings['word_spacing'] : '', + '#options' => array( + '' => '', + "-1em" => '-1em', + "-0.95em" => '-0.95em', + "-0.9em" => '-0.9em', + "-0.85em" => '-0.85em', + "-0.8em" => '-0.8em', + "-0.75em" => '-0.75em', + "-0.7em" => '-0.7em', + "-0.65em" => '-0.65em', + "-0.6em" => '-0.6em', + "-0.55em" => '-0.55em', + "-0.5em" => '-0.5em', + "-0.45em" => '-0.45em', + "-0.4em" => '-0.4em', + "-0.35em" => '-0.35em', + "-0.3em" => '-0.3em', + "-0.25em" => '-0.25em', + "-0.2em" => '-0.2em', + "-0.15em" => '-0.15em', + "-0.1em" => '-0.1em', + "-0.05em" => '-0.05em', + "normal" => 'normal', + "0.05em" => '0.05em', + "0.1em" => '0.1em', + "0.15em" => '0.15em', + "0.2em" => '0.2em', + "0.25em" => '0.25em', + "0.3em" => '0.3em', + "0.35em" => '0.35em', + "0.4em" => '0.4em', + "0.45em" => '0.45em', + "0.5em" => '0.5em', + "0.55em" => '0.55em', + "0.6em" => '0.6em', + "0.65em" => '0.65em', + "0.7em" => '0.7em', + "0.75em" => '0.75em', + "0.8em" => '0.8em', + "0.85em" => '0.85em', + "0.9em" => '0.9em', + "0.95em" => '0.95em', + "1em" => '1em', + ), + ); + + // decoration + $form['decoration'] = array( + '#title' => t('Decoration'), + '#type' => 'select', + '#default_value' => isset($settings['decoration']) ? $settings['decoration'] : '', + '#options' => array( + '' => '', + 'none' => t('None'), + 'underline' => t('Underline'), + 'overline' => t('Overline'), + 'line-through' => t('Line-through'), + ), + ); + + // weight + $form['weight'] = array( + '#title' => t('Weight'), + '#type' => 'select', + '#default_value' => isset($settings['weight']) ? $settings['weight'] : '', + '#options' => array( + '' => '', + 'normal' => t('Normal'), + 'bold' => t('Bold'), + 'bolder' => t('Bolder'), + 'lighter' => t('Lighter'), + ), + ); + + // style + $form['style'] = array( + '#title' => t('Style'), + '#type' => 'select', + '#default_value' => isset($settings['style']) ? $settings['style'] : '', + '#options' => array( + '' => '', + 'normal' => t('Normal'), + 'italic' => t('Italic'), + 'oblique' => t('Oblique'), + ), + ); + + // variant + $form['variant'] = array( + '#title' => t('Variant'), + '#type' => 'select', + '#default_value' => isset($settings['variant']) ? $settings['variant'] : '', + '#options' => array( + '' => '', + 'normal' => t('Normal'), + 'small-caps' => t('Small-caps'), + ), + ); + + // case + $form['case'] = array( + '#title' => t('Case'), + '#type' => 'select', + '#default_value' => isset($settings['case']) ? $settings['case'] : '', + '#options' => array( + '' => '', + 'capitalize' => t('Capitalize'), + 'uppercase' => t('Uppercase'), + 'lowercase' => t('Lowercase'), + 'none' => t('None'), + ), + ); + + // alignment + $form['alignment'] = array( + '#title' => t('Align'), + '#type' => 'select', + '#default_value' => isset($settings['alignment']) ? $settings['alignment'] : '', + '#options' => array( + '' => '', + 'justify' => t('Justify'), + 'left' => t('Left'), + 'right' => t('Right'), + 'center' => t('Center'), + ), + ); +} + +/** + * Copy font selector information into the settings + */ +function ctools_stylizer_font_selector_form_submit(&$form, &$form_state, &$values, &$settings) { + $settings = $values; +} + +function ctools_stylizer_font_apply_style(&$stylesheet, $selector, $settings) { + $css = ''; + if (isset($settings['font']) && $settings['font'] !== '') { + $css .= ' font-family: ' . $settings['font'] . ";\n"; + } + + if (isset($settings['size']) && $settings['size'] !== '') { + $css .= ' font-size: ' . $settings['size'] . ";\n"; + } + + if (isset($settings['weight']) && $settings['weight'] !== '') { + $css .= ' font-weight: ' . $settings['weight'] . ";\n"; + } + + if (isset($settings['style']) && $settings['style'] !== '') { + $css .= ' font-style: ' . $settings['style'] . ";\n"; + } + + if (isset($settings['variant']) && $settings['variant'] !== '') { + $css .= ' font-variant: ' . $settings['variant'] . ";\n"; + } + + if (isset($settings['case']) && $settings['case'] !== '') { + $css .= ' text-transform: ' . $settings['case'] . ";\n"; + } + + if (isset($settings['decoration']) && $settings['decoration'] !== '') { + $css .= ' text-decoration: ' . $settings['decoration'] . ";\n"; + } + + if (isset($settings['alignment']) && $settings['alignment'] !== '') { + $css .= ' text-align: ' . $settings['alignment'] . ";\n"; + } + + if (isset($settings['letter_spacing']) && $settings['letter_spacing'] !== '') { + $css .= ' letter-spacing: ' . $settings['letter_spacing'] . ";\n"; + } + + if (isset($settings['word_spacing']) && $settings['word_spacing'] !== '') { + $css .= ' word-spacing: ' . $settings['word_spacing'] . ";\n"; + } + + if ($css) { + $stylesheet .= $selector . " {\n" . $css . "}\n"; + } +} + +/** + * Border selector form + */ +function ctools_stylizer_border_selector_form(&$form, &$form_state, $label, $settings) { + // Family + $form['#prefix'] = '
'; + $form['#type'] = 'fieldset'; + $form['#title'] = $label; + $form['#suffix'] = '
'; + $form['#tree'] = TRUE; + + $form['thickness'] = array( + '#title' => t('Thickness'), + '#type' => 'select', + '#default_value' => isset($settings['thickness']) ? $settings['thickness'] : '', + '#options' => array( + '' => '', + "none" => t('None'), + "1px" => '1px', + "2px" => '2px', + "3px" => '3px', + "4px" => '4px', + "5px" => '5px', + ), + ); + + $form['style'] = array( + '#title' => t('style'), + '#type' => 'select', + '#default_value' => isset($settings['style']) ? $settings['style'] : '', + '#options' => array( + '' => '', + 'solid' => t('Solid'), + 'dotted' => t('Dotted'), + 'dashed' => t('Dashed'), + 'double' => t('Double'), + 'groove' => t('Groove'), + 'ridge' => t('Ridge'), + 'inset' => t('Inset'), + 'outset' => t('Outset'), + ), + ); +} + +/** + * Copy border selector information into the settings + */ +function ctools_stylizer_border_selector_form_submit(&$form, &$form_state, &$values, &$settings) { + $settings = $values; +} + +function ctools_stylizer_border_apply_style(&$stylesheet, $selector, $settings, $color, $which = NULL) { + $border = 'border'; + if ($which) { + $border .= '-' . $which; + } + + $css = ''; + if (isset($settings['thickness']) && $settings['thickness'] !== '') { + if ($settings['thickness'] == 'none') { + $css .= ' ' . $border . ': none'; + } + else { + $css .= ' ' . $border . '-width: ' . $settings['thickness'] . ";\n"; + + if (isset($settings['style']) && $settings['style'] !== '') { + $css .= ' ' . $border . '-style: ' . $settings['style'] . ";\n"; + } + + $css .= ' ' . $border . '-color: ' . $color . ";\n"; + } + } + + if ($css) { + $stylesheet .= $selector . " {\n" . $css . "}\n"; + } + +} + +/** + * padding selector form + */ +function ctools_stylizer_padding_selector_form(&$form, &$form_state, $label, $settings) { + // Family + $form['#prefix'] = '
'; + $form['#type'] = 'fieldset'; + $form['#title'] = $label; + $form['#suffix'] = '
'; + $form['#tree'] = TRUE; + + $options = array( + '' => '', + "0.05em" => '0.05em', + "0.1em" => '0.1em', + "0.15em" => '0.15em', + "0.2em" => '0.2em', + "0.25em" => '0.25em', + "0.3em" => '0.3em', + "0.35em" => '0.35em', + "0.4em" => '0.4em', + "0.45em" => '0.45em', + "0.5em" => '0.5em', + "0.55em" => '0.55em', + "0.6em" => '0.6em', + "0.65em" => '0.65em', + "0.7em" => '0.7em', + "0.75em" => '0.75em', + "0.8em" => '0.8em', + "0.85em" => '0.85em', + "0.9em" => '0.9em', + "0.95em" => '0.95em', + "1.0em" => '1.0em', + "1.05em" => '1.05em', + "1.1em" => '1.1em', + "1.15em" => '1.15em', + "1.2em" => '1.2em', + "1.25em" => '1.25em', + "1.3em" => '1.3em', + "1.35em" => '1.35em', + "1.4em" => '1.4em', + "1.45em" => '1.45em', + "1.5em" => '1.5em', + "1.55em" => '1.55em', + "1.6em" => '1.6em', + "1.65em" => '1.65em', + "1.7em" => '1.7em', + "1.75em" => '1.75em', + "1.8em" => '1.8em', + "1.85em" => '1.85em', + "1.9em" => '1.9em', + "1.95em" => '1.95em', + "2.0em" => '2.0em', + "2.05em" => '2.05em', + "2.1em" => '2.1em', + "2.15em" => '2.15em', + "2.2em" => '2.2em', + "2.25em" => '2.25em', + "2.3em" => '2.3em', + "2.35em" => '2.35em', + "2.4em" => '2.4em', + "2.45em" => '2.45em', + "2.5em" => '2.5em', + "2.55em" => '2.55em', + "2.6em" => '2.6em', + "2.65em" => '2.65em', + "2.7em" => '2.7em', + "2.75em" => '2.75em', + "2.8em" => '2.8em', + "2.85em" => '2.85em', + "2.9em" => '2.9em', + "2.95em" => '2.95em', + "3.0em" => '3.0em', + "3.05em" => '3.05em', + "3.1em" => '3.1em', + "3.15em" => '3.15em', + "3.2em" => '3.2em', + "3.25em" => '3.25em', + "3.3em" => '3.3em', + "3.35em" => '3.35em', + "3.4em" => '3.4em', + "3.45em" => '3.45em', + "3.5em" => '3.5em', + "3.55em" => '3.55em', + "3.6em" => '3.6em', + "3.65em" => '3.65em', + "3.7em" => '3.7em', + "3.75em" => '3.75em', + "3.8em" => '3.8em', + "3.85em" => '3.85em', + "3.9em" => '3.9em', + "3.95em" => '3.95em', + ); + + $form['top'] = array( + '#title' => t('Top'), + '#type' => 'select', + '#default_value' => isset($settings['top']) ? $settings['top'] : '', + '#options' => $options, + ); + + $form['right'] = array( + '#title' => t('Right'), + '#type' => 'select', + '#default_value' => isset($settings['right']) ? $settings['right'] : '', + '#options' => $options, + ); + + $form['bottom'] = array( + '#title' => t('Bottom'), + '#type' => 'select', + '#default_value' => isset($settings['bottom']) ? $settings['bottom'] : '', + '#options' => $options, + ); + + $form['left'] = array( + '#title' => t('Left'), + '#type' => 'select', + '#default_value' => isset($settings['left']) ? $settings['left'] : '', + '#options' => $options, + ); +} + +/** + * Copy padding selector information into the settings + */ +function ctools_stylizer_padding_selector_form_submit(&$form, &$form_state, &$values, &$settings) { + $settings = $values; +} + +function ctools_stylizer_padding_apply_style(&$stylesheet, $selector, $settings) { + $css = ''; + + if (isset($settings['top']) && $settings['top'] !== '') { + $css .= ' padding-top: ' . $settings['top'] . ";\n"; + } + + if (isset($settings['right']) && $settings['right'] !== '') { + $css .= ' padding-right: ' . $settings['right'] . ";\n"; + } + + if (isset($settings['bottom']) && $settings['bottom'] !== '') { + $css .= ' padding-bottom: ' . $settings['bottom'] . ";\n"; + } + + if (isset($settings['left']) && $settings['left'] !== '') { + $css .= ' padding-left: ' . $settings['left'] . ";\n"; + } + + if ($css) { + $stylesheet .= $selector . " {\n" . $css . "}\n"; + } + +} diff --git a/sites/all/modules/contrib/ctools/includes/stylizer.theme.inc b/sites/all/modules/contrib/ctools/includes/stylizer.theme.inc new file mode 100644 index 0000000..85346c1 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/stylizer.theme.inc @@ -0,0 +1,28 @@ + 'form', + 'file' => 'includes/stylizer.inc', + ); + + $theme['ctools_stylizer_preview_form'] = array( + 'render element' => 'form', + 'file' => 'includes/stylizer.inc', + ); + + $theme['ctools_style_icon'] = array( + 'variables' => array('image' => NULL, 'title' => NULL), + 'file' => 'includes/stylizer.inc', + ); +} + diff --git a/sites/all/modules/contrib/ctools/includes/utility.inc b/sites/all/modules/contrib/ctools/includes/utility.inc new file mode 100644 index 0000000..82fc147 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/utility.inc @@ -0,0 +1,31 @@ + 'name')); + foreach ($files as $file) { + require_once DRUPAL_ROOT . '/' . $file->uri; + list($tool) = explode('.', $file->name, 2); + + $function = $module . '_' . str_replace ('-', '_', $tool) . '_' . str_replace('-', '_', $type); + if (function_exists($function)) { + $function($items); + } + } +} diff --git a/sites/all/modules/contrib/ctools/includes/uuid.inc b/sites/all/modules/contrib/ctools/includes/uuid.inc new file mode 100644 index 0000000..6e4c42c --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/uuid.inc @@ -0,0 +1,68 @@ + $params) { + $form_info['order'][$step_id] = $params['title']; + } + } + + if (!isset($step)) { + $keys = array_keys($form_info['order']); + $step = array_shift($keys); + } + + ctools_wizard_defaults($form_info); + + // If automated caching is enabled, ensure that everything is as it + // should be. + if (!empty($form_info['auto cache'])) { + // If the cache mechanism hasn't been set, default to the simple + // mechanism and use the wizard ID to ensure uniqueness so cache + // objects don't stomp on each other. + if (!isset($form_info['cache mechanism'])) { + $form_info['cache mechanism'] = 'simple::wizard::' . $form_info['id']; + } + + // If not set, default the cache key to the wizard ID. This is often + // a unique ID of the object being edited or similar. + if (!isset($form_info['cache key'])) { + $form_info['cache key'] = $form_info['id']; + } + + // If not set, default the cache location to storage. This is often + // somnething like 'conf'. + if (!isset($form_info['cache location'])) { + $form_info['cache location'] = 'storage'; + } + + // If absolutely nothing was set for the cache area to work on + if (!isset($form_state[$form_info['cache location']])) { + ctools_include('cache'); + $form_state[$form_info['cache location']] = ctools_cache_get($form_info['cache mechanism'], $form_info['cache key']); + } + } + + $form_state['step'] = $step; + $form_state['form_info'] = $form_info; + + // Ensure we have form information for the current step. + if (!isset($form_info['forms'][$step])) { + return; + } + + // Ensure that whatever include file(s) were requested by the form info are + // actually included. + $info = $form_info['forms'][$step]; + + if (!empty($info['include'])) { + if (is_array($info['include'])) { + foreach ($info['include'] as $file) { + ctools_form_include_file($form_state, $file); + } + } + else { + ctools_form_include_file($form_state, $info['include']); + } + } + + // This tells drupal_build_form to apply our wrapper to the form. It + // will give it buttons and the like. + $form_state['wrapper_callback'] = 'ctools_wizard_wrapper'; + if (!isset($form_state['rerender'])) { + $form_state['rerender'] = FALSE; + } + + $form_state['no_redirect'] = TRUE; + + $output = drupal_build_form($info['form id'], $form_state); + + if (empty($form_state['executed']) || !empty($form_state['rerender'])) { + if (empty($form_state['title']) && !empty($info['title'])) { + $form_state['title'] = $info['title']; + } + + if (!empty($form_state['ajax render'])) { + // Any include files should already be included by this point: + return $form_state['ajax render']($form_state, $output); + } + + // Automatically use the modal tool if set to true. + if (!empty($form_state['modal']) && empty($form_state['modal return'])) { + ctools_include('modal'); + + // This overwrites any previous commands. + $form_state['commands'] = ctools_modal_form_render($form_state, $output); + } + } + + if (!empty($form_state['executed'])) { + // We use the plugins get_function format because it's powerful and + // not limited to just functions. + ctools_include('plugins'); + + if (isset($form_state['clicked_button']['#wizard type'])) { + $type = $form_state['clicked_button']['#wizard type']; + // If we have a callback depending upon the type of button that was + // clicked, call it. + if ($function = ctools_plugin_get_function($form_info, "$type callback")) { + $function($form_state); + } + + // If auto-caching is on, we need to write the cache on next and + // clear the cache on finish. + if (!empty($form_info['auto cache'])) { + if ($type == 'next') { + ctools_include('cache'); + ctools_cache_set($form_info['cache mechanism'], $form_info['cache key'], $form_state[$form_info['cache location']]); + } + elseif ($type == 'finish') { + ctools_include('cache'); + ctools_cache_clear($form_info['cache mechanism'], $form_info['cache key']); + } + } + + // Set a couple of niceties: + if ($type == 'finish') { + $form_state['complete'] = TRUE; + } + + if ($type == 'cancel') { + $form_state['cancel'] = TRUE; + } + + // If the modal is in use, some special code for it: + if (!empty($form_state['modal']) && empty($form_state['modal return'])) { + if ($type != 'next') { + // Automatically dismiss the modal if we're not going to another form. + ctools_include('modal'); + $form_state['commands'][] = ctools_modal_command_dismiss(); + } + } + } + + if (empty($form_state['ajax'])) { + // redirect, if one is set. + if ($form_state['redirect']) { + if (is_array($form_state['redirect'])) { + call_user_func_array('drupal_goto', $form_state['redirect']); + } + else { + drupal_goto($form_state['redirect']); + } + } + } + else if (isset($form_state['ajax next'])) { + // Clear a few items off the form state so we don't double post: + $next = $form_state['ajax next']; + unset($form_state['ajax next']); + unset($form_state['executed']); + unset($form_state['post']); + unset($form_state['next']); + return ctools_wizard_multistep_form($form_info, $next, $form_state); + } + + // If the callbacks wanted to do something besides go to the next form, + // it needs to have set $form_state['commands'] with something that can + // be rendered. + } + + // Render ajax commands if we have any. + if (isset($form_state['ajax']) && isset($form_state['commands']) && empty($form_state['modal return'])) { + return ajax_render($form_state['commands']); + } + + // Otherwise, return the output. + return $output; +} + +/** + * Provide a wrapper around another form for adding multi-step information. + */ +function ctools_wizard_wrapper($form, &$form_state) { + $form_info = &$form_state['form_info']; + $info = $form_info['forms'][$form_state['step']]; + + // Determine the next form from this step. + // Create a form trail if we're supposed to have one. + $trail = array(); + $previous = TRUE; + foreach ($form_info['order'] as $id => $title) { + if ($id == $form_state['step']) { + $previous = FALSE; + $class = 'wizard-trail-current'; + } + elseif ($previous) { + $not_first = TRUE; + $class = 'wizard-trail-previous'; + $form_state['previous'] = $id; + } + else { + $class = 'wizard-trail-next'; + if (!isset($form_state['next'])) { + $form_state['next'] = $id; + } + if (empty($form_info['show trail'])) { + break; + } + } + + if (!empty($form_info['show trail'])) { + if (!empty($form_info['free trail'])) { + // ctools_wizard_get_path() returns results suitable for + // $form_state['redirect] which can only be directly used in + // drupal_goto. We have to futz a bit with it. + $path = ctools_wizard_get_path($form_info, $id); + $options = array(); + if (!empty($path[1])) { + $options = $path[1]; + } + $title = l($title, $path[0], $options); + } + $trail[] = '' . $title . ''; + } + } + + // Display the trail if instructed to do so. + if (!empty($form_info['show trail'])) { + ctools_add_css('wizard'); + $form['ctools_trail'] = array( + '#markup' => theme(array('ctools_wizard_trail__' . $form_info['id'], 'ctools_wizard_trail'), array('trail' => $trail, 'form_info' => $form_info)), + '#weight' => -1000, + ); + } + + if (empty($form_info['no buttons'])) { + // Ensure buttons stay on the bottom. + $form['buttons'] = array( + '#type' => 'actions', + '#weight' => 1000, + ); + + $button_attributes = array(); + if (!empty($form_state['ajax']) && empty($form_state['modal'])) { + $button_attributes = array('class' => array('ctools-use-ajax')); + } + + if (!empty($form_info['show back']) && isset($form_state['previous'])) { + $form['buttons']['previous'] = array( + '#type' => 'submit', + '#value' => $form_info['back text'], + '#next' => $form_state['previous'], + '#wizard type' => 'next', + '#weight' => -2000, + '#limit_validation_errors' => array(), + // hardcode the submit so that it doesn't try to save data. + '#submit' => array('ctools_wizard_submit'), + '#attributes' => $button_attributes, + ); + + if (isset($form_info['no back validate']) || isset($info['no back validate'])) { + $form['buttons']['previous']['#validate'] = array(); + } + } + + // If there is a next form, place the next button. + if (isset($form_state['next']) || !empty($form_info['free trail'])) { + $form['buttons']['next'] = array( + '#type' => 'submit', + '#value' => $form_info['next text'], + '#next' => !empty($form_info['free trail']) ? $form_state['step'] : $form_state['next'], + '#wizard type' => 'next', + '#weight' => -1000, + '#attributes' => $button_attributes, + ); + } + + // There are two ways the return button can appear. If this is not the + // end of the form list (i.e, there is a next) then it's "update and return" + // to be clear. If this is the end of the path and there is no next, we + // call it 'Finish'. + + // Even if there is no direct return path (some forms may not want you + // leaving in the middle) the final button is always a Finish and it does + // whatever the return action is. + if (!empty($form_info['show return']) && !empty($form_state['next'])) { + $form['buttons']['return'] = array( + '#type' => 'submit', + '#value' => $form_info['return text'], + '#wizard type' => 'return', + '#attributes' => $button_attributes, + ); + } + else if (empty($form_state['next']) || !empty($form_info['free trail'])) { + $form['buttons']['return'] = array( + '#type' => 'submit', + '#value' => $form_info['finish text'], + '#wizard type' => 'finish', + '#attributes' => $button_attributes, + ); + } + + // If we are allowed to cancel, place a cancel button. + if ((isset($form_info['cancel path']) && !isset($form_info['show cancel'])) || !empty($form_info['show cancel'])) { + $form['buttons']['cancel'] = array( + '#type' => 'submit', + '#value' => $form_info['cancel text'], + '#wizard type' => 'cancel', + // hardcode the submit so that it doesn't try to save data. + '#limit_validation_errors' => array(), + '#submit' => array('ctools_wizard_submit'), + '#attributes' => $button_attributes, + ); + } + + // Set up optional validate handlers. + $form['#validate'] = array(); + if (function_exists($info['form id'] . '_validate')) { + $form['#validate'][] = $info['form id'] . '_validate'; + } + if (isset($info['validate']) && function_exists($info['validate'])) { + $form['#validate'][] = $info['validate']; + } + + // Set up our submit handler after theirs. Since putting something here will + // skip Drupal's autodetect, we autodetect for it. + + // We make sure ours is after theirs so that they get to change #next if + // the want to. + $form['#submit'] = array(); + if (function_exists($info['form id'] . '_submit')) { + $form['#submit'][] = $info['form id'] . '_submit'; + } + if (isset($info['submit']) && function_exists($info['submit'])) { + $form['#submit'][] = $info['submit']; + } + $form['#submit'][] = 'ctools_wizard_submit'; + } + + if (!empty($form_state['ajax'])) { + $params = ctools_wizard_get_path($form_state['form_info'], $form_state['step']); + if (count($params) > 1) { + $url = array_shift($params); + $options = array(); + + $keys = array(0 => 'query', 1 => 'fragment'); + foreach ($params as $key => $value) { + if (isset($keys[$key]) && isset($value)) { + $options[$keys[$key]] = $value; + } + } + + $params = array($url, $options); + } + $form['#action'] = call_user_func_array('url', $params); + } + + if (isset($info['wrapper']) && function_exists($info['wrapper'])) { + $form = $info['wrapper']($form, $form_state); + } + + if (isset($form_info['wrapper']) && function_exists($form_info['wrapper'])) { + $form = $form_info['wrapper']($form, $form_state); + } + return $form; +} + +/** + * On a submit, go to the next form. + */ +function ctools_wizard_submit(&$form, &$form_state) { + if (isset($form_state['clicked_button']['#wizard type'])) { + $type = $form_state['clicked_button']['#wizard type']; + + // if AJAX enabled, we proceed slightly differently here. + if (!empty($form_state['ajax'])) { + if ($type == 'next') { + $form_state['ajax next'] = $form_state['clicked_button']['#next']; + } + } + else { + if ($type == 'cancel' && isset($form_state['form_info']['cancel path'])) { + $form_state['redirect'] = $form_state['form_info']['cancel path']; + } + else if ($type == 'next') { + $form_state['redirect'] = ctools_wizard_get_path($form_state['form_info'], $form_state['clicked_button']['#next']); + if (!empty($_GET['destination'])) { + // We don't want drupal_goto redirect this request + // back. ctools_wizard_get_path ensures that the destination is + // carried over on subsequent pages. + unset($_GET['destination']); + } + } + else if (isset($form_state['form_info']['return path'])) { + $form_state['redirect'] = $form_state['form_info']['return path']; + } + else if ($type == 'finish' && isset($form_state['form_info']['cancel path'])) { + $form_state['redirect'] = $form_state['form_info']['cancel path']; + } + } + } +} + +/** + * Create a path from the form info and a given step. + */ +function ctools_wizard_get_path($form_info, $step) { + if (is_array($form_info['path'])) { + foreach ($form_info['path'] as $id => $part) { + $form_info['path'][$id] = str_replace('%step', $step, $form_info['path'][$id]); + } + $path = $form_info['path']; + } + else { + $path = array(str_replace('%step', $step, $form_info['path'])); + } + + // If destination is set, carry it over so it'll take effect when + // saving. The submit handler will unset destination to avoid drupal_goto + // redirecting us. + if (!empty($_GET['destination'])) { + // Ensure that options is an array. + if (!isset($path[1]) || !is_array($path[1])) { + $path[1] = array(); + } + // Ensure that the query part of options is an array. + $path[1] += array('query' => array()); + // Add the destination parameter, if not set already. + $path[1]['query'] += drupal_get_destination(); + } + + return $path; +} + +/** + * Set default parameters and callbacks if none are given. + * Callbacks follows pattern: + * $form_info['id']_$hook + * $form_info['id']_$form_info['forms'][$step_key]_$hook + */ +function ctools_wizard_defaults(&$form_info) { + $hook = $form_info['id']; + $defaults = array( + 'show trail' => FALSE, + 'free trail' => FALSE, + 'show back' => FALSE, + 'show cancel' => FALSE, + 'show return' => FALSE, + 'next text' => t('Continue'), + 'back text' => t('Back'), + 'return text' => t('Update and return'), + 'finish text' => t('Finish'), + 'cancel text' => t('Cancel'), + ); + + if (!empty($form_info['free trail'])) { + $defaults['next text'] = t('Update'); + $defaults['finish text'] = t('Save'); + } + + $form_info = $form_info + $defaults; + // set form callbacks if they aren't defined + foreach ($form_info['forms'] as $step => $params) { + if (!$params['form id']) { + $form_callback = $hook . '_' . $step . '_form'; + $form_info['forms'][$step]['form id'] = $form_callback; + } + } + + // set button callbacks + $callbacks = array( + 'back callback' => '_back', + 'next callback' => '_next', + 'return callback' => '_return', + 'cancel callback' => '_cancel', + 'finish callback' => '_finish', + ); + + foreach ($callbacks as $key => $callback) { + // never overwrite if explicity defined + if (empty($form_info[$key])) { + $wizard_callback = $hook . $callback; + if (function_exists($wizard_callback)) { + $form_info[$key] = $wizard_callback; + } + } + } +} diff --git a/sites/all/modules/contrib/ctools/includes/wizard.theme.inc b/sites/all/modules/contrib/ctools/includes/wizard.theme.inc new file mode 100644 index 0000000..c1a2646 --- /dev/null +++ b/sites/all/modules/contrib/ctools/includes/wizard.theme.inc @@ -0,0 +1,22 @@ + array('trail' => NULL, 'form_info' => NULL, 'divider' => ' » '), + 'file' => 'includes/wizard.theme.inc', + ); +} + +/** + * Themable display of the 'breadcrumb' trail to show the order of the forms. + */ +function theme_ctools_wizard_trail($vars) { + if (!empty($vars['trail'])) { + return '
' . implode($vars['divider'], $vars['trail']) . '
'; + } +} diff --git a/sites/all/modules/contrib/ctools/js/ajax-responder.js b/sites/all/modules/contrib/ctools/js/ajax-responder.js new file mode 100644 index 0000000..1cad618 --- /dev/null +++ b/sites/all/modules/contrib/ctools/js/ajax-responder.js @@ -0,0 +1,126 @@ +/** + * @file + * + * CTools flexible AJAX responder object. + */ + +(function ($) { + Drupal.CTools = Drupal.CTools || {}; + Drupal.CTools.AJAX = Drupal.CTools.AJAX || {}; + /** + * Grab the response from the server and store it. + * + * @todo restore the warm cache functionality + */ + Drupal.CTools.AJAX.warmCache = function () { + // Store this expression for a minor speed improvement. + $this = $(this); + var old_url = $this.attr('href'); + // If we are currently fetching, or if we have fetched this already which is + // ideal for things like pagers, where the previous page might already have + // been seen in the cache. + if ($this.hasClass('ctools-fetching') || Drupal.CTools.AJAX.commandCache[old_url]) { + return false; + } + + // Grab all the links that match this url and add the fetching class. + // This allows the caching system to grab each url once and only once + // instead of grabbing the url once per . + var $objects = $('a[href="' + old_url + '"]') + $objects.addClass('ctools-fetching'); + try { + url = old_url.replace(/\/nojs(\/|$)/g, '/ajax$1'); + $.ajax({ + type: "POST", + url: url, + data: { 'js': 1, 'ctools_ajax': 1}, + global: true, + success: function (data) { + Drupal.CTools.AJAX.commandCache[old_url] = data; + $objects.addClass('ctools-cache-warmed').trigger('ctools-cache-warm', [data]); + }, + complete: function() { + $objects.removeClass('ctools-fetching'); + }, + dataType: 'json' + }); + } + catch (err) { + $objects.removeClass('ctools-fetching'); + return false; + } + + return false; + }; + + /** + * Cachable click handler to fetch the commands out of the cache or from url. + */ + Drupal.CTools.AJAX.clickAJAXCacheLink = function () { + $this = $(this); + if ($this.hasClass('ctools-fetching')) { + $this.bind('ctools-cache-warm', function (event, data) { + Drupal.CTools.AJAX.respond(data); + }); + return false; + } + else { + if ($this.hasClass('ctools-cache-warmed') && Drupal.CTools.AJAX.commandCache[$this.attr('href')]) { + Drupal.CTools.AJAX.respond(Drupal.CTools.AJAX.commandCache[$this.attr('href')]); + return false; + } + else { + return Drupal.CTools.AJAX.clickAJAXLink.apply(this); + } + } + }; + + /** + * Find a URL for an AJAX button. + * + * The URL for this gadget will be composed of the values of items by + * taking the ID of this item and adding -url and looking for that + * class. They need to be in the form in order since we will + * concat them all together using '/'. + */ + Drupal.CTools.AJAX.findURL = function(item) { + var url = ''; + var url_class = '.' + $(item).attr('id') + '-url'; + $(url_class).each( + function() { + var $this = $(this); + if (url && $this.val()) { + url += '/'; + } + url += $this.val(); + }); + return url; + }; + + // Hide these in a ready to ensure that Drupal.ajax is set up first. + $(function() { + Drupal.ajax.prototype.commands.attr = function(ajax, data, status) { + $(data.selector).attr(data.name, data.value); + }; + + + Drupal.ajax.prototype.commands.redirect = function(ajax, data, status) { + if (data.delay > 0) { + setTimeout(function () { + location.href = data.url; + }, data.delay); + } + else { + location.href = data.url; + } + }; + + Drupal.ajax.prototype.commands.reload = function(ajax, data, status) { + location.reload(); + }; + + Drupal.ajax.prototype.commands.submit = function(ajax, data, status) { + $(data.selector).submit(); + } + }); +})(jQuery); diff --git a/sites/all/modules/contrib/ctools/js/auto-submit.js b/sites/all/modules/contrib/ctools/js/auto-submit.js new file mode 100644 index 0000000..a3e9aa4 --- /dev/null +++ b/sites/all/modules/contrib/ctools/js/auto-submit.js @@ -0,0 +1,100 @@ +(function($){ +/** + * To make a form auto submit, all you have to do is 3 things: + * + * ctools_add_js('auto-submit'); + * + * On gadgets you want to auto-submit when changed, add the ctools-auto-submit + * class. With FAPI, add: + * @code + * '#attributes' => array('class' => array('ctools-auto-submit')), + * @endcode + * + * If you want to have auto-submit for every form element, + * add the ctools-auto-submit-full-form to the form. With FAPI, add: + * @code + * '#attributes' => array('class' => array('ctools-auto-submit-full-form')), + * @endcode + * + * If you want to exclude a field from the ctool-auto-submit-full-form auto submission, + * add the class ctools-auto-submit-exclude to the form element. With FAPI, add: + * @code + * '#attributes' => array('class' => array('ctools-auto-submit-exclude')), + * @endcode + * + * Finally, you have to identify which button you want clicked for autosubmit. + * The behavior of this button will be honored if it's ajaxy or not: + * @code + * '#attributes' => array('class' => array('ctools-use-ajax', 'ctools-auto-submit-click')), + * @endcode + * + * Currently only 'select', 'radio', 'checkbox' and 'textfield' types are supported. We probably + * could use additional support for HTML5 input types. + */ + +Drupal.behaviors.CToolsAutoSubmit = { + attach: function(context) { + // 'this' references the form element + function triggerSubmit (e) { + var $this = $(this); + if (!$this.hasClass('ctools-ajaxing')) { + $this.find('.ctools-auto-submit-click').click(); + } + } + + // the change event bubbles so we only need to bind it to the outer form + $('form.ctools-auto-submit-full-form', context) + .add('.ctools-auto-submit', context) + .filter('form, select, input:not(:text, :submit)') + .once('ctools-auto-submit') + .change(function (e) { + // don't trigger on text change for full-form + if ($(e.target).is(':not(:text, :submit, .ctools-auto-submit-exclude)')) { + triggerSubmit.call(e.target.form); + } + }); + + // e.keyCode: key + var discardKeyCode = [ + 16, // shift + 17, // ctrl + 18, // alt + 20, // caps lock + 33, // page up + 34, // page down + 35, // end + 36, // home + 37, // left arrow + 38, // up arrow + 39, // right arrow + 40, // down arrow + 9, // tab + 13, // enter + 27 // esc + ]; + // Don't wait for change event on textfields + $('.ctools-auto-submit-full-form input:text, input:text.ctools-auto-submit', context) + .filter(':not(.ctools-auto-submit-exclude)') + .once('ctools-auto-submit', function () { + // each textinput element has his own timeout + var timeoutID = 0; + $(this) + .bind('keydown keyup', function (e) { + if ($.inArray(e.keyCode, discardKeyCode) === -1) { + timeoutID && clearTimeout(timeoutID); + } + }) + .keyup(function(e) { + if ($.inArray(e.keyCode, discardKeyCode) === -1) { + timeoutID = setTimeout($.proxy(triggerSubmit, this.form), 500); + } + }) + .bind('change', function (e) { + if ($.inArray(e.keyCode, discardKeyCode) === -1) { + timeoutID = setTimeout($.proxy(triggerSubmit, this.form), 500); + } + }); + }); + } +} +})(jQuery); diff --git a/sites/all/modules/contrib/ctools/js/collapsible-div.js b/sites/all/modules/contrib/ctools/js/collapsible-div.js new file mode 100644 index 0000000..134151c --- /dev/null +++ b/sites/all/modules/contrib/ctools/js/collapsible-div.js @@ -0,0 +1,241 @@ +/** + * @file + * Javascript required for a simple collapsible div. + * + * Creating a collapsible div with this doesn't take too much. There are + * three classes necessary: + * + * - ctools-collapsible-container: This is the overall container that will be + * collapsible. This must be a div. + * - ctools-collapsible-handle: This is the title area, and is what will be + * visible when it is collapsed. This can be any block element, such as div + * or h2. + * - ctools-collapsible-content: This is the ocntent area and will only be + * visible when expanded. This must be a div. + * + * Adding 'ctools-collapsible-remember' to the container class will cause the + * state of the container to be stored in a cookie, and remembered from page + * load to page load. This will only work if the container has a unique ID, so + * very carefully add IDs to your containers. + * + * If the class 'ctools-no-container' is placed on the container, the container + * will be the handle. The content will be found by appending '-content' to the + * id of the handle. The ctools-collapsible-handle and + * ctools-collapsible-content classes will not be required in that case, and no + * restrictions on what of data the container is are placed. Like + * ctools-collapsible-remember this requires an id to eist. + * + * The content will be 'open' unless the container class has 'ctools-collapsed' + * as a class, which will cause the container to draw collapsed. + */ + +(function ($) { + // All CTools tools begin with this if they need to use the CTools namespace. + if (!Drupal.CTools) { + Drupal.CTools = {}; + } + + /** + * Object to store state. + * + * This object will remember the state of collapsible containers. The first + * time a state is requested, it will check the cookie and set up the variable. + * If a state has been changed, when the window is unloaded the state will be + * saved. + */ + Drupal.CTools.Collapsible = { + state: {}, + stateLoaded: false, + stateChanged: false, + cookieString: 'ctools-collapsible-state=', + + /** + * Get the current collapsed state of a container. + * + * If set to 1, the container is open. If set to -1, the container is + * collapsed. If unset the state is unknown, and the default state should + * be used. + */ + getState: function (id) { + if (!this.stateLoaded) { + this.loadCookie(); + } + + return this.state[id]; + }, + + /** + * Set the collapsed state of a container for subsequent page loads. + * + * Set the state to 1 for open, -1 for collapsed. + */ + setState: function (id, state) { + if (!this.stateLoaded) { + this.loadCookie(); + } + + this.state[id] = state; + + if (!this.stateChanged) { + this.stateChanged = true; + $(window).unload(this.unload); + } + }, + + /** + * Check the cookie and load the state variable. + */ + loadCookie: function () { + // If there is a previous instance of this cookie + if (document.cookie.length > 0) { + // Get the number of characters that have the list of values + // from our string index. + offset = document.cookie.indexOf(this.cookieString); + + // If its positive, there is a list! + if (offset != -1) { + offset += this.cookieString.length; + var end = document.cookie.indexOf(';', offset); + if (end == -1) { + end = document.cookie.length; + } + + // Get a list of all values that are saved on our string + var cookie = unescape(document.cookie.substring(offset, end)); + + if (cookie != '') { + var cookieList = cookie.split(','); + for (var i = 0; i < cookieList.length; i++) { + var info = cookieList[i].split(':'); + this.state[info[0]] = info[1]; + } + } + } + } + + this.stateLoaded = true; + }, + + /** + * Turn the state variable into a string and store it in the cookie. + */ + storeCookie: function () { + var cookie = ''; + + // Get a list of IDs, saparated by comma + for (i in this.state) { + if (cookie != '') { + cookie += ','; + } + cookie += i + ':' + this.state[i]; + } + + // Save this values on the cookie + document.cookie = this.cookieString + escape(cookie) + ';path=/'; + }, + + /** + * Respond to the unload event by storing the current state. + */ + unload: function() { + Drupal.CTools.Collapsible.storeCookie(); + } + }; + + // Set up an array for callbacks. + Drupal.CTools.CollapsibleCallbacks = []; + Drupal.CTools.CollapsibleCallbacksAfterToggle = []; + + /** + * Bind collapsible behavior to a given container. + */ + Drupal.CTools.bindCollapsible = function () { + var $container = $(this); + + // Allow the specification of the 'no container' class, which means the + // handle and the container can be completely independent. + if ($container.hasClass('ctools-no-container') && $container.attr('id')) { + // In this case, the container *is* the handle and the content is found + // by adding '-content' to the id. Obviously, an id is required. + var handle = $container; + var content = $('#' + $container.attr('id') + '-content'); + } + else { + var handle = $container.children('.ctools-collapsible-handle'); + var content = $container.children('div.ctools-collapsible-content'); + } + + if (content.length) { + // Create the toggle item and place it in front of the toggle. + var toggle = $(''); + handle.before(toggle); + + // If the remember class is set, check to see if we have a remembered + // state stored. + if ($container.hasClass('ctools-collapsible-remember') && $container.attr('id')) { + var state = Drupal.CTools.Collapsible.getState($container.attr('id')); + if (state == 1) { + $container.removeClass('ctools-collapsed'); + } + else if (state == -1) { + $container.addClass('ctools-collapsed'); + } + } + + // If we should start collapsed, do so: + if ($container.hasClass('ctools-collapsed')) { + toggle.toggleClass('ctools-toggle-collapsed'); + content.hide(); + } + + var afterToggle = function () { + if (Drupal.CTools.CollapsibleCallbacksAfterToggle) { + for (i in Drupal.CTools.CollapsibleCallbacksAfterToggle) { + Drupal.CTools.CollapsibleCallbacksAfterToggle[i]($container, handle, content, toggle); + } + } + } + + var clickMe = function () { + if (Drupal.CTools.CollapsibleCallbacks) { + for (i in Drupal.CTools.CollapsibleCallbacks) { + Drupal.CTools.CollapsibleCallbacks[i]($container, handle, content, toggle); + } + } + + // If the container is a table element slideToggle does not do what + // we want, so use toggle() instead. + if ($container.is('table')) { + content.toggle(0, afterToggle); + } + else { + content.slideToggle(100, afterToggle); + } + + $container.toggleClass('ctools-collapsed'); + toggle.toggleClass('ctools-toggle-collapsed'); + + // If we're supposed to remember the state of this class, do so. + if ($container.hasClass('ctools-collapsible-remember') && $container.attr('id')) { + var state = toggle.hasClass('ctools-toggle-collapsed') ? -1 : 1; + Drupal.CTools.Collapsible.setState($container.attr('id'), state); + } + + return false; + } + + // Let both the toggle and the handle be clickable. + toggle.click(clickMe); + handle.click(clickMe); + } + }; + + /** + * Support Drupal's 'behaviors' system for binding. + */ + Drupal.behaviors.CToolsCollapsible = { + attach: function(context) { + $('.ctools-collapsible-container', context).once('ctools-collapsible', Drupal.CTools.bindCollapsible); + } + } +})(jQuery); diff --git a/sites/all/modules/contrib/ctools/js/dependent.js b/sites/all/modules/contrib/ctools/js/dependent.js new file mode 100644 index 0000000..f74ec81 --- /dev/null +++ b/sites/all/modules/contrib/ctools/js/dependent.js @@ -0,0 +1,231 @@ +/** + * @file + * Provides dependent visibility for form items in CTools' ajax forms. + * + * To your $form item definition add: + * - '#process' => array('ctools_process_dependency'), + * - '#dependency' => array('id-of-form-item' => array(list, of, values, that, + * make, this, item, show), + * + * Special considerations: + * - Radios are harder. Because Drupal doesn't give radio groups individual IDs, + * use 'radio:name-of-radio'. + * + * - Checkboxes don't have their own id, so you need to add one in a div + * around the checkboxes via #prefix and #suffix. You actually need to add TWO + * divs because it's the parent that gets hidden. Also be sure to retain the + * 'expand_checkboxes' in the #process array, because the CTools process will + * override it. + */ + +(function ($) { + Drupal.CTools = Drupal.CTools || {}; + Drupal.CTools.dependent = {}; + + Drupal.CTools.dependent.bindings = {}; + Drupal.CTools.dependent.activeBindings = {}; + Drupal.CTools.dependent.activeTriggers = []; + + Drupal.CTools.dependent.inArray = function(array, search_term) { + var i = array.length; + while (i--) { + if (array[i] == search_term) { + return true; + } + } + return false; + } + + + Drupal.CTools.dependent.autoAttach = function() { + // Clear active bindings and triggers. + for (i in Drupal.CTools.dependent.activeTriggers) { + $(Drupal.CTools.dependent.activeTriggers[i]).unbind('change.ctools-dependent'); + } + Drupal.CTools.dependent.activeTriggers = []; + Drupal.CTools.dependent.activeBindings = {}; + Drupal.CTools.dependent.bindings = {}; + + if (!Drupal.settings.CTools) { + return; + } + + // Iterate through all relationships + for (id in Drupal.settings.CTools.dependent) { + // Test to make sure the id even exists; this helps clean up multiple + // AJAX calls with multiple forms. + + // Drupal.CTools.dependent.activeBindings[id] is a boolean, + // whether the binding is active or not. Defaults to no. + Drupal.CTools.dependent.activeBindings[id] = 0; + // Iterate through all possible values + for(bind_id in Drupal.settings.CTools.dependent[id].values) { + // This creates a backward relationship. The bind_id is the ID + // of the element which needs to change in order for the id to hide or become shown. + // The id is the ID of the item which will be conditionally hidden or shown. + // Here we're setting the bindings for the bind + // id to be an empty array if it doesn't already have bindings to it + if (!Drupal.CTools.dependent.bindings[bind_id]) { + Drupal.CTools.dependent.bindings[bind_id] = []; + } + // Add this ID + Drupal.CTools.dependent.bindings[bind_id].push(id); + // Big long if statement. + // Drupal.settings.CTools.dependent[id].values[bind_id] holds the possible values + + if (bind_id.substring(0, 6) == 'radio:') { + var trigger_id = "input[name='" + bind_id.substring(6) + "']"; + } + else { + var trigger_id = '#' + bind_id; + } + + Drupal.CTools.dependent.activeTriggers.push(trigger_id); + + if ($(trigger_id).attr('type') == 'checkbox') { + $(trigger_id).siblings('label').addClass('hidden-options'); + } + + var getValue = function(item, trigger) { + if ($(trigger).size() == 0) { + return null; + } + + if (item.substring(0, 6) == 'radio:') { + var val = $(trigger + ':checked').val(); + } + else { + switch ($(trigger).attr('type')) { + case 'checkbox': + var val = $(trigger).attr('checked') ? true : false; + + if (val) { + $(trigger).siblings('label').removeClass('hidden-options').addClass('expanded-options'); + } + else { + $(trigger).siblings('label').removeClass('expanded-options').addClass('hidden-options'); + } + + break; + default: + var val = $(trigger).val(); + } + } + return val; + } + + var setChangeTrigger = function(trigger_id, bind_id) { + // Triggered when change() is clicked. + var changeTrigger = function() { + var val = getValue(bind_id, trigger_id); + + if (val == null) { + return; + } + + for (i in Drupal.CTools.dependent.bindings[bind_id]) { + var id = Drupal.CTools.dependent.bindings[bind_id][i]; + // Fix numerous errors + if (typeof id != 'string') { + continue; + } + + // This bit had to be rewritten a bit because two properties on the + // same set caused the counter to go up and up and up. + if (!Drupal.CTools.dependent.activeBindings[id]) { + Drupal.CTools.dependent.activeBindings[id] = {}; + } + + if (val != null && Drupal.CTools.dependent.inArray(Drupal.settings.CTools.dependent[id].values[bind_id], val)) { + Drupal.CTools.dependent.activeBindings[id][bind_id] = 'bind'; + } + else { + delete Drupal.CTools.dependent.activeBindings[id][bind_id]; + } + + var len = 0; + for (i in Drupal.CTools.dependent.activeBindings[id]) { + len++; + } + + var object = $('#' + id + '-wrapper'); + if (!object.size()) { + // Some elements can't use the parent() method or they can + // damage things. They are guaranteed to have wrappers but + // only if dependent.inc provided them. This check prevents + // problems when multiple AJAX calls cause settings to build + // up. + var $original = $('#' + id); + if ($original.is('fieldset') || $original.is('textarea')) { + continue; + } + + object = $('#' + id).parent(); + } + + if (Drupal.settings.CTools.dependent[id].type == 'disable') { + if (Drupal.settings.CTools.dependent[id].num <= len) { + // Show if the element if criteria is matched + object.attr('disabled', false); + object.addClass('dependent-options'); + object.children().attr('disabled', false); + } + else { + // Otherwise hide. Use css rather than hide() because hide() + // does not work if the item is already hidden, for example, + // in a collapsed fieldset. + object.attr('disabled', true); + object.children().attr('disabled', true); + } + } + else { + if (Drupal.settings.CTools.dependent[id].num <= len) { + // Show if the element if criteria is matched + object.show(0); + object.addClass('dependent-options'); + } + else { + // Otherwise hide. Use css rather than hide() because hide() + // does not work if the item is already hidden, for example, + // in a collapsed fieldset. + object.css('display', 'none'); + } + } + } + } + + $(trigger_id).bind('change.ctools-dependent', function() { + // Trigger the internal change function + // the attr('id') is used because closures are more confusing + changeTrigger(trigger_id, bind_id); + }); + // Trigger initial reaction + changeTrigger(trigger_id, bind_id); + } + setChangeTrigger(trigger_id, bind_id); + } + } + } + + Drupal.behaviors.CToolsDependent = { + attach: function (context) { + Drupal.CTools.dependent.autoAttach(); + + // Really large sets of fields are too slow with the above method, so this + // is a sort of hacked one that's faster but much less flexible. + $("select.ctools-master-dependent") + .once('ctools-dependent') + .bind('change.ctools-dependent', function() { + var val = $(this).val(); + if (val == 'all') { + $('.ctools-dependent-all').show(0); + } + else { + $('.ctools-dependent-all').hide(0); + $('.ctools-dependent-' + val).show(0); + } + }) + .trigger('change.ctools-dependent'); + } + } +})(jQuery); diff --git a/sites/all/modules/contrib/ctools/js/dropbutton.js b/sites/all/modules/contrib/ctools/js/dropbutton.js new file mode 100644 index 0000000..f505550 --- /dev/null +++ b/sites/all/modules/contrib/ctools/js/dropbutton.js @@ -0,0 +1,94 @@ +/** + * @file + * Implement a simple, clickable dropbutton menu. + * + * See dropbutton.theme.inc for primary documentation. + * + * The javascript relies on four classes: + * - The dropbutton must be fully contained in a div with the class + * ctools-dropbutton. It must also contain the class ctools-no-js + * which will be immediately removed by the javascript; this allows for + * graceful degradation. + * - The trigger that opens the dropbutton must be an a tag wit hthe class + * ctools-dropbutton-link. The href should just be '#' as this will never + * be allowed to complete. + * - The part of the dropbutton that will appear when the link is clicked must + * be a div with class ctools-dropbutton-container. + * - Finally, ctools-dropbutton-hover will be placed on any link that is being + * hovered over, so that the browser can restyle the links. + * + * This tool isn't meant to replace click-tips or anything, it is specifically + * meant to work well presenting menus. + */ + +(function ($) { + Drupal.behaviors.CToolsDropbutton = { + attach: function() { + // Process buttons. All dropbuttons are buttons. + $('.ctools-button') + .once('ctools-button') + .removeClass('ctools-no-js'); + + // Process dropbuttons. Not all buttons are dropbuttons. + $('.ctools-dropbutton').once('ctools-dropbutton', function() { + var $dropbutton = $(this); + var $button = $('.ctools-content', $dropbutton); + var $secondaryActions = $('li', $button).not(':first'); + var $twisty = $(".ctools-link", $dropbutton); + var open = false; + var hovering = false; + var timerID = 0; + + var toggle = function(close) { + // if it's open or we're told to close it, close it. + if (open || close) { + // If we're just toggling it, close it immediately. + if (!close) { + open = false; + $secondaryActions.slideUp(100); + $dropbutton.removeClass('open'); + } + else { + // If we were told to close it, wait half a second to make + // sure that's what the user wanted. + // Clear any previous timer we were using. + if (timerID) { + clearTimeout(timerID); + } + timerID = setTimeout(function() { + if (!hovering) { + open = false; + $secondaryActions.slideUp(100); + $dropbutton.removeClass('open'); + }}, 500); + } + } + else { + // open it. + open = true; + $secondaryActions.animate({height: "show", opacity: "show"}, 100); + $dropbutton.addClass('open'); + } + } + // Hide the secondary actions initially. + $secondaryActions.hide(); + + $twisty.click(function() { + toggle(); + return false; + }); + + $dropbutton.hover( + function() { + hovering = true; + }, // hover in + function() { // hover out + hovering = false; + toggle(true); + return false; + } + ); + }); + } + } +})(jQuery); diff --git a/sites/all/modules/contrib/ctools/js/dropdown.js b/sites/all/modules/contrib/ctools/js/dropdown.js new file mode 100644 index 0000000..c829ae2 --- /dev/null +++ b/sites/all/modules/contrib/ctools/js/dropdown.js @@ -0,0 +1,87 @@ +/** + * @file + * Implement a simple, clickable dropdown menu. + * + * See dropdown.theme.inc for primary documentation. + * + * The javascript relies on four classes: + * - The dropdown must be fully contained in a div with the class + * ctools-dropdown. It must also contain the class ctools-dropdown-no-js + * which will be immediately removed by the javascript; this allows for + * graceful degradation. + * - The trigger that opens the dropdown must be an a tag wit hthe class + * ctools-dropdown-link. The href should just be '#' as this will never + * be allowed to complete. + * - The part of the dropdown that will appear when the link is clicked must + * be a div with class ctools-dropdown-container. + * - Finally, ctools-dropdown-hover will be placed on any link that is being + * hovered over, so that the browser can restyle the links. + * + * This tool isn't meant to replace click-tips or anything, it is specifically + * meant to work well presenting menus. + */ + +(function ($) { + Drupal.behaviors.CToolsDropdown = { + attach: function() { + $('div.ctools-dropdown').once('ctools-dropdown', function() { + var $dropdown = $(this); + var open = false; + var hovering = false; + var timerID = 0; + + $dropdown.removeClass('ctools-dropdown-no-js'); + + var toggle = function(close) { + // if it's open or we're told to close it, close it. + if (open || close) { + // If we're just toggling it, close it immediately. + if (!close) { + open = false; + $("div.ctools-dropdown-container", $dropdown).slideUp(100); + } + else { + // If we were told to close it, wait half a second to make + // sure that's what the user wanted. + // Clear any previous timer we were using. + if (timerID) { + clearTimeout(timerID); + } + timerID = setTimeout(function() { + if (!hovering) { + open = false; + $("div.ctools-dropdown-container", $dropdown).slideUp(100); + } + }, 500); + } + } + else { + // open it. + open = true; + $("div.ctools-dropdown-container", $dropdown) + .animate({height: "show", opacity: "show"}, 100); + } + } + $("a.ctools-dropdown-link", $dropdown).click(function() { + toggle(); + return false; + }); + + $dropdown.hover( + function() { + hovering = true; + }, // hover in + function() { // hover out + hovering = false; + toggle(true); + return false; + }); + // @todo -- just use CSS for this noise. + $("div.ctools-dropdown-container a").hover( + function() { $(this).addClass('ctools-dropdown-hover'); }, + function() { $(this).removeClass('ctools-dropdown-hover'); } + ); + }); + } + } +})(jQuery); diff --git a/sites/all/modules/contrib/ctools/js/jump-menu.js b/sites/all/modules/contrib/ctools/js/jump-menu.js new file mode 100644 index 0000000..7b0928a --- /dev/null +++ b/sites/all/modules/contrib/ctools/js/jump-menu.js @@ -0,0 +1,42 @@ + +(function($) { + Drupal.behaviors.CToolsJumpMenu = { + attach: function(context) { + $('.ctools-jump-menu-hide') + .once('ctools-jump-menu') + .hide(); + + $('.ctools-jump-menu-change') + .once('ctools-jump-menu') + .change(function() { + var loc = $(this).val(); + var urlArray = loc.split('::'); + if (urlArray[1]) { + location.href = urlArray[1]; + } + else { + location.href = loc; + } + return false; + }); + + $('.ctools-jump-menu-button') + .once('ctools-jump-menu') + .click(function() { + // Instead of submitting the form, just perform the redirect. + + // Find our sibling value. + var $select = $(this).parents('form').find('.ctools-jump-menu-select'); + var loc = $select.val(); + var urlArray = loc.split('::'); + if (urlArray[1]) { + location.href = urlArray[1]; + } + else { + location.href = loc; + } + return false; + }); + } + } +})(jQuery); diff --git a/sites/all/modules/contrib/ctools/js/modal.js b/sites/all/modules/contrib/ctools/js/modal.js new file mode 100644 index 0000000..c757ef2 --- /dev/null +++ b/sites/all/modules/contrib/ctools/js/modal.js @@ -0,0 +1,696 @@ +/** + * @file + * + * Implement a modal form. + * + * @see modal.inc for documentation. + * + * This javascript relies on the CTools ajax responder. + */ + +(function ($) { + // Make sure our objects are defined. + Drupal.CTools = Drupal.CTools || {}; + Drupal.CTools.Modal = Drupal.CTools.Modal || {}; + + /** + * Display the modal + * + * @todo -- document the settings. + */ + Drupal.CTools.Modal.show = function(choice) { + var opts = {}; + + if (choice && typeof choice == 'string' && Drupal.settings[choice]) { + // This notation guarantees we are actually copying it. + $.extend(true, opts, Drupal.settings[choice]); + } + else if (choice) { + $.extend(true, opts, choice); + } + + var defaults = { + modalTheme: 'CToolsModalDialog', + throbberTheme: 'CToolsModalThrobber', + animation: 'show', + animationSpeed: 'fast', + modalSize: { + type: 'scale', + width: .8, + height: .8, + addWidth: 0, + addHeight: 0, + // How much to remove from the inner content to make space for the + // theming. + contentRight: 25, + contentBottom: 45 + }, + modalOptions: { + opacity: .55, + background: '#fff' + }, + modalClass: 'default' + }; + + var settings = {}; + $.extend(true, settings, defaults, Drupal.settings.CToolsModal, opts); + + if (Drupal.CTools.Modal.currentSettings && Drupal.CTools.Modal.currentSettings != settings) { + Drupal.CTools.Modal.modal.remove(); + Drupal.CTools.Modal.modal = null; + } + + Drupal.CTools.Modal.currentSettings = settings; + + var resize = function(e) { + // When creating the modal, it actually exists only in a theoretical + // place that is not in the DOM. But once the modal exists, it is in the + // DOM so the context must be set appropriately. + var context = e ? document : Drupal.CTools.Modal.modal; + + if (Drupal.CTools.Modal.currentSettings.modalSize.type == 'scale') { + var width = $(window).width() * Drupal.CTools.Modal.currentSettings.modalSize.width; + var height = $(window).height() * Drupal.CTools.Modal.currentSettings.modalSize.height; + } + else { + var width = Drupal.CTools.Modal.currentSettings.modalSize.width; + var height = Drupal.CTools.Modal.currentSettings.modalSize.height; + } + + // Use the additionol pixels for creating the width and height. + $('div.ctools-modal-content', context).css({ + 'width': width + Drupal.CTools.Modal.currentSettings.modalSize.addWidth + 'px', + 'height': height + Drupal.CTools.Modal.currentSettings.modalSize.addHeight + 'px' + }); + $('div.ctools-modal-content .modal-content', context).css({ + 'width': (width - Drupal.CTools.Modal.currentSettings.modalSize.contentRight) + 'px', + 'height': (height - Drupal.CTools.Modal.currentSettings.modalSize.contentBottom) + 'px' + }); + } + + if (!Drupal.CTools.Modal.modal) { + Drupal.CTools.Modal.modal = $(Drupal.theme(settings.modalTheme)); + if (settings.modalSize.type == 'scale') { + $(window).bind('resize', resize); + } + } + + resize(); + + $('span.modal-title', Drupal.CTools.Modal.modal).html(Drupal.CTools.Modal.currentSettings.loadingText); + Drupal.CTools.Modal.modalContent(Drupal.CTools.Modal.modal, settings.modalOptions, settings.animation, settings.animationSpeed, settings.modalClass); + $('#modalContent .modal-content').html(Drupal.theme(settings.throbberTheme)).addClass('ctools-modal-loading'); + + // Position autocomplete results based on the scroll position of the modal. + $('#modalContent .modal-content').delegate('input.form-autocomplete', 'keyup', function() { + $('#autocomplete').css('top', $(this).position().top + $(this).outerHeight() + $(this).offsetParent().filter('#modal-content').scrollTop()); + }); + }; + + /** + * Hide the modal + */ + Drupal.CTools.Modal.dismiss = function() { + if (Drupal.CTools.Modal.modal) { + Drupal.CTools.Modal.unmodalContent(Drupal.CTools.Modal.modal); + } + }; + + /** + * Provide the HTML to create the modal dialog. + */ + Drupal.theme.prototype.CToolsModalDialog = function () { + var html = '' + html += ' '; + + return html; + } + + /** + * Provide the HTML to create the throbber. + */ + Drupal.theme.prototype.CToolsModalThrobber = function () { + var html = ''; + html += ' '; + + return html; + }; + + /** + * Figure out what settings string to use to display a modal. + */ + Drupal.CTools.Modal.getSettings = function (object) { + var match = $(object).attr('class').match(/ctools-modal-(\S+)/); + if (match) { + return match[1]; + } + } + + /** + * Click function for modals that can be cached. + */ + Drupal.CTools.Modal.clickAjaxCacheLink = function () { + Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(this)); + return Drupal.CTools.AJAX.clickAJAXCacheLink.apply(this); + }; + + /** + * Handler to prepare the modal for the response + */ + Drupal.CTools.Modal.clickAjaxLink = function () { + Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(this)); + return false; + }; + + /** + * Submit responder to do an AJAX submit on all modal forms. + */ + Drupal.CTools.Modal.submitAjaxForm = function(e) { + var $form = $(this); + var url = $form.attr('action'); + + setTimeout(function() { Drupal.CTools.AJAX.ajaxSubmit($form, url); }, 1); + return false; + } + + /** + * Bind links that will open modals to the appropriate function. + */ + Drupal.behaviors.ZZCToolsModal = { + attach: function(context) { + // Bind links + // Note that doing so in this order means that the two classes can be + // used together safely. + /* + * @todo remimplement the warm caching feature + $('a.ctools-use-modal-cache', context).once('ctools-use-modal', function() { + $(this).click(Drupal.CTools.Modal.clickAjaxCacheLink); + Drupal.CTools.AJAX.warmCache.apply(this); + }); + */ + + $('area.ctools-use-modal, a.ctools-use-modal', context).once('ctools-use-modal', function() { + var $this = $(this); + $this.click(Drupal.CTools.Modal.clickAjaxLink); + // Create a drupal ajax object + var element_settings = {}; + if ($this.attr('href')) { + element_settings.url = $this.attr('href'); + element_settings.event = 'click'; + element_settings.progress = { type: 'throbber' }; + } + var base = $this.attr('href'); + Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); + }); + + // Bind buttons + $('input.ctools-use-modal, button.ctools-use-modal', context).once('ctools-use-modal', function() { + var $this = $(this); + $this.click(Drupal.CTools.Modal.clickAjaxLink); + var button = this; + var element_settings = {}; + + // AJAX submits specified in this manner automatically submit to the + // normal form action. + element_settings.url = Drupal.CTools.Modal.findURL(this); + if (element_settings.url == '') { + element_settings.url = $(this).closest('form').attr('action'); + } + element_settings.event = 'click'; + element_settings.setClick = true; + + var base = $this.attr('id'); + Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); + + // Make sure changes to settings are reflected in the URL. + $('.' + $(button).attr('id') + '-url').change(function() { + Drupal.ajax[base].options.url = Drupal.CTools.Modal.findURL(button); + }); + }); + + // Bind our custom event to the form submit + $('#modal-content form', context).once('ctools-use-modal', function() { + var $this = $(this); + var element_settings = {}; + + element_settings.url = $this.attr('action'); + element_settings.event = 'submit'; + element_settings.progress = { 'type': 'throbber' } + var base = $this.attr('id'); + + Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings); + Drupal.ajax[base].form = $this; + + $('input[type=submit], button', this).click(function(event) { + Drupal.ajax[base].element = this; + this.form.clk = this; + // Stop autocomplete from submitting. + if (Drupal.autocompleteSubmit && !Drupal.autocompleteSubmit()) { + return false; + } + // An empty event means we were triggered via .click() and + // in jquery 1.4 this won't trigger a submit. + if (event.bubbles == undefined) { + $(this.form).trigger('submit'); + return false; + } + }); + }); + + // Bind a click handler to allow elements with the 'ctools-close-modal' + // class to close the modal. + $('.ctools-close-modal', context).once('ctools-close-modal') + .click(function() { + Drupal.CTools.Modal.dismiss(); + return false; + }); + } + }; + + // The following are implementations of AJAX responder commands. + + /** + * AJAX responder command to place HTML within the modal. + */ + Drupal.CTools.Modal.modal_display = function(ajax, response, status) { + if ($('#modalContent').length == 0) { + Drupal.CTools.Modal.show(Drupal.CTools.Modal.getSettings(ajax.element)); + } + $('#modal-title').html(response.title); + // Simulate an actual page load by scrolling to the top after adding the + // content. This is helpful for allowing users to see error messages at the + // top of a form, etc. + $('#modal-content').html(response.output).scrollTop(0); + + // Attach behaviors within a modal dialog. + var settings = response.settings || ajax.settings || Drupal.settings; + Drupal.attachBehaviors('#modalContent', settings); + + if ($('#modal-content').hasClass('ctools-modal-loading')) { + $('#modal-content').removeClass('ctools-modal-loading'); + } + else { + // If the modal was already shown, and we are simply replacing its + // content, then focus on the first focusable element in the modal. + // (When first showing the modal, focus will be placed on the close + // button by the show() function called above.) + $('#modal-content :focusable:first').focus(); + } + } + + /** + * AJAX responder command to dismiss the modal. + */ + Drupal.CTools.Modal.modal_dismiss = function(command) { + Drupal.CTools.Modal.dismiss(); + $('link.ctools-temporary-css').remove(); + } + + /** + * Display loading + */ + //Drupal.CTools.AJAX.commands.modal_loading = function(command) { + Drupal.CTools.Modal.modal_loading = function(command) { + Drupal.CTools.Modal.modal_display({ + output: Drupal.theme(Drupal.CTools.Modal.currentSettings.throbberTheme), + title: Drupal.CTools.Modal.currentSettings.loadingText + }); + } + + /** + * Find a URL for an AJAX button. + * + * The URL for this gadget will be composed of the values of items by + * taking the ID of this item and adding -url and looking for that + * class. They need to be in the form in order since we will + * concat them all together using '/'. + */ + Drupal.CTools.Modal.findURL = function(item) { + var url = ''; + var url_class = '.' + $(item).attr('id') + '-url'; + $(url_class).each( + function() { + var $this = $(this); + if (url && $this.val()) { + url += '/'; + } + url += $this.val(); + }); + return url; + }; + + + /** + * modalContent + * @param content string to display in the content box + * @param css obj of css attributes + * @param animation (fadeIn, slideDown, show) + * @param speed (valid animation speeds slow, medium, fast or # in ms) + * @param modalClass class added to div#modalContent + */ + Drupal.CTools.Modal.modalContent = function(content, css, animation, speed, modalClass) { + // If our animation isn't set, make it just show/pop + if (!animation) { + animation = 'show'; + } + else { + // If our animation isn't "fadeIn" or "slideDown" then it always is show + if (animation != 'fadeIn' && animation != 'slideDown') { + animation = 'show'; + } + } + + if (!speed) { + speed = 'fast'; + } + + // Build our base attributes and allow them to be overriden + css = jQuery.extend({ + position: 'absolute', + left: '0px', + margin: '0px', + background: '#000', + opacity: '.55' + }, css); + + // Add opacity handling for IE. + css.filter = 'alpha(opacity=' + (100 * css.opacity) + ')'; + content.hide(); + + // If we already have modalContent, remove it. + if ($('#modalBackdrop').length) $('#modalBackdrop').remove(); + if ($('#modalContent').length) $('#modalContent').remove(); + + // position code lifted from http://www.quirksmode.org/viewport/compatibility.html + if (self.pageYOffset) { // all except Explorer + var wt = self.pageYOffset; + } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict + var wt = document.documentElement.scrollTop; + } else if (document.body) { // all other Explorers + var wt = document.body.scrollTop; + } + + // Get our dimensions + + // Get the docHeight and (ugly hack) add 50 pixels to make sure we dont have a *visible* border below our div + var docHeight = $(document).height() + 50; + var docWidth = $(document).width(); + var winHeight = $(window).height(); + var winWidth = $(window).width(); + if( docHeight < winHeight ) docHeight = winHeight; + + // Create our divs + $('body').append(''); + + // Get a list of the tabbable elements in the modal content. + var getTabbableElements = function () { + var tabbableElements = $('#modalContent :tabbable'), + radioButtons = tabbableElements.filter('input[type="radio"]'); + + // The list of tabbable elements from jQuery is *almost* right. The + // exception is with groups of radio buttons. The list from jQuery will + // include all radio buttons, when in fact, only the selected radio button + // is tabbable, and if no radio buttons in a group are selected, then only + // the first is tabbable. + if (radioButtons.length > 0) { + // First, build up an index of which groups have an item selected or not. + var anySelected = {}; + radioButtons.each(function () { + var name = this.name; + + if (typeof anySelected[name] === 'undefined') { + anySelected[name] = radioButtons.filter('input[name="' + name + '"]:checked').length !== 0; + } + }); + + // Next filter out the radio buttons that aren't really tabbable. + var found = {}; + tabbableElements = tabbableElements.filter(function () { + var keep = true; + + if (this.type == 'radio') { + if (anySelected[this.name]) { + // Only keep the selected one. + keep = this.checked; + } + else { + // Only keep the first one. + if (found[this.name]) { + keep = false; + } + found[this.name] = true; + } + } + + return keep; + }); + } + + return tabbableElements.get(); + }; + + // Keyboard and focus event handler ensures only modal elements gain focus. + modalEventHandler = function( event ) { + target = null; + if ( event ) { //Mozilla + target = event.target; + } else { //IE + event = window.event; + target = event.srcElement; + } + + var parents = $(target).parents().get(); + for (var i = 0; i < parents.length; ++i) { + var position = $(parents[i]).css('position'); + if (position == 'absolute' || position == 'fixed') { + return true; + } + } + + if ($(target).is('#modalContent, body') || $(target).filter('*:visible').parents('#modalContent').length) { + // Allow the event only if target is a visible child node + // of #modalContent. + return true; + } + else { + getTabbableElements()[0].focus(); + } + + event.preventDefault(); + }; + $('body').bind( 'focus', modalEventHandler ); + $('body').bind( 'keypress', modalEventHandler ); + + // Keypress handler Ensures you can only TAB to elements within the modal. + // Based on the psuedo-code from WAI-ARIA 1.0 Authoring Practices section + // 3.3.1 "Trapping Focus". + modalTabTrapHandler = function (evt) { + // We only care about the TAB key. + if (evt.which != 9) { + return true; + } + + var tabbableElements = getTabbableElements(), + firstTabbableElement = tabbableElements[0], + lastTabbableElement = tabbableElements[tabbableElements.length - 1], + singleTabbableElement = firstTabbableElement == lastTabbableElement, + node = evt.target; + + // If this is the first element and the user wants to go backwards, then + // jump to the last element. + if (node == firstTabbableElement && evt.shiftKey) { + if (!singleTabbableElement) { + lastTabbableElement.focus(); + } + return false; + } + // If this is the last element and the user wants to go forwards, then + // jump to the first element. + else if (node == lastTabbableElement && !evt.shiftKey) { + if (!singleTabbableElement) { + firstTabbableElement.focus(); + } + return false; + } + // If this element isn't in the dialog at all, then jump to the first + // or last element to get the user into the game. + else if ($.inArray(node, tabbableElements) == -1) { + // Make sure the node isn't in another modal (ie. WYSIWYG modal). + var parents = $(node).parents().get(); + for (var i = 0; i < parents.length; ++i) { + var position = $(parents[i]).css('position'); + if (position == 'absolute' || position == 'fixed') { + return true; + } + } + + if (evt.shiftKey) { + lastTabbableElement.focus(); + } + else { + firstTabbableElement.focus(); + } + } + }; + $('body').bind('keydown', modalTabTrapHandler); + + // Create our content div, get the dimensions, and hide it + var modalContent = $('#modalContent').css('top','-1000px'); + var mdcTop = wt + ( winHeight / 2 ) - ( modalContent.outerHeight() / 2); + var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2); + $('#modalBackdrop').css(css).css('top', 0).css('height', docHeight + 'px').css('width', docWidth + 'px').show(); + modalContent.css({top: mdcTop + 'px', left: mdcLeft + 'px'}).hide()[animation](speed); + + // Bind a click for closing the modalContent + modalContentClose = function(){close(); return false;}; + $('.close').bind('click', modalContentClose); + + // Bind a keypress on escape for closing the modalContent + modalEventEscapeCloseHandler = function(event) { + if (event.keyCode == 27) { + close(); + return false; + } + }; + + $(document).bind('keydown', modalEventEscapeCloseHandler); + + // Per WAI-ARIA 1.0 Authoring Practices, initial focus should be on the + // close button, but we should save the original focus to restore it after + // the dialog is closed. + var oldFocus = document.activeElement; + $('.close').focus(); + + // Close the open modal content and backdrop + function close() { + // Unbind the events + $(window).unbind('resize', modalContentResize); + $('body').unbind( 'focus', modalEventHandler); + $('body').unbind( 'keypress', modalEventHandler ); + $('body').unbind( 'keydown', modalTabTrapHandler ); + $('.close').unbind('click', modalContentClose); + $('body').unbind('keypress', modalEventEscapeCloseHandler); + $(document).trigger('CToolsDetachBehaviors', $('#modalContent')); + + // Set our animation parameters and use them + if ( animation == 'fadeIn' ) animation = 'fadeOut'; + if ( animation == 'slideDown' ) animation = 'slideUp'; + if ( animation == 'show' ) animation = 'hide'; + + // Close the content + modalContent.hide()[animation](speed); + + // Remove the content + $('#modalContent').remove(); + $('#modalBackdrop').remove(); + + // Restore focus to where it was before opening the dialog + $(oldFocus).focus(); + }; + + // Move and resize the modalBackdrop and modalContent on window resize. + modalContentResize = function(){ + + // Reset the backdrop height/width to get accurate document size. + $('#modalBackdrop').css('height', '').css('width', ''); + + // Position code lifted from: + // http://www.quirksmode.org/viewport/compatibility.html + if (self.pageYOffset) { // all except Explorer + var wt = self.pageYOffset; + } else if (document.documentElement && document.documentElement.scrollTop) { // Explorer 6 Strict + var wt = document.documentElement.scrollTop; + } else if (document.body) { // all other Explorers + var wt = document.body.scrollTop; + } + + // Get our heights + var docHeight = $(document).height(); + var docWidth = $(document).width(); + var winHeight = $(window).height(); + var winWidth = $(window).width(); + if( docHeight < winHeight ) docHeight = winHeight; + + // Get where we should move content to + var modalContent = $('#modalContent'); + var mdcTop = wt + ( winHeight / 2 ) - ( modalContent.outerHeight() / 2); + var mdcLeft = ( winWidth / 2 ) - ( modalContent.outerWidth() / 2); + + // Apply the changes + $('#modalBackdrop').css('height', docHeight + 'px').css('width', docWidth + 'px').show(); + modalContent.css('top', mdcTop + 'px').css('left', mdcLeft + 'px').show(); + }; + $(window).bind('resize', modalContentResize); + }; + + /** + * unmodalContent + * @param content (The jQuery object to remove) + * @param animation (fadeOut, slideUp, show) + * @param speed (valid animation speeds slow, medium, fast or # in ms) + */ + Drupal.CTools.Modal.unmodalContent = function(content, animation, speed) + { + // If our animation isn't set, make it just show/pop + if (!animation) { var animation = 'show'; } else { + // If our animation isn't "fade" then it always is show + if (( animation != 'fadeOut' ) && ( animation != 'slideUp')) animation = 'show'; + } + // Set a speed if we dont have one + if ( !speed ) var speed = 'fast'; + + // Unbind the events we bound + $(window).unbind('resize', modalContentResize); + $('body').unbind('focus', modalEventHandler); + $('body').unbind('keypress', modalEventHandler); + $('body').unbind( 'keydown', modalTabTrapHandler ); + $('.close').unbind('click', modalContentClose); + $('body').unbind('keypress', modalEventEscapeCloseHandler); + $(document).trigger('CToolsDetachBehaviors', $('#modalContent')); + + // jQuery magic loop through the instances and run the animations or removal. + content.each(function(){ + if ( animation == 'fade' ) { + $('#modalContent').fadeOut(speed, function() { + $('#modalBackdrop').fadeOut(speed, function() { + $(this).remove(); + }); + $(this).remove(); + }); + } else { + if ( animation == 'slide' ) { + $('#modalContent').slideUp(speed,function() { + $('#modalBackdrop').slideUp(speed, function() { + $(this).remove(); + }); + $(this).remove(); + }); + } else { + $('#modalContent').remove(); + $('#modalBackdrop').remove(); + } + } + }); + }; + +$(function() { + Drupal.ajax.prototype.commands.modal_display = Drupal.CTools.Modal.modal_display; + Drupal.ajax.prototype.commands.modal_dismiss = Drupal.CTools.Modal.modal_dismiss; +}); + +})(jQuery); diff --git a/sites/all/modules/contrib/ctools/js/states-show.js b/sites/all/modules/contrib/ctools/js/states-show.js new file mode 100644 index 0000000..88df584 --- /dev/null +++ b/sites/all/modules/contrib/ctools/js/states-show.js @@ -0,0 +1,43 @@ +/** + * @file + * Custom state for handling visibility + */ + +/** + * Add a new state to Drupal #states. We use this to toggle element-invisible + * to show/hidden #states elements. This allows elements to be visible to + * screen readers. + * + * To use: + * $form['my_form_field'] = array( + * .. + * // Only show this field if 'some_other_field' is checked. + * '#states => array( + * 'show' => array( + * 'some-other-field' => array('checked' => TRUE), + * ), + * ), + * .. + * // Required to load the 'show' state handler. + * '#attached' => array( + * 'js' => array(ctools_attach_js('states-show')), + * ), + * ); + */ + +(function ($) { + 'use strict'; + + Drupal.states.State.aliases.hidden = '!show'; + + // Show/hide form items by toggling the 'element-invisible' class. This is a + // more accessible option than the core 'visible' state. + $(document).bind('state:show', function(e) { + if (e.trigger) { + var element = $(e.target).closest('.form-item, .form-submit, .form-wrapper'); + element.toggle(e.value); + e.value === true ? element.removeClass('element-invisible') : element.addClass('element-invisible'); + } + }); + +})(jQuery); diff --git a/sites/all/modules/contrib/ctools/js/stylizer.js b/sites/all/modules/contrib/ctools/js/stylizer.js new file mode 100644 index 0000000..16d6c49 --- /dev/null +++ b/sites/all/modules/contrib/ctools/js/stylizer.js @@ -0,0 +1,220 @@ + +(function ($) { + Drupal.CTools = Drupal.CTools || {}; + Drupal.CTools.Stylizer = {}; + + Drupal.CTools.Stylizer.addFarbtastic = function(context) { + // This behavior attaches by ID, so is only valid once on a page. + if ($('ctools_stylizer_color_scheme_form .color-form.Stylizer-processed').size()) { + return; + } + + var form = $('.color-form', context); + var inputs = []; + var hooks = []; + var locks = []; + var focused = null; + + // Add Farbtastic + $(form).prepend('
').addClass('color-processed'); + var farb = $.farbtastic('#placeholder'); + + // Decode reference colors to HSL + /*var reference = Drupal.settings.Stylizer.reference.clone(); + for (i in reference) { + reference[i] = farb.RGBToHSL(farb.unpack(reference[i])); + } */ + + // Set up colorscheme selector + $('#edit-scheme', form).change(function () { + var colors = this.options[this.selectedIndex].value; + if (colors != '') { + colors = colors.split(','); + for (i in colors) { + callback(inputs[i], colors[i], false, true); + } + } + }); + + /** + * Shift a given color, using a reference pair (ref in HSL). + * + * This algorithm ensures relative ordering on the saturation and luminance + * axes is preserved, and performs a simple hue shift. + * + * It is also symmetrical. If: shift_color(c, a, b) == d, + * then shift_color(d, b, a) == c. + */ + function shift_color(given, ref1, ref2) { + // Convert to HSL + given = farb.RGBToHSL(farb.unpack(given)); + + // Hue: apply delta + given[0] += ref2[0] - ref1[0]; + + // Saturation: interpolate + if (ref1[1] == 0 || ref2[1] == 0) { + given[1] = ref2[1]; + } + else { + var d = ref1[1] / ref2[1]; + if (d > 1) { + given[1] /= d; + } + else { + given[1] = 1 - (1 - given[1]) * d; + } + } + + // Luminance: interpolate + if (ref1[2] == 0 || ref2[2] == 0) { + given[2] = ref2[2]; + } + else { + var d = ref1[2] / ref2[2]; + if (d > 1) { + given[2] /= d; + } + else { + given[2] = 1 - (1 - given[2]) * d; + } + } + + return farb.pack(farb.HSLToRGB(given)); + } + + /** + * Callback for Farbtastic when a new color is chosen. + */ + function callback(input, color, propagate, colorscheme) { + // Set background/foreground color + $(input).css({ + backgroundColor: color, + 'color': farb.RGBToHSL(farb.unpack(color))[2] > 0.5 ? '#000' : '#fff' + }); + + // Change input value + if (input.value && input.value != color) { + input.value = color; + + // Update locked values + if (propagate) { + var i = input.i; + for (j = i + 1; ; ++j) { + if (!locks[j - 1] || $(locks[j - 1]).is('.unlocked')) break; + var matched = shift_color(color, reference[input.key], reference[inputs[j].key]); + callback(inputs[j], matched, false); + } + for (j = i - 1; ; --j) { + if (!locks[j] || $(locks[j]).is('.unlocked')) break; + var matched = shift_color(color, reference[input.key], reference[inputs[j].key]); + callback(inputs[j], matched, false); + } + + } + + // Reset colorscheme selector + if (!colorscheme) { + resetScheme(); + } + } + + } + + /** + * Reset the color scheme selector. + */ + function resetScheme() { + $('#edit-scheme', form).each(function () { + this.selectedIndex = this.options.length - 1; + }); + } + + // Focus the Farbtastic on a particular field. + function focus() { + var input = this; + // Remove old bindings + focused && $(focused).unbind('keyup', farb.updateValue) + .unbind('keyup', resetScheme) + .parent().removeClass('item-selected'); + + // Add new bindings + focused = this; + farb.linkTo(function (color) { callback(input, color, true, false); }); + farb.setColor(this.value); + $(focused).keyup(farb.updateValue).keyup(resetScheme) + .parent().addClass('item-selected'); + } + + // Initialize color fields + $('#palette input.form-text', form) + .each(function () { + // Extract palette field name + this.key = this.id.substring(13); + + // Link to color picker temporarily to initialize. + farb.linkTo(function () {}).setColor('#000').linkTo(this); + + // Add lock + var i = inputs.length; + if (inputs.length) { + var lock = $('
').toggle( + function () { + $(this).addClass('unlocked'); + $(hooks[i - 1]).attr('class', + locks[i - 2] && $(locks[i - 2]).is(':not(.unlocked)') ? 'hook up' : 'hook' + ); + $(hooks[i]).attr('class', + locks[i] && $(locks[i]).is(':not(.unlocked)') ? 'hook down' : 'hook' + ); + }, + function () { + $(this).removeClass('unlocked'); + $(hooks[i - 1]).attr('class', + locks[i - 2] && $(locks[i - 2]).is(':not(.unlocked)') ? 'hook both' : 'hook down' + ); + $(hooks[i]).attr('class', + locks[i] && $(locks[i]).is(':not(.unlocked)') ? 'hook both' : 'hook up' + ); + } + ); + $(this).after(lock); + locks.push(lock); + }; + + // Add hook + var $this = $(this); + var hook = $('
'); + $this.after(hook); + hooks.push(hook); + + $this.parent().find('.lock').click(); + this.i = i; + inputs.push(this); + }) + .focus(focus); + + $('#palette label', form); + + // Focus first color + focus.call(inputs[0]); + }; + + Drupal.behaviors.CToolsColorSettings = { + attach: function() { + $('.ctools-stylizer-color-edit:not(.ctools-color-processed)') + .addClass('ctools-color-processed') + .each(function() { + Drupal.CTools.Stylizer.addFarbtastic('#' + $(this).attr('id')); + }); + + $('div.form-item div.ctools-style-icon:not(.ctools-color-processed)') + .addClass('ctools-color-processed') + .click(function() { + $widget = $('input', $(this).parent()); + // Toggle if a checkbox, turn on if a radio. + $widget.attr('checked', !$widget.attr('checked') || $widget.is('input[type=radio]')); + }); + } + } +})(jQuery); diff --git a/sites/all/modules/contrib/ctools/page_manager/css/page-manager.css b/sites/all/modules/contrib/ctools/page_manager/css/page-manager.css new file mode 100644 index 0000000..1a7dd5e --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/css/page-manager.css @@ -0,0 +1,372 @@ +body form#page-manager-list-pages-form { + margin: 0 0 20px 0; +} + +#page-manager-list-pages-form .form-item { + padding-right: 1em; /* LTR */ + float: left; /* LTR */ + margin-top: 0; + margin-bottom: 0; +} + +#page-manager-list-pages { + width: 100%; +} + +#edit-order-wrapper { + clear: left; /* LTR */ +} + +#edit-pages-apply, +#edit-pages-reset { + margin-top: 1.65em; + float: left; /* LTR */ +} + +#page-manager-edit { + margin-top: 1em; +} + +#page-manager-edit table { + width: 100%; +} + +#page-manager-edit table tr.even { + background-color: #fafafa; +} + +#page-manager-list-pages tr.page-manager-disabled td { + color: #999; +} + +#page-manager-list-pages tr.page-manager-locked td.page-manager-page-name { + font-style: italic; + color: green; + padding-left: 22px; /* LTR */ + background: url(../images/locked.png) no-repeat scroll left center; /* LTR */ +} + +#page-manager-list-pages tr.page-manager-locked-other td.page-manager-page-name { + font-style: italic; + color: red; + padding-left: 22px; /* LTR */ + background: url(../images/locked-other.png) no-repeat scroll left center; /* LTR */ +} + +/* Force the background color to inherit so that the dropbuttons do not need + a specific background color. */ +#page-manager-list-pages td.page-manager-page-operations { + vertical-align: top; + position: relative; + text-align: right; +} + +#page-manager-list-pages td.page-manager-page-operations .ctools-dropbutton { + text-align: left; /* LTR */ +} + + + +#page-manager-edit .page-manager-wrapper { + margin: 0; + padding: 0 0 0 149px; + color: #494949; +} + +#page-manager-edit .page-manager-tabs { + border: 1px solid #aaa; +} + +#page-manager-edit .page-manager-edit-operations { + float: left; /* LTR */ + width: 150px; + margin-left: -150px; + margin-top: -1px; + height: 100%; + font-size: 90%; +} + +#page-manager-edit .page-manager-edit-operations .inside { + border-top: 1px solid #aaa; + border-bottom: 1px solid #aaa; +} + +#page-manager-edit .page-manager-edit-operations ul { + margin-top: 0; + margin-bottom: 0; + padding: 0; + margin-left: 0; +} + +#page-manager-edit .page-manager-edit-operations li { + list-style: none; + background: #F6F6F6; + border-top: 1px solid #aaa; + border-left: 1px solid #aaa; + border-right: 1px solid #aaa; + padding: 0 0 0 0; + margin: 0; + line-height: 2em; +} + +#page-manager-edit .page-manager-edit-operations li.active, +#page-manager-edit .page-manager-edit-operations li.active-group .page-manager-group-title { + background: #FFFFFF url(../images/arrow-active.png) no-repeat scroll right center; +} + +#page-manager-edit .page-manager-edit-operations li.changed, +#page-manager-edit .page-manager-edit-operations li.changed-group .page-manager-group-title { + background-color: #ffe; + font-weight: bold; +} + +/** provide a reset for non active stray paths */ +#page-manager-edit .page-manager-edit-operations li.active-group li.not-active .page-manager-group-title, +#page-manager-edit .page-manager-edit-operations li.changed-group li.not-changed .page-manager-group-title { + background: #F6F6F6; +} + +#page-manager-edit .page-manager-edit-operations li.active { + border-right: 1px solid white; +} + +#page-manager-edit .page-manager-edit-operations li.active a, +#page-manager-edit .page-manager-edit-operations li.active a:hover { + background: #FFFFFF url(../images/arrow-active.png) no-repeat scroll right center; + color: #000000; + font-weight: bold; +} + +#page-manager-edit .page-manager-edit-operations li.operation-first { + border-top: none; +} + +#page-manager-edit .page-manager-edit-operations li li.operation-first { + border-top: 1px solid #aaa; +} + +#page-manager-edit .page-manager-edit-operations li a { + display: block; + padding: 0 0 0 .5em; + color: #494949; +} + +#page-manager-edit .page-manager-edit-operations li a:hover { + background-color: #eee; + text-decoration: none; +} + +#page-manager-edit .ctools-collapsible-container { + display: inline-block; + position: relative; + *float: left; /* LTR */ + width: 100%; +} + +#page-manager-edit .page-manager-edit-operations li .ctools-collapsible-handle:hover { + background-color: #eee; +} + +#page-manager-edit .page-manager-edit-operations li li { + border-right: none; + border-left: none; + margin-left: 1em; +} + +#page-manager-edit .page-manager-edit-operations li ul { +} + +#page-manager-edit .page-manager-group-title { + line-height: 2em; + font-weight: bold; + padding: 0 0 0 .5em; +} + +/* Change the position of the arrow on the dropdown to look nicer with our defaults. */ +#page-manager-edit .page-manager-edit-operations .ctools-toggle { + background-position: 0 9px; + width: 10px; +} + +#page-manager-edit .page-manager-ajax-pad { + float: left; /* LTR */ + width: 100%; + border-left: none; + height: 100%; + background: white; +} + +/** A riser to force the ajax pad to a minimum height. **/ +#page-manager-edit .page-manager-ajax-pad .page-manager-riser { + width: 1px; + float: right; /* LTR */ + height: 400px; +} + +#page-manager-edit .page-manager-ajax-pad .page-manager-riser span { + display: none; +} + +#page-manager-edit .page-manager-ajax-pad .content-title { + font-weight: bold; + font-size: 120%; + background-color: #fafafa; + border-bottom: 1px solid #aaa; + border-left: 1px solid #aaa; + margin-left: -1px; + padding: 2px 5px 2px 20px; +} + +#page-manager-edit .actions { + padding: 0 0 0 20px; +} + +#page-manager-edit .primary-actions li { + border-top: 1px solid #aaa; +} + +#page-manager-edit .secondary-actions { + border-bottom: 1px solid #aaa; +} + +#page-manager-edit .handler-actions { + float: right; /* LTR */ +} + +#page-manager-edit .actions .page-manager-group-title { + float: left; /* LTR */ + padding-left: 0; +} + +#page-manager-edit .actions ul { + float: right; /* LTR */ + text-align: right; /* LTR */ + padding: 0; + margin: 0; + border-right: 1px solid #aaa; +} + +#page-manager-edit .handler-title .actions ul { + border-right: none; +} + +#page-manager-edit .actions li { + float: left; /* LTR */ + background: none; + list-style: none; + border-left: 1px solid #aaa; + margin: 0; + padding: 0; +} + +#page-manager-edit .actions ul li.operation-last { + border-right: none; +} + +#page-manager-edit .actions li a:hover { + background-color: #eee; + text-decoration: none; +} + +#page-manager-edit .actions li a { + display: block; + padding: 0.2em 0.5em; + color:#0062A0; + background-color: #F6F6F6; +} + +#page-manager-edit .page-manager-changed { + float: right; /* LTR */ + font-style: italic; + color: #f93; + padding-left: 1em; + padding-right: 22px; + background: url(../images/locked.png) no-repeat scroll right center; +} + +#page-manager-edit .page-manager-ajax-pad .content-content { + padding: .5em 20px; +} + +#page-manager-edit .page-manager-ajax-pad textarea { + width: 100%; +} + +#page-manager-edit .changed-notification { + border: 1px solid #aaa; + background-color: #ffe; + color: #494949; + padding: 1em; + margin-top: 1em; +} + +#page-manager-edit .ctools-locked { + margin-bottom: 2em; +} + +#page-manager-page-summary .title { + font-weight: bold; + font-size: 160%; +} + +#page-manager-page-summary .handler-summary { +} + +#page-manager-page-summary .page-summary-operation { + text-align: right; +} + +#page-manager-page-summary .page-summary-label { + width: 8em; + font-weight: bold; +} + + +.handler-summary dl { + margin: 0; +} + +.handler-summary dt { + margin: 0; + padding: 0; +} + +.handler-summary dd { + margin: 0; +} + +.handler-summary ol { + margin: 0; +} + +.handler-summary .handler-title { + border-top: 1px solid #ddd; + border-bottom: 1px solid #ddd; + background: #fafafa; + padding: 0 0 0 5px; + margin-top: .5em; +} + +.handler-summary .handler-title .title-label { + font-weight: bold; + font-size: 120%; + line-height: 1.75em; +} + + +/** Override that obnoxious float on throbber. **/ +#page-manager-edit .progress-disabled { + float: none; +} + +#page-manager-edit .progress-disabled + .ajax-progress { + float: right; + position: relative; + top: -2em; +} + +#page-manager-list-pages-form .progress-disabled + .ajax-progress { + position: relative; + top: 2em; + left: -.5em; +} diff --git a/sites/all/modules/contrib/ctools/page_manager/help/about.html b/sites/all/modules/contrib/ctools/page_manager/help/about.html new file mode 100644 index 0000000..fa58aca --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/about.html @@ -0,0 +1,11 @@ +The Page Manager module creates and manages pages in your Drupal site. Pages are defined as items that have a path and provide output to the user. It is a complete round trip from getting user input to providing user output. + +There are two types of pages that the Page Manager currently supports: +
+
Custom pages
+
Custom pages are defined completely by the administrator. Their path, access control and visible menu characteristics are completely arbitrary.
+
System pages
+
System pages are defined by Drupal and Drupal modules. They primarily override pre-existing pages to provide different functionality. They often do not allow such features as access control in favor of what already exists, and they will usually 'fall back' to default Drupal behavior. +
+ +Both types of pages figure out what to show the user by using Variants. Variants are output handlers, and every page should have at least one. Most pages will simply have only one. Pages with multiple variants will choose one and only one Variant to display content to the user and will use the Selection Rules to figure out which Variant to display. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/help/api-task-handler.html b/sites/all/modules/contrib/ctools/page_manager/help/api-task-handler.html new file mode 100644 index 0000000..4544a2a --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/api-task-handler.html @@ -0,0 +1,43 @@ +task handler definition: + title -- visible title of the task handler. + description -- description of the task handler. + task type -- The type of the task this handler can service. + render -- callback of the function to render the handler. The arguments to this callback are specific to the task type. + admin title -- callback to render the admin title as this handler is listed. + params: $handler, $task, $subtask_id + admin summary -- callback to render what's in the collapsible info as the handler is listed. Optional. + params: $handler, $task, $subtask_id + default conf -- either an array() of default conf data or a callback that returns an array. + params: $handler, $task, $subtask_id + save -- callback to call just prior to the task handler being saved so it can adjust its data. + params: &$handler, $update (as drupal_write_record would receive) + export -- callback to call just prior to the task being exported. It should return text to append to the export if necessary. + params: &$handler, $indent + + forms => array( + 'id' => array( + 'form' => form callback (receives $form, $form_state) + 'submit' => submit callback + 'validate' => validate callback + 'include' => an optional file to include to get functionality for this form. Must include full path. + 'no return' => hide the 'return' button, meaning that the form requires extra steps if submitted + 'alternate next' => an alternate next form. Used for hidden edit forms that don't have tabs. + 'no blocks' => if TRUE, use Drupal's mechanism to not render blocks for this form. + ) + ) + ), + + 'add forms' => array( + 'form1', => t('form title'), + 'form2', => t('form title'), + // ...etc.../ +), + 'edit forms' => array( + 'id' => t('tab name'), + 'id2' => t('tab name'), + ), + + If a form name is blank it is a 'hidden' form -- it has no tab but can still be reached. + + +Notes: Because #required validation cannot be skipped when clicking cancel, please don't use it. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/help/api-task-type.html b/sites/all/modules/contrib/ctools/page_manager/help/api-task-type.html new file mode 100644 index 0000000..eb87265 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/api-task-type.html @@ -0,0 +1,2 @@ + +defines a task type, grouping tasks together and providing a common UI for them. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/help/api-task.html b/sites/all/modules/contrib/ctools/page_manager/help/api-task.html new file mode 100644 index 0000000..cd6e3d0 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/api-task.html @@ -0,0 +1,38 @@ +task definition: + title -- visible title of the task. + description -- description of the task. + hook menu -- function to delegate from hook_menu. Params: &$items, $task + hook menu alter -- function to delegate from hook_menu_alter. Params: &$items, $task + hook theme -- function to delegate from hook_theme. Params: &$items, $task + + admin name -- if set an admin menu will appear in the delegator UI + admin description -- to describe the admin menu + + admin access callback -- if set, the callback to use to determine administrative + access to this task. Defaults to user_access. Note that this is required even + if delegator isn't handling administration, since this gets used to on handler + edit forms. + admin access arguments -- If set, the arguments to use to determine administrative + access to this task. Defaults to array('administer delegator'); + + type -- The type of the task, used to determine which handlers can service it. + + subtasks -- can be TRUE in which case it supports subtasks with the default + configuration or a string (array?) with callbacks to fetch subtask data. + subtask callback -- A callback which returns just one subtask. Param: $task, $subtask_id + subtasks callback -- A callback which returns an array of all subtasks. + This MUST return an array, even if it's empty.Param: $task + + default handlers -- If the task contains any default handlers, they can be included here. + +task names must not contain a - as that is used to separate the task name from the subtask ID. + +subtasks implement data very similar to their parent task. In particular, they +implement the following items exactly like their task: + hook menu + hook menu alter + description + admin name + admin description + admin access callback + admin access arguments diff --git a/sites/all/modules/contrib/ctools/page_manager/help/custom-pages-access.html b/sites/all/modules/contrib/ctools/page_manager/help/custom-pages-access.html new file mode 100644 index 0000000..a2643c2 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/custom-pages-access.html @@ -0,0 +1,2 @@ + +Please visit http://drupal.org/node/528072 to help provide this documentation page. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/help/custom-pages-arguments.html b/sites/all/modules/contrib/ctools/page_manager/help/custom-pages-arguments.html new file mode 100644 index 0000000..516a429 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/custom-pages-arguments.html @@ -0,0 +1,2 @@ + +Please visit http://drupal.org/node/528058 to help provide this documentation page. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/help/custom-pages-menu.html b/sites/all/modules/contrib/ctools/page_manager/help/custom-pages-menu.html new file mode 100644 index 0000000..48cf9c3 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/custom-pages-menu.html @@ -0,0 +1,2 @@ + +Please visit http://drupal.org/node/528078 to help provide this documentation page. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/help/custom-pages.html b/sites/all/modules/contrib/ctools/page_manager/help/custom-pages.html new file mode 100644 index 0000000..18e66d4 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/custom-pages.html @@ -0,0 +1,2 @@ + +Please visit http://drupal.org/node/528050 to help provide this documentation page. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/help/getting-started-create.html b/sites/all/modules/contrib/ctools/page_manager/help/getting-started-create.html new file mode 100644 index 0000000..a3d295e --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/getting-started-create.html @@ -0,0 +1,2 @@ + +Please visit http://drupal.org/node/528038 to help provide this documentation page. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/help/getting-started-custom-nodes.html b/sites/all/modules/contrib/ctools/page_manager/help/getting-started-custom-nodes.html new file mode 100644 index 0000000..d62eb0f --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/getting-started-custom-nodes.html @@ -0,0 +1,2 @@ + +Please visit http://drupal.org/node/528044 to help provide this documentation page. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/help/getting-started-custom-vocabulary.html b/sites/all/modules/contrib/ctools/page_manager/help/getting-started-custom-vocabulary.html new file mode 100644 index 0000000..7148cd9 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/getting-started-custom-vocabulary.html @@ -0,0 +1,2 @@ + +Please visit http://drupal.org/node/528046 to help provide this documentation page. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/help/getting-started-members.html b/sites/all/modules/contrib/ctools/page_manager/help/getting-started-members.html new file mode 100644 index 0000000..87b2122 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/getting-started-members.html @@ -0,0 +1,2 @@ + +Please visit http://drupal.org/node/528040 to help provide this documentation page. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/help/getting-started-page-list.html b/sites/all/modules/contrib/ctools/page_manager/help/getting-started-page-list.html new file mode 100644 index 0000000..d60bea4 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/getting-started-page-list.html @@ -0,0 +1,2 @@ + +Please visit http://drupal.org/node/528036 to help provide this documentation page. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/help/getting-started.html b/sites/all/modules/contrib/ctools/page_manager/help/getting-started.html new file mode 100644 index 0000000..4e4f24a --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/getting-started.html @@ -0,0 +1,15 @@ + +Note: this page is currently very preliminary. Please visit http://drupal.org/node/528034 to help provide this documentation page! + +This is a quick summary: + +
    +
  • Visit administer >> site building >> pages to get to the primary page manager interface.
  • +
  • You can add custom pages for your basic landing pages, front pages, whatever you like for normal content display.
  • +
  • You can use the system pages to create finer control of how taxonomy vocabularies, nodes and user profiles are displayed.
  • +
  • When you add your first custom page, do not bother with the optional features. You will not need these until you get to more advanced tasks.
  • +
  • The selection rules are the key to creating node displays for just one node type.
  • +
  • Everything in this system is pluggable. A little PHP knowledge and exploration of the plugins directories can take you a long way.
  • +
+ + diff --git a/sites/all/modules/contrib/ctools/page_manager/help/page-task-type.html b/sites/all/modules/contrib/ctools/page_manager/help/page-task-type.html new file mode 100644 index 0000000..c382c76 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/page-task-type.html @@ -0,0 +1,4 @@ + +Additional 'task' keys support: + +operations -- a list of operations suitable for theme('links') \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/help/page_manager.help.ini b/sites/all/modules/contrib/ctools/page_manager/help/page_manager.help.ini new file mode 100644 index 0000000..05cadb4 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/page_manager.help.ini @@ -0,0 +1,59 @@ +[advanced help settings] +line break = TRUE + +[about] +title = About Page Manager +weight = -100 + +[getting-started] +title = Getting Started +weight = -80 + +[getting-started-page-list] +title = The page list +weight = -90 +parent = getting-started + +[getting-started-create] +title = Creating a page +weight = -80 +parent = getting-started + +[getting-started-members] +title = Tutorial: Make a page that looks different for members +weight = -70 +parent = getting-started + +[getting-started-custom-nodes] +title = Tutorial: Customize the look of a single node type +weight = -60 +parent = getting-started + +[getting-started-custom-vocabulary] +title = Tutorial: Customize the look of a single taxonomy vocabulary +weight = -50 +parent = getting-started + +[custom-pages] +title = Custom pages +weight = -50 + +[custom-pages-arguments] +title = Arguments +weight = -100 +parent = custom-pages + +[custom-pages-access] +title = Access control +weight = -90 +parent = custom-pages + +[custom-pages-menu] +title = Menu items +weight = -80 +parent = custom-pages + +[variants] +title = Variants +weight = -40 + diff --git a/sites/all/modules/contrib/ctools/page_manager/help/variants.html b/sites/all/modules/contrib/ctools/page_manager/help/variants.html new file mode 100644 index 0000000..48cf9c3 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/help/variants.html @@ -0,0 +1,2 @@ + +Please visit http://drupal.org/node/528078 to help provide this documentation page. \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/images/arrow-active.png b/sites/all/modules/contrib/ctools/page_manager/images/arrow-active.png new file mode 100644 index 0000000..3bbd3c2 Binary files /dev/null and b/sites/all/modules/contrib/ctools/page_manager/images/arrow-active.png differ diff --git a/sites/all/modules/contrib/ctools/page_manager/images/locked-other.png b/sites/all/modules/contrib/ctools/page_manager/images/locked-other.png new file mode 100644 index 0000000..b84a154 Binary files /dev/null and b/sites/all/modules/contrib/ctools/page_manager/images/locked-other.png differ diff --git a/sites/all/modules/contrib/ctools/page_manager/images/locked.png b/sites/all/modules/contrib/ctools/page_manager/images/locked.png new file mode 100644 index 0000000..2116eb1 Binary files /dev/null and b/sites/all/modules/contrib/ctools/page_manager/images/locked.png differ diff --git a/sites/all/modules/contrib/ctools/page_manager/js/page-list.js b/sites/all/modules/contrib/ctools/page_manager/js/page-list.js new file mode 100644 index 0000000..16b5291 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/js/page-list.js @@ -0,0 +1,44 @@ + +/** + * Provide some extra responses for the page list so we can have automatic + * on change. + */ + +Drupal.behaviors.PageManagerList = function() { + var timeoutID = 0; + $('form#page-manager-list-pages-form select:not(.pm-processed)') + .addClass('pm-processed') + .change(function() { + $('#edit-pages-apply').click(); + }); + $('form#page-manager-list-pages-form input[type=text]:not(.pm-processed)') + .addClass('pm-processed') + .keyup(function(e) { + switch (e.keyCode) { + case 16: // shift + case 17: // ctrl + case 18: // alt + case 20: // caps lock + case 33: // page up + case 34: // page down + case 35: // end + case 36: // home + case 37: // left arrow + case 38: // up arrow + case 39: // right arrow + case 40: // down arrow + case 9: // tab + case 13: // enter + case 27: // esc + return false; + default: + if (!$('#edit-pages-apply').hasClass('ctools-ajaxing')) { + if ((timeoutID)) { + clearTimeout(timeoutID); + } + + timeoutID = setTimeout(function() { $('#edit-pages-apply').click(); }, 300); + } + } + }); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/page_manager.admin.inc b/sites/all/modules/contrib/ctools/page_manager/page_manager.admin.inc new file mode 100644 index 0000000..0f164fe --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/page_manager.admin.inc @@ -0,0 +1,1904 @@ + 'page_manager', + 'topic' => 'getting-started', + 'type' => t('See the getting started guide for more information.'), + ))); + } + + $tasks = page_manager_get_tasks_by_type('page'); + $pages = array('operations' => array(), 'tasks' => array()); + + page_manager_get_pages($tasks, $pages); + + // Add lock icon to all locked tasks. + global $user; + + ctools_include('object-cache'); + $locks = ctools_object_cache_test_objects('page_manager_page', $pages['tasks']); + foreach ($locks as $task_name => $lock) { + if ($lock->uid == $user->uid) { + $pages['rows'][$task_name]['class'][] = ' page-manager-locked'; + $pages['rows'][$task_name]['title'] = t('This page is currently locked for editing by you. Nobody else may edit this page until these changes are saved or canceled.'); + } + else { + $pages['rows'][$task_name]['class'][] = ' page-manager-locked-other'; + $pages['rows'][$task_name]['title'] = t('This page is currently locked for editing by another user. You may not edit this page without breaking the lock.'); + } + } + + $input = $_POST; + + // Respond to a reset command by clearing session and doing a drupal goto + // back to the base URL. + if (isset($input['op']) && $input['op'] == t('Reset')) { + unset($_SESSION['page_manager']['#admin']); + if (!$js) { + drupal_goto($_GET['q']); + } + // clear everything but form id, form build id and form token: + $keys = array_keys($input); + foreach ($keys as $id) { + if ($id != 'form_id' && $id != 'form_build_id' && $id != 'form_token') { + unset($input[$id]); + } + } + $replace_form = TRUE; + } + if (count($input) <= 1) { + if (isset($_SESSION['page_manager']['#admin']) && is_array($_SESSION['page_manager']['#admin'])) { + $input = $_SESSION['page_manager']['#admin']; + } + } + else { + $_SESSION['page_manager']['#admin'] = $input; + unset($_SESSION['page_manager']['#admin']['q']); + } + + $form_state = array( + 'pages' => &$pages, + 'input' => $input, + 'rerender' => TRUE, + 'no_redirect' => TRUE, + ); + + // This form will sort and filter the pages. + $form = drupal_build_form('page_manager_list_pages_form', $form_state); + + $header = array( + array('data' => t('Type'), 'class' => array('page-manager-page-type')), + array('data' => t('Module'), 'class' => array('page-manager-page-module')), + array('data' => t('Name'), 'class' => array('page-manager-page-name')), + array('data' => t('Title'), 'class' => array('page-manager-page-title')), + array('data' => t('Path'), 'class' => array('page-manager-page-path')), + array('data' => t('Storage'), 'class' => array('page-manager-page-storage')), + ); + + $header[] = array('data' => t('Operations'), 'class' => array('page-manager-page-operations')); + $table = theme('table', array('header' => $header, 'rows' => $pages['rows'], 'attributes' => array('id' => 'page-manager-list-pages'))); + + $operations = ''; + + drupal_add_css(drupal_get_path('module', 'page_manager') . '/css/page-manager.css'); + + + if (!$js) { + return array('#markup' => drupal_render($form) . $table . $operations); + } + + ctools_include('ajax'); + $commands = array(); + $commands[] = ajax_command_replace('#page-manager-list-pages', $table); + if (!empty($replace_form)) { + $commands[] = ajax_command_replace('#page-manager-list-pages-form', drupal_render($form)); + } + print ajax_render($commands); + ajax_footer(); +} + +/** + * Sort tasks into buckets based upon whether or not they have subtasks. + */ +function page_manager_get_pages($tasks, &$pages, $task_id = NULL) { + foreach ($tasks as $id => $task) { + if (empty($task_id) && !empty($task['page operations'])) { + $pages['operations'] = array_merge($pages['operations'], $task['page operations']); + } + + // If a type has subtasks, add its subtasks in its own table. + if (!empty($task['subtasks'])) { + page_manager_get_pages(page_manager_get_task_subtasks($task), $pages, $task['name']); + continue; + } + + if (isset($task_id)) { + $task_name = page_manager_make_task_name($task_id, $task['name']); + } + else { + $task_name = $task['name']; + } + + $class = array('page-task-' . $id); + if (isset($task['row class'])) { + $class[] = $task['row class']; + } + + if (!empty($task['disabled'])) { + $class[] = 'page-manager-disabled'; + } + + $path = array(); + $visible_path = ''; + if (!empty($task['admin path'])) { + foreach (explode('/', $task['admin path']) as $bit) { + if (isset($bit[0]) && $bit[0] != '!') { + $path[] = $bit; + } + } + + $path = implode('/', $path); + if (empty($task['disabled']) && strpos($path, '%') === FALSE) { + $visible_path = l('/' . $task['admin path'], $path); + } + else { + $visible_path = '/' . $task['admin path']; + } + } + + $row = array('data' => array(), 'class' => $class, 'title' => strip_tags($task['admin description'])); + + $type = isset($task['admin type']) ? $task['admin type'] : t('System'); + if (isset($task['module'])) { + $module = $task['module']; + } + elseif (isset($task['subtask']->export_module)) { + $module = $task['subtask']->export_module; + } + else { + $module = ''; + } + $pages['types'][$type] = $type; + $row['data']['type'] = array('data' => $type, 'class' => array('page-manager-page-type')); + $row['data']['module'] = array('data' => $module, 'class' => array('page-manager-page-module')); + $row['data']['name'] = array('data' => $task_name, 'class' => array('page-manager-page-name')); + $row['data']['title'] = array('data' => $task['admin title'], 'class' => array('page-manager-page-title')); + $row['data']['path'] = array('data' => $visible_path, 'class' => array('page-manager-page-path')); + + $storage = isset($task['storage']) ? $task['storage'] : t('In code'); + $pages['storages'][$storage] = $storage; + $row['data']['storage'] = array('data' => $storage, 'class' => array('page-manager-page-storage')); + + +/* + if (empty($task['disabled'])) { + $item = menu_get_item($path); + if (empty($item)) { + dsm($path); + } + else { + dsm($item); + } + } +*/ + $operations = array( + array( + 'title' => t('Edit'), + 'href' => page_manager_edit_url($task_name), + ), + ); + + if (!empty($task['enable callback'])) { + if (!empty($task['disabled'])) { + array_unshift($operations, array( + 'title' => t('Enable'), + 'href' => 'admin/structure/pages/nojs/enable/' . $task_name, + 'query' => array('token' => drupal_get_token($task_name)), + )); + } + else { + $operations[] = array( + 'title' => t('Disable'), + 'href' => 'admin/structure/pages/nojs/disable/' . $task_name, + 'query' => array('token' => drupal_get_token($task_name)), + ); + } + } + + $ops = theme('links__ctools_dropbutton', array('links' => $operations, 'attributes' => array('class' => array('links', 'inline')))); + + $row['data']['operations'] = array('data' => $ops, 'class' => array('page-manager-page-operations')); + + $pages['disabled'][$task_name] = !empty($task['disabled']); + $pages['tasks'][] = $task_name; + $pages['rows'][$task_name] = $row; + } +} + +/** + * Provide a form for sorting and filtering the list of pages. + */ +function page_manager_list_pages_form($form, &$form_state) { + // This forces the form to *always* treat as submitted which is + // necessary to make it work. + if (empty($_POST)) { + $form["#programmed"] = TRUE; + } + $form['#action'] = url('admin/structure/pages/nojs/', array('absolute' => TRUE)); + if (!variable_get('clean_url', FALSE)) { + $form['q'] = array( + '#type' => 'hidden', + '#value' => $_GET['q'], + ); + } + + $all = array('all' => t('')); + + $form['type'] = array( + '#type' => 'select', + '#title' => t('Type'), + '#options' => $all + $form_state['pages']['types'], + '#default_value' => 'all', + ); + + $form['storage'] = array( + '#type' => 'select', + '#title' => t('Storage'), + '#options' => $all + $form_state['pages']['storages'], + '#default_value' => 'all', + ); + + $form['disabled'] = array( + '#type' => 'select', + '#title' => t('Enabled'), + '#options' => $all + array('0' => t('Enabled'), '1' => t('Disabled')), + '#default_value' => 'all', + ); + + $form['search'] = array( + '#type' => 'textfield', + '#title' => t('Search'), + ); + + $form['order'] = array( + '#type' => 'select', + '#title' => t('Sort by'), + '#options' => array( + 'disabled' => t('Enabled, title'), + 'title' => t('Title'), + 'name' => t('Name'), + 'path' => t('Path'), + 'type' => t('Type'), + 'storage' => t('Storage'), + ), + '#default_value' => 'disabled', + ); + + $form['sort'] = array( + '#type' => 'select', + '#title' => t('Order'), + '#options' => array( + 'asc' => t('Up'), + 'desc' => t('Down'), + ), + '#default_value' => 'asc', + ); + + $form['submit'] = array( + '#name' => '', // so it won't in the $_GET args + '#type' => 'submit', + '#id' => 'edit-pages-apply', + '#value' => t('Apply'), + '#attributes' => array('class' => array('use-ajax-submit ctools-auto-submit-click')), + ); + + $form['reset'] = array( + '#type' => 'submit', + '#id' => 'edit-pages-reset', + '#value' => t('Reset'), + '#attributes' => array('class' => array('use-ajax-submit')), + ); + + $form['#attached']['js'] = array(ctools_attach_js('auto-submit'), ctools_attach_js('page-list', 'page_manager')); + $form['#attached']['library'][] = array('system', 'drupal.ajax'); + $form['#attached']['library'][] = array('system', 'jquery.form'); + $form['#prefix'] = '
'; + $form['#suffix'] = '
'; + $form['#attributes'] = array('class' => array('ctools-auto-submit-full-form')); + + return $form; +} + +/** + * Accept submission from the page manager sort/filter form and apply it + * to the list of pages. + */ +function page_manager_list_pages_form_submit(&$form, &$form_state) { + // Filter and re-sort the pages. + + // This is a copy. + $rows = $form_state['pages']['rows']; + + $sorts = array(); + foreach ($rows as $name => $data) { + // Filter + if ($form_state['values']['type'] != 'all' && $form_state['values']['type'] != $data['data']['type']['data']) { + continue; + } + + if ($form_state['values']['storage'] != 'all' && $form_state['values']['storage'] != $data['data']['storage']['data']) { + continue; + } + + if ($form_state['values']['disabled'] != 'all' && $form_state['values']['disabled'] != $form_state['pages']['disabled'][$name]) { + continue; + } + + if ($form_state['values']['search'] && + strpos($data['data']['name']['data'], $form_state['values']['search']) === FALSE && + strpos($data['data']['path']['data'], $form_state['values']['search']) === FALSE && + strpos($data['data']['title']['data'], $form_state['values']['search']) === FALSE) { + continue; + } + // Set up sorting + switch ($form_state['values']['order']) { + case 'disabled': + $sorts[$name] = !$form_state['pages']['disabled'][$name] . $data['data']['title']['data']; + break; + case 'title': + $sorts[$name] = $data['data']['title']['data']; + break; + case 'name': + $sorts[$name] = $data['data']['name']['data']; + break; + case 'path': + $sorts[$name] = $data['data']['path']['data']; + break; + case 'type': + $sorts[$name] = $data['data']['type']['data']; + break; + case 'storage': + $sorts[$name] = $data['data']['storage']['data']; + break; + } + } + + // Now actually sort + if ($form_state['values']['sort'] == 'desc') { + arsort($sorts); + } + else { + asort($sorts); + } + + // Nuke the original. + $form_state['pages']['rows'] = array(); + // And restore. + foreach ($sorts as $name => $title) { + $form_state['pages']['rows'][$name] = $rows[$name]; + } + +} + +/** + * Render the edit page for a a page, custom or system. + */ +function page_manager_edit_page($page) { + drupal_set_title($page->subtask['admin title'], PASS_THROUGH); + // Provide and process the save page form before anything else. + $form_state = array('page' => &$page); + $built_form = drupal_build_form('page_manager_save_page_form', $form_state); + $form = drupal_render($built_form); + + $operations = page_manager_get_operations($page); + $args = array('summary'); + $rendered_operations = page_manager_render_operations($page, $operations, $args, array('class' => array('operations-main')), 'nav'); + $content = page_manager_get_operation_content(FALSE, $page, $args, $operations); + $output = theme('page_manager_edit_page', array('page' => $page, 'save' => $form, 'operations' => $rendered_operations, 'content' => $content)); + return array('#markup' => $output); +} + +/** + * Entry point to edit a single operation for a page. + * + * @param $js + * Whether or not the page was called via javascript. + * @param $page + * The cached page that is being edited. + * @param ... + * A number of items used to drill down into the actual operation called. + */ +function page_manager_edit_page_operation() { + $args = func_get_args(); + $js = array_shift($args); + $page = array_shift($args); + + $operations = page_manager_get_operations($page); + $content = page_manager_get_operation_content($js, $page, $args, $operations); + + // If the operation requested we go somewhere else afterward, oblige it. + if (isset($content['new trail'])) { + $args = $content['new trail']; + // Get operations again, for the operation may have changed their availability. + $operations = page_manager_get_operations($page); + $content = page_manager_get_operation_content($js, $page, $args, $operations); + } + + // Rendering the content may have been a form submission that changed the + // operations, such as renaming or adding a handler. Thus we get a new set + // of operations. + $operations = page_manager_get_operations($page); + $rendered_operations = page_manager_render_operations($page, $operations, $args, array('class' => array('operations-main')), 'nav'); + + // Since this form should never be submitted to this page, process it late so + // that we can be sure it notices changes. + $form_state = array('page' => &$page); + $built_form = drupal_build_form('page_manager_save_page_form', $form_state); + $form = drupal_render($built_form); + + $output = theme('page_manager_edit_page', array('page' => $page, 'save' => $form, 'operations' => $rendered_operations, 'content' => $content)); + + if ($js) { + $commands = array(); + $commands[] = ajax_command_replace('#page-manager-edit', $output); + + print ajax_render($commands); + ajax_footer(); + return; + } + + drupal_set_title($page->subtask['admin title'], PASS_THROUGH); + return $output; +} + +/** + * Take the operations array from a task and expand it. + * + * This allows some of the operations to be dynamic, based upon settings + * on the task or the task's handlers. Each operation should have a type. In + * addition to all the types allowed in page_manager_render_operations, these + * types will be dynamically replaced with something else: + * - 'handlers': An automatically created group that contains all the task's + * handlers and appropriate links. + * - 'function': A callback (which will be placed in the 'function' parameter + * that should return an array of operations. This can be used to provide + * additional, dynamic links if needed. + */ +function page_manager_get_operations($page, $operations = NULL) { + if (!isset($operations)) { + // All tasks have at least these 2 ops: + $operations = array( + 'summary' => array( + 'title' => t('Summary'), + 'description' => t('Get a summary of the information about this page.'), + 'path' => 'admin/structure/pages/edit', + 'ajax' => FALSE, + 'no operations' => TRUE, + 'form info' => array( + 'no buttons' => TRUE, + ), + 'form' => 'page_manager_page_summary', + ), + 'actions' => array( + 'type' => 'group', + 'title' => '', + 'class' => array('operations-actions'), + 'location' => 'primary', + 'children' => array(), + ), + ); + + if (isset($page->subtask['operations'])) { + $operations += $page->subtask['operations']; + // add actions separately. + if (!empty($page->subtask['operations']['actions'])) { + $operations['actions']['children'] += $page->subtask['operations']['actions']['children']; + } + } + $operations['handlers'] = array('type' => 'handlers'); + } + + $result = array(); + foreach ($operations as $id => $operation) { + if (empty($operation['type'])) { + $operation['type'] = 'operation'; + } + switch ($operation['type']) { + case 'handlers': + $result[$id] = page_manager_get_handler_operations($page); + break; + case 'function': + if (function_exists($operation['function'])) { + $retval = $function($page, $operation); + if (is_array($retval)) { + $result[$id] = $retval; + } + } + break; + default: + $result[$id] = $operation; + } + } + + if (!empty($page->subtask['enable callback']) && !empty($page->subtask['disabled']) && empty($result['actions']['children']['enable'])) { + $result['actions']['children']['enable'] = array( + 'title' => t('Enable'), + 'description' => t('Activate this page so that it will be in use in your system.'), + 'form' => 'page_manager_enable_form', + 'ajax' => FALSE, + 'silent' => TRUE, + 'no update and save' => TRUE, + 'form info' => array( + 'finish text' => t('Enable'), + ), + ); + } + + if (!empty($page->subtask['enable callback']) && empty($page->subtask['disabled']) && empty($result['actions']['children']['disable'])) { + $result['actions']['children']['disable'] = array( + 'title' => t('Disable'), + 'description' => t('De-activate this page. The data will remain but the page will not be in use on your system.'), + 'form' => 'page_manager_disable_form', + 'ajax' => FALSE, + 'silent' => TRUE, + 'no update and save' => TRUE, + 'form info' => array( + 'finish text' => t('Disable'), + ), + ); + } + + $result['actions']['children']['add'] = array( + 'title' => t('Add variant'), + 'description' => t('Add a new variant to this page.'), + 'form' => 'page_manager_handler_add', + 'ajax' => FALSE, + 'silent' => TRUE, // prevents a message about updating and prevents this item from showing as changed. + 'no update and save' => TRUE, // get rid of update and save button which is bad here. + 'form info' => array( + 'finish text' => t('Create variant'), + ), + ); + + // Restrict variant import due to security implications. + if (user_access('use ctools import')) { + $result['actions']['children']['import'] = array( + 'title' => t('Import variant'), + 'description' => t('Add a new variant to this page from code exported from another page.'), + 'form' => 'page_manager_handler_import', + ); + } + + if (count($page->handlers) > 1) { + $result['actions']['children']['rearrange'] = array( + 'title' => t('Reorder variants'), + 'ajax' => FALSE, + 'description' => t('Change the priority of the variants to ensure that the right one gets selected.'), + 'form' => 'page_manager_handler_rearrange', + ); + } + + // This is a special operation used to configure a new task handler before + // it is added. + if (isset($page->new_handler)) { + $plugin = page_manager_get_task_handler($page->new_handler->handler); + $result['actions']['children']['configure'] = array( + 'title' => t('Configure'), + 'description' => t('Configure a newly created variant prior to actually adding it to the page.'), + 'ajax' => FALSE, + 'no update and save' => TRUE, // get rid of update and save button which is bad here. + 'form info' => array( + // We use our own cancel and finish callback to handle the fun stuff. + 'finish callback' => 'page_manager_handler_add_finish', + 'cancel callback' => 'page_manager_handler_add_cancel', + 'show trail' => TRUE, + 'show back' => TRUE, + 'finish text' => t('Create variant'), + ), + 'form' => array( + 'forms' => $plugin['forms'], + ), + ); + + foreach ($page->forms as $id) { + if (isset($plugin['add features'][$id])) { + $result['actions']['children']['configure']['form']['order'][$id] = $plugin['add features'][$id]; + } + else if (isset($plugin['required forms'][$id])) { + $result['actions']['children']['configure']['form']['order'][$id] = $plugin['required forms'][$id]; + } + } + } + + if ($page->locked) { + $result['actions']['children']['break-lock'] = array( + 'title' => t('Break lock'), + 'description' => t('Break the lock on this page so that you can edit it.'), + 'form' => 'page_manager_break_lock', + 'ajax' => FALSE, + 'no update and save' => TRUE, // get rid of update and save button which is bad here. + 'form info' => array( + 'finish text' => t('Break lock'), + ), + 'even locked' => TRUE, // show button even if locked + 'silent' => TRUE, + ); + } + + drupal_alter('page_manager_operations', $result, $page); + return $result; +} + +/** + * Collect all the operations related to task handlers (variants) and + * build a menu. + */ +function page_manager_get_handler_operations(&$page) { + ctools_include('export'); + $group = array( + 'type' => 'group', + 'class' => array('operations-handlers'), + 'title' => t('Variants'), + ); + + $operations = array(); + + // If there is only one variant, let's not have it collapsible. + $collapsible = count($page->handler_info) != 1; + foreach ($page->handler_info as $id => $info) { + if ($info['changed'] & PAGE_MANAGER_CHANGED_DELETED) { + continue; + } + $handler = $page->handlers[$id]; + $plugin = page_manager_get_task_handler($handler->handler); + + $operations[$id] = array( + 'type' => 'group', + 'class' => array('operations-handlers-' . $id), + 'title' => page_manager_get_handler_title($plugin, $handler, $page->task, $page->subtask_id), + 'collapsible' => $collapsible, + 'children' => array(), + ); + + $operations[$id]['children']['actions'] = array( + 'type' => 'group', + 'class' => array('operations-handlers-actions-' . $id), + 'title' => t('Variant operations'), + 'children' => array(), + 'location' => $id, + ); + + // There needs to be a 'summary' item here for variants. + $operations[$id]['children']['summary'] = array( + 'title' => t('Summary'), + 'description' => t('Get a summary of the information about this variant.'), + 'form info' => array( + 'no buttons' => TRUE, + ), + 'form' => 'page_manager_handler_summary', + ); + + if ($plugin && isset($plugin['operations'])) { + $operations[$id]['children'] += $plugin['operations']; + } + + $actions = &$operations[$id]['children']['actions']['children']; + + $actions['clone'] = array( + 'title' => t('Clone'), + 'description' => t('Make an exact copy of this variant.'), + 'form' => 'page_manager_handler_clone', + ); + $actions['export'] = array( + 'title' => t('Export'), + 'description' => t('Export this variant into code to import into another page.'), + 'form' => 'page_manager_handler_export', + ); + if ($handler->export_type == (EXPORT_IN_CODE | EXPORT_IN_DATABASE)) { + $actions['delete'] = array( + 'title' => t('Revert'), + 'description' => t('Remove all changes to this variant and revert to the version in code.'), + 'form' => 'page_manager_handler_delete', + 'no update and save' => TRUE, + 'form info' => array( + 'finish text' => t('Revert'), + ), + ); + } + else if ($handler->export_type != EXPORT_IN_CODE) { + $actions['delete'] = array( + 'title' => t('Delete'), + 'description' => t('Remove this variant from the page completely.'), + 'form' => 'page_manager_handler_delete', + 'form info' => array( + 'finish text' => t('Delete'), + 'save text' => t('Delete and save'), + ), + ); + } + if (!empty($handler->disabled)) { + $actions['enable'] = array( + 'title' => t('Enable'), + 'description' => t('Activate this variant so that it will be in use in your system.'), + 'form' => 'page_manager_handler_enable', + 'silent' => TRUE, + 'form info' => array( + 'finish text' => t('Enable'), + 'save text' => t('Enable and save'), + ), + ); + } + else { + $actions['disable'] = array( + 'title' => t('Disable'), + 'description' => t('De-activate this variant. The data will remain but the variant will not be in use on your system.'), + 'form' => 'page_manager_handler_disable', + 'silent' => TRUE, + 'form info' => array( + 'finish text' => t('Disable'), + 'save text' => t('Disable and save'), + ), + ); + } + + drupal_alter('page_manager_variant_operations', $operations[$id], $handler); + } + if (empty($operations)) { + $operations['empty'] = array( + 'type' => 'text', + 'title' => t('No variants'), + ); + } + + $group['children'] = $operations; + return $group; +} + +/** + * Get an operation from a trail. + * + * @return array($operation, $active, $args) + */ +function page_manager_get_operation($operations, $trail) { + $args = $trail; + $stop = FALSE; + $active = array(); + $titles = array(); + // Drill down into operations array: + while (!$stop) { + $check = reset($args); + $stop = TRUE; + if (is_array($operations)) { + if (isset($operations[$check])) { + $active[] = $check; + $operation = array_shift($args); + // check to see if this operation has children. If so, we continue. + if (!isset($operations[$check]['children'])) { + $operations = $operations[$check]; + } + else { + $titles[] = $operations[$check]['title']; + $operations = $operations[$check]['children']; + // continue only if the operation hs children. + $stop = FALSE; + } + } + } + } + + return array($operations, $active, $args, $titles); +} + +/** + * Fetch the content for an operation. + * + * First, this drills down through the arguments to find the operation, and + * turns whatever it finds into the active trail which is then used to + * hilite where we are when rendering the operation list. + * + * The arguments are discovered from the URL, and are an exact match for where + * the operation is in the hierarchy. For example, handlers/foo/settings will + * be the operation to edit the settings for the handler named foo. This comes + * in as an array ('handlers', 'foo', 'settings') and is used to find where the + * data for that operation is in the array. + */ +function page_manager_get_operation_content($js, &$page, $trail, $operations) { + list($operation, $active, $args, $titles) = page_manager_get_operation($operations, $trail); + // Once we've found the operation, send it off to render. + if ($operation) { + $content = _page_manager_get_operation_content($js, $page, $active, $operation, $titles, $args); + } + + if (empty($content)) { + $content = _page_manager_get_operation_content($js, $page, array('summary'), $operations['summary']); + } + + return $content; +} + +/** + * Fetch the content for an operation, after it's been discovered from arguments. + * + * This system runs through the CTools form wizard. Each operation specifies a form + * or set of forms that it may use. Operations may also specify wrappers and can + * set their own next/finish handlers so that they can make additional things happen + * at the end. + */ +function _page_manager_get_operation_content($js, &$page, $active, $operation, $titles = array(), $args = array()) { + if (isset($operation['form'])) { + $form_info = array( + 'id' => 'page_manager_page', + 'finish text' => t('Update'), + 'show trail' => FALSE, + 'show back' => FALSE, + 'show return' => FALSE, + 'show cancel' => FALSE, + 'next callback' => 'page_manager_edit_page_next', + 'finish callback' => 'page_manager_edit_page_finish', + // Items specific to the 'edit' routines that will get moved over: + 'path' => page_manager_edit_url($page->task_name, $active) . "/%step", + // wrapper function to add an extra finish button. + 'wrapper' => 'page_manager_operation_wrapper', + ); + + // If $operation['form'] is simply a string, then it is the function + // name of the form. + if (!is_array($operation['form'])) { + $form_info['order'] = array( + 'form' => $operation['title'], + ); + $form_info['forms'] = array( + 'form' => array('form id' => $operation['form']), + ); + if (isset($operation['wrapper'])) { + $form_info['forms']['form']['wrapper'] = $operation['wrapper']; + } + } + // Otherwise it's the order and forms arrays directly. + else { + $form_info['order'] = $operation['form']['order']; + $form_info['forms'] = $operation['form']['forms']; + } + + // Allow the operation to override any form info settings: + if (isset($operation['form info'])) { + foreach ($operation['form info'] as $key => $setting) { + $form_info[$key] = $setting; + } + } + + if (!empty($page->subtask['operations include'])) { + // Quickly load any files necessary to display the forms. + $page->subtask['operations include']['function'] = 'nop'; + ctools_plugin_get_function($page->subtask, 'operations include'); + } + + $step = array_shift($args); + // If step is unset, go with the basic step. + if (!isset($step)) { + $step = current(array_keys($form_info['order'])); + } + + // If it is locked, hide the buttonzzz! + if ($page->locked && empty($operation['even locked'])) { + $form_info['no buttons'] = TRUE; + } + + ctools_include('wizard'); + $form_state = array( + 'page' => $page, + 'type' => 'edit', + 'ajax' => $js && (!isset($operation['ajax']) || !empty($operation['ajax'])), + 'rerender' => TRUE, + 'trail' => $active, + 'task_name' => $page->task_name, + 'task_id' => $page->task_id, + 'task' => $page->task, + 'subtask_id' => $page->subtask_id, + 'subtask' => $page->subtask, + 'operation' => $operation, + ); + + if ($active && $active[0] == 'handlers' && isset($form_state['page']->handlers[$form_state['trail'][1]])) { + $form_state['handler_id'] = $form_state['trail'][1]; + $form_state['handler'] = &$form_state['page']->handlers[$form_state['handler_id']]; + } + + if ($active && $active[0] == 'actions' && $active[1] == 'configure' && isset($form_state['page']->new_handler)) { + $form_state['handler_id'] = $form_state['page']->new_handler->name; + $form_state['handler'] = &$form_state['page']->new_handler; + } + + $built_form = ctools_wizard_multistep_form($form_info, $step, $form_state); + $output = drupal_render($built_form); + $title = empty($form_state['title']) ? $operation['title'] : $form_state['title']; + $titles[] = $title; + $title = implode(' » ', array_filter($titles)); + + // If there are messages for the form, render them. + if ($messages = theme('status_messages')) { + $output = $messages . $output; + } + + $description = isset($operation['admin description']) ? $operation['admin description'] : (isset($operation['description']) ? $operation['description'] : ''); + $return = array( + 'title' => $title, + 'content' => $output, + 'description' => $description, + ); + + // Append any extra content, used for the preview which is submitted then + // rendered. + if (isset($form_state['content'])) { + $return['content'] .= $form_state['content']; + } + + // If the form wanted us to go somewhere else next, pass that along. + if (isset($form_state['new trail'])) { + $return['new trail'] = $form_state['new trail']; + } + } + else { + $return = array( + 'title' => t('Error'), + 'content' => t('This operation trail does not exist.'), + ); + } + + $return['active'] = $active; + return $return; +} + +function page_manager_operation_wrapper($form, &$form_state) { + if (empty($form_state['operation']['no update and save']) && !empty($form['buttons']['return']['#wizard type']) && $form['buttons']['return']['#wizard type']) { + $form['buttons']['save'] = array( + '#type' => 'submit', + '#value' => !empty($form_state['form_info']['save text']) ? $form_state['form_info']['save text'] : t('Update and save'), + '#wizard type' => 'finish', + '#attributes' => $form['buttons']['return']['#attributes'], + '#save' => TRUE, + ); + } + + return $form; +} + +/** + * Callback generated when the an operation edit finished. + */ +function page_manager_edit_page_finish(&$form_state) { + if (empty($form_state['operation']['silent'])) { + if (empty($form_state['clicked_button']['#save'])) { + drupal_set_message(t('The page has been updated. Changes will not be permanent until you save.')); + } + else { + drupal_set_message(t('The page has been updated and saved.')); + } + $path = array(); + foreach ($form_state['trail'] as $operation) { + $path[] = $operation; + $form_state['page']->changes[implode('/', $path)] = TRUE; + } + } + + // If a handler was modified, set it to changed so we know to overwrite it. + if (isset($form_state['handler_id'])) { + $form_state['page']->handler_info[$form_state['handler_id']]['changed'] |= PAGE_MANAGER_CHANGED_CACHED; + } + + // While we make buttons go away on locked pages, it is still possible to + // have a lock a appear while you were editing, and have your changes + // disappear. This at least warns the user that this has happened. + if (!empty($page->locked)) { + drupal_set_message(t('Unable to update changes due to lock.')); + } + + // If the 'Update and Save' button was selected, write our cache out. + if (!empty($form_state['clicked_button']['#save'])) { + page_manager_save_page_cache($form_state['page']); + page_manager_clear_page_cache($form_state['page']->task_name); + $form_state['page'] = page_manager_get_page_cache($form_state['page']->task_name); + } + else { + if (empty($form_state['do not cache'])) { + page_manager_set_page_cache($form_state['page']); + } + } + + // We basically always want to force a rerender when the forms + // are finished, so make sure there is a new trail. + if (empty($form_state['new trail'])) { + // force a rerender to get rid of old form items that may have changed + // during save. + $form_state['new trail'] = $form_state['trail']; + } + + if (isset($form_state['new trail']) && empty($form_state['ajax'])) { + $form_state['redirect'] = page_manager_edit_url($form_state['page']->task_name, $form_state['new trail']); + } + + $form_state['complete'] = TRUE; +} + +/** + * Callback generated when the 'next' button is clicked. + * + * All we do here is store the cache. + */ +function page_manager_edit_page_next(&$form_state) { + page_manager_set_page_cache($form_state['page']); +} + +/** + * Callback generated when the 'cancel' button is clicked. + * + * All we do here is clear the cache. + */ +function page_manager_edit_page_cancel(&$form_state) { + $page = $form_state['page']; +} + +/** + * Render an operations array. + * + * This renders an array of operations into a series of nested UL statements, + * with ajax automatically on unless specified otherwise. Operations will + * automatically have the URLs generated nested. + * + * Each operation should have a 'type', which tells the renderer how to deal + * with it: + * - 'operation': An AJAX link to render. This is the default and is + * assumed if a type is not specified. Other fields for the operation: + * - - 'title': The text to display. Can be an image. Must be pre-sanitized. + * - - 'description': Text to place in the hover box over the link using the + * title attribute. + * - - 'arguments': Anything optional to put at the end of the URL. + * - - 'path': If set, overrides the default path. + * - - 'no operations': If set, the path will not have operations appended. + * - - 'no task': If set, the path will not have the task id. + * - - 'no link': If set, this item will just be text, not a link. + * - - 'ajax': If set to TRUE, ajax will be used. The default is TRUE. + * - - 'class': An optional class to specify for the link. + * - - 'form': The form to display for this operation, if using a single form. + * - - 'forms': An array of forms that must be paired with 'order' of this + * operation uses multiple forms. See wizard tool for details. + * - - 'order': The form order to use for multiple forms. See wizard tool for + * details. + * - - 'form info': Form info overrides for the wizard. See the wizard tool + * for available settings + * - 'group': + * - - 'title': The title of the link. May be HTML. + * - - 'title class': A class to apply to the title. + * - - 'children': An array of more operations that this group represents. + * All operations within this group will have this group's ID as part + * of the AJAX url to make it easier to find. + * - - 'class': A class to apply to the UL of the children. + * - - 'collapsible': If TRUE the collapsible tool will be used. + */ +function page_manager_render_operations(&$page, $operations, $active_trail, $attributes, $location, $parents = array()) { + drupal_add_library('system', 'drupal.ajax'); + + if (!isset($output[$location])) { + $output[$location] = ''; + } + + $keys = array_keys($operations); + $first = array_shift($keys); + $last = array_pop($keys); + + // Make sure the 'first' and 'last' operations are part of THIS nav tree: + while ($keys && isset($operations[$first]['location']) && $operations[$first]['location'] != $location) { + $first = array_shift($keys); + } + while ($keys && isset($operations[$last]['location']) && $operations[$last]['location'] != $location) { + $last = array_pop($keys); + } + + $active = reset($active_trail); + foreach ($operations as $id => $operation) { + $current_path = ''; + if ($parents) { + $current_path .= implode('/', $parents) . '/'; + } + $current_path .= $id; + + if (empty($operation['type'])) { + $operation['type'] = 'operation'; + } + + // We only render an li for things in the same nav tree. + if (empty($operation['location']) || $operation['location'] == $location) { + if (!is_array($attributes['class'])) { + $attributes['class'] = array($attributes['class']); + } + + $class = empty($attributes['class']) || !is_array($attributes['class']) ? array() : $attributes['class']; + + if ($id == $first) { + $class[] = 'operation-first'; + } + else if ($id == $last) { + $class[] = 'operation-last'; + } + + if (empty($operation['silent']) && !empty($page->changes[$current_path])) { + $class[] = $operation['type'] == 'group' ? 'changed-group' : 'changed'; + } + else { + $class[] = 'not-changed'; + } + + if ($active == $id) { + $class[] = $operation['type'] == 'group' ? 'active-group' : 'active'; + } + else { + $class[] = 'not-active'; + } + + $output[$location] .= '
  • '; + } + + switch ($operation['type']) { + case 'text': + $output[$location] .= $operation['title']; + break; + case 'operation': + $path = isset($operation['path']) ? $operation['path'] : 'admin/structure/pages/nojs/operation'; + if (!isset($operation['no task'])) { + $path .= '/' . $page->task_name; + } + + if (!isset($operation['no operations'])) { + $path .= '/' . $current_path; + if (isset($operation['arguments'])) { + $path .= '/' . $arguments; + } + } + + $class = array('page-manager-operation'); + if (!isset($operation['ajax']) || !empty($operation['ajax'])) { + $class[] = 'use-ajax'; + } + if (!empty($operation['class'])) { + $class[] = $operation['class']; + } + + $description = isset($operation['description']) ? $operation['description'] : ''; + if (empty($operation['silent']) && !empty($page->changes[$current_path])) { + $description .= ' ' . t('This setting contains unsaved changes.'); + } + + $output[$location] .= l($operation['title'], $path, array('attributes' => array('id' => 'page-manager-operation-' . $id, 'class' => $class, 'title' => $description), 'html' => TRUE)); + break; + case 'group': + if ($active == $id) { + $trail = $active_trail; + array_shift($trail); + } + else { + $trail = array(); + } + $group_location = isset($operation['location']) ? $operation['location'] : $location; + $temp = page_manager_render_operations($page, $operation['children'], $trail, $operation, $group_location, array_merge($parents, array($id))); + if ($temp) { + foreach ($temp as $id => $text) { + if (empty($output[$id])) { + $output[$id] = ''; + } + $output[$id] .= $text; + } + } + break; + } + + if (empty($operation['location']) || $operation['location'] == $location) { + $output[$location] .= '
  • '; + } + } + + if ($output[$location]) { + $classes = isset($attributes['class']) && is_array($attributes['class']) ? $attributes['class'] : array(); + $classes[] = 'page-manager-operations'; + + $output[$location] = '
      ' . $output[$location] . '
    '; + + if (!empty($attributes['title'])) { + $class = ''; + if (isset($attributes['title class'])) { + $class = $attributes['title class']; + } + $title = '
    ' . $attributes['title'] . '
    '; + + if (!empty($attributes['collapsible'])) { + $output[$location] = theme('ctools_collapsible', array('handle' => $title, 'content' => $output[$location], 'collapsed' => empty($active_trail))); + } + else { + $output[$location] = $title . $output[$location]; + } + } + return $output; + } +} + +/** + * Provide a simple form for saving the page manager info out of the cache. + */ +function page_manager_save_page_form($form, &$form_state) { + if (!empty($form_state['page']->changed)) { + $form['markup'] = array( + '#markup' => '
    ' . t('You have unsaved changes to this page. You must select Save to write them to the database, or Cancel to discard these changes. Please note that if you have changed any form, you must submit that form before saving.') . '
    ', + ); + + // Always make sure we submit back to the proper page. + $form['#action'] = url('admin/structure/pages/edit/' . $form_state['page']->task_name); + $form['save'] = array( + '#type' => 'submit', + '#value' => t('Save'), + '#submit' => array('page_manager_save_page_form_submit'), + ); + + $form['cancel'] = array( + '#type' => 'submit', + '#value' => t('Cancel'), + '#submit' => array('page_manager_save_page_form_cancel'), + ); + return $form; + } +} + +/** + * Save the page. + */ +function page_manager_save_page_form_submit(&$form, &$form_state) { + page_manager_save_page_cache($form_state['page']); +} + +/** + * Discard changes to the page. + */ +function page_manager_save_page_form_cancel($form, &$form_state) { + drupal_set_message(t('All pending changes have been discarded, and the page is now unlocked.')); + page_manager_clear_page_cache($form_state['page']->task_name); + + if (!empty($form_state['page']->new)) { + $form_state['redirect'] = 'admin/structure/pages'; + } +} + +// -------------------------------------------------------------------------- +// Handler (variant) related forms. + +/** + * Add a new task handler. + */ +function page_manager_handler_add($form, &$form_state) { + // Get a list of possible task handlers for this task. + return page_manager_handler_add_form($form, $form_state); +} + +/** + * Handler related forms. + */ +function page_manager_handler_add_submit(&$form, &$form_state) { + $cache = $form_state['page']; + $plugin = page_manager_get_task_handler($form_state['values']['handler']); + + // Create a new handler. + $handler = page_manager_new_task_handler($plugin); + if (!empty($form_state['values']['title'])) { + $handler->conf['title'] = $form_state['values']['title']; + } + else { + $handler->conf['title'] = $plugin['title']; + } + $handler->conf['name'] = $form_state['values']['name']; + $cache->new_handler = $handler; + + // Figure out which forms to present them with + $cache->forms = array(); + + $features = $form_state['values']['features']; + if (isset($features[$form_state['values']['handler']])) { + $cache->forms = array_merge($cache->forms, array_keys(array_filter($features[$form_state['values']['handler']]))); + } + + if (isset($plugin['required forms'])) { + $cache->forms = array_merge($cache->forms, array_keys($plugin['required forms'])); + } + + $form_state['no_rerender'] = TRUE; + if (!empty($cache->forms)) { + // Tell the form to go to the config page. + drupal_set_message(t('Before this variant can be added, it must be configured. When you are finished, click "Create variant" at the end of this wizard to add this to your page.')); + $form_state['new trail'] = array('actions', 'configure'); + } + else { + // It has no forms at all. Add the variant and go to its first operation. + page_manager_handler_add_finish($form_state); + } +} + +/** + * Finish the add process and make the new handler official. + */ +function page_manager_handler_add_finish(&$form_state) { + $page = &$form_state['page']; + $handler = $page->new_handler; + page_manager_handler_add_to_page($page, $handler); + + // Remove the temporary page. + unset($page->new_handler); + unset($page->forms); + + // Set the new destination + $plugin = page_manager_get_task_handler($handler->handler); + if (!empty($plugin['add finish'])) { + $location = $plugin['add finish']; + } + else { + $keys = array_keys($plugin['operations']); + $location = reset($keys); + } + + $form_state['new trail'] = array('handlers', $handler->name, $location); + + // Pass through. + page_manager_edit_page_finish($form_state); +} + +/** + * Throw away a new handler and return to the add form + */ +function page_manager_handler_add_cancel(&$form_state) { + $form_state['new trail'] = array('handlers', 'add'); + + // Remove the temporary page. + unset($page->new_handler); + unset($page->forms); +} + +/** + * Provide a consistent UI for adding handlers. + */ +function page_manager_handler_add_form($form, $form_state, $features = array()) { + $task = $form_state['task']; + $task_handler_plugins = page_manager_get_task_handler_plugins($task); + if (empty($task_handler_plugins)) { + drupal_set_message(t('There are currently no variants available and a page may not be added. Perhaps you need to install the Panels module to get a variant?'), 'error'); + $form['buttons']['return']['#disabled'] = TRUE; + return; + } + + foreach ($task_handler_plugins as $id => $plugin) { + $options[$id] = $plugin['title']; + if (isset($plugin['add features'])) { + $features[$id] = $plugin['add features']; + } + } + + if (!isset($form_state['type']) || $form_state['type'] != 'add') { + $form['title'] = array( + '#type' => 'textfield', + '#title' => t('Title'), + '#description' => t('Administrative title of this variant. If you leave blank it will be automatically assigned.'), + ); + + $form['name'] = array( + '#type' => 'machine_name', + '#title' => t('Machine name'), + '#required' => FALSE, + '#description' => t("A unique machine-readable name for this variant. It must only contain lowercase letters, numbers, and underscores. This name will be used when exporting the variant. If left empty the variant's name will be used instead."), + '#size' => 32, + '#maxlength' => 32, + '#machine_name' => array( + 'exists' => 'page_manager_handler_check_machine_name', + 'source' => array('title'), + ), + '#field_prefix' => '' . $form_state['task_name'] . '__', + '#field_suffix' => '‎', + ); + } + + $form['handler'] = array( + '#title' => t('Variant type'), + '#type' => 'select', + '#options' => $options, + ); + + // This set of checkboxes is not dangerous at all. + $form['features'] = array( + '#type' => 'item', + '#title' => t('Optional features'), + '#description' => t('Check any optional features you need to be presented with forms for configuring them. If you do not check them here you will still be able to utilize these features once the new page is created. If you are not sure, leave these unchecked.'), + '#tree' => TRUE, + ); + + ctools_include('dependent'); + foreach ($features as $plugin => $feature_list) { + foreach ($feature_list as $feature_id => $feature) { + $form['features'][$plugin][$feature_id] = array( + '#type' => 'checkbox', + '#title' => $feature, + ); + if (!empty($form_state['page']->forms) && in_array($feature_id, $form_state['page']->forms)) { + $form['features'][$plugin][$feature_id]['#default_value'] = TRUE; + } + + if ($plugin != 'default') { + $form['features'][$plugin][$feature_id] += array( + '#dependency' => array('edit-handler' => array($plugin)), + ); + } + } + } + + return $form; +} + +/* + * Check if handler's machine-name is unique + */ +function page_manager_handler_check_machine_name($name, $element, $form_state) { + $name = $form_state['task_name'] . '__' . $name; + + return count(ctools_export_load_object('page_manager_handlers', 'names', array($name))); +} + +/** + * Rearrange the order of variants. + */ +function page_manager_handler_import($form, &$form_state) { + $form['title'] = array( + '#type' => 'textfield', + '#title' => t('Variant name'), + '#description' => t('Enter the name of the new variant.'), + ); + + if (user_access('use ctools import')) { + $form['object'] = array( + '#type' => 'textarea', + '#title' => t('Paste variant code here'), + '#rows' => 15, + ); + } + // Users ordinarily can't get here without the 'import' permission, due to + // security implications. In case they somehow do, though, disable the form + // widget for extra safety. + else { + $form['shoveoff'] = array( + '#markup' => '
    ' . t('You do not have sufficient permissions to perform this action.') . '
    ', + ); + } + + return $form; +} + +/** + * Make sure that an import actually provides a handler. + */ +function page_manager_handler_import_validate($form, &$form_state) { + if (!user_access('use ctools import')) { + form_error($form['shoveoff'], t('You account permissions do not permit you to import.')); + return; + } + ob_start(); + eval($form_state['values']['object']); + ob_end_clean(); + + if (empty($handler)) { + $errors = ob_get_contents(); + if (empty($errors)) { + $errors = t('No variant found.'); + } + + form_error($form['object'], t('Unable to get a variant from the import. Errors reported: @errors', array('@errors' => $errors))); + } + + $form_state['handler'] = $handler; +} + +/** + * Clone an existing task handler into a new handler. + */ +function page_manager_handler_import_submit(&$form, &$form_state) { + $handler = $form_state['handler']; + + page_manager_handler_add_to_page($form_state['page'], $handler, $form_state['values']['title']); + + $plugin = page_manager_get_task_handler($handler->handler); + // It has no forms at all. Add the variant and go to its first operation. + $keys = array_keys($plugin['operations']); + $form_state['new trail'] = array('handlers', $handler->name, reset($keys)); +} + +/** + * Rearrange the order of variants. + */ +function page_manager_handler_rearrange($form, &$form_state) { + $page = $form_state['page']; + + $form['handlers'] = array('#tree' => TRUE); + + foreach ($page->handler_info as $id => $info) { + if ($info['changed'] & PAGE_MANAGER_CHANGED_DELETED) { + continue; + } + $handler = $page->handlers[$id]; + $plugin = page_manager_get_task_handler($handler->handler); + + $form['handlers'][$id]['title'] = array( + '#markup' => page_manager_get_handler_title($plugin, $handler, $page->task, $page->subtask_id), + ); + + $form['handlers'][$id]['weight'] = array( + '#type' => 'weight', + '#default_value' => $info['weight'], + '#delta' => 30, + ); + } + + return $form; +} + +function page_manager_handler_rearrange_submit(&$form, &$form_state) { + $handler_info = &$form_state['page']->handler_info; + + foreach ($form_state['values']['handlers'] as $id => $info) { + if ($handler_info[$id]['weight'] = $info['weight']) { + $handler_info[$id]['weight'] = $info['weight']; + $handler_info[$id]['changed'] |= PAGE_MANAGER_CHANGED_MOVED; + } + } + + // Sort the new cache. + uasort($handler_info, '_page_manager_handler_sort'); + +} + +/** + * Used as a callback to uasort to sort the task cache by weight. + * + * The 'name' field is used as a backup when weights are the same, which + * can happen when multiple modules put items out there at the same + * weight. + */ +function _page_manager_handler_sort($a, $b) { + if ($a['weight'] < $b['weight']) { + return -1; + } + elseif ($a['weight'] > $b['weight']) { + return 1; + } + elseif ($a['name'] < $b['name']) { + return -1; + } + elseif ($a['name'] > $b['name']) { + return 1; + } +} + +/** + * Rearrange the order of variants. + */ +function page_manager_handler_delete($form, &$form_state) { + if ($form_state['handler']->type == t('Overridden')) { + $text = t('Reverting the variant will delete the variant that is in the database, reverting it to the original default variant. This deletion will not be made permanent until you click Save.'); + } + else { + $text = t('Are you sure you want to delete this variant? This deletion will not be made permanent until you click Save.'); + } + $form['markup'] = array( + '#markup' => '

    ' . $text . '

    ', + ); + + return $form; +} + +/** + * Submit handler to delete a view. + */ +function page_manager_handler_delete_submit(&$form, &$form_state) { + $form_state['page']->handler_info[$form_state['handler_id']]['changed'] |= PAGE_MANAGER_CHANGED_DELETED; + $form_state['new trail'] = array('summary'); +} + +/** + * Entry point to export a page. + */ +function page_manager_handler_export($form, &$form_state) { + $export = page_manager_export_task_handler($form_state['handler']); + + $lines = substr_count($export, "\n"); + $form['code'] = array( + '#type' => 'textarea', + '#default_value' => $export, + '#rows' => $lines, + ); + + unset($form['buttons']); + return $form; +} + +/** + * Rearrange the order of variants. + */ +function page_manager_handler_clone($form, &$form_state) { + // This provides its own button because it does something totally different. + $form['title'] = array( + '#type' => 'textfield', + '#title' => t('Variant name'), + '#description' => t('Enter the name of the new variant.'), + ); + + return $form; +} + +/** + * Clone an existing task handler into a new handler. + */ +function page_manager_handler_clone_submit(&$form, &$form_state) { + $export = page_manager_export_task_handler($form_state['handler']); + ob_start(); + eval($export); + ob_end_clean(); + + page_manager_handler_add_to_page($form_state['page'], $handler, $form_state['values']['title']); + + $plugin = page_manager_get_task_handler($handler->handler); + // It has no forms at all. Add the variant and go to its first operation. + $keys = array_keys($plugin['operations']); + $form_state['new trail'] = array('handlers', $handler->name, reset($keys)); +} + +/** + * Form to enable a handler. + */ +function page_manager_handler_enable($form, &$form_state) { + $form['markup'] = array( + '#markup' => t('This variant is currently disabled. Enabling it will make it available in your system. This will not take effect until you save this page.'), + ); + + return $form; +} + +/** + * Enable the page after it has been confirmed. + */ +function page_manager_handler_enable_submit(&$form, &$form_state) { + $form_state['handler']->disabled = FALSE; + $form_state['page']->handler_info[$form_state['handler_id']]['disabled'] = FALSE; + $form_state['page']->handler_info[$form_state['handler_id']]['changed'] |= PAGE_MANAGER_CHANGED_STATUS; + $form_state['new trail'] = array('handlers', $form_state['handler_id'], 'actions', 'disable'); +} + +/** + * Form to disable a page. + */ +function page_manager_handler_disable($form, &$form_state) { + $form['markup'] = array( + '#markup' => t('This variant is currently enabled. Disabling it will make it unavailable in your system, and it will not be used. This will not take effect until you save this page.'), + ); + + return $form; +} + +/** + * Form to disable a page. + */ +function page_manager_handler_summary($form, &$form_state) { + $handler = $form_state['handler']; + $page = $form_state['page']; + $plugin = page_manager_get_task_handler($handler->handler); + + $form['markup'] = array( + '#markup' => page_manager_get_handler_summary($plugin, $handler, $page, FALSE), + ); + + return $form; +} + +/** + * Disable the page after it has been confirmed. + */ +function page_manager_handler_disable_submit(&$form, &$form_state) { + $form_state['handler']->disabled = TRUE; + $form_state['page']->handler_info[$form_state['handler_id']]['disabled'] = TRUE; + $form_state['page']->handler_info[$form_state['handler_id']]['changed'] |= PAGE_MANAGER_CHANGED_STATUS; + $form_state['new trail'] = array('handlers', $form_state['handler_id'], 'actions', 'enable'); +} + +/** + * Break the lock on a page so that it can be edited. + */ +function page_manager_break_lock($form, &$form_state) { + $form['markup'] = array( + '#markup' => t('Breaking the lock on this page will discard any pending changes made by the locking user. Are you REALLY sure you want to do this?') + ); + + return $form; +} + +/** + * Submit to break the lock on a page. + */ +function page_manager_break_lock_submit(&$form, &$form_state) { + $page = &$form_state['page']; + $form_state['page']->locked = FALSE; + ctools_object_cache_clear_all('page_manager_page', $page->task_name); + $form_state['do not cache'] = TRUE; + drupal_set_message(t('The lock has been cleared and all changes discarded. You may now make changes to this page.')); + + $form_state['new trail'] = array('summary'); +} + +/** + * Form to enable a page. + */ +function page_manager_enable_form($form, &$form_state) { + $form['markup'] = array( + '#markup' => t('Enabling this page will immediately make it available in your system (there is no need to wait for a save.)'), + ); + + return $form; +} + +/** + * Enable the page after it has been confirmed. + */ +function page_manager_enable_form_submit(&$form, &$form_state) { + $page = &$form_state['page']; + if ($function = ctools_plugin_get_function($page->subtask, 'enable callback')) { + $result = $function($page, FALSE); + menu_rebuild(); + } + $form_state['new trail'] = array('actions', 'disable'); + + // We don't want to cause this to cache if it wasn't already. If it was + // cached, however, we want to update the enabled state. + if (empty($form_state['page']->changed)) { + $form_state['do not cache'] = TRUE; + } +} + +/** + * Form to disable a page. + */ +function page_manager_disable_form($form, &$form_state) { + $form['markup'] = array( + '#markup' => t('Disabling this page will immediately make it unavailable in your system (there is no need to wait for a save.)'), + ); + + return $form; +} + +/** + * Disable the page after it has been confirmed. + */ +function page_manager_disable_form_submit(&$form, &$form_state) { + $page = &$form_state['page']; + if ($function = ctools_plugin_get_function($page->subtask, 'enable callback')) { + $result = $function($page, TRUE); + menu_rebuild(); + $form_state['new trail'] = array('actions', 'enable'); + + // We don't want to cause this to cache if it wasn't already. If it was + // cached, however, we want to update the enabled state. + if (empty($form_state['page']->changed)) { + $form_state['do not cache'] = TRUE; + } + } +} + +/** + * Print the summary information for a page. + */ +function page_manager_page_summary($form, &$form_state) { + $page = $form_state['page']; + + $output = ''; + +/* + if (isset($form_state['subtask']['admin title'])) { + $form_state['title'] = $form_state['subtask']['admin title']; + } +*/ + + if (isset($form_state['subtask']['admin description'])) { + $output .= '
    ' . $form_state['subtask']['admin description'] . '
    '; + } + + $output .= page_manager_get_page_summary($page->task, $page->subtask); + + if (!empty($page->handlers)) { + foreach ($page->handler_info as $id => $info) { + if ($info['changed'] & PAGE_MANAGER_CHANGED_DELETED) { + continue; + } + + $handler = $page->handlers[$id]; + $plugin = page_manager_get_task_handler($handler->handler); + + $output .= '
    '; + $output .= page_manager_get_handler_summary($plugin, $handler, $page); + $output .= '
    '; + + } + } + else { + $output .= '

    ' . t('This page has no variants and thus no output of its own.') . '

    '; + } + + $links = array( + array( + 'title' => ' » ' . t('Add a new variant'), + 'href' => page_manager_edit_url($page->task_name, array('actions', 'add')), + 'html' => TRUE, + ), + ); + + $output .= ''; + $form['markup'] = array( + '#markup' => $output, + ); + + return $form; +} + +/** + * Menu callback to enable or disable a page + */ +function page_manager_enable_page($disable, $js, $page) { + if (!isset($_GET['token']) || !drupal_valid_token($_GET['token'], $page->task_name)) { + return MENU_ACCESS_DENIED; + } + if ($page->locked) { + if ($disable) { + drupal_set_message(t('Unable to disable due to lock.')); + } + else { + drupal_set_message(t('Unable to enable due to lock.')); + } + } + else { + if ($function = ctools_plugin_get_function($page->subtask, 'enable callback')) { + $result = $function($page, $disable); + menu_rebuild(); + + // We want to re-cache this if it's changed so that status is properly + // updated on the changed form. + if (!empty($page->changed)) { + page_manager_set_page_cache($page); + } + } + } + + // For now $js is not actually in use on this. + drupal_goto('admin/structure/pages'); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/page_manager.api.php b/sites/all/modules/contrib/ctools/page_manager/page_manager.api.php new file mode 100644 index 0000000..03e2e75 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/page_manager.api.php @@ -0,0 +1,39 @@ + array( + 'identifier' => 'handler', + 'bulk export' => TRUE, + 'export callback' => 'page_manager_export_task_handler', + 'load callback' => 'page_manager_export_task_handler_load', + 'delete callback' => 'page_manager_delete_task_handler', + 'primary key' => 'did', + 'api' => array( + 'owner' => 'page_manager', + 'api' => 'pages_default', + 'minimum_version' => 1, + 'current_version' => 1, + ), + ), + 'fields' => array( + 'did' => array( + 'type' => 'serial', + 'not null' => TRUE, + 'description' => 'Primary ID field for the table. Not used for anything except internal lookups.', + 'no export' => TRUE, + ), + 'name' => array( + 'type' => 'varchar', + 'length' => '255', + 'description' => 'Unique ID for this task handler. Used to identify it programmatically.', + ), + 'task' => array( + 'type' => 'varchar', + 'length' => '64', + 'description' => 'ID of the task this handler is for.', + ), + 'subtask' => array( + 'type' => 'varchar', + 'length' => '64', + 'description' => 'ID of the subtask this handler is for.', + 'not null' => TRUE, + 'default' => '', + ), + 'handler' => array( + 'type' => 'varchar', + 'length' => '64', + 'description' => 'ID of the task handler being used.', + ), + 'weight' => array( + 'type' => 'int', + 'description' => 'The order in which this handler appears. Lower numbers go first.', + ), + 'conf' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'Serialized configuration of the handler, if needed.', + 'not null' => TRUE, + 'serialize' => TRUE, + 'object default' => array(), + ), + ), + 'primary key' => array('did'), + 'unique keys' => array( + 'name' => array('name'), + ), + 'indexes' => array('fulltask' => array('task', 'subtask', 'weight')), + ); + + $schema['page_manager_weights'] = array( + 'description' => 'Contains override weights for page_manager handlers that are in code.', + 'fields' => array( + 'name' => array( + 'type' => 'varchar', + 'length' => '255', + 'description' => 'Unique ID for this task handler. Used to identify it programmatically.', + 'not null' => TRUE, + 'default' => '', + ), + 'weight' => array( + 'type' => 'int', + 'description' => 'The order in which this handler appears. Lower numbers go first.', + ), + ), + 'primary key' => array('name'), + 'indexes' => array( + 'weights' => array('name', 'weight'), + ), + ); + + $schema['page_manager_pages'] = array( + 'description' => 'Contains page subtasks for implementing pages with arbitrary tasks.', + 'export' => array( + 'identifier' => 'page', + 'bulk export' => TRUE, + 'export callback' => 'page_manager_page_export', + 'api' => array( + 'owner' => 'page_manager', + 'api' => 'pages_default', + 'minimum_version' => 1, + 'current_version' => 1, + ), + ), + 'fields' => array( + 'pid' => array( + 'type' => 'serial', + 'not null' => TRUE, + 'description' => 'Primary ID field for the table. Not used for anything except internal lookups.', + 'no export' => TRUE, + ), + 'name' => array( + 'type' => 'varchar', + 'length' => '255', + 'description' => 'Unique ID for this subtask. Used to identify it programmatically.', + ), + 'task' => array( + 'type' => 'varchar', + 'length' => '64', + 'description' => 'What type of page this is, so that we can use the same mechanism for creating tighter UIs for targeted pages.', + 'default' => 'page', + ), + 'admin_title' => array( + 'type' => 'varchar', + 'length' => '255', + 'description' => 'Human readable title for this page subtask.', + ), + 'admin_description' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'Administrative description of this item.', + 'object default' => '', + ), + 'path' => array( + 'type' => 'varchar', + 'length' => '255', + 'description' => 'The menu path that will invoke this task.', + ), + 'access' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'Access configuration for this path.', + 'not null' => TRUE, + 'serialize' => TRUE, + 'object default' => array(), + ), + 'menu' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'Serialized configuration of Drupal menu visibility settings for this item.', + 'not null' => TRUE, + 'serialize' => TRUE, + 'object default' => array(), + ), + 'arguments' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'Configuration of arguments for this menu item.', + 'not null' => TRUE, + 'serialize' => TRUE, + 'object default' => array(), + ), + 'conf' => array( + 'type' => 'text', + 'size' => 'big', + 'description' => 'Serialized configuration of the page, if needed.', + 'not null' => TRUE, + 'serialize' => TRUE, + 'object default' => array(), + ), + ), + 'primary key' => array('pid'), + 'unique keys' => array( + 'name' => array('name'), + ), + 'indexes' => array('task' => array('task')), + ); + + return $schema; +} + +/** + * Implements hook_install(). + */ +function page_manager_install() { + db_update('system') + ->fields(array('weight' => 99)) + ->condition('name', 'page_manager') + ->execute(); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/page_manager.module b/sites/all/modules/contrib/ctools/page_manager/page_manager.module new file mode 100644 index 0000000..f3cb743 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/page_manager.module @@ -0,0 +1,1349 @@ + array( + 'title' => t('Use Page Manager'), + 'description' => t("Allows users to use most of Page Manager's features, though restricts some of the most powerful, potentially site-damaging features. Note that even the reduced featureset still allows for enormous control over your website."), + 'restrict access' => TRUE, + ), + 'administer page manager' => array( + 'title' => t('Administer Page Manager'), + 'description' => t('Allows complete control over Page Manager, i.e., complete control over your site. Grant with extreme caution.'), + 'restrict access' => TRUE, + ), + ); + +} + +/** + * Implements hook_ctools_plugin_directory() to let the system know + * where our task and task_handler plugins are. + */ +function page_manager_ctools_plugin_directory($owner, $plugin_type) { + if ($owner == 'page_manager') { + return 'plugins/' . $plugin_type; + } + if ($owner == 'ctools' && $plugin_type == 'cache') { + return 'plugins/' . $plugin_type; + } +} + +/** + * Implements hook_ctools_plugin_type() to inform the plugin system that Page + * Manager owns task, task_handler, and page_wizard plugin types. + * + * All of these are empty because the defaults all work. + */ +function page_manager_ctools_plugin_type() { + return array( + 'tasks' => array(), + 'task_handlers' => array(), + 'page_wizards' => array(), + ); +} + +/** + * Delegated implementation of hook_menu(). + */ +function page_manager_menu() { + // For some reason, some things can activate modules without satisfying + // dependencies. I don't know how, but this helps prevent things from + // whitescreening when this happens. + if (!module_exists('ctools')) { + return; + } + + $items = array(); + $base = array( + 'access arguments' => array('use page manager'), + 'file' => 'page_manager.admin.inc', + 'theme callback' => 'ajax_base_page_theme', + ); + + $items['admin/structure/pages'] = array( + 'title' => 'Pages', + 'description' => 'Add, edit and remove overridden system pages and user defined pages from the system.', + 'page callback' => 'page_manager_list_page', + ) + $base; + + $items['admin/structure/pages/list'] = array( + 'title' => 'List', + 'page callback' => 'page_manager_list_page', + 'type' => MENU_DEFAULT_LOCAL_TASK, + 'weight' => -10, + ) + $base; + + $items['admin/structure/pages/edit/%page_manager_cache'] = array( + 'title' => 'Edit', + 'page callback' => 'page_manager_edit_page', + 'page arguments' => array(4), + 'type' => MENU_NORMAL_ITEM, + ) + $base; + + $items['admin/structure/pages/%ctools_js/operation/%page_manager_cache'] = array( + 'page callback' => 'page_manager_edit_page_operation', + 'page arguments' => array(3, 5), + 'type' => MENU_NORMAL_ITEM, + ) + $base; + + $items['admin/structure/pages/%ctools_js/enable/%page_manager_cache'] = array( + 'page callback' => 'page_manager_enable_page', + 'page arguments' => array(FALSE, 3, 5), + 'type' => MENU_CALLBACK, + ) + $base; + + $items['admin/structure/pages/%ctools_js/disable/%page_manager_cache'] = array( + 'page callback' => 'page_manager_enable_page', + 'page arguments' => array(TRUE, 3, 5), + 'type' => MENU_CALLBACK, + ) + $base; + + $tasks = page_manager_get_tasks(); + + // Provide menu items for each task. + foreach ($tasks as $task_id => $task) { + // Allow the task to add its own menu items. + if ($function = ctools_plugin_get_function($task, 'hook menu')) { + $function($items, $task); + } + + // And for those that provide subtasks, provide menu items for them, as well. + foreach (page_manager_get_task_subtasks($task) as $subtask_id => $subtask) { + // Allow the task to add its own menu items. + if ($function = ctools_plugin_get_function($task, 'hook menu')) { + $function($items, $subtask); + } + } + } + + return $items; +} + +function page_manager_admin_paths() { + /* @todo FIX ME this is a major resource suck. */ + return; + + $items = array(); + ctools_include('page', 'page_manager', 'plugins/tasks'); + $pages = page_manager_page_load_all(); + foreach ($pages as $page) { + // Make sure the page we're on is set to be an administrative path and that + // it is not set to be a frontpage path. + if ((isset($page->conf['admin_paths']) && $page->conf['admin_paths']) && (!isset($page->make_frontpage) || !$page->make_frontpage)) { + $path_parts = explode('/', $page->path); + foreach ($path_parts as $key => $part) { + if (strpos($part, '%') !== FALSE || strpos($part, '!') !== FALSE) { + $path_parts[$key] = '*'; + } + } + $path = implode('/', $path_parts); + if ($page->menu['type'] == 'default tab') { + array_pop($path_parts); + $parent_path = implode('/', $path_parts); + $items[$parent_path] = TRUE; + } + $items[$path] = TRUE; + } + } + return $items; +} + +/** + * Implements hook_menu_alter. + * + * Get a list of all tasks and delegate to them. + */ +function page_manager_menu_alter(&$items) { + // For some reason, some things can activate modules without satisfying + // dependencies. I don't know how, but this helps prevent things from + // whitescreening when this happens. + if (!module_exists('ctools')) { + return; + } + + $tasks = page_manager_get_tasks(); + + foreach ($tasks as $task) { + if ($function = ctools_plugin_get_function($task, 'hook menu alter')) { + $function($items, $task); + } + // let the subtasks alter the menu items too. + foreach (page_manager_get_task_subtasks($task) as $subtask_id => $subtask) { + if ($function = ctools_plugin_get_function($subtask, 'hook menu alter')) { + $function($items, $subtask); + } + } + } + + // Override the core node revisions display to use the configured Page + // display handler. + if (!variable_get('page_manager_node_view_disabled', TRUE) && isset($items['node/%node/revisions/%/view'])) { + // Abstract the basic settings. + $item = array( + // Handle the page arguments. + 'load arguments' => array(3), + 'page arguments' => array(1, TRUE), + + // Replace the normal node_show call with Page Manager's node view. + 'page callback' => 'page_manager_node_view_page', + + // Provide the correct path to the Page Manager file. + 'file' => 'node_view.inc', + 'file path' => drupal_get_path('module', 'page_manager') . '/plugins/tasks', + ); + // Re-build the menu item using the normal values from node.module. + $items['node/%node/revisions/%/view'] = array( + 'title' => 'Revisions', + 'access callback' => '_node_revision_access', + 'access arguments' => array(1), + ) + $item; + } + + return $items; +} + +/* + * Implements hook_theme() + */ +function page_manager_theme() { + // For some reason, some things can activate modules without satisfying + // dependencies. I don't know how, but this helps prevent things from + // whitescreening when this happens. + if (!module_exists('ctools')) { + return; + } + + $base = array( + 'path' => drupal_get_path('module', 'page_manager') . '/theme', + 'file' => 'page_manager.theme.inc', + ); + + $items = array( + 'page_manager_handler_rearrange' => array( + 'render element' => 'form', + ) + $base, + 'page_manager_edit_page' => array( + 'template' => 'page-manager-edit-page', + 'variables' => array('page' => NULL, 'save' => NULL, 'operations' => array(), 'content' => array()), + ) + $base, + 'page_manager_lock' => array( + 'variables' => array('page' => array()), + ) + $base, + 'page_manager_changed' => array( + 'variables' => array('text' => NULL, 'description' => NULL), + ) + $base, + ); + + // Allow task plugins to have theme registrations by passing through: + $tasks = page_manager_get_tasks(); + + // Provide menu items for each task. + foreach ($tasks as $task_id => $task) { + if ($function = ctools_plugin_get_function($task, 'hook theme')) { + $function($items, $task); + } + } + + return $items; +} + +// -------------------------------------------------------------------------- +// Page caching +// +// The page cache is used to store a page temporarily, using the ctools object +// cache. When loading from the page cache, it will either load the cached +// version, or if there is not one, load the real thing and create a cache +// object which can then be easily stored. + +/** + * Get the cached changes to a given task handler. + */ +function page_manager_get_page_cache($task_name) { + $caches = drupal_static(__FUNCTION__, array()); + if (!isset($caches[$task_name])) { + ctools_include('object-cache'); + $cache = ctools_object_cache_get('page_manager_page', $task_name); + if (!$cache) { + $cache = new stdClass(); + $cache->task_name = $task_name; + list($cache->task_id, $cache->subtask_id) = page_manager_get_task_id($cache->task_name); + + $cache->task = page_manager_get_task($cache->task_id); + if (empty($cache->task)) { + return FALSE; + } + + if ($cache->subtask_id) { + $cache->subtask = page_manager_get_task_subtask($cache->task, $cache->subtask_id); + if (empty($cache->subtask)) { + return FALSE; + } + } + else { + $cache->subtask = $cache->task; + $cache->subtask['name'] = ''; + } + + $cache->handlers = page_manager_load_sorted_handlers($cache->task, $cache->subtask_id); + $cache->handler_info = array(); + foreach ($cache->handlers as $id => $handler) { + $cache->handler_info[$id] = array( + 'weight' => $handler->weight, + 'changed' => FALSE, + 'name' => $id, + ); + } + } + else { + // ensure the task is loaded. + page_manager_get_task($cache->task_id); + } + + if ($task_name != '::new') { + $cache->locked = ctools_object_cache_test('page_manager_page', $task_name); + } + else { + $cache->locked = FALSE; + } + + $caches[$task_name] = $cache; + } + + return $caches[$task_name]; +} + +/** + * Store changes to a task handler in the object cache. + */ +function page_manager_set_page_cache($page) { + if (!empty($page->locked)) { + return; + } + + if (empty($page->task_name)) { + return; + } + + ctools_include('object-cache'); + $page->changed = TRUE; + $cache = ctools_object_cache_set('page_manager_page', $page->task_name, $page); +} + +/** + * Remove an item from the object cache. + */ +function page_manager_clear_page_cache($name) { + ctools_include('object-cache'); + ctools_object_cache_clear('page_manager_page', $name); +} + +/** + * Write all changes from the page cache and clear it out. + */ +function page_manager_save_page_cache($cache) { + // Save the subtask: + if ($function = ctools_plugin_get_function($cache->task, 'save subtask callback')) { + $function($cache->subtask, $cache); + } + + // Iterate through handlers and save/delete/update as necessary. + // Go through each of the task handlers, check to see if it needs updating, + // and update it if so. + foreach ($cache->handler_info as $id => $info) { + $handler = &$cache->handlers[$id]; + // If it has been marked for deletion, delete it. + + if ($info['changed'] & PAGE_MANAGER_CHANGED_DELETED) { + page_manager_delete_task_handler($handler); + } + // If it has been somehow edited (or added), write the cached version + elseif ($info['changed'] & PAGE_MANAGER_CHANGED_CACHED) { + // Make sure we get updated weight from the form for this. + $handler->weight = $info['weight']; + page_manager_save_task_handler($handler); + } + // Otherwise, check to see if it has moved and, if so, update the weight. + elseif ($info['weight'] != $handler->weight) { + // Theoretically we could only do this for in code objects, but since our + // load mechanism checks for all, this is less database work. + page_manager_update_task_handler_weight($handler, $info['weight']); + } + + // Set enable/disabled status. + if ($info['changed'] & PAGE_MANAGER_CHANGED_STATUS) { + ctools_include('export'); + ctools_export_set_object_status($cache->handlers[$id], $info['disabled']); + } + } + + page_manager_clear_page_cache($cache->task_name); + + if (!empty($cache->path_changed) || !empty($cache->new)) { + // Force a menu rebuild to make sure the menu entries are set. + menu_rebuild(); + } + cache_clear_all(); +} + +/** + * Menu callback to load a page manager cache object for menu callbacks. + */ +function page_manager_cache_load($task_name) { + // load context plugin as there may be contexts cached here. + ctools_include('context'); + return page_manager_get_page_cache($task_name); +} + +/** + * Generate a unique name for a task handler. + * + * Task handlers need to be named but they aren't allowed to set their own + * names. Instead, they are named based upon their parent task and type. + */ +function page_manager_handler_get_name($task_name, $handlers, $handler) { + $base = str_replace('-', '_', $task_name); + $name = ''; + + // Optional machine name. + if (!empty($handler->conf['name'])) { + $name = $base . '__' . $handler->conf['name']; + if (count(ctools_export_load_object('page_manager_handlers', 'names', array($name)))) { + $name = ''; + } + } + + // If no machine name was provided or the name is in use, generate a unique name. + if (empty($name)) { + $base .= '__' . $handler->handler; + + // Use the ctools uuid generator to generate a unique id. + $name = $base . '_' . ctools_uuid_generate(); + } + + return $name; +} + +/** + * Import a handler into a page. + * + * This is used by both import and clone, since clone just exports the + * handler and immediately imports it. + */ +function page_manager_handler_add_to_page(&$page, &$handler, $title = NULL) { + $last = end($page->handler_info); + $handler->weight = $last ? $last['weight'] + 1 : 0; + $handler->task = $page->task_id; + $handler->subtask = $page->subtask_id; + $handler->export_type = EXPORT_IN_DATABASE; + $handler->type = t('Normal'); + + if ($title) { + $handler->conf['title'] = $title; + $handler->conf['name'] = trim(preg_replace('/[^a-z0-9_]+/', '-', strtolower($title)), '-'); + } + else { + $handler->conf['name'] = ''; + } + + $name = page_manager_handler_get_name($page->task_name, $page->handlers, $handler); + + $handler->name = $name; + + $page->handlers[$name] = $handler; + $page->handler_info[$name] = array( + 'weight' => $handler->weight, + 'name' => $handler->name, + 'changed' => PAGE_MANAGER_CHANGED_CACHED, + ); +} + +// -------------------------------------------------------------------------- +// Database routines +// +// This includes fetching plugins and plugin info as well as specialized +// fetch methods to get groups of task handlers per task. + +/** + * Load a single task handler by name. + * + * Handlers can come from multiple sources; either the database or by normal + * export method, which is handled by the ctools library, but handlers can + * also be bundled with task/subtask. We have to check there and perform + * overrides as appropriate. + * + * Handlers bundled with the task are of a higher priority than default + * handlers provided by normal code, and are of a lower priority than + * the database, so we have to check the source of handlers when we have + * multiple to choose from. + */ +function page_manager_load_task_handler($task, $subtask_id, $name) { + ctools_include('export'); + $result = ctools_export_load_object('page_manager_handlers', 'names', array($name)); + $handlers = page_manager_get_default_task_handlers($task, $subtask_id); + return page_manager_compare_task_handlers($result, $handlers, $name); +} + +/** + * Load all task handlers for a given task/subtask. + */ +function page_manager_load_task_handlers($task, $subtask_id = NULL, $default_handlers = NULL) { + ctools_include('export'); + $conditions = array( + 'task' => $task['name'], + ); + + if (isset($subtask_id)) { + $conditions['subtask'] = $subtask_id; + } + + $handlers = ctools_export_load_object('page_manager_handlers', 'conditions', $conditions); + $defaults = isset($default_handlers) ? $default_handlers : page_manager_get_default_task_handlers($task, $subtask_id); + foreach ($defaults as $name => $default) { + $result = page_manager_compare_task_handlers($handlers, $defaults, $name); + + if ($result) { + $handlers[$name] = $result; + // Ensure task and subtask are correct, because it's easy to change task + // names when editing a default and fail to do it on the associated handlers. + $result->task = $task['name']; + $result->subtask = $subtask_id; + } + } + + // Override weights from the weight table. + if ($handlers) { + $names = array(); + $placeholders = array(); + foreach ($handlers as $handler) { + $names[] = $handler->name; + $placeholders[] = "'%s'"; + } + + $result = db_query('SELECT name, weight FROM {page_manager_weights} WHERE name IN (:names)', array(':names' => $names)); + foreach ($result as $weight) { + $handlers[$weight->name]->weight = $weight->weight; + } + } + + return $handlers; +} + +/** + * Get the default task handlers from a task, if they exist. + * + * Tasks can contain 'default' task handlers which are provided by the + * default task. Because these can come from either the task or the + * subtask, the logic is abstracted to reduce code duplication. + */ +function page_manager_get_default_task_handlers($task, $subtask_id) { + // Load default handlers that are provied by the task/subtask itself. + $handlers = array(); + if ($subtask_id) { + $subtask = page_manager_get_task_subtask($task, $subtask_id); + if (isset($subtask['default handlers'])) { + $handlers = $subtask['default handlers']; + } + } + else if (isset($task['default handlers'])) { + $handlers = $task['default handlers']; + } + + return $handlers; +} + +/** + * Compare a single task handler from two lists and provide the correct one. + * + * Task handlers can be gotten from multiple sources. As exportable objects, + * they can be provided by default hooks and the database. But also, because + * they are tightly bound to tasks, they can also be provided by default + * tasks. This function reconciles where to pick up a task handler between + * the exportables list and the defaults provided by the task itself. + * + * @param $result + * A list of handlers provided by export.inc + * @param $handlers + * A list of handlers provided by the default task. + * @param $name + * Which handler to compare. + * @return + * Which handler to use, if any. May be NULL. + */ +function page_manager_compare_task_handlers($result, $handlers, $name) { + // Compare our special default handler against the actual result, if + // any, and do the right thing. + if (!isset($result[$name]) && isset($handlers[$name])) { + $handlers[$name]->type = t('Default'); + $handlers[$name]->export_type = EXPORT_IN_CODE; + return $handlers[$name]; + } + else if (isset($result[$name]) && !isset($handlers[$name])) { + return $result[$name]; + } + else if (isset($result[$name]) && isset($handlers[$name])) { + if ($result[$name]->export_type & EXPORT_IN_DATABASE) { + $result[$name]->type = t('Overridden'); + $result[$name]->export_type = $result[$name]->export_type | EXPORT_IN_CODE; + return $result[$name]; + } + else { + // In this case, our default is a higher priority than the standard default. + $handlers[$name]->type = t('Default'); + $handlers[$name]->export_type = EXPORT_IN_CODE; + return $handlers[$name]; + } + } +} + +/** + * Load all task handlers for a given task and subtask and sort them. + */ +function page_manager_load_sorted_handlers($task, $subtask_id = NULL, $enabled = FALSE) { + $handlers = page_manager_load_task_handlers($task, $subtask_id); + if ($enabled) { + foreach ($handlers as $id => $handler) { + if (!empty($handler->disabled)) { + unset($handlers[$id]); + } + } + } + uasort($handlers, 'page_manager_sort_task_handlers'); + return $handlers; +} + +/** + * Callback for uasort to sort task handlers. + * + * Task handlers are sorted by weight then by name. + */ +function page_manager_sort_task_handlers($a, $b) { + if ($a->weight < $b->weight) { + return -1; + } + elseif ($a->weight > $b->weight) { + return 1; + } + elseif ($a->name < $b->name) { + return -1; + } + elseif ($a->name > $b->name) { + return 1; + } + + return 0; +} + +/** + * Write a task handler to the database. + */ +function page_manager_save_task_handler(&$handler) { + $update = (isset($handler->did)) ? array('did') : array(); + // Let the task handler respond to saves: + if ($function = ctools_plugin_load_function('page_manager', 'task_handlers', $handler->handler, 'save')) { + $function($handler, $update); + } + + drupal_write_record('page_manager_handlers', $handler, $update); + db_delete('page_manager_weights') + ->condition('name', $handler->name) + ->execute(); + + // If this was previously a default handler, we may have to write task handlers. + if (!$update) { + // @todo wtf was I going to do here? + } + return $handler; +} + +/** + * Remove a task handler. + */ +function page_manager_delete_task_handler($handler) { + // Let the task handler respond to saves: + if ($function = ctools_plugin_load_function('page_manager', 'task_handlers', $handler->handler, 'delete')) { + $function($handler); + } + db_delete('page_manager_handlers') + ->condition('name', $handler->name) + ->execute(); + db_delete('page_manager_weights') + ->condition('name', $handler->name) + ->execute(); +} + +/** + * Export a task handler into code suitable for import or use as a default + * task handler. + */ +function page_manager_export_task_handler($handler, $indent = '') { + ctools_include('export'); + ctools_include('plugins'); + $handler = clone $handler; + + $append = ''; + if ($function = ctools_plugin_load_function('page_manager', 'task_handlers', $handler->handler, 'export')) { + $append = $function($handler, $indent); + } + + $output = ctools_export_object('page_manager_handlers', $handler, $indent); + $output .= $append; + + return $output; +} + +/** + * Loads page manager handler for export. + * + * Callback to load page manager handler within ctools_export_crud_load(). + * + * @param string $name + * The name of the handler to load. + * + * @return + * Loaded page manager handler object, extended with external properties. + */ +function page_manager_export_task_handler_load($name) { + $table = 'page_manager_handlers'; + $schema = ctools_export_get_schema($table); + $export = $schema['export']; + + $result = ctools_export_load_object($table, 'names', array($name)); + if (isset($result[$name])) { + $handler = $result[$name]; + + // Weight is stored in additional table so that in-code task handlers + // don't need to get written to the database just because they have their + // weight changed. Therefore, handler could have no correspondent database + // entry. Revert will not be performed for this handler and the weight + // will not be reverted. To make possible revert of the weight field + // export_type must simulate that the handler is stored in the database. + $handler->export_type = EXPORT_IN_DATABASE; + + // Also, page manager handler weight should be overriden with correspondent + // weight from page_manager_weights table, if there is one. + $result = db_query('SELECT weight FROM {page_manager_weights} WHERE name = (:names)', array(':names' => $handler->name))->fetchField(); + if (is_numeric($result)) { + $handler->weight = $result; + } + return $handler; + } +} + +/** + * Create a new task handler object. + * + * @param $plugin + * The plugin this task handler is created from. + */ +function page_manager_new_task_handler($plugin) { + // Generate a unique name. Unlike most named objects, we don't let people choose + // names for task handlers because they mostly don't make sense. + + // Create a new, empty handler object. + $handler = new stdClass; + $handler->title = $plugin['title']; + $handler->task = NULL; + $handler->subtask = NULL; + $handler->name = NULL; + $handler->handler = $plugin['name']; + $handler->weight = 0; + $handler->conf = array(); + + // These are provided by the core export API provided by ctools and we + // set defaults here so that we don't cause notices. Perhaps ctools should + // provide a way to do this for us so we don't have to muck with it. + $handler->export_type = EXPORT_IN_DATABASE; + $handler->type = t('Local'); + + if (isset($plugin['default conf'])) { + if (is_array($plugin['default conf'])) { + $handler->conf = $plugin['default conf']; + } + else if (function_exists($plugin['default conf'])) { + $handler->conf = $plugin['default conf']($handler); + } + } + + return $handler; +} + +/** + * Set an overidden weight for a task handler. + * + * We do this so that in-code task handlers don't need to get written + * to the database just because they have their weight changed. + */ +function page_manager_update_task_handler_weight($handler, $weight) { + db_delete('page_manager_weights') + ->condition('name', $handler->name) + ->execute(); + db_insert('page_manager_weights') + ->fields(array( + 'name' => $handler->name, + 'weight' => $weight, + )) + ->execute(); +} + + +/** + * Shortcut function to get task plugins. + */ +function page_manager_get_tasks() { + ctools_include('plugins'); + return ctools_get_plugins('page_manager', 'tasks'); +} + +/** + * Shortcut function to get a task plugin. + */ +function page_manager_get_task($id) { + ctools_include('plugins'); + return ctools_get_plugins('page_manager', 'tasks', $id); +} + +/** + * Get all tasks for a given type. + */ +function page_manager_get_tasks_by_type($type) { + ctools_include('plugins'); + $all_tasks = ctools_get_plugins('page_manager', 'tasks'); + $tasks = array(); + foreach ($all_tasks as $id => $task) { + if (isset($task['task type']) && $task['task type'] == $type) { + $tasks[$id] = $task; + } + } + + return $tasks; +} + +/** + * Fetch all subtasks for a page managertask. + * + * @param $task + * A loaded $task plugin object. + */ +function page_manager_get_task_subtasks($task) { + if (empty($task['subtasks'])) { + return array(); + } + + if ($function = ctools_plugin_get_function($task, 'subtasks callback')) { + $retval = $function($task); + if (is_array($retval)) { + return $retval; + } + } + + return array(); +} + +/** + * Fetch all subtasks for a page managertask. + * + * @param $task + * A loaded $task plugin object. + * @param $subtask_id + * The subtask ID to load. + */ +function page_manager_get_task_subtask($task, $subtask_id) { + if (empty($task['subtasks'])) { + return; + } + + if ($function = ctools_plugin_get_function($task, 'subtask callback')) { + return $function($task, $subtask_id); + } +} + +/** + * Shortcut function to get task handler plugins. + */ +function page_manager_get_task_handlers() { + ctools_include('plugins'); + return ctools_get_plugins('page_manager', 'task_handlers'); +} + +/** + * Shortcut function to get a task handler plugin. + */ +function page_manager_get_task_handler($id) { + ctools_include('plugins'); + return ctools_get_plugins('page_manager', 'task_handlers', $id); +} + +/** + * Retrieve a list of all applicable task handlers for a given task. + * + * This looks at the $task['handler type'] and compares that to $task_handler['handler type']. + * If the task has no type, the id of the task is used instead. + */ +function page_manager_get_task_handler_plugins($task, $all = FALSE) { + $type = isset($task['handler type']) ? $task['handler type'] : $task['name']; + $name = $task['name']; + + $handlers = array(); + $task_handlers = page_manager_get_task_handlers(); + foreach ($task_handlers as $id => $handler) { + $task_type = is_array($handler['handler type']) ? $handler['handler type'] : array($handler['handler type']); + if (in_array($type, $task_type) || in_array($name, $task_type)) { + if ($all || !empty($handler['visible'])) { + $handlers[$id] = $handler; + } + } + } + + return $handlers; +} + +/** + * Get the title for a given handler. + * + * If the plugin has no 'admin title' function, the generic title of the + * plugin is used instead. + */ +function page_manager_get_handler_title($plugin, $handler, $task, $subtask_id) { + $function = ctools_plugin_get_function($plugin, 'admin title'); + if ($function) { + return $function($handler, $task, $subtask_id); + } + else { + return $plugin['title']; + } +} + +/** + * Get the admin summary (additional info) for a given handler. + */ +function page_manager_get_handler_summary($plugin, $handler, $page, $title = TRUE) { + if ($function = ctools_plugin_get_function($plugin, 'admin summary')) { + return $function($handler, $page->task, $page->subtask, $page, $title); + } +} + +/** + * Get the admin summary (additional info) for a given page. + */ +function page_manager_get_page_summary($task, $subtask) { + if ($function = ctools_plugin_get_function($subtask, 'admin summary')) { + return $function($task, $subtask); + } +} + +/** + * Split a task name into a task id and subtask id, if applicable. + */ +function page_manager_get_task_id($task_name) { + if (strpos($task_name, '-') !== FALSE) { + return explode('-', $task_name, 2); + } + else { + return array($task_name, NULL); + } +} + +/** + * Turn a task id + subtask_id into a task name. + */ +function page_manager_make_task_name($task_id, $subtask_id) { + if ($subtask_id) { + return $task_id . '-' . $subtask_id; + } + else { + return $task_id; + } +} + +/** + * Get the render function for a handler. + */ +function page_manager_get_renderer($handler) { + return ctools_plugin_load_function('page_manager', 'task_handlers', $handler->handler, 'render'); +} + +// -------------------------------------------------------------------------- +// Functions existing on behalf of tasks and task handlers + + +/** + * Page manager arg load function because menu system will not load extra + * files for these; they must be in a .module. + */ +function pm_arg_load($value, $subtask, $argument) { + page_manager_get_task('page'); + return _pm_arg_load($value, $subtask, $argument); +} + +/** + * Special arg_load function to use %menu_tail like functionality to + * get everything after the arg together as a single value. + */ +function pm_arg_tail_load($value, $subtask, $argument, $map) { + $value = implode('/', array_slice($map, $argument)); + page_manager_get_task('page'); + return _pm_arg_load($value, $subtask, $argument); +} + +/** + * Special menu _load() function for the user:uid argument. + * + * This is just the normal page manager argument. It only exists so that + * the to_arg can exist. + */ +function pm_uid_arg_load($value, $subtask, $argument) { + page_manager_get_task('page'); + return _pm_arg_load($value, $subtask, $argument); +} + +/** + * to_arg function for the user:uid argument to provide the arg for the + * current global user. + */ +function pm_uid_arg_to_arg($arg) { + return user_uid_optional_to_arg($arg); +} + +/** + * Callback for access control ajax form on behalf of page.inc task. + * + * Returns the cached access config and contexts used. + */ +function page_manager_page_ctools_access_get($argument) { + $page = page_manager_get_page_cache($argument); + + $contexts = array(); + + // Load contexts based on argument data: + if ($arguments = _page_manager_page_get_arguments($page->subtask['subtask'])) { + $contexts = ctools_context_get_placeholders_from_argument($arguments); + } + + return array($page->subtask['subtask']->access, $contexts); +} + +/** + * Callback for access control ajax form on behalf of page.inc task. + * + * Writes the changed access to the cache. + */ +function page_manager_page_ctools_access_set($argument, $access) { + $page = page_manager_get_page_cache($argument); + $page->subtask['subtask']->access = $access; + page_manager_set_page_cache($page); +} + +/** + * Callback for access control ajax form on behalf of context task handler. + * + * Returns the cached access config and contexts used. + */ +function page_manager_task_handler_ctools_access_get($argument) { + list($task_name, $name) = explode('*', $argument); + $page = page_manager_get_page_cache($task_name); + if (empty($name)) { + $handler = &$page->new_handler; + } + else { + $handler = &$page->handlers[$name]; + } + + if (!isset($handler->conf['access'])) { + $handler->conf['access'] = array(); + } + + ctools_include('context-task-handler'); + + $contexts = ctools_context_handler_get_all_contexts($page->task, $page->subtask, $handler); + + return array($handler->conf['access'], $contexts); +} + +/** + * Callback for access control ajax form on behalf of context task handler. + * + * Writes the changed access to the cache. + */ +function page_manager_task_handler_ctools_access_set($argument, $access) { + list($task_name, $name) = explode('*', $argument); + $page = page_manager_get_page_cache($task_name); + if (empty($name)) { + $handler = &$page->new_handler; + } + else { + $handler = &$page->handlers[$name]; + } + + $handler->conf['access'] = $access; + page_manager_set_page_cache($page); +} + +/** + * Form a URL to edit a given page given the trail. + */ +function page_manager_edit_url($task_name, $trail = array()) { + if (!is_array($trail)) { + $trail = array($trail); + } + + if (empty($trail) || $trail == array('summary')) { + return "admin/structure/pages/edit/$task_name"; + } + + return 'admin/structure/pages/nojs/operation/' . $task_name . '/' . implode('/', $trail); +} + +/** + * Watch menu links during the menu rebuild, and re-parent things if we need to. + */ +function page_manager_menu_link_alter(&$item) { + return; +/** -- disabled, concept code -- + static $mlids = array(); + // Keep an array of mlids as links are saved that we can use later. + if (isset($item['mlid'])) { + $mlids[$item['path']] = $item['mlid']; + } + + if (isset($item['parent_path'])) { + if (isset($mlids[$item['parent_path']])) { + $item['plid'] = $mlids[$item['parent_path']]; + } + else { + // Since we didn't already see an mlid, let's check the database for one. + $mlid = db_query('SELECT mlid FROM {menu_links} WHERE router_path = :path', array('path' => $item['parent_path']))->fetchField(); + if ($mlid) { + $item['plid'] = $mlid; + } + } + } + */ +} + +/** + * Callback to list handlers available for export. + */ +function page_manager_page_manager_handlers_list() { + $list = $types = array(); + $tasks = page_manager_get_tasks(); + foreach ($tasks as $type => $info) { + if (empty($info['non-exportable'])) { + $types[] = $type; + } + } + + $handlers = ctools_export_load_object('page_manager_handlers'); + foreach ($handlers as $handler) { + if (in_array($handler->task, $types)) { + $plugin = page_manager_get_task_handler($handler->handler); + $title = page_manager_get_handler_title($plugin, $handler, $tasks[$handler->task], $handler->subtask); + + if ($title) { + $list[$handler->name] = check_plain("$handler->task: $title ($handler->name)"); + } + else { + $list[$handler->name] = check_plain("$handler->task: ($handler->name)"); + } + } + } + return $list; +} + +/** + * Callback to bulk export page manager pages. + */ +function page_manager_page_manager_pages_to_hook_code($names = array(), $name = 'foo') { + $schema = ctools_export_get_schema('page_manager_pages'); + $export = $schema['export']; + $objects = ctools_export_load_object('page_manager_pages', 'names', array_values($names)); + if ($objects) { + $code = "/**\n"; + $code .= " * Implements hook_{$export['default hook']}()\n"; + $code .= " */\n"; + $code .= "function " . $name . "_{$export['default hook']}() {\n"; + foreach ($objects as $object) { + // Have to implement our own because this export func sig requires it + $code .= $export['export callback']($object, TRUE, ' '); + $code .= " \${$export['identifier']}s['" . check_plain($object->$export['key']) . "'] = \${$export['identifier']};\n\n"; + } + $code .= " return \${$export['identifier']}s;\n"; + $code .= "}\n"; + return $code; + } +} + +/** + * Get the current page information. + * + * @return $page + * An array containing the following information. + * + * - 'name': The name of the page as used in the page manager admin UI. + * - 'task': The plugin for the task in use. If this is a system page it + * will contain information about that page, such as what functions + * it uses. + * - 'subtask': The plugin for the subtask. If this is a custom page, this + * will contain information about that custom page. See 'subtask' in this + * array to get the actual page object. + * - 'handler': The actual handler object used. If using panels, see + * $page['handler']->conf['display'] for the actual panels display + * used to render. + * - 'contexts': The context objects used to render this page. + * - 'arguments': The raw arguments from the URL used on this page. + */ +function page_manager_get_current_page($page = NULL) { + static $current = array(); + if (isset($page)) { + $current = $page; + } + + return $current; +} + +/** + * Implementation of hook_panels_dashboard_blocks(). + * + * Adds page information to the Panels dashboard. + */ +function page_manager_panels_dashboard_blocks(&$vars) { + $vars['links']['page_manager'] = array( + 'weight' => -100, + 'title' => l(t('Panel page'), 'admin/structure/pages/add'), + 'description' => t('Panel pages can be used as landing pages. They have a URL path, accept arguments and can have menu entries.'), + ); + + module_load_include('inc', 'page_manager', 'page_manager.admin'); + $tasks = page_manager_get_tasks_by_type('page'); + $pages = array('operations' => array()); + + page_manager_get_pages($tasks, $pages); + $count = 0; + $rows = array(); + foreach ($pages['rows'] as $id => $info) { + $rows[] = array( + 'data' => array( + $info['data']['title'], + $info['data']['operations'], + ), + 'class' => $info['class'], + ); + + // Only show 10. + if (++$count >= 10) { + break; + } + } + + $vars['blocks']['page_manager'] = array( + 'weight' => -100, + 'title' => t('Manage pages'), + 'link' => l(t('Go to list'), 'admin/structure/pages'), + 'content' => theme('table', array('header' => array(), 'rows' => $rows, 'attributes' => array('class' => 'panels-manage'))), + 'class' => 'dashboard-pages', + 'section' => 'right', + ); +} + +/** + * Implement pseudo-hook to fetch addressable content. + * + * For Page Manager, the address will be an array. The first + * element will be the $task and the second element will be the + * $task_handler. The third elements will be the arguments + * provided. + */ +function page_manager_addressable_content($address, $type) { + if (count($address) < 3) { + return; + } + + $task_name = array_shift($address); + $subtask_name = array_shift($address); + $handler_name = array_shift($address); + if ($address) { + $arguments = array_shift($address); + } + + // Since $arguments is an array of arbitrary size, we need to implode it: + if (!empty($arguments)) { + // The only choices we have for separators since :: is already + // used involve ., - or _. Since - and _ are more common than . + // in URLs, let's try .. as an argument separator. + $arguments = explode('..', $arguments); + } + else { + // implode does not return an empty array on an empty + // string so do it specifically. + $arguments = array(); + } + + $task = page_manager_get_task($task_name); + if (!$task) { + return; + } + + $handler = page_manager_load_task_handler($task, $subtask_name, $handler_name); + if (!$handler) { + return; + } + + $handler_plugin = page_manager_get_task_handler($handler->handler); + if (!$handler_plugin) { + return; + } + + // Load the contexts for the task. + ctools_include('context'); + ctools_include('context-task-handler'); + $contexts = ctools_context_handler_get_task_contexts($task, $subtask_name, $arguments); + + // With contexts loaded, ensure the task is accessible. Tasks without a callback + // are automatically accessible. + $function = ctools_plugin_get_function($task, 'access callback'); + if ($function && !$function($task, $subtask_name, $contexts)) { + return; + } + + $function = ctools_plugin_get_function($handler_plugin, 'addressable callback'); + if ($function) { + return $function($task, $subtask_name, $handler, $address, $contexts, $arguments, $type); + } +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/cache/page_manager_context.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/cache/page_manager_context.inc new file mode 100644 index 0000000..2f01b56 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/cache/page_manager_context.inc @@ -0,0 +1,70 @@ + t('Page manager context'), + 'cache get' => 'page_manager_cache_page_manager_context_cache_get', + 'cache set' => 'page_manager_cache_page_manager_context_cache_set', + 'cache finalize' => 'page_manager_cache_page_manager_context_cache_finalize', + + // We don't support a clear because the general uses of clear have no effect + // on us. +); + +function page_manager_cache_page_manager_context_cache_get($data, $key) { + $page = page_manager_get_page_cache($data); + if ($page) { + if (!empty($page->context_cache[$key])) { + return $page->context_cache[$key]; + } + else { + ctools_include('context-task-handler'); + if ($key == 'temp') { + $handler = $page->new_handler; + } + else { + $handler = $page->handlers[$key]; + } + return ctools_context_handler_get_task_object($page->task, $page->subtask, $handler); + } + } +} + +function page_manager_cache_page_manager_context_cache_set($data, $key, $object) { + $page = page_manager_get_page_cache($data); + if ($page) { + $page->context_cache[$key] = $object; + return page_manager_set_page_cache($page); + } +} + +/** + * Copy temporary data from the page manager cache + */ +function page_manager_cache_page_manager_context_cache_finalize($data, $key, $object) { + // Statically cached so there shouldn't be any worries. It's an object so + // referencing ensures that we'll get the right one. + $page = page_manager_get_page_cache($data); + if ($page) { + if ($key == 'temp') { + $handler = $page->new_handler; + } + else { + $handler = $page->handlers[$key]; + } + $handler->conf['contexts'] = $object->contexts; + $handler->conf['relationships'] = $object->relationships; + + if (isset($page->context_cache[$key])) { + unset($page->context_cache[$key]); + } + return page_manager_set_page_cache($page); + } +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/task_handlers/http_response.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/task_handlers/http_response.inc new file mode 100644 index 0000000..c4eba8e --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/task_handlers/http_response.inc @@ -0,0 +1,332 @@ + 'context', + 'visible' => TRUE, // may be added up front. + + // Administrative fields. + 'title' => t('HTTP response code'), + 'admin summary' => 'page_manager_http_response_admin_summary', + 'admin title' => 'page_manager_http_response_title', + 'operations' => array( + 'settings' => array( + 'title' => t('General'), + 'description' => t('Change general settings for this variant.'), + 'form' => 'page_manager_http_response_edit_settings', + ), + 'criteria' => array( + 'title' => t('Selection rules'), + 'description' => t('Control the criteria used to decide whether or not this variant is used.'), + 'ajax' => FALSE, + 'form' => array( + 'order' => array( + 'form' => t('Selection rules'), + ), + 'forms' => array( + 'form' => array( + 'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc', + 'form id' => 'ctools_context_handler_edit_criteria', + ), + ), + ), + ), + 'context' => array( + 'title' => t('Contexts'), + 'ajax' => FALSE, + 'description' => t('Add additional context objects to this variant that can be used by the content.'), + 'form' => array( + 'order' => array( + 'form' => t('Context'), + ), + 'forms' => array( + 'form' => array( + 'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc', + 'form id' => 'ctools_context_handler_edit_context', + ), + ), + ), + ), + ), + + // Callback to render the data. + 'render' => 'page_manager_http_response_render', + + 'add features' => array( + 'criteria' => t('Selection rules'), + 'context' => t('Contexts'), + ), + // Where to go when finished. + 'add finish' => 'settings', + + 'required forms' => array( + 'settings' => t('Panel settings'), + ), + + 'edit forms' => array( + 'criteria' => t('Selection rules'), + 'settings' => t('General'), + 'context' => t('Contexts'), + ), + 'forms' => array( + 'settings' => array( + 'form id' => 'page_manager_http_response_edit_settings', + ), + 'context' => array( + 'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc', + 'form id' => 'ctools_context_handler_edit_context', + ), + 'criteria' => array( + 'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc', + 'form id' => 'ctools_context_handler_edit_criteria', + ), + ), + 'default conf' => array( + 'title' => t('HTTP response code'), + 'contexts' => array(), + 'relationships' => array(), + 'code' => '404', + 'destination' => '', + ), +); + +/** + * Provide a list of the response codes we support. + * + * Abstracted so it can be more readily used both on input and output. + */ +function page_manager_http_response_codes() { + return array( + 403 => t('403 Access denied'), + 404 => t('404 Page not found'), + 410 => t('410 Gone'), + 301 => t('301 Redirect'), + ); +} + +function page_manager_http_response_admin_summary($handler, $task, $subtask, $page, $show_title = TRUE) { + $task_name = page_manager_make_task_name($task['name'], $subtask['name']); + $output = ''; + + ctools_include('context'); + ctools_include('context-task-handler'); + + // Get the operations + $operations = page_manager_get_operations($page); + + // Get operations for just this handler. + $operations = $operations['handlers']['children'][$handler->name]['children']['actions']['children']; + $args = array('handlers', $handler->name, 'actions'); + $rendered_operations = page_manager_render_operations($page, $operations, array(), array('class' => array('actions')), 'actions', $args); + + $plugin = page_manager_get_task_handler($handler->handler); + + $object = ctools_context_handler_get_task_object($task, $subtask, $handler); + $context = ctools_context_load_contexts($object, TRUE); + + $access = ctools_access_group_summary(!empty($handler->conf['access']) ? $handler->conf['access'] : array(), $context); + if ($access) { + $access = t('This panel will be selected if @conditions.', array('@conditions' => $access)); + } + else { + $access = t('This panel will always be selected.'); + } + + $rows = array(); + + $type = $handler->type == t('Default') ? t('In code') : $handler->type; + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('Storage')), + array('class' => array('page-summary-data'), 'data' => $type), + array('class' => array('page-summary-operation'), 'data' => ''), + ); + + if (!empty($handler->disabled)) { + $link = l(t('Enable'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'actions', 'enable'))); + $text = t('Disabled'); + } + else { + $link = l(t('Disable'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'actions', 'disable'))); + $text = t('Enabled'); + } + + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('Status')), + array('class' => array('page-summary-data'), 'data' => $text), + array('class' => array('page-summary-operation'), 'data' => $link), + ); + + $link = l(t('Edit'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'criteria'))); + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('Selection rule')), + array('class' => array('page-summary-data'), 'data' => $access), + array('class' => array('page-summary-operation'), 'data' => $link), + ); + + $link = l(t('Edit'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'settings'))); + $codes = page_manager_http_response_codes(); + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('Response code')), + array('class' => array('page-summary-data'), 'data' => $codes[$handler->conf['code']]), + array('class' => array('page-summary-operation'), 'data' => $link), + ); + + $info = theme('table', array('header' => array(), 'rows' => $rows, 'attributes' => array('class' => array('page-manager-handler-summary')))); + + $title = $handler->conf['title']; + if ($title != t('Panel')) { + $title = t('Panel: @title', array('@title' => $title)); + } + + $output .= '
    '; + if ($show_title) { + $output .= '
    '; + $output .= '
    ' . $rendered_operations['actions'] . '
    '; + $output .= '' . $title . ''; + } + + $output .= '
    '; + $output .= $info; + $output .= '
    '; + + return $output; +} + +/** + * Set up a title for the panel based upon the selection rules. + */ +function page_manager_http_response_title($handler, $task, $subtask) { + if (isset($handler->conf['title'])) { + return check_plain($handler->conf['title']); + } + else { + return t('HTTP response code'); + } +} + +/** + * General settings for the panel + */ +function page_manager_http_response_edit_settings($form, &$form_state) { + ctools_include('page_manager.admin', 'page_manager', ''); + ctools_include('export', 'ctools'); + + $conf = $form_state['handler']->conf; + $form['title'] = array( + '#type' => 'textfield', + '#default_value' => $conf['title'], + '#title' => t('Administrative title'), + '#description' => t('Administrative title of this variant.'), + ); + + $name = isset($conf['name']) ? $conf['name'] : FALSE; + $form['name'] = array( + '#type' => 'machine_name', + '#title' => t('Machine name'), + '#required' => FALSE, + '#default_value' => $name, + '#description' => t("A unique machine-readable name for this variant. It must only contain lowercase letters, numbers, and underscores. This name will be used when exporting the variant. If left empty the variant's name will be used instead."), + '#size' => 32, + '#maxlength' => 32, + '#machine_name' => array( + 'exists' => 'page_manager_handler_check_machine_name', + 'source' => array('title'), + ), + '#field_prefix' => '' . $form_state['task_name'] . '__', + '#field_suffix' => '‎', + ); + + $form['code'] = array( + '#title' => t('Response code'), + '#type' => 'select', + '#options' => page_manager_http_response_codes(), + '#default_value' => $conf['code'], + ); + + ctools_include('dependent'); + $form['destination'] = array( + '#type' => 'textfield', + '#title' => t('Redirect destination'), + '#default_value' => $conf['destination'], + '#dependency' => array('edit-code' => array(301)), + '#description' => t('Enter the path to redirect to. You may use keyword substitutions from contexts. You can use external urls (http://www.example.com/foo) or internal urls (node/1).'), + ); + + return $form; +} + +function page_manager_http_response_edit_settings_submit($form, &$form_state) { + $machine_name = $form_state['handler']->name; + $name = $form_state['task_name'] . '__' . $form_state['values']['name']; + + // If new name doesn't equal machine name, we need to update and redirect. + if ($machine_name !== $name) { + $form_state['handler']->name = $name; + // If there's a trail, we need to replace it for redirection. + if (isset($form_state['trail'])) { + $form_state['new trail'] = $form_state['trail']; + $delta = array_search($machine_name, $form_state['new trail']); + $form_state['new trail'][$delta] = $name; + } + // If handler id is set, replace it. + if ($form_state['handler_id']) { + $form_state['handler_id'] = $name; + } + // If we're defining a new custom handler, move page handler to new name. + if (isset($form_state['page']->handlers[$machine_name]) && isset($form_state['page']->handler_info[$machine_name])) { + $form_state['page']->handlers[$name] = $form_state['page']->handlers[$machine_name]; + unset($form_state['page']->handlers[$machine_name]); + $form_state['page']->handler_info[$name] = $form_state['page']->handler_info[$machine_name]; + unset($form_state['page']->handler_info[$machine_name]); + } + } + + $form_state['handler']->conf['title'] = $form_state['values']['title']; + $form_state['handler']->conf['name'] = $form_state['values']['name']; + $form_state['handler']->conf['code'] = $form_state['values']['code']; + $form_state['handler']->conf['destination'] = $form_state['values']['destination']; +} + +function page_manager_http_response_render($handler, $base_contexts, $args, $test = TRUE) { + // Go through arguments and see if they match. + ctools_include('context'); + ctools_include('context-task-handler'); + + // Add my contexts + $contexts = ctools_context_handler_get_handler_contexts($base_contexts, $handler); + + // Test. + if ($test && !ctools_context_handler_select($handler, $contexts)) { + return; + } + + if (isset($handler->handler)) { + ctools_context_handler_pre_render($handler, $contexts, $args); + } + + $info['response code'] = $handler->conf['code']; + if ($info['response code'] == 301) { + $path = ctools_context_keyword_substitute($handler->conf['destination'], array(), $contexts); + $url = parse_url($path); + if (isset($url['query'])) { + $path = strtr($path, array('?' . $url['query'] => '')); + $info['query'] = drupal_get_query_array($url['query']); + } + if (isset($url['fragment'])) { + $path = strtr($path, array('#' . $url['fragment'] => '')); + $info['fragment'] = $url['fragment']; + } + + $info['destination'] = rtrim($path, '?'); + } + + return $info; +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/blog.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/blog.inc new file mode 100644 index 0000000..bab2dd2 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/blog.inc @@ -0,0 +1,121 @@ + 'page', + + 'title' => t('All blogs'), + 'admin title' => t('All blogs'), + 'admin description' => t('When enabled, this overrides the default Drupal behavior for the all blogs at /blog. If no variant is selected, the default Drupal most recent blog posts will be shown.'), + 'admin path' => 'blog', + + // Menu hooks so that we can alter the node/%node menu entry to point to us. + 'hook menu alter' => 'page_manager_blog_menu_alter', + + // This is task uses 'context' handlers and must implement these to give the + // handler data it needs. + 'handler type' => 'context', + + // Allow this to be enabled or disabled: + 'disabled' => variable_get('page_manager_blog_disabled', TRUE), + 'enable callback' => 'page_manager_blog_enable', + 'access callback' => 'page_manager_blog_access_check', + ); +} + +/** + * Callback defined by page_manager_blog_page_manager_tasks(). + * + * Alter the node edit input so that node edit comes to us rather than the + * normal node edit process. + */ +function page_manager_blog_menu_alter(&$items, $task) { + if (variable_get('page_manager_blog_disabled', TRUE)) { + return; + } + + $callback = $items['blog']['page callback']; + // Override the node edit handler for our purpose. + if ($callback == 'blog_page_last' || variable_get('page_manager_override_anyway', FALSE)) { + $items['blog']['page callback'] = 'page_manager_blog'; + $items['blog']['file path'] = $task['path']; + $items['blog']['file'] = $task['file']; + } + else { + variable_set('page_manager_blog_disabled', TRUE); + if (!empty($GLOBALS['page_manager_enabling_blog'])) { + drupal_set_message(t('Page manager module is unable to enable blog because some other module already has overridden with %callback.', array('%callback' => $callback)), 'warning'); + } + return; + } + +} + +/** + * Entry point for our overridden node edit. + * + * This function asks its assigned handlers who, if anyone, would like + * to run with it. If no one does, it passes through to Drupal core's + * node edit, which is node_page_edit(). + */ +function page_manager_blog() { + // Load my task plugin + $task = page_manager_get_task('blog'); + + ctools_include('context'); + ctools_include('context-task-handler'); + $output = ctools_context_handler_render($task, '', array(), array()); + if ($output !== FALSE) { + return $output; + } + + module_load_include('inc', 'blog', 'blog.pages'); + $function = 'blog_page_last'; + foreach (module_implements('page_manager_override') as $module) { + $call = $module . '_page_manager_override'; + if (($rc = $call('blog')) && function_exists($rc)) { + $function = $rc; + break; + } + } + + // Otherwise, fall back. + return $function(); +} + +/** + * Callback to enable/disable the page from the UI. + */ +function page_manager_blog_enable($cache, $status) { + variable_set('page_manager_blog_disabled', $status); + // Set a global flag so that the menu routine knows it needs + // to set a message if enabling cannot be done. + if (!$status) { + $GLOBALS['page_manager_enabling_blog'] = TRUE; + } +} + +/** + * Callback to determine if a page is accessible. + * + * @param $task + * The task plugin. + * @param $subtask_id + * The subtask id + * @param $contexts + * The contexts loaded for the task. + * @return + * TRUE if the current user can access the page. + */ +function page_manager_blog_access_check($task, $subtask_id, $contexts) { + return user_access('access content'); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/blog_user.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/blog_user.inc new file mode 100644 index 0000000..351e4de --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/blog_user.inc @@ -0,0 +1,152 @@ + 'page', + 'title' => t('User blog'), + 'admin title' => t('User blog'), + 'admin description' => t('When enabled, this overrides the default Drupal behavior for displaying user blogs at blog/%user. If no variant is selected, the default Drupal user blog will be used.'), + 'admin path' => 'blog/%user', + + // Callback to add items to the page managertask administration form: + 'task admin' => 'page_manager_blog_user_task_admin', + + 'hook menu alter' => 'page_manager_blog_user_menu_alter', + + // This is task uses 'context' handlers and must implement these to give the + // handler data it needs. + 'handler type' => 'context', // handler type -- misnamed + 'get arguments' => 'page_manager_blog_user_get_arguments', + 'get context placeholders' => 'page_manager_blog_user_get_contexts', + + // Allow this to be enabled or disabled: + 'disabled' => variable_get('page_manager_blog_user_disabled', TRUE), + 'enable callback' => 'page_manager_blog_user_enable', + 'access callback' => 'page_manager_blog_user_access_check', + ); +} + +/** + * Callback defined by page_manager_blog_user_page_manager_tasks(). + * + * Alter the user view input so that user view comes to us rather than the + * normal user view process. + */ +function page_manager_blog_user_menu_alter(&$items, $task) { + if (variable_get('page_manager_blog_user_disabled', TRUE)) { + return; + } + + // Override the user view handler for our purpose. + if ($items['blog/%user_uid_optional']['page callback'] == 'blog_page_user' || variable_get('page_manager_override_anyway', FALSE)) { + $items['blog/%user_uid_optional']['page callback'] = 'page_manager_blog_user'; + $items['blog/%user_uid_optional']['file path'] = $task['path']; + $items['blog/%user_uid_optional']['file'] = $task['file']; + } + else { + // automatically disable this task if it cannot be enabled. + variable_set('page_manager_blog_user_disabled', TRUE); + if (!empty($GLOBALS['page_manager_enabling_blog_user'])) { + drupal_set_message(t('Page manager module is unable to enable blog/%user because some other module already has overridden with %callback.', array('%callback' => $items['blog/%user']['page callback'])), 'error'); + } + } +} + +/** + * Entry point for our overridden user view. + * + * This function asks its assigned handlers who, if anyone, would like + * to run with it. If no one does, it passes through to Drupal core's + * user view, which is user_page_view(). + */ +function page_manager_blog_user($account) { + // Load my task plugin: + $task = page_manager_get_task('blog_user'); + + // Load the account into a context. + ctools_include('context'); + ctools_include('context-task-handler'); + $contexts = ctools_context_handler_get_task_contexts($task, '', array($account)); + + $output = ctools_context_handler_render($task, '', $contexts, array($account->uid)); + if ($output !== FALSE) { + return $output; + } + + module_load_include('inc', 'blog', 'blog.pages'); + $function = 'blog_page_user'; + foreach (module_implements('page_manager_override') as $module) { + $call = $module . '_page_manager_override'; + if (($rc = $call('blog_user')) && function_exists($rc)) { + $function = $rc; + break; + } + } + + // Otherwise, fall back. + return $function($account); +} + +/** + * Callback to get arguments provided by this task handler. + * + * Since this is the node view and there is no UI on the arguments, we + * create dummy arguments that contain the needed data. + */ +function page_manager_blog_user_get_arguments($task, $subtask_id) { + return array( + array( + 'keyword' => 'user', + 'identifier' => t('User being viewed'), + 'id' => 1, + 'name' => 'uid', + 'settings' => array(), + ), + ); +} + +/** + * Callback to get context placeholders provided by this handler. + */ +function page_manager_blog_user_get_contexts($task, $subtask_id) { + return ctools_context_get_placeholders_from_argument(page_manager_blog_user_get_arguments($task, $subtask_id)); +} + +/** + * Callback to enable/disable the page from the UI. + */ +function page_manager_blog_user_enable($cache, $status) { + variable_set('page_manager_blog_user_disabled', $status); + + // Set a global flag so that the menu routine knows it needs + // to set a message if enabling cannot be done. + if (!$status) { + $GLOBALS['page_manager_enabling_blog_user'] = TRUE; + } +} + +/** + * Callback to determine if a page is accessible. + * + * @param $task + * The task plugin. + * @param $subtask_id + * The subtask id + * @param $contexts + * The contexts loaded for the task. + * @return + * TRUE if the current user can access the page. + */ +function page_manager_blog_user_access_check($task, $subtask_id, $contexts) { + $context = reset($contexts); + return blog_page_user_access($context->data); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/comment_reply.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/comment_reply.inc new file mode 100644 index 0000000..ffbafe4 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/comment_reply.inc @@ -0,0 +1,162 @@ + 'page', + + 'title' => t('Comment Reply page'), + 'admin title' => t('Comment Reply page'), + 'admin description' => t('When enabled, this overrides the default Drupal behavior for the site contact page at /contact. If no variant is selected, the default Drupal contact form will be used.'), + 'admin path' => 'comment/reply/%node', + + // Menu hooks so that we can alter the node/%node menu entry to point to us. + 'hook menu alter' => 'page_manager_comment_reply_menu_alter', + + // This is task uses 'context' handlers and must implement these to give the + // handler data it needs. + 'handler type' => 'context', + 'get arguments' => 'page_manager_comment_reply_get_arguments', + 'get context placeholders' => 'page_manager_comment_reply_get_contexts', + + // Allow this to be enabled or disabled: + 'disabled' => variable_get('page_manager_comment_reply_disabled', TRUE), + 'enable callback' => 'page_manager_comment_reply_enable', + 'access callback' => 'page_manager_comment_reply_check', + ); +} + +/** + * Callback to enable/disable the page from the UI. + */ +function page_manager_comment_reply_enable($cache, $status) { + variable_set('page_manager_comment_reply_disabled', $status); + // Set a global flag so that the menu routine knows it needs + // to set a message if enabling cannot be done. + if (!$status) { + $GLOBALS['page_manager_enabling_comment_reply'] = TRUE; + } +} + + +/** + * Entry point for our overridden comment. + * + */ +function page_manager_comment_reply_page($node, $pid = NULL){ + // Load my task plugin + $task = page_manager_get_task('comment_reply'); + + // Load the node into a context. + ctools_include('context'); + ctools_include('context-task-handler'); + + $contexts = ctools_context_handler_get_task_contexts($task, '', array($node, $pid)); + + if (array_key_exists('argument_cid_3', $contexts) && $contexts['argument_cid_3']->data->nid != $node->nid) { + // Attempting to reply to a comment not belonging to the current nid. + drupal_set_message(t('The comment you are replying to does not exist.'), 'error'); + drupal_goto("node/$node->nid"); + } + + $output = ctools_context_handler_render($task, '', $contexts, array($node, $pid)); + if ($output !== FALSE) { + return $output; + } + + $function = 'comment_reply'; + foreach (module_implements('page_manager_override') as $module) { + $call = $module . '_page_manager_override'; + if (($rc = $call('comment_reply')) && function_exists($rc)) { + $function = $rc; + break; + } + } + + module_load_include('inc', 'comment', 'comment.pages'); + return $function($node, $pid); +} + +/** + * Callback to get arguments provided by this task handler. + * + * Since this is the node view and there is no UI on the arguments, we + * create dummy arguments that contain the needed data. + */ +function page_manager_comment_reply_get_arguments($task, $subtask_id) { + return array( + array( + 'keyword' => 'node', + 'identifier' => t('Node being commented on'), + 'id' => 2, + 'name' => 'entity_id:node', + 'settings' => array(), + ), + array( + 'keyword' => 'comment', + 'identifier' => t('Comment being replied to'), + 'id' => 3, + 'name' => 'entity_id:comment', + 'settings' => array(), + ), + ); +} + +/** + * Callback to get context placeholders provided by this handler. + */ +function page_manager_comment_reply_get_contexts($task, $subtask_id) { + return ctools_context_get_placeholders_from_argument(page_manager_comment_reply_get_arguments($task, $subtask_id)); +} + +/** + * Callback defined by page_manager_node_view_page_manager_tasks(). + * + * Alter the node view input so that node view comes to us rather than the + * normal node view process. + */ +function page_manager_comment_reply_menu_alter(&$items, $task) { + if (variable_get('page_manager_comment_reply_disabled', TRUE)) { + return; + } + // Override the node view handler for our purpose. + $callback = $items['comment/reply/%node']['page callback']; + if ($callback == 'comment_reply' || variable_get('page_manager_override_anyway', FALSE)) { + $items['comment/reply/%node']['page callback'] = 'page_manager_comment_reply_page'; + $items['comment/reply/%node']['file path'] = $task['path']; + $items['comment/reply/%node']['file'] = $task['file']; + } + else { + // automatically disable this task if it cannot be enabled. + variable_set('page_manager_comment_reply_disabled', TRUE); + if (!empty($GLOBALS['page_manager_enabling_comment_reply'])) { + drupal_set_message(t('Page manager module is unable to enable comment/reply/%node because some other module already has overridden with %callback.', array('%callback' => $callback)), 'error'); + } + } + + // @todo override node revision handler as well? +} + +/** + * Callback to determine if a page is accessible. + * + * @param $task + * The task plugin. + * @param $subtask_id + * The subtask id + * @param $contexts + * The contexts loaded for the task. + * @return + * TRUE if the current user can access the page. + */ +function page_manager_comment_reply_access_check($task, $subtask_id, $contexts) { + $context = reset($contexts); + return TRUE; +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/contact_site.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/contact_site.inc new file mode 100644 index 0000000..f871869 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/contact_site.inc @@ -0,0 +1,129 @@ + 'page', + + 'title' => t('Site contact page'), + 'admin title' => t('Site contact page'), + 'admin description' => t('When enabled, this overrides the default Drupal behavior for the site contact page at /contact. If no variant is selected, the default Drupal contact form will be used.'), + 'admin path' => 'contact', + + // Menu hooks so that we can alter the node/%node menu entry to point to us. + 'hook menu alter' => 'page_manager_contact_site_menu_alter', + + // This is task uses 'context' handlers and must implement these to give the + // handler data it needs. + 'handler type' => 'context', + + // Allow this to be enabled or disabled: + 'disabled' => variable_get('page_manager_contact_site_disabled', TRUE), + 'enable callback' => 'page_manager_contact_site_enable', + 'access callback' => 'page_manager_contact_access_check', + ); +} + +/** + * Callback defined by page_manager_contact_site_page_manager_tasks(). + * + * Alter the node edit input so that node edit comes to us rather than the + * normal node edit process. + */ +function page_manager_contact_site_menu_alter(&$items, $task) { + if (variable_get('page_manager_contact_site_disabled', TRUE)) { + return; + } + + $callback = $items['contact']['page callback']; + if ($callback == 'drupal_get_form') { + $callback = $items['contact']['page arguments'][0]; + } + + // Override the node edit handler for our purpose. + if ($callback == 'contact_site_form' || variable_get('page_manager_override_anyway', FALSE)) { + $items['contact']['page callback'] = 'page_manager_contact_site'; + $items['contact']['file path'] = $task['path']; + $items['contact']['file'] = $task['file']; + } + else { + variable_set('page_manager_contact_site_disabled', TRUE); + if (!empty($GLOBALS['page_manager_enabling_contact_site'])) { + drupal_set_message(t('Page manager module is unable to enable contact because some other module already has overridden with %callback.', array('%callback' => $callback)), 'warning'); + } + return; + } + +} + +/** + * Entry point for our overridden site contact. + * + * This function asks its assigned handlers who, if anyone, would like + * to run with it. If no one does, it passes through to Drupal core's + * node edit, which is node_page_edit(). + */ +function page_manager_contact_site() { + // Load my task plugin + $task = page_manager_get_task('contact_site'); + + ctools_include('context'); + ctools_include('context-task-handler'); + $output = ctools_context_handler_render($task, '', array(), array()); + if ($output !== FALSE) { + return $output; + } + + module_load_include('inc', 'contact', 'contact.pages'); + $function = 'contact_site_form'; + foreach (module_implements('page_manager_override') as $module) { + $call = $module . '_page_manager_override'; + if (($rc = $call('contact_site')) && function_exists($rc)) { + $function = $rc; + break; + } + } + + // Otherwise, fall back. + if ($function == 'contact_site_form') { + return drupal_get_form($function); + } + + return $function(); +} + +/** + * Callback to enable/disable the page from the UI. + */ +function page_manager_contact_site_enable($cache, $status) { + variable_set('page_manager_contact_site_disabled', $status); + // Set a global flag so that the menu routine knows it needs + // to set a message if enabling cannot be done. + if (!$status) { + $GLOBALS['page_manager_enabling_contact_site'] = TRUE; + } +} + +/** + * Callback to determine if a page is accessible. + * + * @param $task + * The task plugin. + * @param $subtask_id + * The subtask id + * @param $contexts + * The contexts loaded for the task. + * @return + * TRUE if the current user can access the page. + */ +function page_manager_contact_access_check($task, $subtask_id, $contexts) { + return user_access('access site-wide contact form'); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/contact_user.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/contact_user.inc new file mode 100644 index 0000000..5c86816 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/contact_user.inc @@ -0,0 +1,155 @@ + 'page', + 'title' => t('User contact'), + 'admin title' => t('User contact'), + 'admin description' => t('When enabled, this overrides the default Drupal behavior for displaying the user contact form at user/%user/contact. If no variant is selected, the default Drupal user contact form will be used.'), + 'admin path' => 'user/%user/contact', + + // Callback to add items to the page managertask administration form: + 'task admin' => 'page_manager_contact_user_task_admin', + + 'hook menu alter' => 'page_manager_contact_user_menu_alter', + + // This is task uses 'context' handlers and must implement these to give the + // handler data it needs. + 'handler type' => 'context', // handler type -- misnamed + 'get arguments' => 'page_manager_contact_user_get_arguments', + 'get context placeholders' => 'page_manager_contact_user_get_contexts', + + // Allow this to be enabled or disabled: + 'disabled' => variable_get('page_manager_contact_user_disabled', TRUE), + 'enable callback' => 'page_manager_contact_user_enable', + 'access callback' => 'page_manager_contact_user_access_check', + ); +} + +/** + * Callback defined by page_manager_contact_user_page_manager_tasks(). + * + * Alter the user view input so that user view comes to us rather than the + * normal user view process. + */ +function page_manager_contact_user_menu_alter(&$items, $task) { + if (variable_get('page_manager_contact_user_disabled', TRUE)) { + return; + } + $callback = $items['user/%user/contact']['page callback']; + if ($callback == 'drupal_get_form') { + $callback = $items['user/%user/contact']['page arguments'][0]; + } + + // Override the user view handler for our purpose. + if ($callback == 'contact_personal_form' || variable_get('page_manager_override_anyway', FALSE)) { + $items['user/%user/contact']['page callback'] = 'page_manager_contact_user'; + $items['user/%user/contact']['file path'] = $task['path']; + $items['user/%user/contact']['file'] = $task['file']; + } + else { + // automatically disable this task if it cannot be enabled. + variable_set('page_manager_contact_user_disabled', TRUE); + if (!empty($GLOBALS['page_manager_enabling_contact_user'])) { + drupal_set_message(t('Page manager module is unable to enable user/%user/contact because some other module already has overridden with %callback.', array('%callback' => $callback)), 'error'); + } + } +} + +/** + * Entry point for our overridden user view. + * + * This function asks its assigned handlers who, if anyone, would like + * to run with it. If no one does, it passes through to Drupal core's + * user view, which is user_page_view(). + */ +function page_manager_contact_user($form_id, $account) { + // Load my task plugin: + $task = page_manager_get_task('contact_user'); + + // Load the account into a context. + ctools_include('context'); + ctools_include('context-task-handler'); + $contexts = ctools_context_handler_get_task_contexts($task, '', array($account)); + + $output = ctools_context_handler_render($task, '', $contexts, array($account->uid)); + if ($output !== FALSE) { + return $output; + } + + module_load_include('inc', 'contact', 'contact.pages'); + $function = 'contact_personal_form'; + foreach (module_implements('page_manager_override') as $module) { + $call = $module . '_page_manager_override'; + if (($rc = $call('contact_user')) && function_exists($rc)) { + $function = $rc; + break; + } + } + + // Otherwise, fall back. + return drupal_get_form($function, $account); +} + +/** + * Callback to get arguments provided by this task handler. + * + * Since this is the node view and there is no UI on the arguments, we + * create dummy arguments that contain the needed data. + */ +function page_manager_contact_user_get_arguments($task, $subtask_id) { + return array( + array( + 'keyword' => 'user', + 'identifier' => t('User being viewed'), + 'id' => 1, + 'name' => 'uid', + 'settings' => array(), + ), + ); +} + +/** + * Callback to get context placeholders provided by this handler. + */ +function page_manager_contact_user_get_contexts($task, $subtask_id) { + return ctools_context_get_placeholders_from_argument(page_manager_contact_user_get_arguments($task, $subtask_id)); +} + +/** + * Callback to enable/disable the page from the UI. + */ +function page_manager_contact_user_enable($cache, $status) { + variable_set('page_manager_contact_user_disabled', $status); + + // Set a global flag so that the menu routine knows it needs + // to set a message if enabling cannot be done. + if (!$status) { + $GLOBALS['page_manager_enabling_contact_user'] = TRUE; + } +} + +/** + * Callback to determine if a page is accessible. + * + * @param $task + * The task plugin. + * @param $subtask_id + * The subtask id + * @param $contexts + * The contexts loaded for the task. + * @return + * TRUE if the current user can access the page. + */ +function page_manager_blog_contact_user_access_check($task, $subtask_id, $contexts) { + $context = reset($contexts); + return _contact_personal_tab_access($context->data); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/node_edit.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/node_edit.inc new file mode 100644 index 0000000..61ef13a --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/node_edit.inc @@ -0,0 +1,185 @@ + 'page', + + 'title' => t('Node add/edit form'), + 'admin title' => t('Node add/edit form'), + 'admin description' => t('When enabled, this overrides the default Drupal behavior for adding or edit nodes at node/%node/edit and node/add/%node_type. If you add variants, you may use selection criteria such as node type or language or user access to provide different edit forms for nodes. If no variant is selected, the default Drupal node edit will be used.'), + 'admin path' => 'node/%node/edit', + + // Menu hooks so that we can alter the node/%node menu entry to point to us. + 'hook menu' => 'page_manager_node_edit_menu', + 'hook menu alter' => 'page_manager_node_edit_menu_alter', + + // This is task uses 'context' handlers and must implement these to give the + // handler data it needs. + 'handler type' => 'context', + 'get arguments' => 'page_manager_node_edit_get_arguments', + 'get context placeholders' => 'page_manager_node_edit_get_contexts', + + // Allow this to be enabled or disabled: + 'disabled' => variable_get('page_manager_node_edit_disabled', TRUE), + 'enable callback' => 'page_manager_node_edit_enable', + 'access callback' => 'page_manager_node_edit_access_check', + ); +} + +/** + * Callback defined by page_manager_node_edit_page_manager_tasks(). + * + * Alter the node edit input so that node edit comes to us rather than the + * normal node edit process. + */ +function page_manager_node_edit_menu_alter(&$items, $task) { + if (variable_get('page_manager_node_edit_disabled', TRUE)) { + return; + } + + $callback = $items['node/%node/edit']['page callback']; + // Override the node edit handler for our purpose. + if ($callback == 'node_page_edit' || variable_get('page_manager_override_anyway', FALSE)) { + $items['node/%node/edit']['page callback'] = 'page_manager_node_edit'; + $items['node/%node/edit']['file path'] = $task['path']; + $items['node/%node/edit']['file'] = $task['file']; + } + else { + variable_set('page_manager_node_edit_disabled', TRUE); + if (!empty($GLOBALS['page_manager_enabling_node_edit'])) { + drupal_set_message(t('Page manager module is unable to enable node/%node/edit because some other module already has overridden with %callback.', array('%callback' => $callback)), 'warning'); + } + return; + } + + // Also catch node/add handling: + foreach (node_type_get_types() as $type) { + $path = 'node/add/' . str_replace('_', '-', $type->type); + if ($items[$path]['page callback'] != 'node_add') { + if (!empty($GLOBALS['page_manager_enabling_node_edit'])) { + drupal_set_message(t('Page manager module is unable to override @path because some other module already has overridden with %callback. Node edit will be enabled but that edit path will not be overridden.', array('@path' => $path, '%callback' => $items[$path]['page callback'])), 'warning'); + } + continue; + } + + $items[$path]['page callback'] = 'page_manager_node_add'; + $items[$path]['file path'] = $task['path']; + $items[$path]['file'] = $task['file']; + // Why str_replace things back? + $items[$path]['page arguments'] = array($type->type); + } +} + +/** + * Entry point for our overridden node edit. + * + * This function asks its assigned handlers who, if anyone, would like + * to run with it. If no one does, it passes through to Drupal core's + * node edit, which is node_page_edit(). + */ +function page_manager_node_edit($node) { + // Load my task plugin + $task = page_manager_get_task('node_edit'); + + // Load the node into a context. + ctools_include('context'); + ctools_include('context-task-handler'); + $contexts = ctools_context_handler_get_task_contexts($task, '', array($node)); + + $arg = array(isset($node->nid) ? $node->nid : $node->type); + $output = ctools_context_handler_render($task, '', $contexts, $arg); + if ($output === FALSE) { + // Fall back! + // We've already built the form with the context, so we can't build it again, or + // form_clean_id will mess up our ids. But we don't really need to, either: + $context = reset($contexts); + $output = $context->form; + } + + return $output; +} + +/** + * Callback to handle the process of adding a node. + * + * This creates a basic $node and passes that off to page_manager_node_edit(). + * It is modelled after Drupal's node_add() function. + * + * Unlike node_add() we do not need to check node_access because that was + * already checked by the menu system. + */ +function page_manager_node_add($type) { + global $user; + + $types = node_type_get_types(); + + // Initialize settings: + $node = (object) array( + 'uid' => $user->uid, + 'name' => (isset($user->name) ? $user->name : ''), + 'type' => $type, + 'language' => LANGUAGE_NONE, + ); + + drupal_set_title(t('Create @name', array('@name' => $types[$type]->name)), PASS_THROUGH); + return page_manager_node_edit($node); +} + +/** + * Callback to get arguments provided by this task handler. + * + * Since this is the node edit and there is no UI on the arguments, we + * create dummy arguments that contain the needed data. + */ +function page_manager_node_edit_get_arguments($task, $subtask_id) { + return array( + array( + 'keyword' => 'node', + 'identifier' => t('Node being edited'), + 'id' => 1, + 'name' => 'node_edit', + 'settings' => array(), + ), + ); +} + +/** + * Callback to get context placeholders provided by this handler. + */ +function page_manager_node_edit_get_contexts($task, $subtask_id) { + return ctools_context_get_placeholders_from_argument(page_manager_node_edit_get_arguments($task, $subtask_id)); +} + +/** + * Callback to enable/disable the page from the UI. + */ +function page_manager_node_edit_enable($cache, $status) { + variable_set('page_manager_node_edit_disabled', $status); + // Set a global flag so that the menu routine knows it needs + // to set a message if enabling cannot be done. + if (!$status) { + $GLOBALS['page_manager_enabling_node_edit'] = TRUE; + } +} + +/** + * Callback to determine if a page is accessible. + * + * @param $task + * The task plugin. + * @param $subtask_id + * The subtask id + * @param $contexts + * The contexts loaded for the task. + * @return + * TRUE if the current user can access the page. + */ +function page_manager_node_edit_access_check($task, $subtask_id, $contexts) { + $context = reset($contexts); + return node_access('update', $context->data); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/node_view.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/node_view.inc new file mode 100644 index 0000000..89a2912 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/node_view.inc @@ -0,0 +1,166 @@ + 'page', + + 'title' => t('Node template'), + + 'admin title' => t('Node template'), + 'admin description' => t('When enabled, this overrides the default Drupal behavior for displaying nodes at node/%node. If you add variants, you may use selection criteria such as node type or language or user access to provide different views of nodes. If no variant is selected, the default Drupal node view will be used. This page only affects nodes viewed as pages, it will not affect nodes viewed in lists or at other locations. Also please note that if you are using pathauto, aliases may make a node to be somewhere else, but as far as Drupal is concerned, they are still at node/%node.'), + 'admin path' => 'node/%node', + + // Menu hooks so that we can alter the node/%node menu entry to point to us. + 'hook menu' => 'page_manager_node_view_menu', + 'hook menu alter' => 'page_manager_node_view_menu_alter', + + // This is task uses 'context' handlers and must implement these to give the + // handler data it needs. + 'handler type' => 'context', + 'get arguments' => 'page_manager_node_view_get_arguments', + 'get context placeholders' => 'page_manager_node_view_get_contexts', + + // Allow this to be enabled or disabled: + 'disabled' => variable_get('page_manager_node_view_disabled', TRUE), + 'enable callback' => 'page_manager_node_view_enable', + 'access callback' => 'page_manager_node_view_access_check', + ); +} + +/** + * Callback defined by page_manager_node_view_page_manager_tasks(). + * + * Alter the node view input so that node view comes to us rather than the + * normal node view process. + */ +function page_manager_node_view_menu_alter(&$items, $task) { + if (variable_get('page_manager_node_view_disabled', TRUE)) { + return; + } + + // Override the node view handler for our purpose. + $callback = $items['node/%node']['page callback']; + if ($callback == 'node_page_view' || variable_get('page_manager_override_anyway', FALSE)) { + $items['node/%node']['page callback'] = 'page_manager_node_view_page'; + $items['node/%node']['file path'] = $task['path']; + $items['node/%node']['file'] = $task['file']; + } + else { + // automatically disable this task if it cannot be enabled. + variable_set('page_manager_node_view_disabled', TRUE); + if (!empty($GLOBALS['page_manager_enabling_node_view'])) { + drupal_set_message(t('Page manager module is unable to enable node/%node because some other module already has overridden with %callback.', array('%callback' => $callback)), 'error'); + } + } + + // @todo override node revision handler as well? +} + +/** + * Entry point for our overridden node view. + * + * This function asks its assigned handlers who, if anyone, would like + * to run with it. If no one does, it passes through to Drupal core's + * node view, which is node_page_view(). + */ +function page_manager_node_view_page($node) { + // Load my task plugin + $task = page_manager_get_task('node_view'); + + // Load the node into a context. + ctools_include('context'); + ctools_include('context-task-handler'); + + // Load all contexts. + $contexts = ctools_context_handler_get_task_contexts($task, '', array($node)); + + // Build the full output using the configured CTools plugin. + $output = ctools_context_handler_render($task, '', $contexts, array($node->nid)); + if ($output !== FALSE) { + node_tag_new($node); + return $output; + } + + // Try loading an override plugin. + foreach (module_implements('page_manager_override') as $module) { + $call = $module . '_page_manager_override'; + if (($rc = $call('node_view')) && function_exists($rc)) { + return $rc($node); + } + } + + // Prepare the node to be displayed so all of the regular hooks are triggered. + $default_output = node_page_view($node); + + // Otherwise, fall back to the default output generated by node_page_view(). + return $default_output; +} + +/** + * Callback to get arguments provided by this task handler. + * + * Since this is the node view and there is no UI on the arguments, we + * create dummy arguments that contain the needed data. + */ +function page_manager_node_view_get_arguments($task, $subtask_id) { + return array( + array( + 'keyword' => 'node', + 'identifier' => t('Node being viewed'), + 'id' => 1, + 'name' => 'entity_id:node', + 'settings' => array(), + ), + ); +} + +/** + * Callback to get context placeholders provided by this handler. + */ +function page_manager_node_view_get_contexts($task, $subtask_id) { + return ctools_context_get_placeholders_from_argument(page_manager_node_view_get_arguments($task, $subtask_id)); +} + +/** + * Callback to enable/disable the page from the UI. + */ +function page_manager_node_view_enable($cache, $status) { + variable_set('page_manager_node_view_disabled', $status); + + // Set a global flag so that the menu routine knows it needs + // to set a message if enabling cannot be done. + if (!$status) { + $GLOBALS['page_manager_enabling_node_view'] = TRUE; + } +} + +/** + * Callback to determine if a page is accessible. + * + * @param $task + * The task plugin. + * @param $subtask_id + * The subtask id + * @param $contexts + * The contexts loaded for the task. + * @return + * TRUE if the current user can access the page. + */ +function page_manager_node_view_access_check($task, $subtask_id, $contexts) { + $context = reset($contexts); + return node_access('view', $context->data); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/page.admin.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/page.admin.inc new file mode 100644 index 0000000..97bd37b --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/page.admin.inc @@ -0,0 +1,1521 @@ + $access_callback, + 'access arguments' => $access_arguments, + 'file' => 'plugins/tasks/page.admin.inc', + ); + + $items['admin/structure/pages/add'] = array( + 'title' => 'Add custom page', + 'page callback' => 'page_manager_page_add_subtask', + 'page arguments' => array(), + 'type' => MENU_LOCAL_ACTION, + ) + $base; + + $items['admin/structure/pages/import'] = array( + 'title' => 'Import page', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('page_manager_page_import_subtask', 'page'), + 'type' => MENU_LOCAL_ACTION, + ) + $base; + if ($access_callback == 'user_access') { + $items['admin/structure/pages/import']['access callback'] = 'ctools_access_multiperm'; + $items['admin/structure/pages/import']['access arguments'][] = 'use ctools import'; + } + + // AJAX callbacks for argument modal. + $items['admin/structure/pages/argument'] = array( + 'page callback' => 'page_manager_page_subtask_argument_ajax', + 'type' => MENU_CALLBACK, + ) + $base; + + // Add menu entries for each subtask + foreach (page_manager_page_load_all() as $subtask_id => $subtask) { + if (!empty($subtask->disabled)) { + continue; + } + + if (!isset($subtask->access['type'])) { + $subtask->access['type'] = 'none'; + } + if (!isset($subtask->access['settings'])) { + $subtask->access['settings'] = NULL; + } + + $path = array(); + $page_arguments = array((string) $subtask_id); + $access_arguments = array($subtask->access); + $load_arguments = array($subtask_id, '%index', '%map'); + + // Replace named placeholders with our own placeholder to load contexts. + $position = 0; + + foreach (explode('/', $subtask->path) as $bit) { + // Remove things like double slashes completely. + if (!isset($bit) || $bit === '') { + continue; + } + + if ($bit[0] == '%' && $bit != '%') { + $placeholder = '%pm_arg'; + + // Chop off that %. + $name = substr($bit, 1); + + // Check to see if the argument plugin wants to use a different + // placholder. This will allow to_args. + if (!empty($subtask->arguments[$name])) { + ctools_include('context'); + if (!empty($subtask->arguments[$name]['name'])) { + $plugin = ctools_get_argument($subtask->arguments[$name]['name']); + if (isset($plugin['path placeholder'])) { + if (function_exists($plugin['path placeholder'])) { + $placeholder = $plugin['path placeholder']($subtask->arguments[$name]); + } + else { + $placeholder = $plugin['path placeholder']; + } + } + } + } + // If an argument, swap it out with our argument loader and make sure + // the argument gets passed through to the page callback. + $path[] = $placeholder; + $page_arguments[] = $position; + $access_arguments[] = $position; + } + else if ($bit[0] != '!') { + $path[] = $bit; + } + + // Increment position. We do it like this to skip empty items that + // could happen from erroneous paths like: this///that + $position++; + } + + $menu_path = implode('/', $path); + + $items[$menu_path] = page_manager_page_menu_item($task, $subtask->menu, $access_arguments, $page_arguments, $load_arguments); + + // Add a parent menu item if one is configured. + if (isset($subtask->menu['type']) && $subtask->menu['type'] == 'default tab') { + array_pop($path); + $parent_path = implode('/', $path); + $items[$parent_path] = page_manager_page_menu_item($task, $subtask->menu['parent'], $access_arguments, $page_arguments, $load_arguments); + } + } +} + +/** + * Create a menu item for page manager pages. + * + * @param $menu + * The configuration to use. It will contain a type, and depending on the + * type may also contain weight, title and name. These are presumed to have + * been configured from the UI. + * @param $access_arguments + * Arguments that go with ctools_access_menu; it should be loaded with + * the access plugin type, settings, and positions of any arguments that + * may produce contexts. + * @param $page_arguments + * This should be seeded with the subtask name for easy loading and like + * the access arguments above should contain positions of arguments so + * that the menu system passes contexts through. + * @param $load_arguments + * Arguments to send to the arg loader; should be the subtask id and '%index'. + */ +function page_manager_page_menu_item($task, $menu, $access_arguments, $page_arguments, $load_arguments) { + $item = array( + 'access callback' => 'ctools_access_menu', + 'access arguments' => $access_arguments, + 'page callback' => 'page_manager_page_execute', + 'page arguments' => $page_arguments, + 'load arguments' => $load_arguments, + 'file' => 'plugins/tasks/page.inc', + ); + + if (isset($menu['title'])) { + $item['title'] = $menu['title']; + } + if (isset($menu['weight'])) { + $item['weight'] = $menu['weight']; + } + + if (empty($menu['type'])) { + $menu['type'] = 'none'; + } + + switch ($menu['type']) { + case 'none': + default: + $item['type'] = MENU_CALLBACK; + break; + + case 'normal': + $item['type'] = MENU_NORMAL_ITEM; + // Insert item into the proper menu + $item['menu_name'] = $menu['name']; + break; + + case 'tab': + $item['type'] = MENU_LOCAL_TASK; + break; + + case 'action': + $item['type'] = MENU_LOCAL_ACTION; + break; + + case 'default tab': + $item['type'] = MENU_DEFAULT_LOCAL_TASK; + break; + } + + return $item; +} + +/** + * Page callback to add a subtask. + */ +function page_manager_page_add_subtask($task_name = NULL, $step = NULL) { + ctools_include('context'); + $task = page_manager_get_task('page'); + $task_handler_plugins = page_manager_get_task_handler_plugins($task); + if (empty($task_handler_plugins)) { + drupal_set_message(t('There are currently no variants available and a page may not be added. Perhaps you need to install the Panels module to get a variant?'), 'error'); + return ' '; + } + + $form_info = array( + 'id' => 'page_manager_add_page', + 'show trail' => TRUE, + 'show back' => TRUE, + 'show return' => FALSE, + 'next callback' => 'page_manager_page_add_subtask_next', + 'finish callback' => 'page_manager_page_add_subtask_finish', + 'return callback' => 'page_manager_page_add_subtask_finish', + 'cancel callback' => 'page_manager_page_add_subtask_cancel', + 'add order' => array( + 'basic' => t('Basic settings'), + 'argument' => t('Argument settings'), + 'access' => t('Access control'), + 'menu' => t('Menu settings'), + ), + 'forms' => array( + 'basic' => array( + 'form id' => 'page_manager_page_form_basic', + ), + 'access' => array( + 'form id' => 'page_manager_page_form_access', + ), + 'menu' => array( + 'form id' => 'page_manager_page_form_menu', + ), + 'argument' => array( + 'form id' => 'page_manager_page_form_argument', + ), + ), + ); + + if ($task_name) { + $page = page_manager_get_page_cache($task_name); + if (empty($page)) { + return MENU_NOT_FOUND; + } + + $form_info['path'] = "admin/structure/pages/add/$task_name/%step"; + } + else { + $new_page = page_manager_page_new(); + $new_page->name = NULL; + + $page = new stdClass(); + page_manager_page_new_page_cache($new_page, $page); + $form_info['path'] = 'admin/structure/pages/add/%task_name/%step'; + } + + if ($step && $step != 'basic') { + $handler_plugin = page_manager_get_task_handler($page->handler); + + $form_info['forms'] += $handler_plugin['forms']; + + if (isset($page->forms)) { + foreach ($page->forms as $id) { + if (isset($form_info['add order'][$id])) { + $form_info['order'][$id] = $form_info['add order'][$id]; + } + else if (isset($handler_plugin['add features'][$id])) { + $form_info['order'][$id] = $handler_plugin['add features'][$id]; + } + else if (isset($handler_plugin['required forms'][$id])) { + $form_info['order'][$id] = $handler_plugin['required forms'][$id]; + } + } + } + else { + $form_info['order'] = $form_info['add order']; + } + + // This means we just submitted our form from the default list + // of steps, which we've traded in for a newly generated list of + // steps above. We need to translate this 'next' step into what + // our questions determined would be next. + if ($step == 'next') { + $keys = array_keys($form_info['order']); + // get rid of 'basic' from the list of forms. + array_shift($keys); + $step = array_shift($keys); + + // If $step == 'basic' at this point, we were not presented with any + // additional forms at all. Let's just save and go! + if ($step == 'basic') { + page_manager_save_page_cache($page); + // Redirect to the new page's task handler editor. + drupal_goto(page_manager_edit_url($page->task_name)); + } + } + } + else { + $form_info['show trail'] = FALSE; + $form_info['order'] = array( + 'basic' => t('Basic settings'), + 'next' => t('A meaningless second page'), + ); + } + + ctools_include('wizard'); + $form_state = array( + 'task' => $task, + 'subtask' => $page->subtask, + 'page' => &$page, + 'type' => 'add', + 'task_id' => 'page', + 'task_name' => $page->task_name, + 'creating' => TRUE, + ); + + if (!empty($page->handlers)) { + $keys = array_keys($page->handlers); + $key = array_shift($keys); + $form_state['handler'] = &$page->handlers[$key]; + $form_state['handler_id'] = $key; + } + + $output = ctools_wizard_multistep_form($form_info, $step, $form_state); + + if (!$output) { + // redirect. + drupal_redirect_form(array(), $form_state['redirect']); + } + + return $output; +} + +/** + * Callback generated when the add page process is finished. + */ +function page_manager_page_add_subtask_finish(&$form_state) { + $page = &$form_state['page']; + // Update the cache with changes. + page_manager_set_page_cache($page); + + $handler = $form_state['handler']; + $handler_plugin = page_manager_get_task_handler($handler->handler); + + // Redirect to the new page's task handler editor. + if (isset($handler_plugin['add finish'])) { + $form_state['redirect'] = page_manager_edit_url($page->task_name, array('handlers', $handler->name, $handler_plugin['add finish'])); + } + else { + $form_state['redirect'] = page_manager_edit_url($page->task_name); + } + return; +} + +/** + * Callback generated when the 'next' button is clicked. + * + * All we do here is store the cache. + */ +function page_manager_page_add_subtask_next(&$form_state) { + if (empty($form_state['task_name']) || $form_state['task_name'] == 'page') { + // We may not have known the path to go next, because we didn't yet know the + // task name. This fixes that. + $form_state['form_info']['path'] = str_replace('%task_name', $form_state['page']->task_name, $form_state['form_info']['path']); + + $form_state['redirect'] = ctools_wizard_get_path($form_state['form_info'], $form_state['clicked_button']['#next']); + } + + // Update the cache with changes. + page_manager_set_page_cache($form_state['page']); +} + +/** + * Callback generated when the 'cancel' button is clicked. + * + * All we do here is clear the cache. + */ +function page_manager_page_add_subtask_cancel(&$form_state) { + // Wipe all our stored changes. + if (isset($form_state['page']->task_name)) { + page_manager_clear_page_cache($form_state['page']->task_name); + } +} + +/** + * Basic settings form for a page manager page. + */ +function page_manager_page_form_basic($form, &$form_state) { + $page = &$form_state['page']->subtask['subtask']; + $task = $form_state['task']; + + $form['admin_title'] = array( + '#type' => 'textfield', + '#title' => t('Administrative title'), + '#description' => t('The name of this page. This will appear in the administrative interface to easily identify it.'), + '#default_value' => $page->admin_title, + ); + + $form['name'] = array( + '#type' => 'machine_name', + '#title' => t('Machine name'), + '#machine_name' => array( + 'exists' => 'page_manager_page_load', + 'source' => array('admin_title'), + ), + '#description' => t('The machine readable name of this page. It must be unique, and it must contain only alphanumeric characters and underscores. Once created, you will not be able to change this value!'), + '#default_value' => $page->name, + ); + + if (isset($page->pid) || empty($form_state['creating'])) { + $form['name']['#disabled'] = TRUE; + $form['name']['#value'] = $page->name; + } + + $form['admin_description'] = array( + '#type' => 'textarea', + '#title' => t('Administrative description'), + '#description' => t('A description of what this page is, does or is for, for administrative use.'), + '#default_value' => $page->admin_description, + ); + + // path + $form['path'] = array( + '#type' => 'textfield', + '#title' => t('Path'), + '#description' => t('The URL path to get to this page. You may create named placeholders for variable parts of the path by using %name for required elements and !name for optional elements. For example: "node/%node/foo", "forum/%forum" or "dashboard/!input". These named placeholders can be turned into contexts on the arguments form.'), + '#default_value' => $page->path, + '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q='), + ); + + $frontpage = variable_get('site_frontpage', 'node'); + + $path = array(); + if ($page->path) { + foreach (explode('/', $page->path) as $bit) { + if ($bit[0] != '!') { + $path[] = $bit; + } + } + } + + $path = implode('/', $path); + + if (empty($path) || $path != $frontpage) { + $form['frontpage'] = array( + '#type' => 'checkbox', + '#default_value' => !empty($page->make_frontpage), + '#title' => t('Make this your site home page.'), + '#description' => t('To set this panel as your home page you must create a unique path name with no % placeholders in the path. The site home page is currently set to %homepage on the !siteinfo configuration form.', array('!siteinfo' => l(t('Site Information'), 'admin/config/system/site-information'), '%homepage' => '/' . $frontpage)), + ); + $form['admin_paths'] = array( + '#type' => 'checkbox', + '#default_value' => !empty($page->conf['admin_paths']), + '#title' => t('Use this page in an admin overlay.'), + '#description' => t('Admin overlays are used in many places in Drupal 7 and administrative custom pages should probably utilize this feature.'), + ); + } + else if ($path == $frontpage) { + $form['frontpage_markup'] = array( + '#value' => '' . t('This page is currently set to be your site home page. This can be modified on the !siteinfo configuration form.', array('!siteinfo' => l(t('Site Information'), 'admin/settings/site-information'))) . '', + ); + + $form['frontpage'] = array( + '#type' => 'value', + '#value' => TRUE, + ); + } + + if (!isset($page->pid) && !empty($form_state['creating'])) { + $features['default'] = array( + 'access' => t('Access control'), + 'menu' => t('Visible menu item'), + ); + + module_load_include('inc', 'page_manager', 'page_manager.admin'); + $form = page_manager_handler_add_form($form, $form_state, $features); + } + + return $form; +} + +function page_manager_page_form_basic_validate_filter($value) { + return $value === -1; +} + +/** + * Validate the basic form. + */ +function page_manager_page_form_basic_validate(&$form, &$form_state) { + // Ensure path is unused by other pages. + $page = $form_state['page']->subtask['subtask']; + $name = !empty($form_state['values']['name']) ? $form_state['values']['name'] : $page->name; + if (empty($name)) { + form_error($form['name'], t('Name is required.')); + } + + // If this is new, make sure the name is unique: + if (empty($page->name)) { + $test = page_manager_page_load($name); + if ($test) { + form_error($form['name'], t('That name is used by another page: @page', array('@page' => $test->admin_title))); + } + + // Ensure name fits the rules: + if (preg_match('/[^a-zA-Z0-9_]/', $form_state['values']['name'])) { + form_error($form['name'], t('Page name must be alphanumeric or underscores only.')); + } + } + + $pages = page_manager_page_load_all(); + foreach ($pages as $test) { + if ($test->name != $name && $test->path == $form_state['values']['path'] && empty($test->disabled)) { + form_error($form['path'], t('That path is used by another page: @page', array('@page' => $test->admin_title))); + } + } + + // Ensure path is unused by things NOT pages. We do the double check because + // we're checking against our page callback. + $path = array(); + if (empty($form_state['values']['path'])) { + form_error($form['path'], t('Path is required.')); + // stop processing here if there is no path. + return; + } + + $found = FALSE; + $error = FALSE; + foreach (explode('/', $form_state['values']['path']) as $position => $bit) { + if (!isset($bit) || $bit === '') { + continue; + } + + if ($bit == '%' || $bit == '!') { + form_error($form['path'], t('You cannot have an unnamed placeholder (% or ! by itself). Please name your placeholder by adding a short piece of descriptive text to the % or !, such as %user or %node.')); + } + + if ($bit[0] == '%') { + if ($found) { + form_error($form['path'], t('You cannot have a dynamic path element after an optional path element.')); + } + + if ($position == 0) { + form_error($form['path'], t('The first element in a path may not be dynamic.')); + } + + $path[] = '%'; + } + else if ($bit[0] == '!') { + $found = TRUE; + } + else { + if ($found) { + form_error($form['path'], t('You cannot have a static path element after an optional path element.')); + } + $path[] = $bit; + } + } + + // Check to see if something that isn't a page manager page is using the path. + $path = implode('/', $path); + $result = db_query('SELECT * FROM {menu_router} WHERE path = :path', array(':path' => $path)); + foreach ($result as $router) { + if ($router->page_callback != 'page_manager_page_execute') { + form_error($form['path'], t('That path is already in use. This system cannot override existing paths.')); + } + } + + // Ensure the path is not already an alias to something else. + if (strpos($path, '%') === FALSE) { + $alias = db_query('SELECT alias, source FROM {url_alias} WHERE alias = :path', array(':path' => $path))->fetchObject(); + if ($alias) { + form_error($form['path'], t('That path is currently assigned to be an alias for @alias. This system cannot override existing aliases.', array('@alias' => $alias->source))); + } + } + else { + if (!empty($form_state['values']['frontpage'])) { + form_error($form['path'], t('You cannot make this page your site home page if it uses % placeholders.')); + } + } + + // Ensure path is properly formed. + $args = page_manager_page_get_named_arguments($form_state['values']['path']); + if ($invalid_args = array_filter($args, 'page_manager_page_form_basic_validate_filter')) { + foreach ($invalid_args as $arg => $position) { + form_error($form['path'], t('Duplicated argument %arg', array('%arg' => $arg))); + } + } + + if (isset($args['%'])) { + form_error($form['path'], t('Invalid arg %. All arguments must be named with keywords.')); + } + + $form_state['arguments'] = $args; +} + +/** + * Store the values from the basic settings form. + */ +function page_manager_page_form_basic_submit(&$form, &$form_state) { + $page = &$form_state['page']->subtask['subtask']; + $cache = &$form_state['page']; + + // If this is a new thing, then we have to do a bunch of setup to create + // the cache record with the right ID and some basic data that we could + // not know until we asked the user some questions. + if (!isset($page->pid) && !empty($form_state['creating'])) { + // Update the data with our new name. + $page->name = $form_state['values']['name']; + $form_state['page']->task_name = page_manager_make_task_name($form_state['task_id'], $page->name); + $cache->handler = $form_state['values']['handler']; + $cache->subtask_id = $page->name; + $plugin = page_manager_get_task_handler($cache->handler); + + // If they created and went back, there might be old, dead handlers + // that are not going to be added. + // + // Remove them: + $cache->handlers = array(); + $cache->handler_info = array(); + + // Create a new handler. + $handler = page_manager_new_task_handler($plugin); + $title = !empty($form_state['values']['title']) ? $form_state['values']['title'] : $plugin['title']; + page_manager_handler_add_to_page($cache, $handler, $title); + + // Figure out which forms to present them with + $cache->forms = array(); + $cache->forms[] = 'basic'; // This one is always there. + if (!empty($form_state['arguments'])) { + $cache->forms[] = 'argument'; + } + + $features = $form_state['values']['features']; + $cache->forms = array_merge($cache->forms, array_keys(array_filter($features['default']))); + if (isset($features[$form_state['values']['handler']])) { + $cache->forms = array_merge($cache->forms, array_keys(array_filter($features[$form_state['values']['handler']]))); + } + + if (isset($plugin['required forms'])) { + $cache->forms = array_merge($cache->forms, array_keys($plugin['required forms'])); + } + } + + $page->admin_title = $form_state['values']['admin_title']; + $cache->subtask['admin title'] = check_plain($form_state['values']['admin_title']); + + $page->admin_description = $form_state['values']['admin_description']; + $cache->subtask['admin description'] = filter_xss_admin($form_state['values']['admin_description']); + + if ($page->path != $form_state['values']['path']) { + $page->path = $form_state['values']['path']; + page_manager_page_recalculate_arguments($page); + $cache->path_changed = TRUE; + } + + $page->make_frontpage = !empty($form_state['values']['frontpage']); + $page->conf['admin_paths'] = !empty($form_state['values']['admin_paths']); +} + +/** + * Form to handle menu item controls. + */ +function page_manager_page_form_menu($form, &$form_state) { + ctools_include('dependent'); + $form['menu'] = array( + '#prefix' => '
    ', + '#suffix' => '
    ', + '#tree' => TRUE, + ); + + $menu = $form_state['page']->subtask['subtask']->menu; + if (empty($menu)) { + $menu = array( + 'type' => 'none', + 'title' => '', + 'weight' => 0, + 'name' => 'navigation', + 'parent' => array( + 'type' => 'none', + 'title' => '', + 'weight' => 0, + 'name' => 'navigation', + ), + ); + } + + $form['menu']['type'] = array( + '#title' => t('Type'), + '#type' => 'radios', + '#options' => array( + 'none' => t('No menu entry'), + 'normal' => t('Normal menu entry'), + 'tab' => t('Menu tab'), + 'default tab' => t('Default menu tab'), + 'action' => t('Local action'), + ), + '#default_value' => $menu['type'], + ); + + $form['menu']['title'] = array( + '#title' => t('Title'), + '#type' => 'textfield', + '#default_value' => $menu['title'], + '#description' => t('If set to normal or tab, enter the text to use for the menu item.'), + '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab', 'action')), + ); + + list($major, $minor) = explode('.', VERSION, 2); + + // Only display the menu selector if menu module is enabled. + if (module_exists('menu')) { + $form['menu']['name'] = array( + '#title' => t('Menu'), + '#type' => 'select', + '#options' => menu_get_menus(), + '#default_value' => $menu['name'], + '#description' => t('Insert item into an available menu.'), + '#dependency' => array('radio:menu[type]' => array('normal')), + ); + } + else { + $form['menu']['name'] = array( + '#type' => 'value', + '#value' => $menu['name'], + ); + $form['menu']['markup'] = array( + '#value' => t('Menu selection requires the activation of menu module.'), + ); + } + $form['menu']['weight'] = array( + '#title' => t('Weight'), + '#type' => 'textfield', + '#default_value' => isset($menu['weight']) ? $menu['weight'] : 0, + '#description' => t('The lower the weight the higher/further left it will appear.'), + '#dependency' => array('radio:menu[type]' => array('normal', 'tab', 'default tab', 'action')), + ); + + $form['menu']['parent']['type'] = array( + '#prefix' => '
    ', + '#suffix' => '
    ', + '#title' => t('Parent menu item'), + '#type' => 'radios', + '#options' => array('none' => t('No menu entry'), 'normal' => t('Normal menu item'), 'tab' => t('Menu tab')), + '#default_value' => $menu['parent']['type'], + '#description' => t('When providing a menu item as a default tab, Drupal needs to know what the parent menu item of that tab will be. Sometimes the parent will already exist, but other times you will need to have one created. The path of a parent item will always be the same path with the last part left off. i.e, if the path to this view is foo/bar/baz, the parent path would be foo/bar.'), + '#dependency' => array('radio:menu[type]' => array('default tab')), + ); + $form['menu']['parent']['title'] = array( + '#title' => t('Parent item title'), + '#type' => 'textfield', + '#default_value' => $menu['parent']['title'], + '#description' => t('If creating a parent menu item, enter the title of the item.'), + '#dependency' => array('radio:menu[type]' => array('default tab'), 'radio:menu[parent][type]' => array('normal', 'tab')), + '#dependency_count' => 2, + ); + // Only display the menu selector if menu module is enabled. + if (module_exists('menu')) { + $form['menu']['parent']['name'] = array( + '#title' => t('Parent item menu'), + '#type' => 'select', + '#options' => menu_get_menus(), + '#default_value' => $menu['parent']['name'], + '#description' => t('Insert item into an available menu.'), + '#dependency' => array('radio:menu[type]' => array('default tab'), 'radio:menu[parent][type]' => array('normal')), + '#dependency_count' => 2, + ); + } + else { + $form['menu']['parent']['name'] = array( + '#type' => 'value', + '#value' => $menu['parent']['name'], + ); + } + $form['menu']['parent']['weight'] = array( + '#title' => t('Parent weight'), + '#type' => 'textfield', + '#default_value' => $menu['parent']['weight'], + '#size' => 5, + '#description' => t('Enter the weight of the parent item. The lower the number, the more to the left it will be.'), + '#dependency' => array('radio:menu[type]' => array('default tab'), 'radio:menu[parent][type]' => array('tab', 'normal')), + '#dependency_count' => 2, + ); + + return $form; +} + +/** + * Validate handler for the menu form for add/edit page task. + */ +function page_manager_page_form_menu_validate(&$form, &$form_state) { + // If setting a 'normal' menu entry, make sure that any placeholders + // support the to_arg stuff. + + if ($form_state['values']['menu']['type'] == 'normal') { + $page = $form_state['page']->subtask['subtask']; + + foreach (explode('/', $page->path) as $bit) { + if (!isset($bit) || $bit === '') { + continue; + } + + if ($bit[0] == '%') { + // Chop off that %. + $name = substr($bit, 1); + + // Check to see if the argument plugin allows to arg: + if (!empty($page->arguments[$name])) { + ctools_include('context'); + $plugin = ctools_get_argument($page->arguments[$name]['name']); + if (!empty($plugin['path placeholder to_arg'])) { + continue; + } + } + + form_error($form['menu']['type'], t('Paths with non optional placeholders cannot be used as normal menu items unless the selected argument handler provides a default argument to use for the menu item.')); + return; + } + } + } +} + +/** + * Submit handler for the menu form for add/edit page task. + */ +function page_manager_page_form_menu_submit(&$form, &$form_state) { + $form_state['page']->subtask['subtask']->menu = $form_state['values']['menu']; + $form_state['page']->path_changed = TRUE; +} + +/** + * Form to handle menu item controls. + */ +function page_manager_page_form_access($form, &$form_state) { + ctools_include('context'); + $form_state['module'] = 'page_manager_page'; + $form_state['callback argument'] = $form_state['page']->task_name; + $form_state['access'] = $form_state['page']->subtask['subtask']->access; + $form_state['no buttons'] = TRUE; + $form_state['contexts'] = array(); + + // Load contexts based on argument data: + if ($arguments = _page_manager_page_get_arguments($form_state['page']->subtask['subtask'])) { + $form_state['contexts'] = ctools_context_get_placeholders_from_argument($arguments); + } + + ctools_include('context-access-admin'); + $form = ctools_access_admin_form($form, $form_state); + + return $form; +} + +/** + * Submit handler to deal with access control changes. + */ +function page_manager_page_form_access_submit(&$form, &$form_state) { + $form_state['page']->subtask['subtask']->access['logic'] = $form_state['values']['logic']; + $form_state['page']->path_changed = TRUE; +} + +/** + * Form to handle assigning argument handlers to named arguments. + */ +function page_manager_page_form_argument($form, &$form_state) { + $page = &$form_state['page']->subtask['subtask']; + $path = $page->path; + + $arguments = page_manager_page_get_named_arguments($path); + + $form['table'] = array( + '#theme' => 'page_manager_page_form_argument_table', + '#page-manager-path' => $path, + 'argument' => array(), + ); + + $task_name = $form_state['page']->task_name; + foreach ($arguments as $keyword => $position) { + $conf = array(); + + if (isset($page->temporary_arguments[$keyword]) && !empty($form_state['allow temp'])) { + $conf = $page->temporary_arguments[$keyword]; + } + else if (isset($page->arguments[$keyword])) { + $conf = $page->arguments[$keyword]; + } + + $context = t('No context assigned'); + + $plugin = array(); + if ($conf && isset($conf['name'])) { + ctools_include('context'); + $plugin = ctools_get_argument($conf['name']); + + if (isset($plugin['title'])) { + $context = $plugin['title']; + } + } + + $form['table']['argument'][$keyword]['#keyword'] = $keyword; + $form['table']['argument'][$keyword]['#position'] = $position; + $form['table']['argument'][$keyword]['#context'] = $context; + + // The URL for this ajax button + $form['table']['argument'][$keyword]['change-url'] = array( + '#attributes' => array('class' => array("page-manager-context-$keyword-change-url")), + '#type' => 'hidden', + '#value' => url("admin/structure/pages/argument/change/$task_name/$keyword", array('absolute' => TRUE)), + ); + $form['table']['argument'][$keyword]['change'] = array( + '#type' => 'submit', + '#value' => t('Change'), + '#attributes' => array('class' => array('ctools-use-modal')), + '#id' => "page-manager-context-$keyword-change", + ); + + $form['table']['argument'][$keyword]['settings'] = array(); + + // Only show the button if this has a settings form available: + if (!empty($plugin)) { + // The URL for this ajax button + $form['table']['argument'][$keyword]['settings-url'] = array( + '#attributes' => array('class' => array("page-manager-context-$keyword-settings-url")), + '#type' => 'hidden', + '#value' => url("admin/structure/pages/argument/settings/$task_name/$keyword", array('absolute' => TRUE)), + ); + $form['table']['argument'][$keyword]['settings'] = array( + '#type' => 'submit', + '#value' => t('Settings'), + '#attributes' => array('class' => array('ctools-use-modal')), + '#id' => "page-manager-context-$keyword-settings", + ); + } + } + + return $form; +} + +/** + * Theme the table for this form. + */ +function theme_page_manager_page_form_argument_table($vars) { + $form = $vars['form']; + $header = array( + array('data' => t('Argument'), 'class' => array('page-manager-argument')), + array('data' => t('Position in path'), 'class' => array('page-manager-position')), + array('data' => t('Context assigned'), 'class' => array('page-manager-context')), + array('data' => t('Operations'), 'class' => array('page-manager-operations')), + ); + + $rows = array(); + + ctools_include('modal'); + ctools_modal_add_js(); + foreach (element_children($form['argument']) as $key) { + $row = array(); + $row[] = '%' . check_plain($form['argument'][$key]['#keyword']); + $row[] = check_plain($form['argument'][$key]['#position']); + $row[] = $form['argument'][$key]['#context'] . '   ' . drupal_render($form['argument'][$key]['change']);; + $row[] = drupal_render($form['argument'][$key]['settings']) . drupal_render($form['argument'][$key]); + + $rows[] = array('data' => $row); + } + + if (!$rows) { + $rows[] = array(array('data' => t('The path %path has no arguments to configure.', array('%path' => $form['#page-manager-path'])), 'colspan' => 4)); + } + + $attributes = array( + 'id' => 'page-manager-argument-table', + ); + + $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => $attributes)); + return $output; +} + +/** + * Ajax entry point to edit an item + */ +function page_manager_page_subtask_argument_ajax($step = NULL, $task_name = NULL, $keyword = NULL) { + ctools_include('ajax'); + ctools_include('modal'); + ctools_include('context'); + ctools_include('wizard'); + + if (!$step) { + return ctools_ajax_render_error(); + } + + if (!$cache = page_manager_get_page_cache($task_name)) { + return ctools_ajax_render_error(t('Invalid object name.')); + } + + $page = &$cache->subtask['subtask']; + $path = $page->path; + $arguments = page_manager_page_get_named_arguments($path); + + // Load stored object from cache. + if (!isset($arguments[$keyword])) { + return ctools_ajax_render_error(t('Invalid keyword.')); + } + + // Set up wizard info + $form_info = array( + 'id' => 'page_manager_page_argument', + 'path' => "admin/structure/pages/argument/%step/$task_name/$keyword", + 'show cancel' => TRUE, + 'next callback' => 'page_manager_page_argument_next', + 'finish callback' => 'page_manager_page_argument_finish', + 'cancel callback' => 'page_manager_page_argument_cancel', + 'order' => array( + 'change' => t('Change context type'), + 'settings' => t('Argument settings'), + ), + 'forms' => array( + 'change' => array( + 'title' => t('Change argument'), + 'form id' => 'page_manager_page_argument_form_change', + ), + 'settings' => array( + 'title' => t('Argument settings'), + 'form id' => 'page_manager_page_argument_form_settings', + ), + ), + ); + + $form_state = array( + 'page' => $cache, + 'keyword' => $keyword, + 'ajax' => TRUE, + 'modal' => TRUE, + 'modal return' => TRUE, + 'commands' => array(), + ); + + $output = ctools_wizard_multistep_form($form_info, $step, $form_state); + if (!empty($form_state['cancel'])) { + $commands = array(ctools_modal_command_dismiss()); + } + else if (!empty($form_state['complete'])) { + if (isset($page->temporary_arguments[$keyword])) { + $page->arguments[$keyword] = $page->temporary_arguments[$keyword]; + } + + if (isset($page->temporary_arguments)) { + unset($page->temporary_arguments); + } + + // Update the cache with changes. + page_manager_set_page_cache($cache); + + // Rerender the table so we can ajax it back in. + // Go directly to the form and retrieve it using a blank form and + // a clone of our current form state. This is an abbreviated + // drupal_get_form that is halted prior to render and is never + // fully processed, but is guaranteed to produce the same form we + // started with so we don't have to do crazy stuff to rerender + // just part of it. + + // @todo should there be a tool to do this? + + $clone_state = $form_state; + $clone_state['allow temp'] = TRUE; + $form = drupal_build_form('page_manager_page_form_argument', $form_state); + + // Render just the table portion. + $output = drupal_render($form['table']); + $commands = array( + ajax_command_replace('#page-manager-argument-table', $output), + ctools_modal_command_dismiss(), + ); + } + else { + $commands = ctools_modal_form_render($form_state, $output); + } + print ajax_render($commands); + ajax_footer(); + exit; +} + +/** + * Callback generated when the add page process is finished. + */ +function page_manager_page_argument_finish(&$form_state) { +} + +/** + * Callback generated when the 'next' button is clicked. + * + * All we do here is store the cache. + */ +function page_manager_page_argument_next(&$form_state) { + // Update the cache with changes. + page_manager_set_page_cache($form_state['page']); +} + +/** + * Callback generated when the 'cancel' button is clicked. + * + * We might have some temporary data lying around. We must remove it. + */ +function page_manager_page_argument_cancel(&$form_state) { + $page = &$form_state['page']->subtask['subtask']; + if (isset($page->temporary_arguments)) { + unset($page->temporary_arguments); + // Update the cache with changes. + page_manager_set_page_cache($page); + } +} + +/** + * Basic settings form for a page manager page. + */ +function page_manager_page_argument_form_change($form, &$form_state) { + $page = &$form_state['page']->subtask['subtask']; + $keyword = &$form_state['keyword']; + + ctools_include('context'); + $plugins = ctools_get_arguments(); + + $options = array(); + foreach ($plugins as $id => $plugin) { + if (empty($plugin['no ui'])) { + $options[$id] = $plugin['title']; + } + } + + asort($options); + + $options = array('' => t('No context selected')) + $options; + + $argument = ''; + if (isset($page->arguments[$keyword]) && isset($page->arguments[$keyword]['name'])) { + $argument = $page->arguments[$keyword]['name']; + } + + $form['argument'] = array( + '#type' => 'radios', + '#options' => $options, + '#default_value' => $argument, + ); + + return $form; +} + +/** + * Submit handler to change an argument. + */ +function page_manager_page_argument_form_change_submit(&$form, &$form_state) { + $page = &$form_state['page']->subtask['subtask']; + $keyword = &$form_state['keyword']; + $argument = $form_state['values']['argument']; + + // If the argument is not changing, we do not need to do anything. + if (isset($page->arguments[$keyword]['name']) && $page->arguments[$keyword]['name'] == $argument) { + // Set the task to cancel since no change means do nothing: + $form_state['clicked_button']['#wizard type'] = 'cancel'; + return; + } + + ctools_include('context'); + + // If switching to the no context, just wipe out the old data. + if (empty($argument)) { + $form_state['clicked_button']['#wizard type'] = 'finish'; + $page->temporary_arguments[$keyword] = array( + 'settings' => array(), + 'identifier' => t('No context'), + ); + return; + } + + $plugin = ctools_get_argument($argument); + + // Acquire defaults. + $settings = array(); + + if (isset($plugin['default'])) { + if (is_array($plugin['default'])) { + $settings = $plugin['default']; + } + else if (function_exists($plugin['default'])) { + $settings = $plugin['default'](); + } + } + + $id = ctools_context_next_id($page->arguments, $argument); + $title = isset($plugin['title']) ? $plugin['title'] : t('No context'); + + // Set the new argument in a temporary location. + $page->temporary_arguments[$keyword] = array( + 'id' => $id, + 'identifier' => $title . ($id > 1 ? ' ' . $id : ''), + 'name' => $argument, + 'settings' => $settings, + ); +} + +/** + * Basic settings form for a page manager page. + */ +function page_manager_page_argument_form_settings($form, &$form_state) { + $page = &$form_state['page']->subtask['subtask']; + $keyword = &$form_state['keyword']; + + if (isset($page->temporary_arguments[$keyword])) { + $conf = $page->temporary_arguments[$keyword]; + } + else if (isset($page->arguments[$keyword])) { + $conf = $page->temporary_arguments[$keyword] = $page->arguments[$keyword]; + } + + if (!isset($conf)) { + // This should be impossible and thus never seen. + $form['error'] = array('#value' => t('Error: missing argument.')); + return; + } + + ctools_include('context'); + $plugin = ctools_get_argument($conf['name']); + + $form['settings'] = array( + '#tree' => TRUE, + ); + + $form['identifier'] = array( + '#type' => 'textfield', + '#title' => t('Context identifier'), + '#description' => t('This is the title of the context used to identify it later in the administrative process. This will never be shown to a user.'), + '#default_value' => $conf['identifier'], + ); + + if (!$plugin) { + // This should be impossible and thus never seen. + $form['error'] = array('#value' => t('Error: missing or invalid argument plugin %argument.', array('%argument', $argument))); + return; + } + + if ($function = ctools_plugin_get_function($plugin, 'settings form')) { + $function($form, $form_state, $conf['settings']); + } + + $form_state['plugin'] = $plugin; + return $form; +} + +/** + * Validate handler for argument settings. + */ +function page_manager_page_argument_form_settings_validate(&$form, &$form_state) { + if ($function = ctools_plugin_get_function($form_state['plugin'], 'settings form validate')) { + $function($form, $form_state); + } +} + +/** + * Submit handler for argument settings. + */ +function page_manager_page_argument_form_settings_submit(&$form, &$form_state) { + if ($function = ctools_plugin_get_function($form_state['plugin'], 'settings form submit')) { + $function($form, $form_state); + } + + $page = &$form_state['page']->subtask['subtask']; + $keyword = &$form_state['keyword']; + // Copy the form to our temporary location which will get moved again when + // finished. Yes, finished is always next but finish can happen from other + // locations so we funnel through that path rather than duplicate. + $page->temporary_arguments[$keyword]['identifier'] = $form_state['values']['identifier']; + if (isset($form_state['values']['settings'])) { + $page->temporary_arguments[$keyword]['settings'] = $form_state['values']['settings']; + } + else { + $page->temporary_arguments[$keyword]['settings'] = array(); + } +} + +/** + * Import a task handler from cut & paste + */ +function page_manager_page_import_subtask($form, &$form_state, $task_name) { + $form_state['task'] = page_manager_get_task($task_name); + + drupal_set_title(t('Import page')); + $form['name'] = array( + '#type' => 'textfield', + '#title' => t('Page name'), + '#description' => t('Enter the name to use for this page if it is different from the source page. Leave blank to use the original name of the page.'), + ); + + $form['path'] = array( + '#type' => 'textfield', + '#title' => t('Path'), + '#description' => t('Enter the path to use for this page if it is different from the source page. Leave blank to use the original path of the page.'), + ); + + $form['overwrite'] = array( + '#type' => 'checkbox', + '#title' => t('Allow overwrite of an existing page'), + '#description' => t('If the name you selected already exists in the database, this page will be allowed to overwrite the existing page.'), + ); + + $form['object'] = array( + '#type' => 'textarea', + '#title' => t('Paste page code here'), + '#rows' => 15, + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Import'), + ); + return $form; +} + +/** + * Ensure we got a valid page. + */ +function page_manager_page_import_subtask_validate(&$form, &$form_state) { + ob_start(); + eval($form_state['values']['object']); + ob_end_clean(); + + if (!isset($page) || !is_object($page)) { + $errors = ob_get_contents(); + if (empty($errors)) { + $errors = t('No handler found.'); + } + form_error($form['object'], t('Unable to get a page from the import. Errors reported: @errors', array('@errors' => $errors))); + } + + if (empty($form_state['values']['name'])) { + $form_state['values']['name'] = $page->name; + } + + $task_name = page_manager_make_task_name('page', $form_state['values']['name']); + $form_state['cache'] = page_manager_get_page_cache($task_name); + + if ($form_state['cache'] && $form_state['cache']->locked) { + form_error($form['name'], t('That page name is in use and locked by another user. You must break the lock on that page before proceeding, or choose a different name.', array('!break' => url(page_manager_edit_url($task_name, array('actions', 'break-lock')))))); + return; + } + + if (empty($form_state['values']['path'])) { + $form_state['values']['path'] = $page->path; + } + + if (empty($form_state['values']['overwrite'])) { + $page->name = NULL; + } + + $form_state['page'] = new stdClass(); + $form_state['page']->subtask['subtask'] = $page; + page_manager_page_form_basic_validate($form, $form_state); +} + +/** + * Submit the import page to create the new page and redirect. + */ +function page_manager_page_import_subtask_submit($form, &$form_state) { + $page = &$form_state['page']->subtask['subtask']; + $page->name = $form_state['values']['name']; + $page->path = $form_state['values']['path']; + + $task_name = page_manager_make_task_name('page', $page->name); + $cache = page_manager_get_page_cache($task_name); + if (!$cache) { + $cache = new stdClass(); + } + + page_manager_page_new_page_cache($page, $cache); + page_manager_set_page_cache($cache); + + $form_state['redirect'] = page_manager_edit_url($task_name); +} + +/** + * Entry point to export a page. + */ +function page_manager_page_form_export($form, &$form_state) { + $page = $form_state['page']->subtask['subtask']; + + $export = page_manager_page_export($page, $form_state['page']->handlers); + + $lines = substr_count($export, "\n"); + $form['code'] = array( + '#type' => 'textarea', + '#default_value' => $export, + '#rows' => $lines, + ); + + unset($form['buttons']); + return $form; +} + +/** + * Entry point to clone a page. + */ +function page_manager_page_form_clone($form, &$form_state) { + $page = &$form_state['page']->subtask['subtask']; + + // This provides its own button because it does something totally different. + unset($form['buttons']); + + $form['admin_title'] = array( + '#type' => 'textfield', + '#title' => t('Administrative title'), + '#description' => t('The name of this page. This will appear in the administrative interface to easily identify it.'), + '#default_value' => $page->admin_title, + ); + + $form['name'] = array( + '#type' => 'machine_name', + '#title' => t('Page name'), + '#machine_name' => array( + 'exists' => 'page_manager_page_load', + 'source' => array('admin_title'), + ), + '#description' => t('Enter the name to the new page It must be unique and contain only alphanumeric characters and underscores.'), + ); + + // path + $form['path'] = array( + '#type' => 'textfield', + '#title' => t('Path'), + '#description' => t('The URL path to get to this page. You may create named placeholders for variable parts of the path by using %name for required elements and !name for optional elements. For example: "node/%node/foo", "forum/%forum" or "dashboard/!input". These named placeholders can be turned into contexts on the arguments form. You cannot use the same path as the original page.'), + '#default_value' => $page->path, + ); + + $form['handlers'] = array( + '#type' => 'checkbox', + '#title' => t('Clone variants'), + '#description' => t('If checked all variants associated with the page will be cloned as well. If not checked the page will be cloned without variants.'), + '#default_value' => TRUE, + ); + + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Clone'), + ); + + return $form; +} + +/** + * Validate clone page form. + */ +function page_manager_page_form_clone_validate(&$form, &$form_state) { + $page = &$form_state['page']->subtask['subtask']; + + $page->old_name = $page->name; + $page->name = NULL; + page_manager_page_form_basic_validate($form, $form_state); +} + +/** + * submit clone page form. + * + * Load the page, change the name(s) to protect the innocent, and if + * requested, load all the task handlers so that they get saved properly too. + */ +function page_manager_page_form_clone_submit(&$form, &$form_state) { + $original = $form_state['page']->subtask['subtask']; + + $original->name = $form_state['values']['name']; + $original->admin_title = $form_state['values']['admin_title']; + $original->path = $form_state['values']['path']; + + $handlers = !empty($form_state['values']['handlers']) ? $form_state['page']->handlers : FALSE; + // Export the handler, which is a fantastic way to clean database IDs out of it. + $export = page_manager_page_export($original, $handlers); + ob_start(); + eval($export); + ob_end_clean(); + + $task_name = page_manager_make_task_name('page', $page->name); + $cache = new stdClass(); + + page_manager_page_new_page_cache($page, $cache); + page_manager_set_page_cache($cache); + + $form_state['redirect'] = page_manager_edit_url($task_name); +} + +/** + * Entry point to export a page. + */ +function page_manager_page_form_delete($form, &$form_state) { + $page = &$form_state['page']->subtask['subtask']; + + if ($page->type == t('Overridden')) { + $text = t('Reverting the page will delete the page that is in the database, reverting it to the original default page. Any changes you have made will be lost and cannot be recovered.'); + } + else { + $text = t('Are you sure you want to delete this page? Deleting a page cannot be undone.'); + } + $form['markup'] = array( + '#value' => '

    ' . $text . '

    ', + ); + + if (empty($form_state['page']->locked)) { + unset($form['buttons']); + $form['delete'] = array( + '#type' => 'submit', + '#value' => $page->type == t('Overridden') ? t('Revert') : t('Delete'), + ); + } + + return $form; +} + +/** + * Submit handler to delete a view. + */ +function page_manager_page_form_delete_submit(&$form, &$form_state) { + $page = $form_state['page']->subtask['subtask']; + page_manager_page_delete($page); + if ($page->type != t('Overridden')) { + $form_state['redirect'] = 'admin/structure/pages'; + drupal_set_message(t('The page has been deleted.')); + } + else { + $form_state['redirect'] = page_manager_edit_url($form_state['page']->task_name, array('summary')); + drupal_set_message(t('The page has been reverted.')); + } +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/page.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/page.inc new file mode 100644 index 0000000..6a8545d --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/page.inc @@ -0,0 +1,787 @@ + t('Custom pages'), + 'description' => t('Administrator created pages that have a URL path, access control and entries in the Drupal menu system.'), + 'non-exportable' => TRUE, + 'subtasks' => TRUE, + 'subtask callback' => 'page_manager_page_subtask', + 'subtasks callback' => 'page_manager_page_subtasks', + 'save subtask callback' => 'page_manager_page_save_subtask', + 'access callback' => 'page_manager_page_access_check', + 'hook menu' => array( + 'file' => 'page.admin.inc', + 'path' => drupal_get_path('module', 'page_manager') . '/plugins/tasks', + 'function' => 'page_manager_page_menu', + ), + 'hook theme' => 'page_manager_page_theme', + // page only items + 'task type' => 'page', + 'page operations' => array( + array( + 'title' => ' » ' . t('Create a new page'), + 'href' => 'admin/structure/pages/add', + 'html' => TRUE, + ), + ), + 'columns' => array( + 'storage' => array( + 'label' => t('Storage'), + 'class' => 'page-manager-page-storage', + ), + ), + 'page type' => 'custom', + + // context only items + 'handler type' => 'context', + 'get arguments' => array( + 'file' => 'page.admin.inc', + 'path' => drupal_get_path('module', 'page_manager') . '/plugins/tasks', + 'function' => 'page_manager_page_get_arguments', + ), + 'get context placeholders' => 'page_manager_page_get_contexts', + 'access restrictions' => 'page_manager_page_access_restrictions', + 'uses handlers' => TRUE, + ); +} + +/** + * Task callback to get all subtasks. + * + * Return a list of all subtasks. + */ +function page_manager_page_subtasks($task) { + $pages = page_manager_page_load_all($task['name']); + $return = array(); + foreach ($pages as $name => $page) { + $return[$name] = page_manager_page_build_subtask($task, $page); + } + + return $return; +} + +/** + * Callback to return a single subtask. + */ +function page_manager_page_subtask($task, $subtask_id) { + $page = page_manager_page_load($subtask_id); + if ($page) { + return page_manager_page_build_subtask($task, $page); + } +} + +/** + * Call back from the administrative system to save a page. + * + * We get the $subtask as created by page_manager_page_build_subtask. + */ +function page_manager_page_save_subtask($subtask) { + $page = &$subtask['subtask']; + + // Ensure $page->arguments contains only real arguments: + $arguments = page_manager_page_get_named_arguments($page->path); + $args = array(); + foreach ($arguments as $keyword => $position) { + if (isset($page->arguments[$keyword])) { + $args[$keyword] = $page->arguments[$keyword]; + } + else { + $args[$keyword] = array( + 'id' => '', + 'identifier' => '', + 'argument' => '', + 'settings' => array(), + ); + } + } + page_manager_page_recalculate_arguments($page); + // Create a real object from the cache + page_manager_page_save($page); + + // Check to see if we should make this the site frontpage. + if (!empty($page->make_frontpage)) { + $path = array(); + foreach (explode('/', $page->path) as $bit) { + if ($bit[0] != '!') { + $path[] = $bit; + } + } + + $path = implode('/', $path); + $front = variable_get('site_frontpage', 'node'); + if ($path != $front) { + variable_set('site_frontpage', $path); + } + } +} + +/** + * Build a subtask array for a given page. + */ +function page_manager_page_build_subtask($task, $page) { + $operations = array(); + $operations['settings'] = array( + 'type' => 'group', + 'class' => array('operations-settings'), + 'title' => t('Settings'), + 'children' => array(), + ); + + $settings = &$operations['settings']['children']; + + $settings['basic'] = array( + 'title' => t('Basic'), + 'description' => t('Edit name, path and other basic settings for the page.'), + 'form' => 'page_manager_page_form_basic', + ); + + $arguments = page_manager_page_get_named_arguments($page->path); + if ($arguments) { + $settings['argument'] = array( + 'title' => t('Arguments'), + 'description' => t('Set up contexts for the arguments on this page.'), + 'form' => 'page_manager_page_form_argument', + ); + } + + $settings['access'] = array( + 'title' => t('Access'), + 'description' => t('Control what users can access this page.'), + 'admin description' => t('Access rules are used to test if the page is accessible and any menu items associated with it are visible.'), + 'form' => 'page_manager_page_form_access', + ); + + $settings['menu'] = array( + 'title' => t('Menu'), + 'description' => t('Provide this page a visible menu or a menu tab.'), + 'form' => 'page_manager_page_form_menu', + ); + + $operations['actions']['children']['clone'] = array( + 'title' => t('Clone'), + 'description' => t('Make a copy of this page'), + 'form' => 'page_manager_page_form_clone', + ); + $operations['actions']['children']['export'] = array( + 'title' => t('Export'), + 'description' => t('Export this page as code that can be imported or embedded into a module.'), + 'form' => 'page_manager_page_form_export', + ); + if ($page->export_type == (EXPORT_IN_CODE | EXPORT_IN_DATABASE)) { + $operations['actions']['children']['delete'] = array( + 'title' => t('Revert'), + 'description' => t('Remove all changes to this page and revert to the version in code.'), + 'form' => 'page_manager_page_form_delete', + ); + } + else if ($page->export_type != EXPORT_IN_CODE) { + $operations['actions']['children']['delete'] = array( + 'title' => t('Delete'), + 'description' => t('Remove this page from your system completely.'), + 'form' => 'page_manager_page_form_delete', + ); + } + + $subtask = array( + 'name' => $page->name, + 'admin title' => check_plain($page->admin_title), + 'admin description' => filter_xss_admin($page->admin_description), + 'admin summary' => 'page_manager_page_admin_summary', + 'admin path' => $page->path, + 'admin type' => t('Custom'), + 'subtask' => $page, + 'operations' => $operations, + 'operations include' => array( + 'file' => 'page.admin.inc', + 'path' => drupal_get_path('module', 'page_manager') . '/plugins/tasks', + ), + 'single task' => empty($page->multiple), + 'row class' => empty($page->disabled) ? 'page-manager-enabled' : 'page-manager-disabled', + 'storage' => $page->type == t('Default') ? t('In code') : $page->type, + 'disabled' => !empty($page->disabled), + // This works for both enable AND disable + 'enable callback' => 'page_manager_page_enable', + ); + + // default handlers may appear from a default subtask. + if (isset($page->default_handlers)) { + $subtask['default handlers'] = $page->default_handlers; + } + return $subtask; +} + +/** + * Delegated implementation of hook_theme(). + */ +function page_manager_page_theme(&$items, $task) { + $base = array( + 'file' => 'page.admin.inc', + 'path' => drupal_get_path('module', 'page_manager') . '/plugins/tasks', + ); + $items['page_manager_page_form_argument_table'] = $base + array( + 'render element' => 'form', + ); + $items['page_manager_page_lock'] = $base + array( + 'variables' => array('lock' => array(), 'task_name' => NULL), + ); + $items['page_manager_page_changed'] = $base + array( + 'variables' => array(), + ); +} + +// -------------------------------------------------------------------------- +// Page execution functions + +/** + * Execute a page task. + * + * This is the callback to entries in the Drupal menu system created by the + * page task. + * + * @param $subtask_id + * The name of the page task used. + * @param ... + * A number of context objects as specified by the user when + * creating named arguments in the path. + */ +function page_manager_page_execute($subtask_id) { + $page = page_manager_page_load($subtask_id); + $task = page_manager_get_task($page->task); + $subtask = page_manager_get_task_subtask($task, $subtask_id); + + // Turn the contexts into a properly keyed array. + $contexts = array(); + $args = array(); + foreach (func_get_args() as $count => $arg) { + if (is_object($arg) && get_class($arg) == 'ctools_context') { + $contexts[$arg->id] = $arg; + $args[] = $arg->original_argument; + } + else if ($count) { + $args[] = $arg; + } + } + + $count = 0; + $names = page_manager_page_get_named_arguments($page->path); + $bits = explode('/', $page->path); + + if ($page->arguments) { + foreach ($page->arguments as $name => $argument) { + // Optional arguments must be converted to contexts too, if they exist. + if ($bits[$names[$name]][0] == '!') { + ctools_include('context'); + $argument['keyword'] = $name; + if (isset($args[$count])) { + // Hack: use a special argument config variable to learn if we need + // to use menu_tail style behavior: + if (empty($argument['settings']['use_tail'])) { + $value = $args[$count]; + } + else { + $value = implode('/', array_slice($args, $count)); + } + + $context = ctools_context_get_context_from_argument($argument, $value); + } + else { + // make sure there is a placeholder context for missing optional contexts. + $context = ctools_context_get_context_from_argument($argument, NULL, TRUE); + // Force the title to blank for replacements + } + if ($context) { + $contexts[$context->id] = $context; + } + } + $count++; + } + } + + if ($function = ctools_plugin_get_function($task, 'page callback')) { + return call_user_func_array($function, array($page, $contexts, $args)); + } + + ctools_include('context-task-handler'); + $output = ctools_context_handler_render($task, $subtask, $contexts, $args); + if ($output === FALSE) { + return MENU_NOT_FOUND; + } + + return $output; +} + +// -------------------------------------------------------------------------- +// Context type callbacks + +/** + * Return a list of arguments used by this task. + */ +function page_manager_page_get_arguments($task, $subtask) { + return _page_manager_page_get_arguments($subtask['subtask']); +} + +function _page_manager_page_get_arguments($page) { + $arguments = array(); + if (!empty($page->arguments)) { + foreach ($page->arguments as $keyword => $argument) { + if (isset($argument['name'])) { + $argument['keyword'] = $keyword; + $arguments[$keyword] = $argument; + } + } + } + return $arguments; +} + +/** + * Get a group of context placeholders for the arguments. + */ +function page_manager_page_get_contexts($task, $subtask) { + ctools_include('context'); + return ctools_context_get_placeholders_from_argument(page_manager_page_get_arguments($task, $subtask)); +} + +/** + * Return a list of arguments used by this task. + */ +function page_manager_page_access_restrictions($task, $subtask, $contexts) { + $page = $subtask['subtask']; + return ctools_access_add_restrictions($page->access, $contexts); +} + +// -------------------------------------------------------------------------- +// Page task database info. + +/** + * Create a new page with defaults appropriately set from schema. + */ +function page_manager_page_new() { + ctools_include('export'); + return ctools_export_new_object('page_manager_pages'); +} + +/** + * Load a single page subtask. + */ +function page_manager_page_load($name) { + ctools_include('export'); + $result = ctools_export_load_object('page_manager_pages', 'names', array($name)); + if (isset($result[$name])) { + return $result[$name]; + } +} + +/** + * Load all page subtasks. + */ +function page_manager_page_load_all($task = NULL) { + ctools_include('export'); + + if (empty($task)) { + return ctools_export_load_object('page_manager_pages'); + } + else { + return ctools_export_load_object('page_manager_pages', 'conditions', array('task' => $task)); + } +} + +/** + * Write a page subtask to the database. + */ +function page_manager_page_save(&$page) { + $update = (isset($page->pid)) ? array('pid') : array(); + $task = page_manager_get_task($page->task); + + if ($function = ctools_plugin_get_function($task, 'save')) { + $function($page, $update); + } + drupal_write_record('page_manager_pages', $page, $update); + + // If this was a default page we may need to write default task + // handlers that we provided as well. + if (!$update && isset($page->default_handlers)) { + $handlers = page_manager_load_task_handlers(page_manager_get_task('page'), $page->name); + foreach ($page->default_handlers as $name => $handler) { + if (!isset($handlers[$name]) || !($handlers[$name]->export_type & EXPORT_IN_DATABASE)) { + // Make sure this is right, as exports can wander a bit. + $handler->subtask = $page->name; + page_manager_save_task_handler($handler); + } + } + } + return $page; +} + +/** + * Remove a page subtask. + */ +function page_manager_page_delete($page) { + $task = page_manager_get_task($page->task); + if ($function = ctools_plugin_get_function($task, 'delete')) { + $function($page); + } + if (!empty($task['uses handlers'])) { + $handlers = page_manager_load_task_handlers($task, $page->name); + foreach ($handlers as $handler) { + page_manager_delete_task_handler($handler); + } + } + db_delete('page_manager_pages') + ->condition('name', $page->name) + ->execute(); + // Make sure that the cache is reset so that the menu rebuild does not + // rebuild this page again. + ctools_include('export'); + ctools_export_load_object_reset('page_manager_pages'); + menu_rebuild(); +} + +/** + * Export a page subtask. + */ +function page_manager_page_export($page, $with_handlers = FALSE, $indent = '') { + $task = page_manager_get_task($page->task); + $append = ''; + + if ($function = ctools_plugin_get_function($task, 'export')) { + $append = $function($page, $indent); + } + + ctools_include('export'); + $output = ctools_export_object('page_manager_pages', $page, $indent); + $output .= $append; + + if ($with_handlers) { + if (is_array($with_handlers)) { + $handlers = $with_handlers; + } + else { + $handlers = page_manager_load_task_handlers(page_manager_get_task('page'), $page->name); + } + $output .= $indent . '$page->default_handlers = array();' . "\n"; + foreach ($handlers as $handler) { + $output .= page_manager_export_task_handler($handler, $indent); + $output .= $indent . '$page->default_handlers[$handler->name] = $handler;' . "\n"; + } + } + return $output; +} + +/** + * Get a list of named arguments in a page manager path. + * + * @param $path + * A normal Drupal path. + * + * @return + * An array of % marked variable arguments, keyed by the argument's name. + * The value will be the position of the argument so that it can easily + * be found. Items with a position of -1 have multiple positions. + */ +function page_manager_page_get_named_arguments($path) { + $arguments = array(); + $bits = explode('/', $path); + foreach ($bits as $position => $bit) { + if ($bit && ($bit[0] == '%' || $bit[0] == '!')) { + // special handling for duplicate path items and substr to remove the % + $arguments[substr($bit, 1)] = isset($arguments[$bit]) ? -1 : $position; + } + } + + return $arguments; +} + +/** + * Load a context from an argument for a given page task. + * + * Helper function for pm_arg_load(), which is in page_manager.module because + * drupal's menu system does not allow loader functions to reside in separate + * files. + * + * @param $value + * The incoming argument value. + * @param $subtask + * The subtask id. + * @param $argument + * The numeric position of the argument in the path, counting from 0. + * + * @return + * A context item if one is configured, the argument if one is not, or + * FALSE if restricted or invalid. + */ +function _pm_arg_load($value, $subtask, $argument) { + $page = page_manager_page_load($subtask); + if (!$page) { + return FALSE; + } + + $path = explode('/', $page->path); + if (empty($path[$argument])) { + return FALSE; + } + + $keyword = substr($path[$argument], 1); + if (empty($page->arguments[$keyword])) { + return $value; + } + + $page->arguments[$keyword]['keyword'] = $keyword; + + ctools_include('context'); + $context = ctools_context_get_context_from_argument($page->arguments[$keyword], $value); + + // convert false equivalents to false. + return $context ? $context : FALSE; +} + +/** + * Provide a nice administrative summary of the page so an admin can see at a + * glance what this page does and how it is configured. + */ +function page_manager_page_admin_summary($task, $subtask) { + $task_name = page_manager_make_task_name($task['name'], $subtask['name']); + $page = $subtask['subtask']; + $output = ''; + + $rows = array(); + + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('Storage')), + array('class' => array('page-summary-data'), 'data' => $subtask['storage']), + array('class' => array('page-summary-operation'), 'data' => ''), + ); + + if (!empty($page->disabled)) { + $link = l(t('Enable'), page_manager_edit_url($task_name, array('handlers', $page->name, 'actions', 'enable'))); + $text = t('Disabled'); + } + else { + $link = l(t('Disable'), page_manager_edit_url($task_name, array('handlers', $page->name, 'actions', 'disable'))); + $text = t('Enabled'); + } + + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('Status')), + array('class' => array('page-summary-data'), 'data' => $text), + array('class' => array('page-summary-operation'), 'data' => $link), + ); + + + $path = array(); + foreach (explode('/', $page->path) as $bit) { + if ($bit[0] != '!') { + $path[] = $bit; + } + } + + $path = implode('/', $path); + $front = variable_get('site_frontpage', 'node'); + + $link = l(t('Edit'), page_manager_edit_url($task_name, array('settings', 'basic'))); + $message = ''; + if ($path == $front) { + $message = t('This is your site home page.'); + } + else if (!empty($page->make_frontpage)) { + $message = t('This page is set to become your site home page.'); + } + + if ($message) { + $rows[] = array( + array('class' => array('page-summary-data'), 'data' => $message, 'colspan' => 2), + array('class' => array('page-summary-operation'), 'data' => $link), + ); + } + + if (strpos($path, '%') === FALSE) { + $path = l('/' . $page->path, $path); + } + else { + $path = '/' . $page->path; + } + + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('Path')), + array('class' => array('page-summary-data'), 'data' => $path), + array('class' => array('page-summary-operation'), 'data' => $link), + ); + + if (empty($access['plugins'])) { + $access['plugins'] = array(); + } + + $contexts = page_manager_page_get_contexts($task, $subtask); + $access = ctools_access_group_summary($page->access, $contexts); + if ($access) { + $access = t('Accessible only if @conditions.', array('@conditions' => $access)); + } + else { + $access = t('This page is publicly accessible.'); + } + + $link = l(t('Edit'), page_manager_edit_url($task_name, array('settings', 'access'))); + + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('Access')), + array('class' => array('page-summary-data'), 'data' => $access), + array('class' => array('page-summary-operation'), 'data' => $link), + ); + + $menu_options = array( + 'none' => t('No menu entry.'), + 'normal' => t('Normal menu entry.'), + 'tab' => t('Menu tab.'), + 'default tab' => t('Default menu tab.'), + 'action' => t('Local action'), + ); + + if (!empty($page->menu)) { + $menu = $menu_options[$page->menu['type']]; + if ($page->menu['type'] != 'none') { + $menu .= ' ' . t('Title: %title.', array('%title' => $page->menu['title'])); + switch ($page->menu['type']) { + case 'default tab': + $menu .= ' ' . t('Parent title: %title.', array('%title' => $page->menu['parent']['title'])); + break; + case 'normal': + if (module_exists('menu')) { + $menus = menu_get_menus(); + $menu .= ' ' . t('Menu block: %title.', array('%title' => $menus[$page->menu['name']])); + } + break; + } + } + } + else { + $menu = t('No menu entry'); + } + + $link = l(t('Edit'), page_manager_edit_url($task_name, array('settings', 'menu'))); + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('Menu')), + array('class' => array('page-summary-data'), 'data' => $menu), + array('class' => array('page-summary-operation'), 'data' => $link), + ); + + $output .= theme('table', array('rows' => $rows, 'attributes' => array('id' => 'page-manager-page-summary'))); + return $output; +} + +/** + * Callback to enable/disable the page from the UI. + */ +function page_manager_page_enable(&$cache, $status) { + $page = &$cache->subtask['subtask']; + ctools_include('export'); + ctools_export_set_object_status($page, $status); + + $page->disabled = FALSE; +} + +/** + * Recalculate the arguments when something like the path changes. + */ +function page_manager_page_recalculate_arguments(&$page) { + // Ensure $page->arguments contains only real arguments: + $arguments = page_manager_page_get_named_arguments($page->path); + $args = array(); + foreach ($arguments as $keyword => $position) { + if (isset($page->arguments[$keyword])) { + $args[$keyword] = $page->arguments[$keyword]; + } + else { + $args[$keyword] = array( + 'id' => '', + 'identifier' => '', + 'argument' => '', + 'settings' => array(), + ); + } + } + $page->arguments = $args; +} + +/** + * When adding or cloning a new page, this creates a new page cache + * and adds our page to it. + * + * This does not check to see if the existing cache is already locked. + * This must be done beforehand. + * + * @param &$page + * The page to create. + * @param &$cache + * The cache to use. If the cache has any existing task handlers, + * they will be marked for deletion. This may be a blank object. + */ +function page_manager_page_new_page_cache(&$page, &$cache) { + // Does a page already exist? If so, we are overwriting it so + // take its pid. + if (!empty($cache->subtask) && !empty($cache->subtask['subtask']) && !empty($cache->subtask['subtask']->pid)) { + $page->pid = $cache->subtask['subtask']->pid; + } + else { + $cache->new = TRUE; + } + + $cache->task_name = page_manager_make_task_name('page', $page->name); + $cache->task_id = 'page'; + $cache->task = page_manager_get_task('page'); + $cache->subtask_id = $page->name; + $page->export_type = EXPORT_IN_DATABASE; + $page->type = t('Normal'); + $cache->subtask = page_manager_page_build_subtask($cache->task, $page); + + if (isset($cache->handlers)) { + foreach($cache->handlers as $id => $handler) { + $cache->handler_info[$id]['changed'] = PAGE_MANAGER_CHANGED_DELETED; + } + } + else { + $cache->handlers = array(); + $cache->handler_info = array(); + } + + if (!empty($page->default_handlers)) { + foreach ($page->default_handlers as $id => $handler) { + page_manager_handler_add_to_page($cache, $handler); + } + } + + $cache->locked = FALSE; + $cache->changed = TRUE; +} + +/** + * Callback to determine if a page is accessible. + * + * @param $task + * The task plugin. + * @param $subtask_id + * The subtask id + * @param $contexts + * The contexts loaded for the task. + * @return + * TRUE if the current user can access the page. + */ +function page_manager_page_access_check($task, $subtask_id, $contexts) { + $page = page_manager_page_load($subtask_id); + return ctools_access($page->access, $contexts); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/poll.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/poll.inc new file mode 100644 index 0000000..073ee0c --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/poll.inc @@ -0,0 +1,121 @@ + 'page', + + 'title' => t('All polls'), + 'admin title' => t('All polls'), + 'admin description' => t('When enabled, this overrides the default Drupal behavior for the polls at /poll. If no variant is selected, the default Drupal most recent polls will be shown.'), + 'admin path' => 'poll', + + // Menu hooks so that we can alter the node/%node menu entry to point to us. + 'hook menu alter' => 'page_manager_poll_menu_alter', + + // This is task uses 'context' handlers and must implement these to give the + // handler data it needs. + 'handler type' => 'context', + + // Allow this to be enabled or disabled: + 'disabled' => variable_get('page_manager_poll_disabled', TRUE), + 'enable callback' => 'page_manager_poll_enable', + 'access callback' => 'page_manager_poll_access_check', + ); +} + +/** + * Callback defined by page_manager_poll_page_manager_tasks(). + * + * Alter the node edit input so that node edit comes to us rather than the + * normal node edit process. + */ +function page_manager_poll_menu_alter(&$items, $task) { + if (variable_get('page_manager_poll_disabled', TRUE)) { + return; + } + + $callback = $items['poll']['page callback']; + // Override the node edit handler for our purpose. + if ($callback == 'poll_page' || variable_get('page_manager_override_anyway', FALSE)) { + $items['poll']['page callback'] = 'page_manager_poll'; + $items['poll']['file path'] = $task['path']; + $items['poll']['file'] = $task['file']; + } + else { + variable_set('page_manager_poll_disabled', TRUE); + if (!empty($GLOBALS['page_manager_enabling_poll'])) { + drupal_set_message(t('Page manager module is unable to enable poll because some other module already has overridden with %callback.', array('%callback' => $callback)), 'warning'); + } + return; + } + +} + +/** + * Entry point for our overridden node edit. + * + * This function asks its assigned handlers who, if anyone, would like + * to run with it. If no one does, it passes through to Drupal core's + * node edit, which is node_page_edit(). + */ +function page_manager_poll() { + // Load my task plugin + $task = page_manager_get_task('poll'); + + ctools_include('context'); + ctools_include('context-task-handler'); + $output = ctools_context_handler_render($task, '', array(), array()); + if ($output !== FALSE) { + return $output; + } + + module_load_include('inc', 'poll', 'poll.pages'); + $function = 'poll_page'; + foreach (module_implements('page_manager_override') as $module) { + $call = $module . '_page_manager_override'; + if (($rc = $call('poll')) && function_exists($rc)) { + $function = $rc; + break; + } + } + + // Otherwise, fall back. + return $function(); +} + +/** + * Callback to enable/disable the page from the UI. + */ +function page_manager_poll_enable($cache, $status) { + variable_set('page_manager_poll_disabled', $status); + // Set a global flag so that the menu routine knows it needs + // to set a message if enabling cannot be done. + if (!$status) { + $GLOBALS['page_manager_enabling_poll'] = TRUE; + } +} + +/** + * Callback to determine if a page is accessible. + * + * @param $task + * The task plugin. + * @param $subtask_id + * The subtask id + * @param $contexts + * The contexts loaded for the task. + * @return + * TRUE if the current user can access the page. + */ +function page_manager_poll_access_check($task, $subtask_id, $contexts) { + return user_access('access content'); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/search.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/search.inc new file mode 100644 index 0000000..efd7415 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/search.inc @@ -0,0 +1,249 @@ + 'page', + 'title' => t('Search'), + + // There are multiple search pages, let's override each of them + // separately. + 'subtasks' => TRUE, + 'subtask callback' => 'page_manager_search_subtask', + 'subtasks callback' => 'page_manager_search_subtasks', + + // Menu hooks so that we can alter the node/%node menu entry to point to us. + 'hook menu alter' => 'page_manager_search_menu_alter', + + // This is task uses 'context' handlers and must implement these to give the + // handler data it needs. + 'handler type' => 'context', + 'get arguments' => 'page_manager_search_get_arguments', + 'get context placeholders' => 'page_manager_search_get_contexts', + 'access callback' => 'page_manager_search_access_check', + + ); +} + +/** + * Callback defined by page_manager_search_page_manager_tasks(). + * + * Alter the search tabs to work with page manager. The search flow is + * quite odd, and tracing through the code takes hours to realize + * that the tab you click on does not normally actually handle + * the search. This tries to account for that. + * + * Note to module authors: This tends to work a lot better with modules + * that override their own search pages if their _alter runs *before* + * this one. + */ +function page_manager_search_menu_alter(&$items, $task) { + // We are creating two sets of tabs. One set is for searching without + // keywords. A second set is for searching *with* keywords. This + // is necessary because search/node/% and search/node need to be + // different due to the way the search menu items function. + + $default_info = search_get_default_module_info(); + if (empty($default_info)) { + // Nothing to do. + return; + } + + // Go through each search module item. + foreach (search_get_info() as $module => $info) { + if (variable_get('page_manager_search_disabled_' . $module, TRUE)) { + continue; + } + + $path = 'search/' . $info['path']; + $callback = $items["$path/%menu_tail"]['page callback']; + + if ($callback == 'search_view' || variable_get('page_manager_override_anyway', FALSE)) { + $items["$path"]['page callback'] = 'page_manager_search_page'; + $items["$path"]['file path'] = $task['path']; + $items["$path"]['file'] = $task['file']; + + $items["$path/%menu_tail"]['page callback'] = 'page_manager_search_page'; + $items["$path/%menu_tail"]['file path'] = $task['path']; + $items["$path/%menu_tail"]['file'] = $task['file']; + } + else { + // automatically disable this task if it cannot be enabled. + variable_set('page_manager_search_disabled_' . $module, TRUE); + if (!empty($GLOBALS['page_manager_enabling_search'])) { + drupal_set_message(t('Page manager module is unable to enable @path because some other module already has overridden with %callback.', array('%callback' => $callback, '@path' => $path)), 'error'); + } + } + } +} + +/** + * Entry point for our overridden search page. + * + */ +function page_manager_search_page($type) { + ctools_include('menu'); +// menu_set_active_trail(ctools_get_menu_trail('search/' . $type)); + + // Get the arguments and construct a keys string out of them. + $args = func_get_args(); + + // We have to remove the $type. + array_shift($args); + + // And implode() it all back together. + $keys = $args ? implode('/', $args) : ''; + + // Allow other modules to alter the search keys + drupal_alter(array('search_keys', 'search_'. $type .'_keys'), $keys); + + // Load my task plugin + $task = page_manager_get_task('search'); + $subtask = page_manager_get_task_subtask($task, $type); + + // Load the node into a context. + ctools_include('context'); + ctools_include('context-task-handler'); + $contexts = ctools_context_handler_get_task_contexts($task, $subtask, array($keys)); + + $output = ctools_context_handler_render($task, $subtask, $contexts, array($keys)); + if ($output !== FALSE) { + return $output; + } + + $function = 'search_view'; + foreach (module_implements('page_manager_override') as $module) { + $call = $module . '_page_manager_override'; + if (($rc = $call('search')) && function_exists($rc)) { + $function = $rc; + break; + } + } + + // Otherwise, fall back. + + // Put the $type back on the arguments. + module_load_include('inc', 'search', 'search.pages'); + array_unshift($args, $type); + return call_user_func_array($function, $args); +} + +/** + * Callback to get arguments provided by this task handler. + * + * Since this is the node view and there is no UI on the arguments, we + * create dummy arguments that contain the needed data. + */ +function page_manager_search_get_arguments($task, $subtask_id) { + return array( + array( + 'keyword' => 'keywords', + 'identifier' => t('Keywords'), + 'id' => 1, + 'name' => 'string', + 'settings' => array('use_tail' => TRUE), + ), + ); +} + +/** + * Callback to get context placeholders provided by this handler. + */ +function page_manager_search_get_contexts($task, $subtask_id) { + return ctools_context_get_placeholders_from_argument(page_manager_search_get_arguments($task, $subtask_id)); +} + +/** + * Callback to enable/disable the page from the UI. + */ +function page_manager_search_enable($cache, $status) { + variable_set('page_manager_search_disabled_' . $cache->subtask_id, $status); + + // Set a global flag so that the menu routine knows it needs + // to set a message if enabling cannot be done. + if (!$status) { + $GLOBALS['page_manager_enabling_search'] = TRUE; + } +} + +/** + * Task callback to get all subtasks. + * + * Return a list of all subtasks. + */ +function page_manager_search_subtasks($task) { + $return = array(); + foreach (search_get_info() as $module => $info) { + if ($info['path']) { + // We don't pass the $info because the subtask build could be called + // singly without the $info when just the subtask needs to be built. + $return[$module] = page_manager_search_build_subtask($task, $module); + } + } + + return $return; +} + +/** + * Callback to return a single subtask. + */ +function page_manager_search_subtask($task, $subtask_id) { + return page_manager_search_build_subtask($task, $subtask_id); +} + +/** + * Build a subtask array for a given page. + */ +function page_manager_search_build_subtask($task, $module) { + $search_info = search_get_info(); + $info = $search_info[$module]; + $path = 'search/' . $info['path']; + $subtask = array( + 'name' => $module, + 'admin title' => $info['title'], + 'admin path' => "$path/!keywords", + 'admin description' => t('Search @type', array('@type' => $info['title'])), + 'admin type' => t('System'), + 'row class' => empty($page->disabled) ? 'page-manager-enabled' : 'page-manager-disabled', + 'storage' => t('In code'), + 'disabled' => variable_get('page_manager_search_disabled_' . $module, TRUE), + // This works for both enable AND disable + 'enable callback' => 'page_manager_search_enable', + ); + + return $subtask; +} + +/** + * Callback to determine if a page is accessible. + * + * @param $task + * The task plugin. + * @param $subtask_id + * The subtask id + * @param $contexts + * The contexts loaded for the task. + * @return + * TRUE if the current user can access the page. + */ +function page_manager_search_access_check($task, $subtask_id, $contexts) { + $context = reset($contexts); + return _search_menu_access($context->data); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/term_view.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/term_view.inc new file mode 100644 index 0000000..37259b9 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/term_view.inc @@ -0,0 +1,377 @@ + 'page', + + 'title' => t('Taxonomy term template'), + 'admin title' => t('Taxonomy term template'), + 'admin description' => t('When enabled, this overrides the default Drupal behavior for displaying taxonomy terms at taxonomy/term/%term. If you add variants, you may use selection criteria such as vocabulary or user access to provide different displays of the taxonomy term and associated nodes. If no variant is selected, the default Drupal taxonomy term display will be used. This page only affects items actually displayed ad taxonomy/term/%term. Some taxonomy terms, such as forums, have their displays moved elsewhere. Also please note that if you are using pathauto, aliases may make a taxonomy terms appear somewhere else, but as far as Drupal is concerned, they are still at taxonomy/term/%term.'), + 'admin path' => 'taxonomy/term/%taxonomy_term', + 'admin summary' => 'page_manager_term_view_admin_summary', + + // Menu hooks so that we can alter the term/%term menu entry to point to us. + 'hook menu' => 'page_manager_term_view_menu', + 'hook menu alter' => 'page_manager_term_view_menu_alter', + + // Provide a setting to the primary settings UI for Panels + 'admin settings' => 'page_manager_term_view_admin_settings', + // Even though we don't have subtasks, this allows us to save our settings. + 'save subtask callback' => 'page_manager_term_view_save', + + // Callback to add items to the page manager task administration form: + 'task admin' => 'page_manager_term_view_task_admin', + + // This is task uses 'context' handlers and must implement these to give the + // handler data it needs. + 'handler type' => 'context', + 'get arguments' => 'page_manager_term_view_get_arguments', + 'get context placeholders' => 'page_manager_term_view_get_contexts', + + // Allow this to be enabled or disabled: + 'disabled' => variable_get('page_manager_term_view_disabled', TRUE), + 'enable callback' => 'page_manager_term_view_enable', + 'access callback' => 'page_manager_term_view_access_check', + + // Allow additional operations + 'operations' => array( + 'settings' => array( + 'title' => t('Settings'), + 'description' => t('Edit name, path and other basic settings for the page.'), + 'form' => 'page_manager_term_view_settings', + ), + ), + ); + } +} + +/** + * Callback defined by page_manager_term_view_page_manager_tasks(). + * + * Alter the term view input so that term view comes to us rather than the + * normal term view process. + */ +function page_manager_term_view_menu_alter(&$items, $task) { + if (variable_get('page_manager_term_view_disabled', TRUE)) { + return; + } + + // Override the term view handler for our purpose, but only if someone else + // has not already done so. + if (isset($items['taxonomy/term/%taxonomy_term']) && $items['taxonomy/term/%taxonomy_term']['page callback'] == 'taxonomy_term_page' || variable_get('page_manager_override_anyway', FALSE)) { + $items['taxonomy/term/%taxonomy_term']['page callback'] = 'page_manager_term_view_page'; + $items['taxonomy/term/%taxonomy_term']['file path'] = $task['path']; + $items['taxonomy/term/%taxonomy_term']['file'] = $task['file']; + } + else { + // automatically disable this task if it cannot be enabled. + variable_set('page_manager_term_view_disabled', TRUE); + + if (isset($items['taxonomy/term/%taxonomy_term']['page callback'])) { + $callback = $items['taxonomy/term/%taxonomy_term']['page callback']; + } + // Because Views changes %taxonomy_term to %views_arg, check to see if that + // is why we can't enable: + else if (isset($items['taxonomy/term/%views_arg']['page callback'])) { + $callback = $items['taxonomy/term/%views_arg']['page callback']; + } + else { + $callback = t('an unknown callback'); + } + if (!empty($GLOBALS['page_manager_enabling_term_view'])) { + drupal_set_message(t('Page manager module is unable to enable taxonomy/term/%taxonomy_term because some other module already has overridden with %callback.', array('%callback' => $callback)), 'error'); + } + } +} + +/** + * Entry point for our overridden term view. + * + * This function asks its assigned handlers who, if anyone, would like + * to run with it. If no one does, it passes through to Drupal core's + * term view, which is term_page_view(). + */ +function page_manager_term_view_page($term, $depth = NULL) { + // Prep the term to be displayed so all of the regular hooks are triggered. + // Rather than calling taxonomy_term_page() directly, as it that would + // potentially load nodes that were not necessary, execute some of the code + // prior to identifying the correct CTools or Page Manager task handler and + // only proceed with the rest of the code if necessary. + + // Assign the term name as the page title. + drupal_set_title($term->name); + + // If there is a menu link to this term, the link becomes the last part + // of the active trail, and the link name becomes the page title. + // Thus, we must explicitly set the page title to be the node title. + $uri = entity_uri('taxonomy_term', $term); + + // Set the term path as the canonical URL to prevent duplicate content. + drupal_add_html_head_link(array('rel' => 'canonical', 'href' => url($uri['path'], $uri['options'])), TRUE); + // Set the non-aliased path as a default shortlink. + drupal_add_html_head_link(array('rel' => 'shortlink', 'href' => url($uri['path'], array_merge($uri['options'], array('alias' => TRUE)))), TRUE); + + // Trigger the main + $build = taxonomy_term_show($term); + + // Load my task plugin + $task = page_manager_get_task('term_view'); + + // Load the term into a context. + ctools_include('context'); + ctools_include('context-task-handler'); + $contexts = ctools_context_handler_get_task_contexts($task, '', array($term, $depth)); + + if (empty($contexts)) { + return MENU_NOT_FOUND; + } + + // Build the full output using the configured CTools plugin. + $output = ctools_context_handler_render($task, '', $contexts, array($term->tid)); + if ($output !== FALSE) { + return $output; + } + + // Try loading an override plugin. + foreach (module_implements('page_manager_override') as $module) { + $call = $module . '_page_manager_override'; + if (($rc = $call('term_view')) && function_exists($rc)) { + return $rc($term, $depth); + } + } + + // Otherwise, fall back to replicating the output normally generated by + // taxonomy_term_page(). + + // Build breadcrumb based on the hierarchy of the term. + $current = (object) array( + 'tid' => $term->tid, + ); + // @todo This overrides any other possible breadcrumb and is a pure hard-coded + // presumption. Make this behavior configurable per vocabulary or term. + $breadcrumb = array(); + while ($parents = taxonomy_get_parents($current->tid)) { + $current = array_shift($parents); + $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid); + } + $breadcrumb[] = l(t('Home'), NULL); + $breadcrumb = array_reverse($breadcrumb); + drupal_set_breadcrumb($breadcrumb); + drupal_add_feed('taxonomy/term/' . $term->tid . '/feed', 'RSS - ' . $term->name); + + if ($nids = taxonomy_select_nodes($term->tid, TRUE, variable_get('default_nodes_main', 10))) { + $nodes = node_load_multiple($nids); + $build += node_view_multiple($nodes); + $build['pager'] = array( + '#theme' => 'pager', + '#weight' => 5, + ); + } + else { + $build['no_content'] = array( + '#prefix' => '

    ', + '#markup' => t('There is currently no content classified with this term.'), + '#suffix' => '

    ', + ); + } + return $build; +} + +/** + * Callback to get arguments provided by this task handler. + * + * Since this is the term view and there is no UI on the arguments, we + * create dummy arguments that contain the needed data. + */ +function page_manager_term_view_get_arguments($task, $subtask_id) { + return array( + array( + 'keyword' => 'term', + 'identifier' => page_manager_term_view_get_type() == 'multiple' ? t('Term(s) being viewed') : t('Term being viewed'), + 'id' => 1, + 'name' => page_manager_term_view_get_type() == 'multiple' ? 'terms' : 'term', + 'settings' => array('input_form' => 'tid', 'breadcrumb' => variable_get('page_manager_taxonomy_breadcrumb', TRUE)), + 'default' => '404', + ), + array( + 'keyword' => 'depth', + 'identifier' => t('Depth'), + 'id' => 1, + 'name' => 'string', + 'settings' => array(), + ), + ); +} + +/** + * Callback to get context placeholders provided by this handler. + */ +function page_manager_term_view_get_contexts($task, $subtask_id) { + return ctools_context_get_placeholders_from_argument(page_manager_term_view_get_arguments($task, $subtask_id)); +} + +/** + * Settings page for this item. + */ +function page_manager_term_view_settings($form, &$form_state) { + // This passes thru because the setting can also appear on the main Panels + // settings form. If $settings is an array it will just pick up the default. + $settings = isset($form_state->update_values) ? $form_state->update_values : array(); + return page_manager_term_view_admin_settings($form, $settings); +} + +/** + * Copy form values into the page cache. + */ +function page_manager_term_view_settings_submit(&$form, &$form_state) { + $form_state['page']->update_values = $form_state['values']; +} + +/** + * Save when the page cache is saved. + */ +function page_manager_term_view_save($subtask, $cache) { + if (isset($cache->update_values)) { + variable_set('page_manager_term_view_type', $cache->update_values['page_manager_term_view_type']); + variable_set('page_manager_taxonomy_breadcrumb', $cache->update_values['page_manager_taxonomy_breadcrumb']); + } +} + +/** + * Provide a setting to the Panels administrative form. + */ +function page_manager_term_view_admin_settings($form, $settings = array()) { + if (empty($settings)) { + $settings = array( + 'page_manager_term_view_type' => page_manager_term_view_get_type(), + 'page_manager_taxonomy_breadcrumb' => variable_get('page_manager_taxonomy_breadcrumb', TRUE), + ); + } + + $form['page_manager_term_view_type'] = array( + '#type' => 'radios', + '#title' => t('Allow multiple terms on taxonomy/term/%term'), + '#options' => array('single' => t('Single term'), 'multiple' => t('Multiple terms')), + '#description' => t('By default, Drupal allows multiple terms as an argument by separating them with commas or plus signs. If you set this to single, that feature will be disabled.') . ' ' . t('This feature does not currently work and is disabled.'), + '#default_value' => $settings['page_manager_term_view_type'], + // @todo -- fix this + '#disabled' => TRUE, + ); + $form['page_manager_taxonomy_breadcrumb'] = array( + '#title' => t('Inject hierarchy of first term into breadcrumb trail'), + '#type' => 'checkbox', + '#default_value' => $settings['page_manager_taxonomy_breadcrumb'], + '#description' => t('If checked, taxonomy term parents will appear in the breadcrumb trail.'), + ); + + return $form; +} + +/** + * Callback to enable/disable the page from the UI. + */ +function page_manager_term_view_enable($cache, $status) { + variable_set('page_manager_term_view_disabled', $status); + + // Set a global flag so that the menu routine knows it needs + // to set a message if enabling cannot be done. + if (!$status) { + $GLOBALS['page_manager_enabling_term_view'] = TRUE; + } +} + +function page_manager_term_view_get_type() { +// $view_type = variable_get('page_manager_term_view_type', 'multiple'); + // Revert to just allowing single. + $view_type = 'single'; + + return $view_type; +} + +/** + * Provide a nice administrative summary of the page so an admin can see at a + * glance what this page does and how it is configured. + */ +function page_manager_term_view_admin_summary($task, $subtask) { + $task_name = page_manager_make_task_name($task['name'], $subtask['name']); + + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('Path')), + array('class' => array('page-summary-data'), 'data' => 'taxonomy/term/%term'), + array('class' => array('page-summary-operation'), 'data' => ''), + ); + + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('Access')), + array('class' => array('page-summary-data'), 'data' => t('This page is publicly accessible.')), + array('class' => array('page-summary-operation'), 'data' => ''), + ); + + $menu = t('No menu entry'); + + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('Menu')), + array('class' => array('page-summary-data'), 'data' => $menu), + array('class' => array('page-summary-operation'), 'data' => ''), + ); + + if (page_manager_term_view_get_type() == 'multiple') { + $message = t('Multiple terms may be used, separated by , or +.'); + } + else { + $message = t('Only a single term may be used.'); + } + + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('%term')), + array('class' => array('page-summary-data'), 'data' => $message), + array('class' => array('page-summary-operation'), 'data' => ''), + ); + + if (variable_get('page_manager_taxonomy_breadcrumb', TRUE)) { + $message = t('Breadcrumb trail will contain taxonomy term hierarchy'); + } + else { + $message = t('Breadcrumb trail will not contain taxonomy term hiearchy.'); + } + + $rows[] = array( + array('class' => array('page-summary-label'), 'data' => t('Breadcrumb')), + array('class' => array('page-summary-data'), 'data' => $message), + array('class' => array('page-summary-operation'), 'data' => ''), + ); + + $output = theme('table', array(), $rows, array('id' => 'page-manager-page-summary')); + return $output; +} + +/** + * Callback to determine if a page is accessible. + * + * @param $task + * The task plugin. + * @param $subtask_id + * The subtask id + * @param $contexts + * The contexts loaded for the task. + * @return + * TRUE if the current user can access the page. + */ +function page_manager_term_view_access_check($task, $subtask_id, $contexts) { + return user_access('access content'); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/user_edit.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/user_edit.inc new file mode 100644 index 0000000..0b11bf0 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/user_edit.inc @@ -0,0 +1,187 @@ + 'page', + 'title' => t('User Edit Template'), + 'admin title' => t('User edit template'), + 'admin description' => t('When enabled, this overrides the default Drupal behavior for displaying user edit form at user/%user/edit.'), + 'admin path' => 'user/%user/edit', + + // Callback to add items to the page managertask administration form: + 'task admin' => 'page_manager_user_edit_task_admin', + + 'hook menu' => 'page_manager_user_edit_menu', + 'hook menu alter' => 'page_manager_user_edit_menu_alter', + + // This is task uses 'context' handlers and must implement these to give the + // handler data it needs. + 'handler type' => 'context', // handler type -- misnamed + 'get arguments' => 'page_manager_user_edit_get_arguments', + 'get context placeholders' => 'page_manager_user_edit_get_contexts', + + // Allow this to be enabled or disabled: + 'disabled' => variable_get('page_manager_user_edit_disabled', TRUE), + 'enable callback' => 'page_manager_user_edit_enable', + 'access callback' => 'page_manager_user_edit_access_check', + ); +} + +/** + * Callback defined by page_manager_user_view_page_manager_tasks(). + * + * Alter the user view input so that user view comes to us rather than the + * normal user view process. + */ +function page_manager_user_edit_menu_alter(&$items, $task) { + if (variable_get('page_manager_user_edit_disabled', TRUE)) { + return; + } + + // Override the user view handler for our purpose. + if ($items['user/%user/edit']['page callback'] == 'drupal_get_form' || variable_get('page_manager_override_anyway', FALSE)) { + $items['user/%user/edit']['page callback'] = 'page_manager_user_edit_page'; + $items['user/%user/edit']['page arguments'] = array(1); + $items['user/%user/edit']['file path'] = $task['path']; + $items['user/%user/edit']['file'] = $task['file']; + if (($categories = _user_categories()) && (count($categories) > 1)) { + foreach ($categories as $key => $category) { + // 'account' is already handled by the MENU_DEFAULT_LOCAL_TASK. + if ($category['name'] != 'account') { + $items['user/%user_category/edit/' . $category['name']]['page callback'] = 'page_manager_user_edit_page'; + $items['user/%user_category/edit/' . $category['name']]['page arguments'] = array(1, 3); + $items['user/%user_category/edit/' . $category['name']]['file path'] = $task['path']; + $items['user/%user_category/edit/' . $category['name']]['file'] = $task['file']; + } + } + } + } + else { + // automatically disable this task if it cannot be enabled. + variable_set('page_manager_user_edit_disabled', TRUE); + if (!empty($GLOBALS['page_manager_enabling_user_edit'])) { + drupal_set_message(t('Page manager module is unable to enable user/%user/edit because some other module already has overridden with %callback.', array('%callback' => $items['user/%user']['page callback'])), 'error'); + } + } +} + +/** + * Entry point for our overridden user view. + * + * This function asks its assigned handlers who, if anyone, would like + * to run with it. If no one does, it passes through to Drupal core's + * user edit, which is drupal_get_form('user_profile_form',$account). + */ +function page_manager_user_edit_page($account, $category = 'account') { + // Store the category on the user for later usage. + $account->user_category = $category; + + // Load my task plugin: + $task = page_manager_get_task('user_edit'); + + // Load the account into a context. + ctools_include('context'); + ctools_include('context-task-handler'); + $contexts = ctools_context_handler_get_task_contexts($task, '', array($account)); + // Build content. @todo -- this may not be right. + user_build_content($account); + + $output = ctools_context_handler_render($task, '', $contexts, array($account->uid)); + if (is_array($output)) { + $output = drupal_render($output); + } + if ($output !== FALSE) { + return $output; + } + + $function = 'drupal_get_form'; + foreach (module_implements('page_manager_override') as $module) { + $call = $module . '_page_manager_override'; + if (($rc = $call('user_edit')) && function_exists($rc)) { + $function = $rc; + break; + } + } + + // Otherwise, fall back. + if ($function == 'drupal_get_form') { + + //In order to ajax fields to work we need to run form_load_include. + //Hence we eschew drupal_get_form and manually build the info and + //call drupal_build_form. + $form_state = array(); + $form_id = 'user_profile_form'; + $args = array($account, $category); + $form_state['build_info']['args'] = $args; + form_load_include($form_state, 'inc', 'user', 'user.pages'); + $output = drupal_build_form($form_id, $form_state); + return $output; + } + //fire off "view" op so that triggers still work + // @todo -- this doesn't work anymore, and the alternatives seem bad. + // will have to figure out how to fix this. + // user_module_invoke('view', $array = array(), $account); + return $function($account); +} + +/** + * Callback to get arguments provided by this task handler. + * + * Since this is the node view and there is no UI on the arguments, we + * create dummy arguments that contain the needed data. + */ +function page_manager_user_edit_get_arguments($task, $subtask_id) { + return array( + array( + 'keyword' => 'user', + 'identifier' => t('User being edited'), + 'id' => 1, + 'name' => 'user_edit', + 'settings' => array(), + ), + ); +} + +/** + * Callback to get context placeholders provided by this handler. + */ +function page_manager_user_edit_get_contexts($task, $subtask_id) { + return ctools_context_get_placeholders_from_argument(page_manager_user_edit_get_arguments($task, $subtask_id)); +} + +/** + * Callback to enable/disable the page from the UI. + */ +function page_manager_user_edit_enable($cache, $status) { + variable_set('page_manager_user_edit_disabled', $status); + // Set a global flag so that the menu routine knows it needs + // to set a message if enabling cannot be done. + if (!$status) { + $GLOBALS['page_manager_enabling_user_edit'] = TRUE; + } +} + +/** + * Callback to determine if a page is accessible. + * + * @param $task + * The task plugin. + * @param $subtask_id + * The subtask id + * @param $contexts + * The contexts loaded for the task. + * @return + * TRUE if the current user can access the page. + */ +function page_manager_user_edit_access_check($task, $subtask_id, $contexts) { + $context = reset($contexts); + return user_edit_access($context->data); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/user_view.inc b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/user_view.inc new file mode 100644 index 0000000..c428384 --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/plugins/tasks/user_view.inc @@ -0,0 +1,161 @@ + 'page', + 'title' => t('User profile template'), + 'admin title' => t('User profile template'), + 'admin description' => t('When enabled, this overrides the default Drupal behavior for displaying user profiles at user/%user. If you add variants, you may use selection criteria such as roles or user access to provide different views of user profiles. If no variant is selected, the default Drupal user view will be used. Please note that if you are using pathauto, aliases may make a node to be somewhere else, but as far as Drupal is concerned, they are still at user/%user.'), + 'admin path' => 'user/%user', + + // Callback to add items to the page managertask administration form: + 'task admin' => 'page_manager_user_view_task_admin', + + 'hook menu' => 'page_manager_user_view_menu', + 'hook menu alter' => 'page_manager_user_view_menu_alter', + + // This is task uses 'context' handlers and must implement these to give the + // handler data it needs. + 'handler type' => 'context', // handler type -- misnamed + 'get arguments' => 'page_manager_user_view_get_arguments', + 'get context placeholders' => 'page_manager_user_view_get_contexts', + + // Allow this to be enabled or disabled: + 'disabled' => variable_get('page_manager_user_view_disabled', TRUE), + 'enable callback' => 'page_manager_user_view_enable', + 'access callback' => 'page_manager_user_view_access_check', + ); +} + +/** + * Callback defined by page_manager_user_view_page_manager_tasks(). + * + * Alter the user view input so that user view comes to us rather than the + * normal user view process. + */ +function page_manager_user_view_menu_alter(&$items, $task) { + if (variable_get('page_manager_user_view_disabled', TRUE)) { + return; + } + + // Override the user view handler for our purpose. + if ($items['user/%user']['page callback'] == 'user_view_page' || variable_get('page_manager_override_anyway', FALSE)) { + $items['user/%user']['page callback'] = 'page_manager_user_view_page'; + $items['user/%user']['file path'] = $task['path']; + $items['user/%user']['file'] = $task['file']; + } + else { + // automatically disable this task if it cannot be enabled. + variable_set('page_manager_user_view_disabled', TRUE); + if (!empty($GLOBALS['page_manager_enabling_user_view'])) { + drupal_set_message(t('Page manager module is unable to enable user/%user because some other module already has overridden with %callback.', array('%callback' => $items['user/%user']['page callback'])), 'error'); + } + } +} + +/** + * Entry point for our overridden user view. + * + * This function asks its assigned handlers who, if anyone, would like + * to run with it. If no one does, it passes through to Drupal core's + * user view, which is user_page_view(). + */ +function page_manager_user_view_page($account) { + // Load my task plugin: + $task = page_manager_get_task('user_view'); + + // Load the account into a context. + ctools_include('context'); + ctools_include('context-task-handler'); + $contexts = ctools_context_handler_get_task_contexts($task, '', array($account)); + + // Build content. @todo -- this may not be right. + user_build_content($account); + + $output = ctools_context_handler_render($task, '', $contexts, array($account->uid)); + if ($output !== FALSE) { + return $output; + } + + $function = 'user_view'; + foreach (module_implements('page_manager_override') as $module) { + $call = $module . '_page_manager_override'; + if (($rc = $call('user_view')) && function_exists($rc)) { + $function = $rc; + break; + } + } + + // Otherwise, fall back. + if ($function == 'user_view') { + module_load_include('inc', 'user', 'user.pages'); + } + //fire off "view" op so that triggers still work + // @todo -- this doesn't work anymore, and the alternatives seem bad. + // will have to figure out how to fix this. +// user_module_invoke('view', $array = array(), $account); + return $function($account); +} + +/** + * Callback to get arguments provided by this task handler. + * + * Since this is the node view and there is no UI on the arguments, we + * create dummy arguments that contain the needed data. + */ +function page_manager_user_view_get_arguments($task, $subtask_id) { + return array( + array( + 'keyword' => 'user', + 'identifier' => t('User being viewed'), + 'id' => 1, + 'name' => 'entity_id:user', + 'settings' => array(), + ), + ); +} + +/** + * Callback to get context placeholders provided by this handler. + */ +function page_manager_user_view_get_contexts($task, $subtask_id) { + return ctools_context_get_placeholders_from_argument(page_manager_user_view_get_arguments($task, $subtask_id)); +} + +/** + * Callback to enable/disable the page from the UI. + */ +function page_manager_user_view_enable($cache, $status) { + variable_set('page_manager_user_view_disabled', $status); + + // Set a global flag so that the menu routine knows it needs + // to set a message if enabling cannot be done. + if (!$status) { + $GLOBALS['page_manager_enabling_user_view'] = TRUE; + } +} + +/** + * Callback to determine if a page is accessible. + * + * @param $task + * The task plugin. + * @param $subtask_id + * The subtask id + * @param $contexts + * The contexts loaded for the task. + * @return + * TRUE if the current user can access the page. + */ +function page_manager_user_view_access_check($task, $subtask_id, $contexts) { + $context = reset($contexts); + return user_view_access($context->data); +} diff --git a/sites/all/modules/contrib/ctools/page_manager/theme/page-manager-edit-page.tpl.php b/sites/all/modules/contrib/ctools/page_manager/theme/page-manager-edit-page.tpl.php new file mode 100644 index 0000000..85510cd --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/theme/page-manager-edit-page.tpl.php @@ -0,0 +1,53 @@ + +
    + +
    + +
    + +
    + +
    +
    +
    + +
    +
    +
    +
    +
    +
    + + +
    + +
    + +
    + +
    + +
    + +
    + +
    + + +
    +
    +
    +
    +
    + +
    \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/page_manager/theme/page_manager.theme.inc b/sites/all/modules/contrib/ctools/page_manager/theme/page_manager.theme.inc new file mode 100644 index 0000000..6435d1c --- /dev/null +++ b/sites/all/modules/contrib/ctools/page_manager/theme/page_manager.theme.inc @@ -0,0 +1,118 @@ +task; + + $page = &$vars['page']; + + $vars['locked'] = ''; + $vars['changed'] = ''; + if (!empty($page->locked)) { + $vars['locked'] = theme('page_manager_lock', array('page' => $page)); + $vars['changed'] = theme('page_manager_changed', array('text' => t('Locked'), 'description' => t('This page is being edited by another user and you cannot make changes to it.'))); + } + else if (!empty($page->new)) { + $vars['changed'] = theme('page_manager_changed', array('text' => t('New'), 'description' => t('This page is newly created and has not yet been saved to the database. It will not be available until you save it.'))); + } + else if (!empty($page->changed)) { + $vars['changed'] = theme('page_manager_changed', array('text' => t('Changed'), 'description' => t('This page has been modified, but these modifications are not yet live. While modifying this page, it is locked from modification by other users.'))); + } + + $form_state = array( + 'page' => &$vars['page'], + ); + + $active = $vars['content']['active']; + if ($active[0] == 'handlers' && isset($vars['operations'][$active[1]])) { + $vars['operations']['secondary'] = $vars['operations'][$active[1]]; + } +} + +/** + * Turn the rearrange form into a table with tablesorting on. + */ +function theme_page_manager_handler_rearrange($vars) { + $form = &$vars['form']; + // Assemble the data for a table from everything in $form['handlers'] + foreach (element_children($form['handlers']) as $id) { + // provide a reference shortcut. + $element = &$form['handlers'][$id]; + if (isset($element['title'])) { + $row = array(); + + $row[] = array( + 'data' => render($element['title']), + 'class' => array('page-manager-handler'), + ); + + $element['weight']['#attributes']['class'][] = 'weight'; + $row[] = render($element['weight']); + + $rows[] = array('data' => $row, 'class' => array('draggable'), 'id' => 'page-manager-row-' . $id); + } + } + + if (empty($rows)) { + $rows[] = array(array('data' => t('No task handlers are defined for this task.'), 'colspan' => '5')); + } + + $header = array( + array('data' => t('Variant'), 'class' => array('page-manager-handler')), + t('Weight'), + ); + + drupal_add_tabledrag('page-manager-arrange-handlers', 'order', 'sibling', 'weight'); + + $output = theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array('id' => 'page-manager-arrange-handlers'))); + $output .= drupal_render_children($form); + return $output; +} + +/** + * Draw the "this task is locked from editing" box. + */ +function theme_page_manager_lock($vars) { + $page = $vars['page']; + + $account = user_load($page->locked->uid); + $name = theme('username', array('account' => $account)); + $lock_age = format_interval(REQUEST_TIME - $page->locked->updated); + $break = url(page_manager_edit_url($page->task_name, array('actions', 'break-lock'))); + + ctools_add_css('ctools'); + $output = '
    '; + $output .= t('This page is being edited by user !user, and is therefore locked from editing by others. This lock is !age old. Click here to break this lock.', array('!user' => $name, '!age' => $lock_age, '!break' => $break)); + $output .= '
    '; + return $output; +} + +/** + * Draw the "you have unsaved changes and this task is locked." message. + */ +function theme_page_manager_changed($vars) { + $text = $vars['text']; + $description = $vars['description']; + + ctools_add_css('ctools'); + $output = '
    '; + $output .= $text; + $output .= '
    '; + + return $output; +} diff --git a/sites/all/modules/contrib/ctools/plugins/access/book.inc b/sites/all/modules/contrib/ctools/plugins/access/book.inc new file mode 100644 index 0000000..4b4f7ea --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/book.inc @@ -0,0 +1,94 @@ + t("Book: node is in a book"), + 'description' => t('Control access based upon a node belonging to a book.'), + 'callback' => 'ctools_book_node_ctools_access_check', + 'default' => array('book' => array()), + 'settings form' => 'ctools_book_node_ctools_access_settings', + 'settings form submit' => 'ctools_book_node_ctools_access_settings_submit', + 'summary' => 'ctools_book_node_ctools_access_summary', + 'required context' => new ctools_context_required(t('Node'), 'node'), + ); +} + +/** + * Settings form for the 'by book_node' access plugin. + */ +function ctools_book_node_ctools_access_settings($form, &$form_state, $conf) { + $options = array( + 'any' => t('In any book'), + ); + $books = book_get_books(); + foreach ($books as $bid => $book) { + $options[$bid] = $book['title']; + } + $form['settings']['book'] = array( + '#title' => t('Book'), + '#type' => 'checkboxes', + '#options' => $options, + '#description' => t('Pass only if the node belongs to one of the selected books'), + '#default_value' => $conf['book'], + '#required' => TRUE, + ); + return $form; +} + +/** + * Check for access. + */ +function ctools_book_node_ctools_access_check($conf, $context) { + // As far as I know there should always be a context at this point, but this + // is safe. + if (empty($context) || empty($context->data) || empty($context->data->book)) { + return FALSE; + } + + if ($conf['book']['any']) { + return !empty($context->data->book); + } + + foreach ($conf['book'] as $bid => $value) { + if ($bid == 'any') { + continue; + } + if ($value && ($bid == $context->data->book['bid'])) { + return TRUE; + } + } + + return FALSE; +} + +/** + * Provide a summary description based upon the checked node_languages. + */ +function ctools_book_node_ctools_access_summary($conf, $context) { + if ($conf['book']['any']) { + return t('@identifier belongs to a book', array('@identifier' => $context->identifier)); + } + + $books = array(); + foreach ($conf['book'] as $bid => $value) { + if ($value) { + $node = node_load($bid); + $books[] = $node->title; + } + } + + if (count($books) == 1) { + return t('@identifier belongs to the book "@book"', array('@book' => $books[0], '@identifier' => $context->identifier)); + } + + return t('@identifier belongs in multiple books', array('@identifier' => $context->identifier)); +} diff --git a/sites/all/modules/contrib/ctools/plugins/access/compare_users.inc b/sites/all/modules/contrib/ctools/plugins/access/compare_users.inc new file mode 100644 index 0000000..c271ff4 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/compare_users.inc @@ -0,0 +1,70 @@ + t("User: compare"), + 'description' => t('Compare two users (logged-in user and user being viewed, for example)'), + 'callback' => 'ctools_compare_users_access_check', + 'default' => array( + 'equality' => 1, + ), + 'settings form' => 'ctools_compare_users_settings', + 'summary' => 'ctools_compare_users_ctools_access_summary', + 'required context' => array( + new ctools_context_required(t('First User'), 'user'), + new ctools_context_required(t("Second User"), 'user') + ), +); + +/** + * Settings form for the 'by perm' access plugin + */ +function ctools_compare_users_settings($form, &$form_state, $conf) { + + $form['settings']['helptext'] = array( + '#type' => 'markup', + '#value' => '
    ' . t('Grant access based on comparison of the two user contexts. For example, to grant access to a user to view their own profile, choose "logged in user" and "user being viewed" and say "grant access if equal". When they\'re the same, access will be granted.') . '
    ', + ); + + $form['settings']['equality'] = array( + '#type' => 'radios', + '#title' => t('Grant access if user contexts are'), + '#options' => array(1 => t('Equal'), 0 => t('Not equal')), + '#default_value' => $conf['equality'], + ); + return $form; +} + +/** + * Check for access. + */ +function ctools_compare_users_access_check($conf, $context) { + + if (empty($context) || count($context) != 2 || empty($context[0]->data) || empty($context[1]->data)) { + return FALSE; + } + $account1 = $context[0]->data; + $account2 = $context[1]->data; + + // xor returns false if the two bools are the same, and true if they are not. + // i.e, if we asked for equality and they are equal, return true. + // If we asked for inequality and they are equal, return false. + return ($account1->uid == $account2->uid) xor empty($conf['equality']); +} + +/** + * Describe an instance of this plugin. + */ +function ctools_compare_users_ctools_access_summary($conf, $context) { + $comparison = !empty($conf['equality']) ? "is" : 'is not'; + + return t('@id1 @comp @id2', array('@comp' => $comparison, '@id1' => $context[0]->identifier, '@id2' => $context[1]->identifier)); +} diff --git a/sites/all/modules/contrib/ctools/plugins/access/context_exists.inc b/sites/all/modules/contrib/ctools/plugins/access/context_exists.inc new file mode 100644 index 0000000..aabc62d --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/context_exists.inc @@ -0,0 +1,51 @@ + t("Context exists"), + 'description' => t('Control access by whether or not a context exists and contains data.'), + 'callback' => 'ctools_context_exists_ctools_access_check', + 'settings form' => 'ctools_context_exists_ctools_access_settings', + 'summary' => 'ctools_context_exists_ctools_access_summary', + 'required context' => new ctools_context_required(t('Context'), 'any', TRUE), + 'defaults' => array('exists' => TRUE), +); + +/** + * Settings form + */ +function ctools_context_exists_ctools_access_settings($form, &$form_state, $conf) { + $form['settings']['exists'] = array( + '#type' => 'radios', + '#description' => t("Check to see if the context exists (contains data) or does not exist (contains no data). For example, if a context is optional and the path does not contain an argument for that context, it will not exist."), + '#options' => array(TRUE => t('Exists'), FALSE => t("Doesn't exist")), + '#default_value' => $conf['exists'], + ); + return $form; +} + +/** + * Check for access + */ +function ctools_context_exists_ctools_access_check($conf, $context) { + // xor returns false if the two bools are the same, and true if they are not. + // i.e, if we asked for context_exists and it does, return true. + // If we asked for context does not exist and it does, return false. + return (empty($context->data) xor !empty($conf['exists'])); +} + +/** + * Provide a summary description based upon the specified context + */ +function ctools_context_exists_ctools_access_summary($conf, $context) { + if (!empty($conf['exists'])) { + return t('@identifier exists', array('@identifier' => $context->identifier)); + } + else { + return t('@identifier does not exist', array('@identifier' => $context->identifier)); + } +} diff --git a/sites/all/modules/contrib/ctools/plugins/access/entity_bundle.inc b/sites/all/modules/contrib/ctools/plugins/access/entity_bundle.inc new file mode 100644 index 0000000..e07a048 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/entity_bundle.inc @@ -0,0 +1,136 @@ + t("Entity: bundle"), + 'description' => t('Control access by entity bundle.'), + 'callback' => 'ctools_entity_bundle_ctools_access_check', + 'default' => array('type' => array()), + 'settings form' => 'ctools_entity_bundle_ctools_access_settings', + 'settings form submit' => 'ctools_entity_bundle_ctools_access_settings_submit', + 'summary' => 'ctools_entity_bundle_ctools_access_summary', + 'restrictions' => 'ctools_entity_bundle_ctools_access_restrictions', + 'get child' => 'ctools_entity_bundle_ctools_access_get_child', + 'get children' => 'ctools_entity_bundle_ctools_access_get_children', +); + +function ctools_entity_bundle_ctools_access_get_child($plugin, $parent, $child) { + $plugins = ctools_entity_bundle_ctools_access_get_children($plugin, $parent); + return $plugins[$parent . ':' . $child]; +} + +function ctools_entity_bundle_ctools_access_get_children($plugin, $parent) { + $entities = entity_get_info(); + $plugins = array(); + foreach ($entities as $entity_type => $entity) { + $plugin['title'] = t('@entity: Bundle', array('@entity' => $entity['label'])); + $plugin['keyword'] = $entity_type; + $plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type)); + $plugin['name'] = $parent . ':' . $entity_type; + $plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type); + $plugins[$parent . ':' . $entity_type] = $plugin; + } + + return $plugins; +} + +/** + * Settings form for the 'by entity_bundle' access plugin + */ +function ctools_entity_bundle_ctools_access_settings($form, &$form_state, $conf) { + $plugin = $form_state['plugin']; + $entity_type = explode(':', $plugin['name']); + $entity_type = $entity_type[1]; + $entity = entity_get_info($entity_type); + foreach ($entity['bundles'] as $type => $info) { + $options[$type] = check_plain($info['label']); + } + + $form['settings']['type'] = array( + '#title' => t('Entity Bundle'), + '#type' => 'checkboxes', + '#options' => $options, + '#description' => t('Only the checked entity bundles will be valid.'), + '#default_value' => $conf['type'], + ); + return $form; +} + +/** + * Compress the entity bundles allowed to the minimum. + */ +function ctools_entity_bundle_ctools_access_settings_submit($form, &$form_state) { + $form_state['values']['settings']['type'] = array_filter($form_state['values']['settings']['type']); +} + +/** + * Check for access. + */ +function ctools_entity_bundle_ctools_access_check($conf, $context, $plugin) { + list($plugin_name, $entity_type) = explode(':', $plugin['name']); + if (!$entity_type) { + return FALSE; + }; + + $entity = entity_get_info($entity_type); + // As far as I know there should always be a context at this point, but this + // is safe. + if (empty($context) || empty($context->data) || empty($context->data->{$entity['entity keys']['bundle']})) { + return FALSE; + } + + if (array_filter($conf['type']) && empty($conf['type'][$context->data->{$entity['entity keys']['bundle']}])) { + return FALSE; + } + + return TRUE; +} + +/** + * Inform the UI that we've eliminated a bunch of possibilities for this + * context. + */ +function ctools_entity_bundle_ctools_access_restrictions($conf, &$context) { + if (isset($context->restrictions['type'])) { + $context->restrictions['type'] = array_unique(array_merge($context->restrictions['type'], array_keys(array_filter($conf['type'])))); + } + else { + $context->restrictions['type'] = array_keys(array_filter($conf['type'])); + } +} + +/** + * Provide a summary description based upon the checked entity_bundle. + */ +function ctools_entity_bundle_ctools_access_summary($conf, $context, $plugin) { + if (!isset($conf['type'])) { + $conf['type'] = array(); + } + + list($plugin_name, $entity_type) = explode(':', $plugin['name']); + if (!$entity_type) { + return t('Error, misconfigured entity_bundle access plugin'); + }; + + $entity = entity_get_info($entity_type); + + $names = array(); + foreach (array_filter($conf['type']) as $type) { + $names[] = check_plain($entity['bundles'][$type]['label']); + } + + if (empty($names)) { + return t('@identifier is any bundle', array('@identifier' => $context->identifier)); + } + + return format_plural(count($names), '@identifier is bundle "@types"', '@identifier bundle is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier)); +} + diff --git a/sites/all/modules/contrib/ctools/plugins/access/entity_field_value.inc b/sites/all/modules/contrib/ctools/plugins/access/entity_field_value.inc new file mode 100644 index 0000000..fa94a48 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/entity_field_value.inc @@ -0,0 +1,410 @@ + t("(Custom) Entity: Field Value"), + 'description' => t('Control access by entity field value.'), + 'callback' => 'ctools_entity_field_value_ctools_access_check', + 'default' => array('type' => array()), + 'settings form' => 'ctools_entity_field_value_ctools_access_settings', + 'settings form submit' => 'ctools_entity_field_value_ctools_access_settings_submit', + 'summary' => 'ctools_entity_field_value_ctools_access_summary', + 'get child' => 'ctools_entity_field_value_ctools_access_get_child', + 'get children' => 'ctools_entity_field_value_ctools_access_get_children', +); + +function ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $child) { + $plugins = &drupal_static(__FUNCTION__, array()); + if (empty($plugins[$parent . ':' . $child])) { + list($entity_type, $bundle_type, $field_name) = explode(':', $child); + $plugins[$parent . ':' . $child] = _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name); + } + + return $plugins[$parent . ':' . $child]; +} + +function ctools_entity_field_value_ctools_access_get_children($plugin, $parent) { + $plugins = &drupal_static(__FUNCTION__, array()); + if (!empty($plugins)) { + return $plugins; + } + $entities = entity_get_info(); + foreach ($entities as $entity_type => $entity) { + foreach ($entity['bundles'] as $bundle_type => $bundle) { + foreach (field_info_instances($entity_type, $bundle_type) as $field_name => $field) { + if (!isset($plugins[$parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name])) { + $plugin = _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name, $entity, $bundle, $field); + $plugins[$parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name] = $plugin; + } + } + } + } + + return $plugins; +} + +function _ctools_entity_field_value_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name, $entity = NULL, $bundle = NULL, $field = NULL) { + // check that the entity, bundle and field arrays have a value. + // If not, load theme using machine names. + if (empty($entity)) { + $entity = entity_get_info($entity_type); + } + + if (empty($bundle)) { + $bundle = $entity['bundles'][$bundle_type]; + } + + if (empty($field)) { + $field_instances = field_info_instances($entity_type, $bundle_type); + $field = $field_instances[$field_name]; + } + + $plugin['title'] = t('@entity @type: @field Field', array('@entity' => $entity['label'], '@type' => $bundle_type, '@field' => $field['label'])); + $plugin['keyword'] = $entity_type; + $plugin['description'] = t('Control access by @entity entity bundle.', array('@entity' => $entity_type)); + $plugin['name'] = $parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name; + $plugin['required context'] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type, array( + 'type' => $bundle_type, + )); + + return $plugin; +} + +/** + * Settings form for the 'by entity_bundle' access plugin + */ +function ctools_entity_field_value_ctools_access_settings($form, &$form_state, $conf) { + $plugin = $form_state['plugin']; + list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']); + $entity_info = entity_get_info($entity_type); + $instances = field_info_instances($entity_type, $bundle_type); + $instance = $instances[$field_name]; + $field = field_info_field_by_id($instance['field_id']); + foreach ($field['columns'] as $column => $attributes) { + $columns[$column] = _field_sql_storage_columnname($field_name, $column); + } + ctools_include('fields'); + $entity = (object)array( + $entity_info['entity keys']['bundle'] => $bundle_type, + ); + + foreach ($columns as $column => $sql_column) { + if (isset($conf[$sql_column])) { + if (is_array($conf[$sql_column])) { + foreach ($conf[$sql_column] as $delta => $conf_value) { + if (is_numeric($delta)) { + if (is_array($conf_value)) { + $entity->{$field_name}[LANGUAGE_NONE][$delta][$column] = $conf_value[$column]; + } + else { + $entity->{$field_name}[LANGUAGE_NONE][$delta][$column] = $conf_value; + } + } + } + } + else { + $entity->{$field_name}[LANGUAGE_NONE][0][$column] = $conf[$sql_column]; + } + } + } + + $form['#parents'] = array('settings'); + $langcode = field_valid_language(NULL); + $form['settings'] += (array) ctools_field_invoke_field($instance, 'form', $entity_type, $entity, $form, $form_state, array('default' => TRUE, 'language' => $langcode)); + // weight is really not important once this is populated and will only interfere with the form layout. + foreach (element_children($form['settings']) as $element) { + unset($form['settings'][$element]['#weight']); + } + + return $form; +} + +function ctools_entity_field_value_ctools_access_settings_submit($form, &$form_state) { + $plugin = $form_state['plugin']; + list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']); + $langcode = field_valid_language(NULL); + $langcode = isset($form_state['input']['settings'][$field_name][$langcode]) ? $langcode : LANGUAGE_NONE; + $instances = field_info_instances($entity_type, $bundle_type); + $instance = $instances[$field_name]; + $field = field_info_field_by_id($instance['field_id']); + foreach ($field['columns'] as $column => $attributes) { + $columns[$column] = _field_sql_storage_columnname($field_name, $column); + } + $items = _ctools_entity_field_value_get_proper_form_items($field, $form_state['values']['settings'][$field_name][$langcode], array_keys($columns)); + foreach ($columns as $column => $sql_column) { + $column_items = _ctools_entity_field_value_filter_items_by_column($items, $column); + $form_state['values']['settings'][$sql_column] = $column_items; + } + $form_state['values']['settings'][$field_name][$langcode] = $items; +} + +function _ctools_entity_field_value_get_proper_form_items($field, $form_items, $columns) { + $items = array(); + + if (!is_array($form_items)) { // Single value item. + foreach ($columns as $column) { + $items[0][$column] = $form_items; + } + return $items; + } + + foreach ($form_items as $delta => $value) { + $item = array(); + if (is_numeric($delta)) { // Array of field values. + if (!is_array($value)) { // Single value in array. + foreach ($columns as $column) { + $item[$column] = $value; + } + } + else { // Value has colums. + foreach ($columns as $column) { + $item[$column] = isset($value[$column]) ? $value[$column] : ''; + } + } + } + $items[] = $item; + } + + // Check if $form_items is an array of columns. + $item = array(); + $has_columns = FALSE; + foreach ($columns as $column) { + if (isset($form_items[$column])) { + $has_columns = TRUE; + $item[$column] = $form_items[$column]; + } + else { + $item[$column] = ''; + } + } + if ($has_columns) { + $items[] = $item; + } + + // Remove empty values. + $items = _field_filter_items($field, $items); + return $items; +} + +function _ctools_entity_field_value_filter_items_by_column($items, $column) { + $column_items = array(); + foreach ($items as $delta => $values) { + $column_items[$delta] = isset($values[$column]) ? $values[$column] : ''; + } + return $column_items; +} + +/** + * Check for access. + */ +function ctools_entity_field_value_ctools_access_check($conf, $context, $plugin) { + if ((!is_object($context)) || (empty($context->data))) { + // If the context doesn't exist -- for example, a newly added entity + // reference is used as a pane visibility criteria -- we deny access. + return FALSE; + } + + list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']); + + if ($field_items = field_get_items($entity_type, $context->data, $field_name)) { + $langcode = field_language($entity_type, $context->data, $field_name); + // Get field storage columns. + $instance = field_info_instance($entity_type, $field_name, $bundle_type); + $field = field_info_field_by_id($instance['field_id']); + $columns = array(); + foreach ($field['columns'] as $column => $attributes) { + $columns[$column] = _field_sql_storage_columnname($field_name, $column); + } + + if (isset($conf[$field_name])) { + // We have settings for this field. + $conf_value_array = _ctools_entity_field_value_ctools_access_get_conf_field_values($conf[$field_name], $langcode); + if (empty($conf_value_array)) { + return FALSE; + } + + // Check field value. + foreach ($field_items as $field_value) { + // Iterate through config values. + foreach ($conf_value_array as $conf_value) { + $match = FALSE; + foreach ($field_value as $field_column => $value) { + // Check access only for stored in config column values. + if (isset($conf_value[$field_column])) { + if ($value == $conf_value[$field_column]) { + $match = TRUE; + } + else { + $match = FALSE; + break; + } + } + } + if ($match) { + return TRUE; + } + } + } + return FALSE; + } + } + + return FALSE; +} + +function _ctools_entity_field_value_ctools_access_get_conf_field_values($values, $langcode = LANGUAGE_NONE) { + if (!is_array($values) || !isset($values[$langcode])) { + return NULL; + } + $conf_values = array(); + + foreach ($values[$langcode] as $delta => $value) { + $conf_values[$delta] = $value; + } + + return $conf_values; +} + +/** + * Provide a summary description based upon the checked entity_bundle. + */ +function ctools_entity_field_value_ctools_access_summary($conf, $context, $plugin) { + list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']); + $instances = field_info_instances($entity_type, $bundle_type); + $instance = $instances[$field_name]; + $field = field_info_field_by_id($instance['field_id']); + $entity_info = entity_get_info($entity_type); + $entity = (object)array( + $entity_info['entity keys']['bundle'] => $bundle_type, + ); + $keys = array(); + $value_keys = array(); + $keyed_elements = array(); + foreach ($field['columns'] as $column => $attributes) { + $conf_key = _field_sql_storage_columnname($field_name, $column); + $keyed_elements["@{$column}_value"] = array(); + + if (isset($conf[$conf_key])) { + if (is_array($conf[$conf_key])) { + $i = 0; + foreach ($conf[$conf_key] as $conf_value) { + if (!is_array($conf_value)) { + $entity->{$field_name}[LANGUAGE_NONE][$i][$column] = $conf_value; + $keyed_elements["@{$column}_value"][$i] = array('#markup' => $conf_value); + } + elseif (isset($conf_value[$column])) { + $entity->{$field_name}[LANGUAGE_NONE][$i][$column] = $conf_value[$column]; + $keyed_elements["@{$column}_value"][$i] = array('#markup' => $conf_value[$column]); + } + $i++; + } + } + else { + $entity->{$field_name}[LANGUAGE_NONE][0][$column] = $conf[$conf_key]; + $keyed_elements["@{$column}_value"][0] = array('#markup' => $conf[$conf_key]); + } + } + + $keys['@' . $column] = $column; + $value_keys[] = "@{$column}_value"; + } + $elements = array(); + $items = isset($entity->{$field_name}[LANGUAGE_NONE]) ? $entity->{$field_name}[LANGUAGE_NONE] : array(); + $view_mode = 'full'; + ctools_include('fields'); + $display = field_get_display($instance, $view_mode, $entity); + if (!isset($display['module'])) { + $display['module'] = $field['module']; + } + if (isset($display['module'])) { + // Choose simple formatter for well known cases. + switch ($display['module']) { + case 'text': + $display['type'] = 'text_default'; + break; + + case 'list': + $display['type'] = 'list_default'; + if ($field['type'] == 'list_boolean') { + $allowed_values = list_allowed_values($field, $instance, $entity_type, $entity); + foreach ($items as $item) { + if (isset($allowed_values[$item['value']])) { + if ($allowed_values[$item['value']] == '') { + $display['type'] = 'list_key'; + break; + } + } + else { + $display['type'] = 'list_key'; + } + } + } + break; + + case 'taxonomy': + $display['type'] = 'taxonomy_term_reference_plain'; + break; + + case 'entityreference': + $display['type'] = 'entityreference_label'; + break; + + default : + // Use field instance formatter setting. + break; + } + + $function = $display['module'] . '_field_formatter_view'; + if (function_exists($function)) { + $entity_group = array(0 => $entity); + $item_group = array(0 => $items); + $instance_group = array(0 => $instance); + field_default_prepare_view($entity_type, $entity_group, $field, $instance_group, LANGUAGE_NONE, $item_group, $display); + $elements = $function($entity_type, $entity, $field, $instance, LANGUAGE_NONE, $item_group[0], $display); + } + } + if (count($elements) > 0) { + foreach ($field['columns'] as $column => $attributes) { + if (count($field['columns']) == 1) { + $keyed_elements["@{$column}_value"] = $elements; + } + } + } + $values = array(); + foreach ($value_keys as $key) { + $output = array(); + $elements = $keyed_elements[$key]; + if (is_array($elements)) { + foreach ($elements as $element_key => $element) { + if (is_numeric($element_key)) { + $value_str= strip_tags(drupal_render($element)); + if (strlen($value_str) > 0) { + $output[] = $value_str; + } + } + } + } + else { + $value_str = strip_tags(drupal_render($elements)); + if (strlen($value_str) > 0) { + $output[] = $value_str; + } + } + $value = implode(', ', $output); + if ($value !== '') { + $values[$key] = implode(', ', $output); + } + } + $string = ''; + $value_count = count($values); + foreach ($keys as $key_name => $column) { + if (isset($values[$key_name . '_value'])) { + $string .= ($value_count > 1) ? " @{$column} = @{$column}_value" : "@{$column}_value"; + } + } + return t('@field is set to "!value"', array('@field' => $instance['label'], '!value' => format_string($string, array_merge($keys, $values)))); +} diff --git a/sites/all/modules/contrib/ctools/plugins/access/front.inc b/sites/all/modules/contrib/ctools/plugins/access/front.inc new file mode 100644 index 0000000..1bbc6e0 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/front.inc @@ -0,0 +1,46 @@ + t('Front page'), + 'description' => t('Is this the front page.'), + 'callback' => 'ctools_front_ctools_access_check', + 'default' => array('negate' => 0), + 'settings form' => 'ctools_front_ctools_access_settings', + 'summary' => 'ctools_front_ctools_access_summary', +); + +/** + * Settings form for the 'by parent term' access plugin + */ +function ctools_front_ctools_access_settings($form, &$form_state, $conf) { + // No additional configuration necessary. + return $form; +} + +/** + * Check for access. + */ +function ctools_front_ctools_access_check($conf, $context) { + if (drupal_is_front_page()) { + return TRUE; + } + else { + return FALSE; + } +} + +/** + * Provide a summary description based upon the checked terms. + */ +function ctools_front_ctools_access_summary($conf, $context) { + return t('The front page'); +} diff --git a/sites/all/modules/contrib/ctools/plugins/access/node.inc b/sites/all/modules/contrib/ctools/plugins/access/node.inc new file mode 100644 index 0000000..e69de29 diff --git a/sites/all/modules/contrib/ctools/plugins/access/node_access.inc b/sites/all/modules/contrib/ctools/plugins/access/node_access.inc new file mode 100644 index 0000000..fcd275d --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/node_access.inc @@ -0,0 +1,89 @@ + t("Node: accessible"), + 'description' => t('Control access with built in Drupal node access test.'), + 'callback' => 'ctools_node_access_ctools_access_check', + 'default' => array('type' => 'view'), + 'settings form' => 'ctools_node_access_ctools_access_settings', + 'settings form submit' => 'ctools_node_access_ctools_access_settings_submit', + 'summary' => 'ctools_node_access_ctools_access_summary', + 'required context' => array( + new ctools_context_required(t('User'), 'user'), + new ctools_context_required(t('Node'), 'node'), + ), +); + +/** + * Settings form for the 'by node_access' access plugin + */ +function ctools_node_access_ctools_access_settings($form, &$form_state, $conf) { + $form['settings']['type'] = array( + '#title' => t('Operation'), + '#type' => 'radios', + '#options' => array( + 'view' => t('View'), + 'update' => t('Update'), + 'delete' => t('Delete'), + 'create' => t('Create nodes of the same type'), + ), + '#description' => t('Using built in Drupal node access rules, determine if the user can perform the selected operation on the node.'), + '#default_value' => $conf['type'], + ); + return $form; +} + +/** + * Check for access. + */ +function ctools_node_access_ctools_access_check($conf, $context) { + // As far as I know there should always be a context at this point, but this + // is safe. + list($user_context, $node_context) = $context; + if (empty($node_context) || empty($node_context->data) || empty($node_context->data->type)) { + return FALSE; + } + + if (empty($user_context) || empty($user_context->data)) { + return FALSE; + } + + if ($conf['type'] == 'create') { + return node_access('create', $node_context->data->type, $user_context->data); + } + else { + return node_access($conf['type'], $node_context->data, $user_context->data); + } +} + +/** + * Provide a summary description based upon the checked node_accesss. + */ +function ctools_node_access_ctools_access_summary($conf, $context) { + list($user_context, $node_context) = $context; + $replacement = array('@user' => $user_context->identifier, '@node' => $node_context->identifier); + + switch ($conf['type']) { + case 'view': + return t('@user can view @node.', $replacement); + + case 'update': + return t('@user can edit @node.', $replacement); + + case 'delete': + return t('@user can delete @node.', $replacement); + + case 'create': + return t('@user can create nodes of the same type as @node.', $replacement); + } +} + diff --git a/sites/all/modules/contrib/ctools/plugins/access/node_comment.inc b/sites/all/modules/contrib/ctools/plugins/access/node_comment.inc new file mode 100644 index 0000000..915ee20 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/node_comment.inc @@ -0,0 +1,31 @@ + t("Node: comments are open"), + 'description' => t('Control access by the nodes comment status.'), + 'callback' => 'ctools_node_comment_ctools_access_check', + 'summary' => 'ctools_node_comment_ctools_access_summary', + 'required context' => new ctools_context_required(t('Node'), 'node'), +); + +/** + * Checks for access. + */ +function ctools_node_comment_ctools_access_check($conf, $context) { + return (!empty($context->data) && $context->data->comment == 2); +} + +/** + * Provides a summary description based upon the checked node_status. + */ +function ctools_node_comment_ctools_access_summary($conf, $context) { + return t('Returns true if the nodes comment status is "open".'); +} diff --git a/sites/all/modules/contrib/ctools/plugins/access/node_language.inc b/sites/all/modules/contrib/ctools/plugins/access/node_language.inc new file mode 100644 index 0000000..0fdcfc6 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/node_language.inc @@ -0,0 +1,114 @@ + t("Node: language"), + 'description' => t('Control access by node language.'), + 'callback' => 'ctools_node_language_ctools_access_check', + 'default' => array('language' => array()), + 'settings form' => 'ctools_node_language_ctools_access_settings', + 'settings form submit' => 'ctools_node_language_ctools_access_settings_submit', + 'summary' => 'ctools_node_language_ctools_access_summary', + 'required context' => new ctools_context_required(t('Node'), 'node'), + ); +} + +/** + * Settings form for the 'by node_language' access plugin + */ +function ctools_node_language_ctools_access_settings($form, &$form_state, $conf) { + $options = array( + 'current' => t('Current site language'), + 'default' => t('Default site language'), + 'no_language' => t('No language'), + ); + $options = array_merge($options, locale_language_list()); + $form['settings']['language'] = array( + '#title' => t('Language'), + '#type' => 'checkboxes', + '#options' => $options, + '#description' => t('Pass only if the node is in one of the selected languages.'), + '#default_value' => $conf['language'], + ); + return $form; +} + +/** + * Check for access. + */ +function ctools_node_language_ctools_access_check($conf, $context) { + // As far as I know there should always be a context at this point, but this + // is safe. + if (empty($context) || empty($context->data) || !isset($context->data->language)) { + return FALSE; + } + + global $language; + + // Specialcase: if 'no language' is checked, return TRUE if the language field is + // empty. + if (!empty($conf['language']['no_language'])) { + if (empty($context->data->language)) { + return TRUE; + } + } + + // Specialcase: if 'current' is checked, return TRUE if the current site language + // matches the node language. + if (!empty($conf['language']['current'])) { + if ($context->data->language == $language->language) { + return TRUE; + } + } + + // Specialcase: If 'default' is checked, return TRUE if the default site language + // matches the node language. + if (!empty($conf['language']['default'])) { + if ($context->data->language == language_default('language')) { + return TRUE; + } + } + + if (array_filter($conf['language']) && empty($conf['language'][$context->data->language])) { + return FALSE; + } + + return TRUE; +} + +/** + * Provide a summary description based upon the checked node_languages. + */ +function ctools_node_language_ctools_access_summary($conf, $context) { + $languages = array( + 'current' => t('Current site language'), + 'default' => t('Default site language'), + 'no_language' => t('No language'), + ); + $languages = array_merge($languages, locale_language_list()); + + if (!isset($conf['language'])) { + $conf['language'] = array(); + } + + $names = array(); + foreach (array_filter($conf['language']) as $language) { + $names[] = $languages[$language]; + } + + if (empty($names)) { + return t('@identifier is in any language', array('@identifier' => $context->identifier)); + } + + return format_plural(count($names), '@identifier language is "@languages"', '@identifier language is one of "@languages"', array('@languages' => implode(', ', $names), '@identifier' => $context->identifier)); +} + diff --git a/sites/all/modules/contrib/ctools/plugins/access/node_status.inc b/sites/all/modules/contrib/ctools/plugins/access/node_status.inc new file mode 100644 index 0000000..ad5ba40 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/node_status.inc @@ -0,0 +1,33 @@ + t("Node: (un)published"), + 'description' => t('Control access by the nodes published status.'), + 'callback' => 'ctools_node_status_ctools_access_check', + 'summary' => 'ctools_node_status_ctools_access_summary', + 'required context' => new ctools_context_required(t('Node'), 'node'), +); + +/** + * Check for access. + */ +function ctools_node_status_ctools_access_check($conf, $context) { + return (!empty($context->data) && $context->data->status); +} + +/** + * Provide a summary description based upon the checked node_statuss. + */ +function ctools_node_status_ctools_access_summary($conf, $context) { + return t('Returns true if the nodes status is "published".'); +} + diff --git a/sites/all/modules/contrib/ctools/plugins/access/node_type.inc b/sites/all/modules/contrib/ctools/plugins/access/node_type.inc new file mode 100644 index 0000000..23a3845 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/node_type.inc @@ -0,0 +1,117 @@ + t("Node: type"), + 'description' => t('Control access by node_type.'), + 'callback' => 'ctools_node_type_ctools_access_check', + 'default' => array('type' => array()), + 'settings form' => 'ctools_node_type_ctools_access_settings', + 'settings form submit' => 'ctools_node_type_ctools_access_settings_submit', + 'summary' => 'ctools_node_type_ctools_access_summary', + 'required context' => new ctools_context_required(t('Node'), 'node'), + 'restrictions' => 'ctools_node_type_ctools_access_restrictions', +); + +/** + * Settings form for the 'by node_type' access plugin + */ +function ctools_node_type_ctools_access_settings($form, &$form_state, $conf) { + $types = node_type_get_types(); + foreach ($types as $type => $info) { + $options[$type] = check_plain($info->name); + } + + $form['settings']['type'] = array( + '#title' => t('Node type'), + '#type' => 'checkboxes', + '#options' => $options, + '#description' => t('Only the checked node types will be valid.'), + '#default_value' => $conf['type'], + ); + return $form; +} + +/** + * Compress the node_types allowed to the minimum. + */ +function ctools_node_type_ctools_access_settings_submit($form, &$form_state) { + $form_state['values']['settings']['type'] = array_filter($form_state['values']['settings']['type']); +} + +/** + * Check for access. + */ +function ctools_node_type_ctools_access_check($conf, $context) { + // As far as I know there should always be a context at this point, but this + // is safe. + if (empty($context) || empty($context->data) || empty($context->data->type)) { + return FALSE; + } + + if (array_filter($conf['type']) && empty($conf['type'][$context->data->type])) { + return FALSE; + } + + return TRUE; +} + +/** + * Inform the UI that we've eliminated a bunch of possibilities for this + * context. + */ +function ctools_node_type_ctools_access_restrictions($conf, &$context) { + if (isset($context->restrictions['type'])) { + $context->restrictions['type'] = array_unique(array_merge($context->restrictions['type'], array_keys(array_filter($conf['type'])))); + } + else { + $context->restrictions['type'] = array_keys(array_filter($conf['type'])); + } +} + +/** + * Provide a summary description based upon the checked node_types. + */ +function ctools_node_type_ctools_access_summary($conf, $context) { + if (!isset($conf['type'])) { + $conf['type'] = array(); + } + $types = node_type_get_types(); + + $names = array(); + // If a node type doesn't exist, let the user know, but prevent a notice. + $missing_types = array(); + + foreach (array_filter($conf['type']) as $type) { + if (!empty($types[$type])) { + $names[] = check_plain($types[$type]->name); + } + else { + $missing_types[] = check_plain($type); + } + } + + if (empty($names) && empty($missing_types)) { + return t('@identifier is any node type', array('@identifier' => $context->identifier)); + } + + if (!empty($missing_types)) { + $output = array(); + if (!empty($names)) { + $output[] = format_plural(count($names), '@identifier is type "@types"', '@identifier type is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier)); + } + $output[] = format_plural(count($missing_types), 'Missing/ deleted type "@types"', 'Missing/ deleted type is one of "@types"', array('@types' => implode(', ', $missing_types))); + return implode(' | ', $output); + } + + return format_plural(count($names), '@identifier is type "@types"', '@identifier type is one of "@types"', array('@types' => implode(', ', $names), '@identifier' => $context->identifier)); +} + diff --git a/sites/all/modules/contrib/ctools/plugins/access/path_visibility.inc b/sites/all/modules/contrib/ctools/plugins/access/path_visibility.inc new file mode 100644 index 0000000..60b8612 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/path_visibility.inc @@ -0,0 +1,88 @@ + t('String: URL path'), + 'description' => t('Control access by the current path.'), + 'callback' => 'ctools_path_visibility_ctools_access_check', + 'settings form' => 'ctools_path_visibility_ctools_access_settings', + 'summary' => 'ctools_path_visibility_ctools_access_summary', + 'required context' => new ctools_context_optional(t('Path'), 'string'), + 'default' => array('visibility_setting' => 1, 'paths' => ''), +); + +/** + * Settings form + */ +function ctools_path_visibility_ctools_access_settings($form, &$form_state, $conf) { + $form['settings']['note'] = array( + '#value' => '
    ' . t('Note: if no context is chosen, the current page path will be used.') . '
    ', + ); + + $form['settings']['visibility_setting'] = array( + '#type' => 'radios', + '#options' => array( + 1 => t('Allow access on the following pages'), + 0 => t('Allow access on all pages except the following pages'), + ), + '#default_value' => $conf['visibility_setting'], + ); + + $form['settings']['paths'] = array( + '#type' => 'textarea', + '#title' => t('Paths'), + '#default_value' => $conf['paths'], + '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '')), + ); + return $form; +} + +/** + * Check for access. + */ +function ctools_path_visibility_ctools_access_check($conf, $context) { + if (isset($context->data)) { + $base_path = $context->data; + } + else { + $base_path = $_GET['q']; + } + + $path = drupal_get_path_alias($base_path); + $page_match = drupal_match_path($path, $conf['paths']); + + // If there's a path alias, we may still be at the un-aliased path + // so check that as well. + if (!isset($context->data) && $path != $base_path) { + $page_match = $page_match || drupal_match_path($base_path, $conf['paths']); + } + + // When $conf['visibility_setting'] has a value of 0, the block is displayed + // on all pages except those listed in $block->pages. When set to 1, it + // is displayed only on those pages listed in $block->pages. + $page_match = !($conf['visibility_setting'] xor $page_match); + + return $page_match; +} + +/** + * Provide a summary description. + */ +function ctools_path_visibility_ctools_access_summary($conf, $context) { + $paths = array(); + foreach (explode("\n", $conf['paths']) as $path) { + $paths[] = check_plain($path); + } + + $identifier = $context->type == 'any' ? t('Current path') : $context->identifier; + if ($conf['visibility_setting']) { + return format_plural(count($paths), '@identifier is "@paths"', '@identifier type is one of "@paths"', array('@paths' => implode(', ', $paths), '@identifier' => $identifier)); + } + else { + return format_plural(count($paths), '@identifier is not "@paths"', '@identifier type is not one of "@paths"', array('@paths' => implode(', ', $paths), '@identifier' => $identifier)); + } +} diff --git a/sites/all/modules/contrib/ctools/plugins/access/perm.inc b/sites/all/modules/contrib/ctools/plugins/access/perm.inc new file mode 100644 index 0000000..67516fa --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/perm.inc @@ -0,0 +1,73 @@ + t("User: permission"), + 'description' => t('Control access by permission string.'), + 'callback' => 'ctools_perm_ctools_access_check', + 'default' => array('perm' => 'access content'), + 'settings form' => 'ctools_perm_ctools_access_settings', + 'summary' => 'ctools_perm_ctools_access_summary', + 'required context' => new ctools_context_required(t('User'), 'user'), +); + +/** + * Settings form for the 'by perm' access plugin + */ +function ctools_perm_ctools_access_settings($form, &$form_state, $conf) { + $perms = array(); + // Get list of permissions + foreach (module_list(FALSE, FALSE, TRUE) as $module) { + // By keeping them keyed by module we can use optgroups with the + // 'select' type. + if ($permissions = module_invoke($module, 'permission')) { + foreach ($permissions as $id => $permission) { + $perms[$module][$id] = $permission['title']; + } + } + } + + $form['settings']['perm'] = array( + '#type' => 'select', + '#options' => $perms, + '#title' => t('Permission'), + '#default_value' => $conf['perm'], + '#description' => t('Only users with the selected permission flag will be able to access this.'), + ); + + return $form; +} + +/** + * Check for access. + */ +function ctools_perm_ctools_access_check($conf, $context) { + // As far as I know there should always be a context at this point, but this + // is safe. + if (empty($context) || empty($context->data)) { + return FALSE; + } + + return user_access($conf['perm'], $context->data); +} + +/** + * Provide a summary description based upon the checked roles. + */ +function ctools_perm_ctools_access_summary($conf, $context) { + if (!isset($conf['perm'])) { + return t('Error, unset permission'); + } + + $permissions = module_invoke_all('permission'); + return t('@identifier has "@perm"', array('@identifier' => $context->identifier, '@perm' => $permissions[$conf['perm']]['title'])); +} + diff --git a/sites/all/modules/contrib/ctools/plugins/access/php.inc b/sites/all/modules/contrib/ctools/plugins/access/php.inc new file mode 100644 index 0000000..35da86d --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/php.inc @@ -0,0 +1,64 @@ + t("PHP Code"), + 'description' => t('Control access through arbitrary PHP code.'), + 'callback' => 'ctools_php_ctools_access_check', + 'default' => array('description' => '', 'php' => ''), + 'settings form' => 'ctools_php_ctools_access_settings', + 'summary' => 'ctools_php_ctools_access_summary', + 'all contexts' => TRUE, +); + +/** + * Settings form for the 'by perm' access plugin + * + * @todo Need a way to provide a list of all available contexts to be used by + * the eval-ed PHP. + */ +function ctools_php_ctools_access_settings($form, &$form_state, $conf) { + $perms = array(); + + $form['settings']['description'] = array( + '#type' => 'textfield', + '#title' => t('Administrative desc'), + '#default_value' => $conf['description'], + '#description' => t('A description for this test for administrative purposes.'), + ); + $form['settings']['php'] = array( + '#type' => 'textarea', + '#title' => t('PHP Code'), + '#default_value' => $conf['php'], + '#description' => t('Access will be granted if the following PHP code returns TRUE. Do not include <?php ?>. Note that executing incorrect PHP-code can break your Drupal site. All contexts will be available in the $contexts variable.'), + ); + if (!user_access('use PHP for settings')) { + $form['settings']['php']['#disabled'] = TRUE; + $form['settings']['php']['#value'] = $conf['php']; + $form['settings']['php']['#description'] .= ' ' . t('You do not have sufficient permissions to edit PHP code.'); + } + return $form; +} + +/** + * Check for access. + */ +function ctools_php_ctools_access_check($__conf, $contexts) { + $access = eval($__conf['php']); + return $access; +} + +/** + * Provide a summary description based upon the checked roles. + */ +function ctools_php_ctools_access_summary($conf, $contexts) { + return !empty($conf['description']) ? check_plain($conf['description']) : t('No description'); +} diff --git a/sites/all/modules/contrib/ctools/plugins/access/role.inc b/sites/all/modules/contrib/ctools/plugins/access/role.inc new file mode 100644 index 0000000..b633254 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/role.inc @@ -0,0 +1,79 @@ + t("User: role"), + 'description' => t('Control access by role.'), + 'callback' => 'ctools_role_ctools_access_check', + 'default' => array('rids' => array()), + 'settings form' => 'ctools_role_ctools_access_settings', + 'settings form submit' => 'ctools_role_ctools_access_settings_submit', + 'summary' => 'ctools_role_ctools_access_summary', + 'required context' => new ctools_context_required(t('User'), 'user'), +); + +/** + * Settings form for the 'by role' access plugin + */ +function ctools_role_ctools_access_settings($form, &$form_state, $conf) { + $form['settings']['rids'] = array( + '#type' => 'checkboxes', + '#title' => t('Role'), + '#default_value' => $conf['rids'], + '#options' => ctools_get_roles(), + '#description' => t('Only the checked roles will be granted access.'), + ); + return $form; +} + +/** + * Compress the roles allowed to the minimum. + */ +function ctools_role_ctools_access_settings_submit($form, &$form_state) { + $form_state['values']['settings']['rids'] = array_keys(array_filter($form_state['values']['settings']['rids'])); +} + +/** + * Check for access. + */ +function ctools_role_ctools_access_check($conf, $context) { + // As far as I know there should always be a context at this point, but this + // is safe. + if (empty($context) || empty($context->data) || !isset($context->data->roles)) { + return FALSE; + } + + $roles = array_keys($context->data->roles); + $roles[] = $context->data->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID; + return (bool) array_intersect($conf['rids'], $roles); +} + +/** + * Provide a summary description based upon the checked roles. + */ +function ctools_role_ctools_access_summary($conf, $context) { + if (!isset($conf['rids'])) { + $conf['rids'] = array(); + } + $roles = ctools_get_roles(); + + $names = array(); + foreach (array_filter($conf['rids']) as $rid) { + $names[] = check_plain($roles[$rid]); + } + + if (empty($names)) { + return t('@identifier can have any role', array('@identifier' => $context->identifier)); + } + + return format_plural(count($names), '@identifier has role "@roles"', '@identifier has one of "@roles"', array('@roles' => implode(', ', $names), '@identifier' => $context->identifier)); +} + diff --git a/sites/all/modules/contrib/ctools/plugins/access/site_language.inc b/sites/all/modules/contrib/ctools/plugins/access/site_language.inc new file mode 100644 index 0000000..9ff2f70 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/site_language.inc @@ -0,0 +1,87 @@ + t("User: language"), + 'description' => t('Control access by the language the user or site currently uses.'), + 'callback' => 'ctools_site_language_ctools_access_check', + 'default' => array('language' => array()), + 'settings form' => 'ctools_site_language_ctools_access_settings', + 'settings form submit' => 'ctools_site_language_ctools_access_settings_submit', + 'summary' => 'ctools_site_language_ctools_access_summary', + ); +} + +/** + * Settings form for the 'by site_language' access plugin + */ +function ctools_site_language_ctools_access_settings($form, &$form_state, $conf) { + $options = array( + 'default' => t('Default site language'), + ); + $options = array_merge($options, locale_language_list()); + $form['settings']['language'] = array( + '#title' => t('Language'), + '#type' => 'checkboxes', + '#options' => $options, + '#description' => t('Pass only if the current site language is one of the selected languages.'), + '#default_value' => $conf['language'], + ); + return $form; +} + +/** + * Check for access. + */ +function ctools_site_language_ctools_access_check($conf, $context) { + global $language; + + // Specialcase: If 'default' is checked, return TRUE if the default site language + // matches the node language. + if (!empty($conf['language']['default'])) { + if ($language->language == language_default('language')) { + return TRUE; + } + } + + if (array_filter($conf['language']) && empty($conf['language'][$language->language])) { + return FALSE; + } + + return TRUE; +} + +/** + * Provide a summary description based upon the checked site_languages. + */ +function ctools_site_language_ctools_access_summary($conf, $context) { + $languages = array( + 'default' => t('Default site language'), + ); + $languages = array_merge($languages, locale_language_list()); + + if (!isset($conf['language'])) { + $conf['language'] = array(); + } + + $names = array(); + foreach (array_filter($conf['language']) as $language) { + $names[] = $languages[$language]; + } + + if (empty($names)) { + return t('Site language is any language'); + } + + return format_plural(count($names), 'Site language is "@languages"', 'Site language is one of "@languages"', array('@languages' => implode(', ', $names))); +} + diff --git a/sites/all/modules/contrib/ctools/plugins/access/string_equal.inc b/sites/all/modules/contrib/ctools/plugins/access/string_equal.inc new file mode 100644 index 0000000..ad1c88d --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/string_equal.inc @@ -0,0 +1,94 @@ + t("String: comparison"), + 'description' => t('Control access by string match.'), + 'callback' => 'ctools_string_equal_ctools_access_check', + 'settings form' => 'ctools_string_equal_ctools_access_settings', + 'summary' => 'ctools_string_equal_ctools_access_summary', + 'required context' => new ctools_context_required(t('String'), 'string'), + 'defaults' => array('operator' => '=', 'value' => '', 'case' => FALSE), +); + +/** + * Settings form + */ +function ctools_string_equal_ctools_access_settings($form, &$form_state, $conf) { + $form['settings']['operator'] = array( + '#type' => 'radios', + '#title' => t('Operator'), + '#options' => array( + '=' => t('Equal'), + '!=' => t('Not equal'), + 'regex' => t('Regular expression'), + '!regex' => t('Not equal to regular expression'), + ), + '#default_value' => $conf['operator'], + '#description' => t('If using a regular expression, you should enclose the pattern in slashes like so: /foo/. If you need to compare against slashes you can use another character to enclose the pattern, such as @. See PHP regex documentation for more.'), + ); + + $form['settings']['value'] = array( + '#type' => 'textfield', + '#title' => t('String'), + '#default_value' => $conf['value'], + ); + + $form['settings']['case'] = array( + '#type' => 'checkbox', + '#title' => t('Case sensitive'), + '#default_value' => $conf['case'], + ); + return $form; +} + +/** + * Check for access + */ +function ctools_string_equal_ctools_access_check($conf, $context) { + if (empty($context) || empty($context->data)) { + $string = ''; + } + else { + $string = $context->data; + } + + $value = $conf['value']; + if (empty($conf['case'])) { + $string = drupal_strtolower($string); + $value = drupal_strtolower($value); + } + + switch ($conf['operator']) { + case '=': + return $string === $value; + case '!=': + return $string !== $value; + case 'regex': + return preg_match($value, $string); + case '!regex': + return !preg_match($value, $string); + } +} + +/** + * Provide a summary description based upon the specified context + */ +function ctools_string_equal_ctools_access_summary($conf, $context) { + $values = array('@identifier' => $context->identifier, '@value' => $conf['value']); + switch ($conf['operator']) { + case '=': + return t('@identifier is "@value"', $values); + case '!=': + return t('@identifier is not "@value"', $values); + case 'regex': + return t('@identifier matches "@value"', $values); + case '!regex': + return t('@identifier does not match "@value"', $values); + } +} + diff --git a/sites/all/modules/contrib/ctools/plugins/access/string_length.inc b/sites/all/modules/contrib/ctools/plugins/access/string_length.inc new file mode 100644 index 0000000..91abf22 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/string_length.inc @@ -0,0 +1,80 @@ + t("String: length"), + 'description' => t('Control access by length of string context.'), + 'callback' => 'ctools_string_length_ctools_access_check', + 'settings form' => 'ctools_string_length_ctools_access_settings', + 'summary' => 'ctools_string_length_ctools_access_summary', + 'required context' => new ctools_context_required(t('String'), 'string'), + 'defaults' => array('operator' => '=', 'length' => 0), +); + +/** + * Settings form for the 'by role' access plugin. + */ +function ctools_string_length_ctools_access_settings($form, &$form_state, $conf) { + $form['settings']['operator'] = array( + '#type' => 'radios', + '#title' => t('Operator'), + '#options' => array( + '>' => t('Greater than'), + '>=' => t('Greater than or equal to'), + '=' => t('Equal to'), + '!=' => t('Not equal to'), + '<' => t('Less than'), + '<=' => t('Less than or equal to'), + ), + '#default_value' => $conf['operator'], + ); + $form['settings']['length'] = array( + '#type' => 'textfield', + '#title' => t('Length of string'), + '#size' => 3, + '#default_value' => $conf['length'], + '#description' => t('Access/visibility will be granted based on string context length.'), + ); + return $form; +} + +/** + * Check for access. + */ +function ctools_string_length_ctools_access_check($conf, $context) { + if (empty($context) || empty($context->data)) { + $length = 0; + } + else { + $length = drupal_strlen($context->data); + } + + switch ($conf['operator']) { + case '<': + return $length < $conf['length']; + case '<=': + return $length <= $conf['length']; + case '=': + return $length == $conf['length']; + case '!=': + return $length != $conf['length']; + case '>': + return $length > $conf['length']; + case '>=': + return $length >= $conf['length']; + } + // Invalid Operator sent, return FALSE. + return FALSE; +} + +/** + * Provide a summary description based upon the checked roles. + */ +function ctools_string_length_ctools_access_summary($conf, $context) { + return t('@identifier must be @comp @length characters', array('@identifier' => $context->identifier, '@comp' => $conf['operator'], '@length' => $conf['length'])); +} diff --git a/sites/all/modules/contrib/ctools/plugins/access/term.inc b/sites/all/modules/contrib/ctools/plugins/access/term.inc new file mode 100644 index 0000000..36e70de --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/term.inc @@ -0,0 +1,129 @@ + t("Taxonomy: term"), + 'description' => t('Control access by a specific term.'), + 'callback' => 'ctools_term_ctools_access_check', + 'default' => array('vids' => array()), + 'settings form' => 'ctools_term_ctools_access_settings', + 'settings form validation' => 'ctools_term_ctools_access_settings_validate', + 'settings form submit' => 'ctools_term_ctools_access_settings_submit', + 'summary' => 'ctools_term_ctools_access_summary', + 'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')), +); + +/** + * Settings form for the 'by term' access plugin + */ +function ctools_term_ctools_access_settings($form, &$form_state, $conf) { + // If no configuration was saved before, set some defaults. + if (empty($conf)) { + $conf = array( + 'vid' => 0, + ); + } + if (!isset($conf['vid'])) { + $conf['vid'] = 0; + } + + $form['settings']['vid'] = array( + '#title' => t('Vocabulary'), + '#type' => 'select', + '#options' => array(), + '#description' => t('Select the vocabulary for this form.'), + '#id' => 'ctools-select-vid', + '#default_value' => $conf['vid'], + '#required' => TRUE, + ); + + ctools_include('dependent'); + $options = array(); + + // A note: Dependency works strangely on these forms as they have never been + // updated to a more modern system so they are not individual forms of their + // own like the content types. + + $form['settings']['#tree'] = TRUE; + + // Loop over each of the configured vocabularies. + foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) { + $options[$vid] = $vocabulary->name; + $form['settings'][$vocabulary->vid] = array( + '#title' => t('Terms'), + '#description' => t('Select a term or terms from @vocabulary.', array('@vocabulary' => $vocabulary->name)), //. $description, + '#dependency' => array('ctools-select-vid' => array($vocabulary->vid)), + '#default_value' => !empty($conf[$vid]) ? $conf[$vid] : '', + '#multiple' => TRUE, + ); + + $terms = array(); + foreach (taxonomy_get_tree($vocabulary->vid) as $tid => $term) { + $terms[$term->tid] = str_repeat('-', $term->depth) . ($term->depth ? ' ' : '') . $term->name; + } + $form['settings'][$vocabulary->vid]['#type'] = 'select'; + $form['settings'][$vocabulary->vid]['#options'] = $terms; + unset($terms); + } + $form['settings']['vid']['#options'] = $options; + return $form; +} + +/** + * Check for access. + */ +function ctools_term_ctools_access_check($conf, $context) { + // As far as I know there should always be a context at this point, but this + // is safe. + if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) { + return FALSE; + } + + // Get the $vid. + if (!isset($conf['vid'])) { + return FALSE; + } + $vid = $conf['vid']; + + // Get the terms. + if (!isset($conf[$vid])) { + return FALSE; + } + + $return = FALSE; + + $terms = array_filter($conf[$vid]); + // For multi-term if any terms coincide, let's call that good enough: + if (isset($context->tids)) { + return (bool) array_intersect($terms, $context->tids); + } + else { + return in_array($context->data->tid, $terms); + } +} + +/** + * Provide a summary description based upon the checked terms. + */ +function ctools_term_ctools_access_summary($conf, $context) { + $vid = $conf['vid']; + $terms = array(); + foreach ($conf[$vid] as $tid) { + $term = taxonomy_term_load($tid); + $terms[] = $term->name; + } + + return format_plural(count($terms), + '@term can be the term "@terms"', + '@term can be one of these terms: @terms', + array('@terms' => implode(', ', $terms), + '@term' => $context->identifier)); +} diff --git a/sites/all/modules/contrib/ctools/plugins/access/term_has_parent.inc b/sites/all/modules/contrib/ctools/plugins/access/term_has_parent.inc new file mode 100644 index 0000000..a079e92 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/term_has_parent.inc @@ -0,0 +1,172 @@ + t("Taxonomy: term has parent(s)"), + 'description' => t('Control access if a term belongs to a specific parent term.'), + 'callback' => 'ctools_term_has_parent_ctools_access_check', + 'default' => array('vid' => array(), 'negate' => 0), + 'settings form' => 'ctools_term_has_parent_ctools_access_settings', + 'settings form submit' => 'ctools_term_has_parent_ctools_access_settings_submit', + 'summary' => 'ctools_term_has_parent_ctools_access_summary', + 'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')), +); + +/** + * Settings form for the 'by parent term' access plugin + */ +function ctools_term_has_parent_ctools_access_settings($form, &$form_state, $conf) { + // If no configuration was saved before, set some defaults. + if (empty($conf)) { + $conf = array( + 'vid' => 0, + ); + } + if (!isset($conf['vid'])) { + $conf['vid'] = 0; + } + + $form['settings']['vid'] = array( + '#title' => t('Vocabulary'), + '#type' => 'select', + '#options' => array(), + '#description' => t('Select the vocabulary for this form.'), + '#id' => 'ctools-select-vid', + '#default_value' => $conf['vid'], + '#required' => TRUE, + ); + + ctools_include('dependent'); + $options = array(); + + // A note: Dependency works strangely on these forms as they have never been + // updated to a more modern system so they are not individual forms of their + // own like the content types. + + $form['settings']['#tree'] = TRUE; + + // Loop over each of the configured vocabularies. + foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) { + $options[$vid] = $vocabulary->name; + $form['settings']['vid_' . $vid] = array( + '#title' => t('Terms'), + '#description' => t('Select a term or terms from @vocabulary.', array('@vocabulary' => $vocabulary->name)), + '#dependency' => array('ctools-select-vid' => array($vocabulary->vid)), + '#default_value' => !empty($conf['vid_' . $vid]) ? $conf['vid_' . $vid] : '', + '#size' => 10, + '#multiple' => TRUE, + //@todo: Remove the following workaround when the following patch is in core. {@see:http://drupal.org/node/1117526} + '#name' => sprintf("settings[%u][]", $vid), + '#attributes' => array('multiple' => 'multiple'), + ); + + $terms = array(); + foreach (taxonomy_get_tree($vocabulary->vid) as $term) { + $terms[$term->tid] = str_repeat('-', $term->depth) . ($term->depth ? ' ' : '') . $term->name; + } + //$form['settings']['vid_' . $vid]['#type'] = 'select'; + $form['settings']['vid_' . $vid]['#type'] = 'checkboxes'; + $form['settings']['vid_' . $vid]['#options'] = $terms; + unset($terms); + } + $form['settings']['vid']['#options'] = $options; + $form['settings']['include_self'] = array( + '#title' => t('Include these term(s) as candidates?'), + '#description' => t('When this rule is evaluated, should the term(s) you select be included as candidates for access?'), + '#default_value' => !empty($conf['include_self']) ? $conf['include_self'] : FALSE, + '#type' => 'checkbox', + ); + return $form; +} + +/** + * Filters values to store less. + */ +function ctools_term_has_parent_ctools_access_settings_submit($form, &$form_state) { + foreach ($form_state['values']['settings'] as $key => $value) { + if (strpos($key, 'vid_') === 0) { + $form_state['values']['settings'][$key] = array_filter($form_state['values']['settings'][$key]); + } + } +} + +/** + * Check for access. + */ +function ctools_term_has_parent_ctools_access_check($conf, $context) { + // As far as I know there should always be a context at this point, but this + // is safe. + if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) { + return FALSE; + } + + // Get the $vid. + if (!isset($conf['vid'])) { + return FALSE; + } + $vid = $conf['vid']; + + // we'll start looking up the hierarchy from our context term id. + $current_term = $context->data->tid; + + $term=''; + + // scan up the tree. + while (true) { + // select parent as term_parent to avoid PHP5 complications with the parent keyword + //@todo: Find a way to reduce the number of queries required for really deep hierarchies. + $term = db_query("SELECT parent AS term_parent, tid AS tid FROM {taxonomy_term_hierarchy} th WHERE th.tid = :tid", array(':tid'=>$current_term))->fetchObject(); + + // if no term is found, get out of the loop + if (!$term || empty($term->tid)) { + break; + } + + // check the term selected, if the user asked it to. + if (!empty($conf['include_self']) && isset($conf['vid_' . $vid][$term->tid])) { + return TRUE; + } + + // did we find the parent TID we were looking for? + if (isset($conf['vid_' . $vid][$term->tid])) { + // YES, we're done! + return TRUE; + } + // Nope, we didn't find it. + + // If this is the top of the hierarchy, stop scanning. + if ($term->term_parent==0) { + break; + } + + // update the parent, and keep scanning. + $current_term = $term->term_parent; + } + + return FALSE; +} + +/** + * Provide a summary description based upon the checked terms. + */ +function ctools_term_has_parent_ctools_access_summary($conf, $context) { + $vid = (int)$conf['vid']; + $terms = array(); + foreach ($conf['vid_' . $vid] as $tid) { + $term = taxonomy_term_load($tid); + $terms[] = $term->name; + } + + return format_plural(count($terms), + '@term can have the parent "@terms"', + '@term can have one of these parents: @terms', + array('@terms' => implode(', ', $terms), + '@term' => $context->identifier)); +} diff --git a/sites/all/modules/contrib/ctools/plugins/access/term_parent.inc b/sites/all/modules/contrib/ctools/plugins/access/term_parent.inc new file mode 100644 index 0000000..acbaf87 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/term_parent.inc @@ -0,0 +1,86 @@ + t("Taxonomy: parent term"), + 'description' => t('Control access by existence of a parent term.'), + 'callback' => 'ctools_term_parent_ctools_access_check', + 'default' => array('vid' => array(), 'negate' => 0), + 'settings form' => 'ctools_term_parent_ctools_access_settings', + 'settings form validation' => 'ctools_term_parent_ctools_access_settings_validate', + 'settings form submit' => 'ctools_term_parent_ctools_access_settings_submit', + 'summary' => 'ctools_term_parent_ctools_access_summary', + 'required context' => new ctools_context_required(t('Term'), array('taxonomy_term', 'terms')), +); + +/** + * Settings form for the 'by parent term' access plugin + */ +function ctools_term_parent_ctools_access_settings($form, &$form_state, $conf) { + // If no configuration was saved before, set some defaults. + if (empty($conf)) { + $conf = array( + 'vid' => 0, + ); + } + if (!isset($conf['vid'])) { + $conf['vid'] = 0; + } + + $form['settings']['vid'] = array( + '#title' => t('Vocabulary'), + '#type' => 'select', + '#options' => array(), + '#description' => t('Select the vocabulary for this form. If there exists a parent term in that vocabulary, this access check will succeed.'), + '#id' => 'ctools-select-vid', + '#default_value' => $conf['vid'], + '#required' => TRUE, + ); + + $options = array(); + + // Loop over each of the configured vocabularies. + foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) { + $options[$vid] = $vocabulary->name; + } + $form['settings']['vid']['#options'] = $options; + return $form; +} + +/** + * Check for access. + */ +function ctools_term_parent_ctools_access_check($conf, $context) { + // As far as I know there should always be a context at this point, but this + // is safe. + if (empty($context) || empty($context->data) || empty($context->data->vid) || empty($context->data->tid)) { + return FALSE; + } + + // Get the $vid. + if (!isset($conf['vid'])) { + return FALSE; + } + $vid = $conf['vid']; + + $count = db_query('SELECT COUNT(*) FROM {taxonomy_term_hierarchy} th INNER JOIN {taxonomy_term_data} td ON th.parent = td.tid WHERE th.tid = :tid AND td.vid = :vid', array(':tid' => $context->data->tid, ':vid' => $vid))->fetchField(); + + return $count ? TRUE : FALSE; +} + +/** + * Provide a summary description based upon the checked terms. + */ +function ctools_term_parent_ctools_access_summary($conf, $context) { + $vocab = taxonomy_vocabulary_load($conf['vid']); + + return t('"@term" has parent in vocabulary "@vocab"', array('@term' => $context->identifier, '@vocab' => $vocab->name)); +} diff --git a/sites/all/modules/contrib/ctools/plugins/access/term_vocabulary.inc b/sites/all/modules/contrib/ctools/plugins/access/term_vocabulary.inc new file mode 100644 index 0000000..b003138 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/term_vocabulary.inc @@ -0,0 +1,127 @@ + t("Taxonomy: vocabulary"), + 'description' => t('Control access by vocabulary.'), + 'callback' => 'ctools_term_vocabulary_ctools_access_check', + 'default' => array('vids' => array()), + 'settings form' => 'ctools_term_vocabulary_ctools_access_settings', + 'settings form submit' => 'ctools_term_vocabulary_ctools_access_settings_submit', + 'summary' => 'ctools_term_vocabulary_ctools_access_summary', + 'required context' => new ctools_context_required(t('Vocabulary'), array( + 'taxonomy_term', + 'terms', + 'taxonomy_vocabulary' + )), +); + +/** + * Settings form for the 'by term_vocabulary' access plugin + */ +function ctools_term_vocabulary_ctools_access_settings($form, &$form_state, $conf) { + $options = array(); + $vocabularies = taxonomy_get_vocabularies(); + foreach ($vocabularies as $voc) { + $options[$voc->machine_name] = check_plain($voc->name); + } + + _ctools_term_vocabulary_ctools_access_map_vids($conf); + + $form['settings']['machine_name'] = array( + '#type' => 'checkboxes', + '#title' => t('Vocabularies'), + '#options' => $options, + '#description' => t('Only the checked vocabularies will be valid.'), + '#default_value' => $conf['machine_name'], + ); + return $form; +} + +/** + * Compress the term_vocabularys allowed to the minimum. + */ +function ctools_term_vocabulary_ctools_access_settings_submit($form, &$form_state) { + $form_state['values']['settings']['machine_name'] = array_filter($form_state['values']['settings']['machine_name']); +} + +/** + * Check for access. + */ +function ctools_term_vocabulary_ctools_access_check($conf, $context) { + // As far as I know there should always be a context at this point, but this + // is safe. + if (empty($context) || empty($context->data) || empty($context->data->vocabulary_machine_name)) { + return FALSE; + } + + _ctools_term_vocabulary_ctools_access_map_vids($conf); + + if (array_filter($conf['machine_name']) && empty($conf['machine_name'][$context->data->vocabulary_machine_name])) { + return FALSE; + } + + return TRUE; +} + +/** + * Provide a summary description based upon the checked term_vocabularys. + */ +function ctools_term_vocabulary_ctools_access_summary($conf, $context) { + if (!isset($conf['type'])) { + $conf['type'] = array(); + } + $vocabularies = taxonomy_get_vocabularies(); + + _ctools_term_vocabulary_ctools_access_map_vids($conf); + + $names = array(); + if (!empty($conf['machine_name'])) { + foreach (array_filter($conf['machine_name']) as $machine_name) { + foreach ($vocabularies as $vocabulary) { + if ($vocabulary->machine_name === $machine_name) { + $names[] = check_plain($vocabulary->name); + continue; + } + } + } + } + + if (empty($names)) { + return t('@identifier is any vocabulary', array('@identifier' => $context->identifier)); + } + + return format_plural(count($names), '@identifier vocabulary is "@machine_names"', '@identifier vocabulary is one of "@machine_names"', array( + '@machine_names' => implode(', ', $names), + '@identifier' => $context->identifier + )); +} + +/** + * Helper function to map the vids from old features to the new machine_name. + * + * Add the machine_name key to $conf if the vids key exist. + * + * @param array $conf + * The configuration of this plugin. + */ +function _ctools_term_vocabulary_ctools_access_map_vids(&$conf) { + if (!empty($conf['vids'])) { + $conf['machine_name'] = array(); + $vocabularies = taxonomy_get_vocabularies(); + foreach ($conf['vids'] as $vid) { + $machine_name = $vocabularies[$vid]->machine_name; + $conf['machine_name'][$machine_name] = $vocabularies[$vid]->machine_name; + } + } +} + + diff --git a/sites/all/modules/contrib/ctools/plugins/access/theme.inc b/sites/all/modules/contrib/ctools/plugins/access/theme.inc new file mode 100644 index 0000000..4f4be6d --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/access/theme.inc @@ -0,0 +1,70 @@ + t("Current theme"), + 'description' => t('Control access by checking which theme is in use.'), + 'callback' => 'ctools_theme_ctools_access_check', + 'default' => array('theme' => variable_get('theme_default', 'garland')), + 'settings form' => 'ctools_theme_ctools_access_settings', + 'summary' => 'ctools_theme_ctools_access_summary', +); + +/** + * Settings form for the 'by theme' access plugin + */ +function ctools_theme_ctools_access_settings($form, &$form_state, $conf) { + $themes = array(); + foreach (list_themes() as $key => $theme) { + $themes[$key] = $theme->info['name']; + } + + $form['settings']['theme'] = array( + '#type' => 'select', + '#options' => $themes, + '#title' => t('Themes'), + '#default_value' => $conf['theme'], + '#description' => t('This will only be accessed if the current theme is the selected theme.'), + ); + return $form; +} + +/** + * Check for access. + */ +function ctools_theme_ctools_access_check($conf, $context) { + if (!empty($GLOBALS['theme'])) { + $theme = $GLOBALS['theme']; + } + else if (!empty($GLOBALS['custom_theme'])) { + $theme = $GLOBALS['custom_theme']; + } + else if (!empty($GLOBALS['user']->theme)) { + $theme = $GLOBALS['user']->theme; + } + else { + $theme = variable_get('theme_default', 'garland'); + } + + return $conf['theme'] == $theme; +} + +/** + * Provide a summary description based upon the checked roles. + */ +function ctools_theme_ctools_access_summary($conf, $context) { + if (!isset($conf['theme'])) { + return t('Error, unset theme'); + } + $themes = list_themes(); + + return t('Current theme is "@theme"', array('@theme' => $themes[$conf['theme']]->info['name'])); +} diff --git a/sites/all/modules/contrib/ctools/plugins/arguments/entity_id.inc b/sites/all/modules/contrib/ctools/plugins/arguments/entity_id.inc new file mode 100644 index 0000000..3063fef --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/arguments/entity_id.inc @@ -0,0 +1,147 @@ + t("Entity: ID"), + 'description' => t('Creates an entity context from an entity ID argument.'), + 'context' => 'ctools_argument_entity_id_context', + 'get child' => 'ctools_argument_entity_id_get_child', + 'get children' => 'ctools_argument_entity_id_get_children', + 'default' => array( + 'entity_id' => '', + ), + 'placeholder form' => 'ctools_argument_entity_id_ctools_argument_placeholder', +); + +function ctools_argument_entity_id_get_child($plugin, $parent, $child) { + $plugins = ctools_argument_entity_id_get_children($plugin, $parent); + return $plugins[$parent . ':' . $child]; +} + +function ctools_argument_entity_id_get_children($original_plugin, $parent) { + $entities = entity_get_info(); + $plugins = array(); + foreach ($entities as $entity_type => $entity) { + $plugin = $original_plugin; + $plugin['title'] = t('@entity: ID', array('@entity' => $entity['label'])); + $plugin['keyword'] = $entity_type; + $plugin['description'] = t('Creates @entity context from an ID argument.', array('@entity' => $entity_type)); + $plugin['name'] = $parent . ':' . $entity_type; + $plugin_id = $parent . ':' . $entity_type; + drupal_alter('ctools_entity_context', $plugin, $entity, $plugin_id); + $plugins[$plugin_id] = $plugin; + } + drupal_alter('ctools_entity_contexts', $plugins); + + return $plugins; +} + +/** + * Discover if this argument gives us the entity we crave. + */ +function ctools_argument_entity_id_context($arg = NULL, $conf = NULL, $empty = FALSE) { + $entity_type = explode(':', $conf['name']); + $entity_type = $entity_type[1]; + // If unset it wants a generic, unfilled context. + if ($empty) { + return ctools_context_create_empty('entity:' . $entity_type); + } + + // We can accept either an entity object or a pure id. + if (is_object($arg)) { + return ctools_context_create('entity:' . $entity_type, $arg); + } + + // Trim spaces and other garbage. + $arg = trim($arg); + + if (!is_numeric($arg)) { + $preg_matches = array(); + $match = preg_match('/\[id: (\d+)\]/', $arg, $preg_matches); + if (!$match) { + $match = preg_match('/^id: (\d+)/', $arg, $preg_matches); + } + + if ($match) { + $id = $preg_matches[1]; + } + if (isset($id) && is_numeric($id)) { + return ctools_context_create('entity:' . $entity_type, $id); + } + return FALSE; + } + + $entities = entity_load($entity_type, array($arg)); + if (empty($entities)) { + return FALSE; + } + + return ctools_context_create('entity:' . $entity_type, reset($entities)); +} + +function ctools_argument_entity_id_settings_form(&$form, &$form_state, $conf) { + $plugin = &$form_state['plugin']; + + $form['settings']['entity'] = array( + '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $plugin['keyword'])), + '#type' => 'textfield', + '#maxlength' => 512, + '#autocomplete_path' => 'ctools/autocomplete/' . $plugin['keyword'], + '#weight' => -10, + ); + + if (!empty($conf['entity_id'])) { + $info = entity_load($plugin['keyword'], array($conf['entity_id'])); + $info = $info[$conf['entity_id']]; + if ($info) { + $entity = entity_get_info($plugin['keyword']); + $uri = entity_uri($plugin['keyword'], $info); + if (is_array($uri) && $entity['entity keys']['label']) { + $link = l(t("'%title' [%type id %id]", array('%title' => $info->{$entity['entity keys']['label']}, '%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), $uri['path'], array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE)); + } + elseif (is_array($uri)) { + $link = l(t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), $uri['path'], array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE)); + } + elseif ($entity['entity keys']['label']) { + $link = l(t("'%title' [%type id %id]", array('%title' => $info->{$entity['entity keys']['label']}, '%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), file_create_url($uri), array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE)); + } + else { + $link = t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id'])); + } + $form['settings']['entity']['#description'] = t('Currently set to !link', array('!link' => $link)); + } + } + + $form['settings']['entity_id'] = array( + '#type' => 'value', + '#value' => isset($conf['entity_id']) ? $conf['entity_id'] : '', + ); + + $form['settings']['entity_type'] = array( + '#type' => 'value', + '#value' => $plugin['keyword'], + ); + + return $form; +} + +function ctools_argument_entity_id_ctools_argument_placeholder($conf) { + $conf = array( + '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $conf['keyword'])), + '#type' => 'textfield', + '#maxlength' => 512, + '#autocomplete_path' => 'ctools/autocomplete/' . $conf['keyword'], + '#weight' => -10, + ); + + return $conf; +} diff --git a/sites/all/modules/contrib/ctools/plugins/arguments/nid.inc b/sites/all/modules/contrib/ctools/plugins/arguments/nid.inc new file mode 100644 index 0000000..9aaec0e --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/arguments/nid.inc @@ -0,0 +1,50 @@ + t("Node: ID"), + 'keyword' => 'node', + 'description' => t('Creates a node context from a node ID argument.'), + 'context' => 'ctools_argument_nid_context', + 'placeholder form' => array( + '#type' => 'textfield', + '#description' => t('Enter the node ID of a node for this argument'), + ), + 'no ui' => TRUE, +); + +/** + * Discover if this argument gives us the node we crave. + */ +function ctools_argument_nid_context($arg = NULL, $conf = NULL, $empty = FALSE) { + // If unset it wants a generic, unfilled context. + if ($empty) { + return ctools_context_create_empty('node'); + } + + // We can accept either a node object or a pure nid. + if (is_object($arg)) { + return ctools_context_create('node', $arg); + } + + if (!is_numeric($arg)) { + return FALSE; + } + + $node = node_load($arg); + if (!$node) { + return FALSE; + } + + return ctools_context_create('node', $node); +} + diff --git a/sites/all/modules/contrib/ctools/plugins/arguments/node_add.inc b/sites/all/modules/contrib/ctools/plugins/arguments/node_add.inc new file mode 100644 index 0000000..c811311 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/arguments/node_add.inc @@ -0,0 +1,32 @@ + t("Node add form: node type"), + // keyword to use for %substitution + 'keyword' => 'node_type', + 'description' => t('Creates a node add form context from a node type argument.'), + 'context' => 'ctools_node_add_context', +); + +/** + * Discover if this argument gives us the node we crave. + */ +function ctools_node_add_context($arg = NULL, $conf = NULL, $empty = FALSE) { + // If unset it wants a generic, unfilled context. + if (!isset($arg)) { + return ctools_context_create_empty('node_add_form'); + } + + return ctools_context_create('node_add_form', $arg); +} + diff --git a/sites/all/modules/contrib/ctools/plugins/arguments/node_edit.inc b/sites/all/modules/contrib/ctools/plugins/arguments/node_edit.inc new file mode 100644 index 0000000..c7cdf29 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/arguments/node_edit.inc @@ -0,0 +1,51 @@ + t("Node edit form: node ID"), + // keyword to use for %substitution + 'keyword' => 'node', + 'description' => t('Creates a node edit form context from a node ID argument.'), + 'context' => 'ctools_node_edit_context', + 'placeholder form' => array( + '#type' => 'textfield', + '#description' => t('Enter the node ID of a node for this argument'), + ), +); + +/** + * Discover if this argument gives us the node we crave. + */ +function ctools_node_edit_context($arg = NULL, $conf = NULL, $empty = FALSE) { + // If unset it wants a generic, unfilled context. + if ($empty) { + return ctools_context_create_empty('node_edit_form'); + } + + // We can accept either a node object or a pure nid. + if (is_object($arg)) { + return ctools_context_create('node_edit_form', $arg); + } + + if (!is_numeric($arg)) { + return FALSE; + } + + $node = node_load($arg); + if (!$node) { + return NULL; + } + + // This will perform a node_access check, so we don't have to. + return ctools_context_create('node_edit_form', $node); +} + diff --git a/sites/all/modules/contrib/ctools/plugins/arguments/rid.inc b/sites/all/modules/contrib/ctools/plugins/arguments/rid.inc new file mode 100644 index 0000000..2661153 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/arguments/rid.inc @@ -0,0 +1,50 @@ + t("Revision: ID"), + 'keyword' => 'revision', + 'description' => t('Creates a node context from a revision ID argument.'), + 'context' => 'ctools_argument_rid_context', + 'placeholder form' => array( + '#type' => 'textfield', + '#description' => t('Enter the revision ID of a node for this argument'), + ), +); + +/** + * Discover if this argument gives us the node we crave. + */ +function ctools_argument_rid_context($arg = NULL, $conf = NULL, $empty = FALSE) { + // If unset it wants a generic, unfilled context. + if ($empty) { + return ctools_context_create_empty('node'); + } + + // We can accept either a node object or a pure nid. + if (is_object($arg)) { + return ctools_context_create('node', $arg); + } + + if (!is_numeric($arg)) { + return FALSE; + } + + $nid = db_query('SELECT nid FROM {node_revision} WHERE vid = :vid', array(':vid' => $arg))->fetchField(); + $node = node_load($nid, $arg); + if (!$node) { + return FALSE; + } + + return ctools_context_create('node', $node); +} + diff --git a/sites/all/modules/contrib/ctools/plugins/arguments/string.inc b/sites/all/modules/contrib/ctools/plugins/arguments/string.inc new file mode 100644 index 0000000..ed4ffbb --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/arguments/string.inc @@ -0,0 +1,64 @@ + t("String"), + // keyword to use for %substitution + 'keyword' => 'string', + 'description' => t('A string is a minimal context that simply holds a string that can be used for some other purpose.'), + 'settings form' => 'ctools_string_settings_form', + 'context' => 'ctools_string_context', + 'placeholder form' => array( + '#type' => 'textfield', + '#description' => t('Enter a value for this argument'), + ), + 'path placeholder' => 'ctools_string_path_placeholder', // This is in pagemanager. +); + +/** + * Discover if this argument gives us the term we crave. + */ +function ctools_string_context($arg = NULL, $conf = NULL, $empty = FALSE) { + // If unset it wants a generic, unfilled context. + if ($empty) { + return ctools_context_create_empty('string'); + } + + $context = ctools_context_create('string', $arg); + $context->original_argument = $arg; + + return $context; +} + +/** + * Settings form for the argument + */ +function ctools_string_settings_form(&$form, &$form_state, $conf) { + $form['settings']['use_tail'] = array( + '#title' => t('Get all arguments after this one'), + '#type' => 'checkbox', + '#default_value' => !empty($conf['use_tail']), + '#description' => t('If checked, this string will include all arguments. For example, if the path is "path/%" and the user visits "path/foo/bar", if this is not checked the string will be "foo". If it is checked the string will be "foo/bar".'), + ); +// return $form; +} + +/** + * Switch the placeholder based upon user settings. + */ +function ctools_string_path_placeholder($argument) { + if (empty($argument['settings']['use_tail'])) { + return '%pm_arg'; + } + else { + return '%pm_arg_tail'; + } +} \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/plugins/arguments/term.inc b/sites/all/modules/contrib/ctools/plugins/arguments/term.inc new file mode 100644 index 0000000..868c8aa --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/arguments/term.inc @@ -0,0 +1,163 @@ + t("Taxonomy term: ID"), + // keyword to use for %substitution + 'keyword' => 'term', + 'description' => t('Creates a single taxonomy term from a taxonomy ID or taxonomy term name.'), + 'context' => 'ctools_term_context', + 'default' => array('input_form' => 'tid', 'breadcrumb' => TRUE, 'transform' => FALSE), + 'settings form' => 'ctools_term_settings_form', + 'placeholder form' => 'ctools_term_ctools_argument_placeholder', + 'breadcrumb' => 'ctools_term_breadcrumb', +); + +/** + * Discover if this argument gives us the term we crave. + */ +function ctools_term_context($arg = NULL, $conf = NULL, $empty = FALSE) { + // If unset it wants a generic, unfilled context. + if ($empty) { + return ctools_context_create_empty('entity:taxonomy_term'); + } + + if (is_object($arg)) { + $term = $arg; + } + else { + switch ($conf['input_form']) { + case 'tid': + default: + if (!is_numeric($arg)) { + return FALSE; + } + $term = taxonomy_term_load($arg); + break; + + case 'term': + if (!empty($conf['transform'])) { + $arg = strtr($arg, '-', ' '); + } + + $terms = taxonomy_get_term_by_name($arg); + + $conf['vids'] = is_array($conf['vids']) ? array_filter($conf['vids']) : NULL; + if ((count($terms) > 1) && isset($conf['vids'])) { + foreach ($terms as $potential) { + foreach ($conf['vids'] as $vid => $active) { + if ($active && $potential->vid == $vid) { + $term = $potential; + // break out of the foreaches AND the case + break 3; + } + } + } + } + $term = array_shift($terms); + break; + } + + if (empty($term)) { + return NULL; + } + } + + if (!empty($conf['vids']) && array_filter($conf['vids']) && empty($conf['vids'][$term->vid])) { + return NULL; + } + + $context = ctools_context_create('entity:taxonomy_term', $term); + $context->original_argument = $arg; + return $context; +} + +/** + * Settings form for the argument + */ +function ctools_term_settings_form(&$form, &$form_state, $conf) { + // @todo allow synonym use like Views does. + $form['settings']['input_form'] = array( + '#title' => t('Argument type'), + '#type' => 'radios', + '#options' => array('tid' => t('Term ID'), 'term' => t('Term name')), + '#default_value' => $conf['input_form'], + '#prefix' => '
    ', + '#suffix' => '
    ', + ); + + $vocabularies = taxonomy_get_vocabularies(); + $options = array(); + foreach ($vocabularies as $vid => $vocab) { + $options[$vid] = $vocab->name; + } + $form['settings']['vids'] = array( + '#title' => t('Limit to these vocabularies'), + '#type' => 'checkboxes', + '#options' => $options, + '#default_value' => !empty($conf['vids']) ? $conf['vids'] : array(), + '#description' => t('If no vocabularies are checked, terms from all vocabularies will be accepted.'), + ); + + $form['settings']['breadcrumb'] = array( + '#title' => t('Inject hierarchy into breadcrumb trail'), + '#type' => 'checkbox', + '#default_value' => !empty($conf['breadcrumb']), + '#description' => t('If checked, taxonomy term parents will appear in the breadcrumb trail.'), + ); + + $form['settings']['transform'] = array( + '#title' => t('Transform dashes in URL to spaces in term name filter values'), + '#type' => 'checkbox', + '#default_value' => !empty($conf['transform']), + ); +// return $form; +} + +/** + * Form fragment to get an argument to convert a placeholder for preview. + */ +function ctools_term_ctools_argument_placeholder($conf) { + switch ($conf['input_form']) { + case 'tid': + default: + return array( + '#type' => 'textfield', + '#description' => t('Enter a taxonomy term ID.'), + ); + case 'term': + return array( + '#type' => 'textfield', + '#description' => t('Enter a taxonomy term name.'), + ); + } +} + +/** + * Inject the breadcrumb trail if necessary. + */ +function ctools_term_breadcrumb($conf, $context) { + if (empty($conf['breadcrumb']) || empty($context->data) || empty($context->data->tid)) { + return; + } + + $breadcrumb = array(); + $current = new stdClass(); + $current->tid = $context->data->tid; + while ($parents = taxonomy_get_parents($current->tid)) { + $current = array_shift($parents); + $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid); + } + + $breadcrumb = array_merge(drupal_get_breadcrumb(), array_reverse($breadcrumb)); + drupal_set_breadcrumb($breadcrumb); +} diff --git a/sites/all/modules/contrib/ctools/plugins/arguments/terms.inc b/sites/all/modules/contrib/ctools/plugins/arguments/terms.inc new file mode 100644 index 0000000..4298ea9 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/arguments/terms.inc @@ -0,0 +1,77 @@ + t("Taxonomy term (multiple): ID"), + // keyword to use for %substitution + 'keyword' => 'term', + 'description' => t('Creates a group of taxonomy terms from a list of tids separated by a comma or a plus sign. In general the first term of the list will be used for panes.'), + 'context' => 'ctools_terms_context', + 'default' => array('breadcrumb' => TRUE), + 'settings form' => 'ctools_terms_settings_form', + 'placeholder form' => array( + '#type' => 'textfield', + '#description' => t('Enter a term ID or a list of term IDs separated by a + or a ,'), + ), + 'breadcrumb' => 'ctools_terms_breadcrumb', +); + +/** + * Discover if this argument gives us the term we crave. + */ +function ctools_terms_context($arg = NULL, $conf = NULL, $empty = FALSE) { + // If unset it wants a generic, unfilled context. + if ($empty) { + return ctools_context_create_empty('terms'); + } + + $terms = ctools_break_phrase($arg); + if (empty($terms->value) || !empty($terms->invalid_input)) { + return FALSE; + } + + $context = ctools_context_create('terms', $terms); + $context->original_argument = $arg; + return $context; +} + +/** + * Settings form for the argument + */ +function ctools_terms_settings_form(&$form, &$form_state, $conf) { + $form['settings']['breadcrumb'] = array( + '#title' => t('Inject hierarchy of first term into breadcrumb trail'), + '#type' => 'checkbox', + '#default_value' => !empty($conf['breadcrumb']), + '#description' => t('If checked, taxonomy term parents will appear in the breadcrumb trail.'), + ); +// return $form; +} + +/** + * Inject the breadcrumb trail if necessary. + */ +function ctools_terms_breadcrumb($conf, $context) { + if (empty($conf['breadcrumb'])) { + return; + } + + $current->tid = $context->tids[0]; + $breadcrumb = array(); + while ($parents = taxonomy_get_parents($current->tid)) { + $current = array_shift($parents); + $breadcrumb[] = l($current->name, 'taxonomy/term/' . $current->tid); + } + + $breadcrumb = array_merge(drupal_get_breadcrumb(), array_reverse($breadcrumb)); + drupal_set_breadcrumb($breadcrumb); +} diff --git a/sites/all/modules/contrib/ctools/plugins/arguments/uid.inc b/sites/all/modules/contrib/ctools/plugins/arguments/uid.inc new file mode 100644 index 0000000..f9d5315 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/arguments/uid.inc @@ -0,0 +1,53 @@ + t("User: ID"), + // keyword to use for %substitution + 'keyword' => 'user', + 'description' => t('Creates a user context from a user ID argument.'), + 'context' => 'ctools_argument_uid_context', + 'placeholder form' => array( + '#type' => 'textfield', + '#description' => t('Enter the user ID of a user for this argument'), + ), + 'default' => array('to_arg' => TRUE), + 'path placeholder' => '%pm_uid_arg', // This is in pagemanager. + 'path placeholder to_arg' => TRUE, + 'no ui' => TRUE, +); + +/** + * Discover if this argument gives us the user we crave. + */ +function ctools_argument_uid_context($arg = NULL, $conf = NULL, $empty = FALSE) { + // If unset it wants a generic, unfilled context. + if ($empty) { + return ctools_context_create_empty('user'); + } + + // We can accept either a node object or a pure nid. + if (is_object($arg)) { + return ctools_context_create('user', $arg); + } + + if (!is_numeric($arg)) { + return NULL; + } + + $account = user_load($arg); + if (!$account) { + return NULL; + } + + return ctools_context_create('user', $account); +} diff --git a/sites/all/modules/contrib/ctools/plugins/arguments/user_edit.inc b/sites/all/modules/contrib/ctools/plugins/arguments/user_edit.inc new file mode 100644 index 0000000..32b2b81 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/arguments/user_edit.inc @@ -0,0 +1,47 @@ + t("User edit form: User ID"), + // keyword to use for %substitution + 'keyword' => 'user', + 'description' => t('Creates a user edit form context from a user ID argument.'), + 'context' => 'ctools_user_edit_context', + 'placeholder form' => array( + '#type' => 'textfield', + '#description' => t('Enter the user ID for this argument.'), + ), +); + +/** + * Discover if this argument gives us the term we crave. + */ +function ctools_user_edit_context($arg = NULL, $conf = NULL, $empty = FALSE) { + // If unset it wants a generic, unfilled context. + if ($empty) { + return ctools_context_create_empty('user_edit_form'); + } + if(is_object($arg)){ + return ctools_context_create('user_edit_form', $arg); + } + if (!is_numeric($arg)) { + return FALSE; + } + + $account= user_load($arg); + if (!$account) { + return NULL; + } + + // This will perform a node_access check, so we don't have to. + return ctools_context_create('user_edit_form', $account); +} \ No newline at end of file diff --git a/sites/all/modules/contrib/ctools/plugins/arguments/user_name.inc b/sites/all/modules/contrib/ctools/plugins/arguments/user_name.inc new file mode 100644 index 0000000..f6f3b46 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/arguments/user_name.inc @@ -0,0 +1,47 @@ + t("User: name"), + // keyword to use for %substitution + 'keyword' => 'user', + 'description' => t('Creates a user context from a user name.'), + 'context' => 'ctools_argument_user_name_context', + 'placeholder form' => array( + '#type' => 'textfield', + '#description' => t('Enter the username of a user for this argument'), + ), +); + +/** + * Discover if this argument gives us the user we crave. + */ +function ctools_argument_user_name_context($arg = NULL, $conf = NULL, $empty = FALSE) { + // If unset it wants a generic, unfilled context. + if ($empty) { + return ctools_context_create_empty('user'); + } + + // We can accept either a node object or a pure nid. + if (is_object($arg)) { + return ctools_context_create('user', $arg); + } + + $account = user_load_by_name($arg); + if (!$account) { + return NULL; + } + return ctools_context_create('user', $account); +} + + + diff --git a/sites/all/modules/contrib/ctools/plugins/arguments/vid.inc b/sites/all/modules/contrib/ctools/plugins/arguments/vid.inc new file mode 100644 index 0000000..064b22d --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/arguments/vid.inc @@ -0,0 +1,46 @@ + t("Vocabulary: ID"), + // keyword to use for %substitution + 'keyword' => 'vocabulary', + 'description' => t('Creates a vocabulary context from a vocabulary ID argument.'), + 'context' => 'ctools_vid_context', + 'placeholder form' => array( + '#type' => 'textfield', + '#description' => t('Enter the vocabulary ID for this argument'), + ), + 'no ui' => TRUE, +); + +/** + * Discover if this argument gives us the vocabulary we crave. + */ +function ctools_vid_context($arg = NULL, $conf = NULL, $empty = FALSE) { + // If unset it wants a generic, unfilled context. + if ($empty) { + return ctools_context_create_empty('entity:taxonomy_vocabulary'); + } + + if (!is_numeric($arg)) { + return NULL; + } + + $vocabulary = taxonomy_vocabulary_load($arg); + if (!$vocabulary) { + return NULL; + } + + return ctools_context_create('vocabulary', $vocabulary); +} + diff --git a/sites/all/modules/contrib/ctools/plugins/cache/export_ui.inc b/sites/all/modules/contrib/ctools/plugins/cache/export_ui.inc new file mode 100644 index 0000000..53483a5 --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/cache/export_ui.inc @@ -0,0 +1,39 @@ + t('Export UI wizard cache'), + 'cache get' => 'ctools_cache_export_ui_cache_get', + 'cache set' => 'ctools_cache_export_ui_cache_set', + // Some operations use a 'finalize' but that really just means set + // for us, since we're not using temporary storage for subsystems. + 'cache finalize' => 'ctools_cache_export_ui_cache_set', +); + +function ctools_cache_export_ui_cache_get($plugin_name, $key) { + ctools_include('export-ui'); + $plugin = ctools_get_export_ui($plugin_name); + $handler = ctools_export_ui_get_handler($plugin); + if ($handler) { + $item = $handler->edit_cache_get($key); + if (!$item) { + $item = ctools_export_crud_load($handler->plugin['schema'], $key); + } + return $item; + } +} + +function ctools_cache_export_ui_cache_set($plugin_name, $key, $item) { + ctools_include('export-ui'); + $plugin = ctools_get_export_ui($plugin_name); + $handler = ctools_export_ui_get_handler($plugin); + if ($handler) { + return $handler->edit_cache_set_key($item, $key); + } +} diff --git a/sites/all/modules/contrib/ctools/plugins/cache/simple.inc b/sites/all/modules/contrib/ctools/plugins/cache/simple.inc new file mode 100644 index 0000000..570398b --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/cache/simple.inc @@ -0,0 +1,51 @@ + t('Simple'), + 'cache get' => 'ctools_cache_simple_cache_get', + 'cache set' => 'ctools_cache_simple_cache_set', + 'cache clear' => 'ctools_cache_simple_cache_clear', +); + +function ctools_cache_simple_cache_get($data, $key) { + ctools_include('object-cache'); + + // Ensure that if there is somehow no data, we at least don't stomp on other + // people's caches. + if (empty($data)) { + $data = 'simple_cache_plugin'; + } + + return ctools_object_cache_get($data, $key); +} + +function ctools_cache_simple_cache_set($data, $key, $object) { + ctools_include('object-cache'); + + // Ensure that if there is somehow no data, we at least don't stomp on other + // people's caches. + if (empty($data)) { + $data = 'simple_cache_plugin'; + } + + return ctools_object_cache_set($data, $key, $object); +} + +function ctools_cache_simple_cache_clear($data, $key) { + ctools_include('object-cache'); + + // Ensure that if there is somehow no data, we at least don't stomp on other + // people's caches. + if (empty($data)) { + $data = 'simple_cache_plugin'; + } + + return ctools_object_cache_clear($data, $key); +} diff --git a/sites/all/modules/contrib/ctools/plugins/content_types/block/block.inc b/sites/all/modules/contrib/ctools/plugins/content_types/block/block.inc new file mode 100644 index 0000000..4d4c31c --- /dev/null +++ b/sites/all/modules/contrib/ctools/plugins/content_types/block/block.inc @@ -0,0 +1,565 @@ + t('Block'), + 'content type' => 'ctools_block_content_type_content_type', +); + +/** + * Return the block content types with the specified $subtype_id. + */ +function ctools_block_content_type_content_type($subtype_id) { + list($module, $delta) = explode('-', $subtype_id, 2); + $module_blocks = module_invoke($module, 'block_info'); + if (isset($module_blocks[$delta])) { + return _ctools_block_content_type_content_type($module, $delta, $module_blocks[$delta]); + } +} + +/** + * Return all block content types available. + * + * Modules wanting to make special adjustments the way that CTools handles their blocks + * can implement an extension to the hook_block() family, where the function name is + * of the form "$module . '_ctools_block_info'". + */ +function ctools_block_content_type_content_types() { + $types = &drupal_static(__FUNCTION__); + if (isset($types)) { + return $types; + } + + $types = array(); + foreach (module_implements('block_info') as $module) { + $module_blocks = module_invoke($module, 'block_info'); + if ($module_blocks) { + foreach ($module_blocks as $delta => $block) { + $info = _ctools_block_content_type_content_type($module, $delta, $block); + // this check means modules can remove their blocks; particularly useful + // if they offer the block some other way (like we do for views) + if ($info) { + $types["$module-$delta"] = $info; + } + } + } + } + return $types; +} + +/** + * Return an info array for a specific block. + */ +function _ctools_block_content_type_content_type($module, $delta, $block) { + // strip_tags used because it goes through check_plain and that + // just looks bad. + $info = array( + 'title' => strip_tags($block['info']), + ); + + // Ask around for further information by invoking the hook_block() extension. + $function = $module . '_ctools_block_info'; + if (!function_exists($function)) { + $function = 'ctools_default_block_info'; + } + $function($module, $delta, $info); + + return $info; +} + +/** + * Load block info from the database. + * + * This is copied from _block_load_blocks(). It doesn't use that + * function because _block_load_blocks sorts by region, and it + * doesn't cache its results anyway. + */ +function _ctools_block_load_blocks() { + if (!module_exists('block')) { + return array(); + } + + $blocks = &drupal_static(__FUNCTION__, NULL); + if (!isset($blocks)) { + global $theme_key; + + $query = db_select('block', 'b'); + $result = $query + ->fields('b') + ->condition('b.theme', $theme_key) + ->orderBy('b.region') + ->orderBy('b.weight') + ->orderBy('b.module') + ->addTag('block_load') + ->addTag('translatable') + ->execute(); + + $block_info = $result->fetchAllAssoc('bid'); + // Allow modules to modify the block list. + drupal_alter('block_list', $block_info); + + $blocks = array(); + foreach ($block_info as $block) { + $blocks["{$block->module}_{$block->delta}"] = $block; + } + } + + return $blocks; +} + +/** + * Fetch the stored info for a block. + * + * The primary reason to use this is so that modules which perform alters + * can have their alters make it to the block. + */ +function _ctools_get_block_info($module, $delta) { + $blocks = _ctools_block_load_blocks(); + + $key = $module . '_' . $delta; + if (isset($blocks[$key])) { + return $blocks[$key]; + } +} + +/** + * Output function for the 'block' content type. Outputs a block + * based on the module and delta supplied in the configuration. + */ +function ctools_block_content_type_render($subtype, $conf) { + list($module, $delta) = _ctools_block_get_module_delta($subtype, $conf); + + $info = _ctools_get_block_info($module, $delta); + $block = module_invoke($module, 'block_view', $delta); + + if (!empty($info)) { + // Valid PHP function names cannot contain hyphens. + $block_delta = str_replace('-', '_', $delta); + + // Allow modules to modify the block before it is viewed, via either + // hook_block_view_alter() or hook_block_view_MODULE_DELTA_alter(). + drupal_alter(array('block_view', "block_view_{$module}_{$block_delta}"), $block, $info); + } + + if (empty($block)) { + return; + } + + $block = (object) $block; + $block->module = $module; + $block->delta = $delta; + + if (!isset($block->title)) { + if ($module == 'block' && !empty($info) && isset($info->title)) { + $block->title = $info->title; + } + else if (isset($block->subject)) { + $block->title = $block->subject; + } + else { + $block->title = NULL; + } + } + + if (module_exists('block') && user_access('administer blocks')) { + $block->admin_links = array( + array( + 'title' => t('Configure block'), + 'href' => "admin/structure/block/manage/$module/$delta/configure", + 'query' => drupal_get_destination(), + ), + ); + } + + return $block; +} + +/** + * Empty form so we can have the default override title. + */ +function ctools_block_content_type_edit_form($form, &$form_state) { + // Does nothing! + return $form; +} + +/** + * Submit function to fix the subtype for really old panel panes. + */ +function ctools_block_content_type_edit_form_submit($form, &$form_state) { + if (empty($form_state['subtype']) && isset($form_state['pane'])) { + $form_state['pane']->subtype = $form_state['conf']['module'] . '-' . $form_state['conf']['delta']; + unset($form_state['conf']['module']); + unset($form_state['conf']['delta']); + } +} + +/** + * Returns an edit form for a block. + */ +//function ctools_block_content_type_edit_form($id, $parents, $conf) { +// if (user_access('administer advanced pane settings')) { +// $form['block_visibility'] = array( +// '#type' => 'checkbox', +// '#title' => t('Use block visibility settings (see block config)'), +// '#default_value' => !empty($conf['block_visibility']), +// '#description' => t('If checked, the block visibility settings for this block will apply to this block.'), +// ); +// // Module-specific block configurations. +// if ($settings = module_invoke($module, 'block', 'configure', $delta)) { +// // Specifically modify a couple of core block forms. +// if ($module == 'block') { +// unset($settings['submit']); +// $settings['info']['#type'] = 'value'; +// $settings['info']['#value'] = $settings['info']['#default_value']; +// } +// ctools_admin_fix_block_tree($settings); +// $form['block_settings'] = array( +// '#type' => 'fieldset', +// '#title' => t('Block settings'), +// '#description' => t('Settings in this section are global and are for all blocks of this type, anywhere in the system.'), +// '#tree' => FALSE, +// ); +// +// +// $form['block_settings'] += $settings; +// } +// } +// +// return $form; +//} + +//function ctools_admin_submit_block(&$form_values) { +// if (!empty($form_values['block_settings'])) { +// module_invoke($form_values['module'], 'block', 'save', $form_values['delta'], $form_values['block_settings']); +// } +//} +// +///** +// * Because form api cannot collapse just part of a tree, and the block settings +// * assume no tree, we have to collapse the tree ourselves. +// */ +//function ctools_admin_fix_block_tree(&$form, $key = NULL) { +// if ($key) { +// if (!empty($form['#parents'])) { +// $form['#parents'] = array_merge(array('configuration', 'block_settings'), $form['#parents']); +// } +// else if (empty($form['#tree'])) { +// $form['#parents'] = array('configuration', 'block_settings', $key); +// } +// } +// +// if (isset($form['#type']) && $form['#type'] == 'textarea' && !empty($form['#rows']) && $form['#rows'] > 10) { +// $form['#rows'] = 10; +// } +// +// foreach (element_children($form) as $key) { +// ctools_admin_fix_block_tree($form[$key], $key); +// } +//} + +/** + * Returns the administrative title for a type. + */ +function ctools_block_content_type_admin_title($subtype, $conf) { + list($module, $delta) = _ctools_block_get_module_delta($subtype, $conf); + $block = module_invoke($module, 'block_info'); + if (empty($block) || empty($block[$delta])) { + return t('Deleted/missing block @module-@delta', array('@module' => $module, '@delta' => $delta)); + } + + // The block description reported by hook_block() is plain text, but the title + // reported by this hook should be HTML. + $title = check_plain($block[$delta]['info']); + return $title; +} + +/** + * Output function for the 'block' content type. Outputs a block + * based on the module and delta supplied in the configuration. + */ +function ctools_block_content_type_admin_info($subtype, $conf) { + list($module, $delta) = _ctools_block_get_module_delta($subtype, $conf); + $block = (object) module_invoke($module, 'block_view', $delta); + + if (!empty($block)) { + // Sanitize the block because ', + 'true' => '', + ); + $view->field['age']->options['type_custom_false'] = $values['false']; + $view->field['age']->options['type_custom_true'] = $values['true']; + $this->assertNotEqual($values['false'], $view->field['age']->advanced_render($view->result[0])); + $this->assertNotEqual($values['true'], $view->field['age']->advanced_render($view->result[1])); + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_field_counter.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_counter.test new file mode 100644 index 0000000..2ddcb6f --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_counter.test @@ -0,0 +1,70 @@ + 'Field: Counter', + 'description' => 'Tests the views_handler_field_counter handler.', + 'group' => 'Views Handlers', + ); + } + + function testSimple() { + $view = $this->getBasicView(); + $view->display['default']->handler->override_option('fields', array( + 'counter' => array( + 'id' => 'counter', + 'table' => 'views', + 'field' => 'counter', + 'relationship' => 'none', + ), + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + ), + )); + $view->preview(); + + $this->assertEqual(1, $view->style_plugin->rendered_fields[0]['counter']); + $this->assertEqual(2, $view->style_plugin->rendered_fields[1]['counter']); + $this->assertEqual(3, $view->style_plugin->rendered_fields[2]['counter']); + $view->destroy(); + + $view = $this->getBasicView(); + $rand_start = rand(5, 10); + $view->display['default']->handler->override_option('fields', array( + 'counter' => array( + 'id' => 'counter', + 'table' => 'views', + 'field' => 'counter', + 'relationship' => 'none', + 'counter_start' => $rand_start + ), + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + ), + )); + $view->preview(); + + $this->assertEqual(0 + $rand_start, $view->style_plugin->rendered_fields[0]['counter']); + $this->assertEqual(1 + $rand_start, $view->style_plugin->rendered_fields[1]['counter']); + $this->assertEqual(2 + $rand_start, $view->style_plugin->rendered_fields[2]['counter']); + } + + // @TODO: Write tests for pager. + function testPager() { + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_field_custom.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_custom.test new file mode 100644 index 0000000..b45fd17 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_custom.test @@ -0,0 +1,47 @@ + 'Field: Custom', + 'description' => 'Test the core views_handler_field_custom handler.', + 'group' => 'Views Handlers', + ); + } + + function viewsData() { + $data = parent::viewsData(); + $data['views_test']['name']['field']['handler'] = 'views_handler_field_custom'; + return $data; + } + + public function testFieldCustom() { + $view = $this->getBasicView(); + + // Alter the text of the field to a random string. + $random = $this->randomName(); + $view->display['default']->handler->override_option('fields', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + 'alter' => array( + 'text' => $random, + ), + ), + )); + + $this->executeView($view); + + $this->assertEqual($random, $view->style_plugin->get_field(0, 'name')); + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_field_date.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_date.test new file mode 100644 index 0000000..7944142 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_date.test @@ -0,0 +1,117 @@ + 'Field: Date', + 'description' => 'Test the core views_handler_field_date handler.', + 'group' => 'Views Handlers', + ); + } + + function viewsData() { + $data = parent::viewsData(); + $data['views_test']['created']['field']['handler'] = 'views_handler_field_date'; + return $data; + } + + public function testFieldDate() { + $view = $this->getBasicView(); + + $view->display['default']->handler->override_option('fields', array( + 'created' => array( + 'id' => 'created', + 'table' => 'views_test', + 'field' => 'created', + 'relationship' => 'none', + // c is iso 8601 date format @see http://php.net/manual/en/function.date.php + 'custom_date_format' => 'c', + 'second_date_format' => 'custom', + 'second_date_format_custom' => 'c', + ), + )); + $time = gmmktime(0, 0, 0, 1, 1, 2000); + + $this->executeView($view); + + $timezones = array( + NULL, + 'UTC', + 'America/New_York', + ); + foreach ($timezones as $timezone) { + $dates = array( + 'small' => format_date($time, 'small', '', $timezone), + 'medium' => format_date($time, 'medium', '', $timezone), + 'large' => format_date($time, 'large', '', $timezone), + 'custom' => format_date($time, 'custom', 'c', $timezone), + 'today time ago custom' => format_date($time, 'custom', 'c', $timezone), + 'today time ago' => t('%time ago', array('%time' => format_interval(120, 2))), + ); + $this->assertRenderedDatesEqual($view, $dates, $timezone); + } + + $intervals = array( + 'raw time ago' => format_interval(REQUEST_TIME - $time, 2), + 'time ago' => t('%time ago', array('%time' => format_interval(REQUEST_TIME - $time, 2))), + // TODO write tests for them +// 'raw time span' => format_interval(REQUEST_TIME - $time, 2), +// 'time span' => t('%time hence', array('%time' => format_interval(REQUEST_TIME - $time, 2))), + ); + $this->assertRenderedDatesEqual($view, $intervals); + } + + protected function assertRenderedDatesEqual($view, $map, $timezone = NULL) { + foreach ($map as $date_format => $expected_result) { + $check_result_number = 0; + + // If it's "today time ago" format we have to check the 6th element. + if ($date_format == 'today time ago') { + $check_result_number = 5; + } + + // Correct the date format. + if ($date_format == 'today time ago custom') { + $date_format = 'today time ago'; + } + $view->field['created']->options['date_format'] = $date_format; + $t_args = array( + '%value' => $expected_result, + '%format' => $date_format, + ); + if (isset($timezone)) { + $t_args['%timezone'] = $timezone; + $message = t('Value %value in %format format for timezone %timezone matches.', $t_args); + $view->field['created']->options['timezone'] = $timezone; + } + else { + $message = t('Value %value in %format format matches.', $t_args); + } + $actual_result = $view->field['created']->advanced_render($view->result[$check_result_number]); + $this->assertEqual($expected_result, $actual_result, $message); + } + } + + /** + * Appends dataSet() with a data row for "today time ago" format testing. + */ + protected function dataSet() { + $data = parent::dataSet(); + $data[] = array( + 'name' => 'David', + 'age' => 25, + 'job' => 'Singer', + 'created' => REQUEST_TIME - 120, + ); + + return $data; + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_field_file_extension.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_file_extension.test new file mode 100644 index 0000000..ab8b0a9 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_file_extension.test @@ -0,0 +1,66 @@ + 'Field: File extension', + 'description' => 'Test the views_handler_field_file_extension handler.', + 'group' => 'Views Handlers', + ); + } + + function dataSet() { + $data = parent::dataSet(); + $data[0]['name'] = 'file.png'; + $data[1]['name'] = 'file.tar'; + $data[2]['name'] = 'file.tar.gz'; + $data[3]['name'] = 'file'; + + return $data; + } + + function viewsData() { + $data = parent::viewsData(); + $data['views_test']['name']['field']['handler'] = 'views_handler_field_file_extension'; + $data['views_test']['name']['real field'] = 'name'; + + return $data; + } + + /** + * Tests the 'extension_detect_tar' handler option. + */ + public function testFileExtensionTarOption() { + $view = $this->getBasicView(); + + $view->display['default']->handler->override_option('fields', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + ), + )); + + $this->executeView($view); + + // Test without the tar option. + $this->assertEqual($view->field['name']->advanced_render($view->result[0]), 'png'); + $this->assertEqual($view->field['name']->advanced_render($view->result[1]), 'tar'); + $this->assertEqual($view->field['name']->advanced_render($view->result[2]), 'gz'); + $this->assertEqual($view->field['name']->advanced_render($view->result[3]), ''); + // Test with the tar option. + $view->field['name']->options['extension_detect_tar'] = TRUE; + $this->assertEqual($view->field['name']->advanced_render($view->result[0]), 'png'); + $this->assertEqual($view->field['name']->advanced_render($view->result[1]), 'tar'); + $this->assertEqual($view->field['name']->advanced_render($view->result[2]), 'tar.gz'); + $this->assertEqual($view->field['name']->advanced_render($view->result[3]), ''); + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_field_file_size.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_file_size.test new file mode 100644 index 0000000..8652754 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_file_size.test @@ -0,0 +1,64 @@ + 'Field: file_size', + 'description' => 'Test the core views_handler_field_file_size handler.', + 'group' => 'Views Handlers', + ); + } + + function dataSet() { + $data = parent::dataSet(); + $data[0]['age'] = 0; + $data[1]['age'] = 10; + $data[2]['age'] = 1000; + $data[3]['age'] = 10000; + + return $data; + } + + function viewsData() { + $data = parent::viewsData(); + $data['views_test']['age']['field']['handler'] = 'views_handler_field_file_size'; + + return $data; + } + + public function testFieldFileSize() { + $view = $this->getBasicView(); + + $view->display['default']->handler->override_option('fields', array( + 'age' => array( + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + ), + )); + + $this->executeView($view); + + // Test with the formatted option. + $this->assertEqual($view->field['age']->advanced_render($view->result[0]), ''); + $this->assertEqual($view->field['age']->advanced_render($view->result[1]), '10 bytes'); + $this->assertEqual($view->field['age']->advanced_render($view->result[2]), '1000 bytes'); + $this->assertEqual($view->field['age']->advanced_render($view->result[3]), '9.77 KB'); + // Test with the bytes option. + $view->field['age']->options['file_size_display'] = 'bytes'; + $this->assertEqual($view->field['age']->advanced_render($view->result[0]), ''); + $this->assertEqual($view->field['age']->advanced_render($view->result[1]), 10); + $this->assertEqual($view->field['age']->advanced_render($view->result[2]), 1000); + $this->assertEqual($view->field['age']->advanced_render($view->result[3]), 10000); + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_field_math.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_math.test new file mode 100644 index 0000000..ac33ac4 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_math.test @@ -0,0 +1,45 @@ + 'Field: Math', + 'description' => 'Test the core views_handler_field_math handler.', + 'group' => 'Views Handlers', + ); + } + + function viewsData() { + $data = parent::viewsData(); + return $data; + } + + public function testFieldCustom() { + $view = $this->getBasicView(); + + // Alter the text of the field to a random string. + $rand1 = rand(0, 100); + $rand2 = rand(0, 100); + $view->display['default']->handler->override_option('fields', array( + 'expression' => array( + 'id' => 'expression', + 'table' => 'views', + 'field' => 'expression', + 'relationship' => 'none', + 'expression' => $rand1 . ' + ' . $rand2, + ), + )); + + $this->executeView($view); + + $this->assertEqual($rand1 + $rand2, $view->style_plugin->get_field(0, 'expression')); + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_field_url.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_url.test new file mode 100644 index 0000000..527e94f --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_url.test @@ -0,0 +1,60 @@ + 'Field: Url', + 'description' => 'Test the core views_handler_field_url handler.', + 'group' => 'Views Handlers', + ); + } + + function viewsData() { + $data = parent::viewsData(); + $data['views_test']['name']['field']['handler'] = 'views_handler_field_url'; + return $data; + } + + public function testFieldUrl() { + $view = $this->getBasicView(); + + $view->display['default']->handler->override_option('fields', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + 'display_as_link' => FALSE, + ), + )); + + $this->executeView($view); + + $this->assertEqual('John', $view->field['name']->advanced_render($view->result[0])); + + // Make the url a link. + $view->delete(); + $view = $this->getBasicView(); + + $view->display['default']->handler->override_option('fields', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + ), + )); + + $this->executeView($view); + + $this->assertEqual(l('John', 'John'), $view->field['name']->advanced_render($view->result[0])); + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_field_xss.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_xss.test new file mode 100644 index 0000000..65a1ce2 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_field_xss.test @@ -0,0 +1,60 @@ + 'Field: Xss', + 'description' => 'Test the core views_handler_field_css handler.', + 'group' => 'Views Handlers', + ); + } + + function dataHelper() { + $map = array( + 'John' => 'John', + "Foo\xC0barbaz" => '', + 'Fooÿñ' => 'Fooÿñ' + ); + + return $map; + } + + + function viewsData() { + $data = parent::viewsData(); + $data['views_test']['name']['field']['handler'] = 'views_handler_field_xss'; + + return $data; + } + + public function testFieldXss() { + $view = $this->getBasicView(); + + $view->display['default']->handler->override_option('fields', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + ), + )); + + $this->executeView($view); + + $counter = 0; + foreach ($this->dataHelper() as $input => $expected_result) { + $view->result[$counter]->views_test_name = $input; + $this->assertEqual($view->field['name']->advanced_render($view->result[$counter]), $expected_result); + $counter++; + } + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_combine.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_combine.test new file mode 100644 index 0000000..99bf1eb --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_combine.test @@ -0,0 +1,105 @@ + 'Filter: Combine', + 'description' => 'Tests the combine filter handler.', + 'group' => 'Views Handlers', + ); + } + + function setUp() { + parent::setUp(); + $this->column_map = array( + 'views_test_name' => 'name', + 'views_test_job' => 'job', + ); + } + + protected function getBasicView() { + $view = parent::getBasicView(); + $fields = $view->display['default']->handler->options['fields']; + $view->display['default']->display_options['fields']['job'] = array( + 'id' => 'job', + 'table' => 'views_test', + 'field' => 'job', + 'relationship' => 'none', + ); + return $view; + } + + public function testFilterCombineContains() { + $view = $this->getBasicView(); + + // Change the filtering. + $view->display['default']->handler->override_option('filters', array( + 'age' => array( + 'id' => 'combine', + 'table' => 'views', + 'field' => 'combine', + 'relationship' => 'none', + 'operator' => 'contains', + 'fields' => array( + 'name', + 'job', + ), + 'value' => 'ing', + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'John', + 'job' => 'Singer', + ), + array( + 'name' => 'George', + 'job' => 'Singer', + ), + array( + 'name' => 'Ringo', + 'job' => 'Drummer', + ), + array( + 'name' => 'Ginger', + 'job' => NULL, + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + /** + * Additional data to test the NULL issue. + */ + protected function dataSet() { + $data_set = parent::dataSet(); + $data_set[] = array( + 'name' => 'Ginger', + 'age' => 25, + 'job' => NULL, + 'created' => gmmktime(0, 0, 0, 1, 2, 2000), + ); + return $data_set; + } + + /** + * Allow {views_test}.job to be NULL. + */ + protected function schemaDefinition() { + $schema = parent::schemaDefinition(); + unset($schema['views_test']['fields']['job']['not null']); + return $schema; + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_date.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_date.test new file mode 100644 index 0000000..8b92ccb --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_date.test @@ -0,0 +1,221 @@ + 'Filter: Date', + 'description' => 'Test the core views_handler_filter_date handler.', + 'group' => 'Views Handlers', + ); + } + + function setUp() { + parent::setUp(); + // Add some basic test nodes. + $this->nodes = array(); + $this->nodes[] = $this->drupalCreateNode(array('created' => 100000)); + $this->nodes[] = $this->drupalCreateNode(array('created' => 200000)); + $this->nodes[] = $this->drupalCreateNode(array('created' => 300000)); + $this->nodes[] = $this->drupalCreateNode(array('created' => time() + 86400)); + + $this->map = array( + 'nid' => 'nid', + ); + $this->enableViewsUi(); + } + + /** + * Test the general offset functionality. + */ + function testOffset() { + $view = $this->views_test_offset(); + // Test offset for simple operator. + $view->set_display('default'); + $view->init_handlers(); + $view->filter['created']->operator = '>'; + $view->filter['created']->value['type'] = 'offset'; + $view->filter['created']->value['value'] = '+1 hour'; + $view->execute_display('default'); + $expected_result = array( + array('nid' => $this->nodes[3]->nid), + ); + $this->assertIdenticalResultset($view, $expected_result, $this->map); + $view->destroy(); + + // Test "first day of" type of relative dates for simple operator. + $view->set_display('default'); + $view->init_handlers(); + $view->filter['created']->operator = '<'; + $view->filter['created']->value['type'] = 'offset'; + $view->filter['created']->value['value'] = 'last day of January 1970'; + $view->execute_display('default'); + $expected_result = array( + array('nid' => $this->nodes[0]->nid), + array('nid' => $this->nodes[1]->nid), + array('nid' => $this->nodes[2]->nid), + ); + $this->assertIdenticalResultset($view, $expected_result, $this->map); + $view->destroy(); + + // Test offset for between operator. + $view->set_display('default'); + $view->init_handlers(); + $view->filter['created']->operator = 'between'; + $view->filter['created']->value['type'] = 'offset'; + $view->filter['created']->value['max'] = '+2 days'; + $view->filter['created']->value['min'] = '+1 hour'; + $view->execute_display('default'); + $expected_result = array( + array('nid' => $this->nodes[3]->nid), + ); + $this->assertIdenticalResultset($view, $expected_result, $this->map); + $view->destroy(); + + // Test "first day of" type of relative dates for between operator. + $view->set_display('default'); + $view->init_handlers(); + $view->filter['created']->operator = 'between'; + $view->filter['created']->value['type'] = 'offset'; + $view->filter['created']->value['max'] = 'last day of January 1970'; + $view->filter['created']->value['min'] = 'first day of January 1970'; + $view->execute_display('default'); + $expected_result = array( + array('nid' => $this->nodes[0]->nid), + array('nid' => $this->nodes[1]->nid), + array('nid' => $this->nodes[2]->nid), + ); + $this->assertIdenticalResultset($view, $expected_result, $this->map); + $view->destroy(); + } + + + /** + * Tests the filter operator between/not between. + */ + function testBetween() { + // Test between with min and max. + $view = $this->views_test_between(); + $view->set_display('default'); + $view->init_handlers(); + $view->filter['created']->operator = 'between'; + $view->filter['created']->value['min'] = format_date(150000, 'custom', 'Y-m-d H:s'); + $view->filter['created']->value['max'] = format_date(250000, 'custom', 'Y-m-d H:s'); + $view->execute_display('default'); + $expected_result = array( + array('nid' => $this->nodes[1]->nid), + ); + $this->assertIdenticalResultset($view, $expected_result, $this->map); + $view->destroy(); + + // Test between with just max. + $view = $this->views_test_between(); + $view->set_display('default'); + $view->init_handlers(); + $view->filter['created']->operator = 'between'; + $view->filter['created']->value['max'] = format_date(250000, 'custom', 'Y-m-d H:s'); + $view->execute_display('default'); + $expected_result = array( + array('nid' => $this->nodes[0]->nid), + array('nid' => $this->nodes[1]->nid), + ); + $this->assertIdenticalResultset($view, $expected_result, $this->map); + $view->destroy(); + + // Test not between with min and max. + $view = $this->views_test_between(); + $view->set_display('default'); + $view->init_handlers(); + $view->filter['created']->operator = 'not between'; + $view->filter['created']->value['min'] = format_date(150000, 'custom', 'Y-m-d H:s'); + $view->filter['created']->value['max'] = format_date(250000, 'custom', 'Y-m-d H:s'); + $view->execute_display('default'); + $expected_result = array( + array('nid' => $this->nodes[0]->nid), + array('nid' => $this->nodes[2]->nid), + array('nid' => $this->nodes[3]->nid), + ); + $this->assertIdenticalResultset($view, $expected_result, $this->map); + $view->destroy(); + + // Test not between with just max. + $view = $this->views_test_between(); + $view->set_display('default'); + $view->init_handlers(); + $view->filter['created']->operator = 'not between'; + $view->filter['created']->value['max'] = format_date(150000, 'custom', 'Y-m-d H:s'); + $view->execute_display('default'); + $expected_result = array( + array('nid' => $this->nodes[1]->nid), + array('nid' => $this->nodes[2]->nid), + array('nid' => $this->nodes[3]->nid), + ); + $this->assertIdenticalResultset($view, $expected_result, $this->map); + $view->destroy(); + } + + /** + * Make sure the validation callbacks works. + */ + function testUiValidation() { + $view = $this->views_test_between(); + $view->save(); + + $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration')); + $this->drupalLogin($admin_user); + menu_rebuild(); + $this->drupalGet('admin/structure/views/view/test_filter_date_between/edit'); + $this->drupalGet('admin/structure/views/nojs/config-item/test_filter_date_between/default/filter/created'); + + $edit = array(); + // Generate a definitive wrong value, which should be checked by validation. + $edit['options[value][value]'] = $this->randomString() . '-------'; + $this->drupalPost(NULL, $edit, t('Apply')); + $this->assertText(t('Invalid date format.'), 'Make sure that validation is runned and the invalidate date format is identified.'); + } + + function views_test_between() { + $view = new view; + $view->name = 'test_filter_date_between'; + $view->description = ''; + $view->tag = ''; + $view->base_table = 'node'; + $view->human_name = ''; + $view->core = 0; + $view->api_version = '3.0'; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['query']['type'] = 'views_query'; + $handler->display->display_options['query']['options']['query_comment'] = FALSE; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Field: Content: Nid */ + $handler->display->display_options['fields']['nid']['id'] = 'nid'; + $handler->display->display_options['fields']['nid']['table'] = 'node'; + $handler->display->display_options['fields']['nid']['field'] = 'nid'; + /* Filter criterion: Content: Post date */ + $handler->display->display_options['filters']['created']['id'] = 'created'; + $handler->display->display_options['filters']['created']['table'] = 'node'; + $handler->display->display_options['filters']['created']['field'] = 'created'; + + return $view; + } + + function views_test_offset() { + $view = $this->views_test_between(); + return $view; + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_equality.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_equality.test new file mode 100644 index 0000000..5bb48c8 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_equality.test @@ -0,0 +1,173 @@ + 'Filter: Equality', + 'description' => 'Test the core views_handler_filter_equality handler.', + 'group' => 'Views Handlers', + ); + } + + function setUp() { + parent::setUp(); + $this->column_map = array( + 'views_test_name' => 'name', + ); + } + + function viewsData() { + $data = parent::viewsData(); + $data['views_test']['name']['filter']['handler'] = 'views_handler_filter_equality'; + + return $data; + } + + function testEqual() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + 'operator' => '=', + 'value' => array('value' => 'Ringo'), + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'Ringo', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + public function testEqualGroupedExposed() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Name, Operator: =, Value: Ringo + $filters['name']['group_info']['default_group'] = 1; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'Ringo', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testNotEqual() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + 'operator' => '!=', + 'value' => array('value' => 'Ringo'), + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'John', + ), + array( + 'name' => 'George', + ), + array( + 'name' => 'Paul', + ), + array( + 'name' => 'Meredith', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + public function testEqualGroupedNotExposed() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Name, Operator: !=, Value: Ringo + $filters['name']['group_info']['default_group'] = 2; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'John', + ), + array( + 'name' => 'George', + ), + array( + 'name' => 'Paul', + ), + array( + 'name' => 'Meredith', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + + protected function getGroupedExposedFilters() { + $filters = array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + 'exposed' => TRUE, + 'expose' => array( + 'operator' => 'name_op', + 'label' => 'name', + 'identifier' => 'name', + ), + 'is_grouped' => TRUE, + 'group_info' => array( + 'label' => 'name', + 'identifier' => 'name', + 'default_group' => 'All', + 'group_items' => array( + 1 => array( + 'title' => 'Name is equal to Ringo', + 'operator' => '=', + 'value' => array('value' => 'Ringo'), + ), + 2 => array( + 'title' => 'Name is not equal to Ringo', + 'operator' => '!=', + 'value' => array('value' => 'Ringo'), + ), + ), + ), + ), + ); + return $filters; + } + +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_in_operator.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_in_operator.test new file mode 100644 index 0000000..3a20f8c --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_in_operator.test @@ -0,0 +1,196 @@ + 'Filter: in_operator', + 'description' => 'Test the core views_handler_filter_in_operator handler.', + 'group' => 'Views Handlers', + ); + } + + function viewsData() { + $data = parent::viewsData(); + $data['views_test']['age']['filter']['handler'] = 'views_handler_filter_in_operator'; + + return $data; + } + + public function testFilterInOperatorSimple() { + $view = $this->getBasicView(); + + // Add a in_operator ordering. + $view->display['default']->handler->override_option('filters', array( + 'age' => array( + 'id' => 'age', + 'field' => 'age', + 'table' => 'views_test', + 'value' => array(26, 30), + 'operator' => 'in', + ), + )); + + $this->executeView($view); + + $expected_result = array( + array( + 'name' => 'Paul', + 'age' => 26, + ), + array( + 'name' => 'Meredith', + 'age' => 30, + ), + ); + + $this->assertEqual(2, count($view->result)); + $this->assertIdenticalResultset($view, $expected_result, array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + )); + + $view->delete(); + $view = $this->getBasicView(); + + // Add a in_operator ordering. + $view->display['default']->handler->override_option('filters', array( + 'age' => array( + 'id' => 'age', + 'field' => 'age', + 'table' => 'views_test', + 'value' => array(26, 30), + 'operator' => 'not in', + ), + )); + + $this->executeView($view); + + $expected_result = array( + array( + 'name' => 'John', + 'age' => 25, + ), + array( + 'name' => 'George', + 'age' => 27, + ), + array( + 'name' => 'Ringo', + 'age' => 28, + ), + ); + + $this->assertEqual(3, count($view->result)); + $this->assertIdenticalResultset($view, $expected_result, array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + )); + } + + public function testFilterInOperatorGroupedExposedSimple() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Age, Operator: in, Value: 26, 30 + $filters['age']['group_info']['default_group'] = 1; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + + $expected_result = array( + array( + 'name' => 'Paul', + 'age' => 26, + ), + array( + 'name' => 'Meredith', + 'age' => 30, + ), + ); + + $this->assertEqual(2, count($view->result)); + $this->assertIdenticalResultset($view, $expected_result, array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + )); + } + + public function testFilterNotInOperatorGroupedExposedSimple() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Age, Operator: in, Value: 26, 30 + $filters['age']['group_info']['default_group'] = 2; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + + $expected_result = array( + array( + 'name' => 'John', + 'age' => 25, + ), + array( + 'name' => 'George', + 'age' => 27, + ), + array( + 'name' => 'Ringo', + 'age' => 28, + ), + ); + + $this->assertEqual(3, count($view->result)); + $this->assertIdenticalResultset($view, $expected_result, array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + )); + } + + protected function getGroupedExposedFilters() { + $filters = array( + 'age' => array( + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + 'exposed' => TRUE, + 'expose' => array( + 'operator' => 'age_op', + 'label' => 'age', + 'identifier' => 'age', + ), + 'is_grouped' => TRUE, + 'group_info' => array( + 'label' => 'age', + 'identifier' => 'age', + 'default_group' => 'All', + 'group_items' => array( + 1 => array( + 'title' => 'Age is one of 26, 30', + 'operator' => 'in', + 'value' => array(26, 30), + ), + 2 => array( + 'title' => 'Age is not one of 26, 30', + 'operator' => 'not in', + 'value' => array(26, 30), + ), + ), + ), + ), + ); + return $filters; + } + +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_numeric.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_numeric.test new file mode 100644 index 0000000..2ab1aea --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_numeric.test @@ -0,0 +1,409 @@ + 'Filter: Numeric', + 'description' => 'Tests the numeric filter handler', + 'group' => 'Views Handlers', + ); + } + + function setUp() { + parent::setUp(); + $this->column_map = array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + ); + } + + function viewsData() { + $data = parent::viewsData(); + $data['views_test']['age']['filter']['allow empty'] = TRUE; + $data['views_test']['id']['filter']['allow empty'] = FALSE; + + return $data; + } + + public function testFilterNumericSimple() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'age' => array( + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + 'operator' => '=', + 'value' => array('value' => 28), + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'Ringo', + 'age' => 28, + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + public function testFilterNumericExposedGroupedSimple() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Age, Operator: =, Value: 28 + $filters['age']['group_info']['default_group'] = 1; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'Ringo', + 'age' => 28, + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + public function testFilterNumericBetween() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'age' => array( + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + 'operator' => 'between', + 'value' => array( + 'min' => 26, + 'max' => 29, + ), + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'George', + 'age' => 27, + ), + array( + 'name' => 'Ringo', + 'age' => 28, + ), + array( + 'name' => 'Paul', + 'age' => 26, + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + + // test not between + $view->delete(); + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'age' => array( + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + 'operator' => 'not between', + 'value' => array( + 'min' => 26, + 'max' => 29, + ), + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'John', + 'age' => 25, + ), + array( + 'name' => 'Paul', + 'age' => 26, + ), + array( + 'name' => 'Meredith', + 'age' => 30, + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + public function testFilterNumericExposedGroupedBetween() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Age, Operator: between, Value: 26 and 29 + $filters['age']['group_info']['default_group'] = 2; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'George', + 'age' => 27, + ), + array( + 'name' => 'Ringo', + 'age' => 28, + ), + array( + 'name' => 'Paul', + 'age' => 26, + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + public function testFilterNumericExposedGroupedNotBetween() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Age, Operator: between, Value: 26 and 29 + $filters['age']['group_info']['default_group'] = 3; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'John', + 'age' => 25, + ), + array( + 'name' => 'Paul', + 'age' => 26, + ), + array( + 'name' => 'Meredith', + 'age' => 30, + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + + public function testFilterNumericEmpty() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'age' => array( + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + 'operator' => 'empty', + ), + )); + + $this->executeView($view); + $resultset = array( + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + + $view->delete(); + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'age' => array( + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + 'operator' => 'not empty', + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'John', + 'age' => 25, + ), + array( + 'name' => 'George', + 'age' => 27, + ), + array( + 'name' => 'Ringo', + 'age' => 28, + ), + array( + 'name' => 'Paul', + 'age' => 26, + ), + array( + 'name' => 'Meredith', + 'age' => 30, + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + + public function testFilterNumericExposedGroupedEmpty() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Age, Operator: empty, Value: + $filters['age']['group_info']['default_group'] = 4; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + + $this->executeView($view); + $resultset = array( + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + public function testFilterNumericExposedGroupedNotEmpty() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Age, Operator: empty, Value: + $filters['age']['group_info']['default_group'] = 5; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'John', + 'age' => 25, + ), + array( + 'name' => 'George', + 'age' => 27, + ), + array( + 'name' => 'Ringo', + 'age' => 28, + ), + array( + 'name' => 'Paul', + 'age' => 26, + ), + array( + 'name' => 'Meredith', + 'age' => 30, + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + + public function testAllowEmpty() { + $view = $this->getBasicView(); + + $view->display['default']->handler->override_option('filters', array( + 'id' => array( + 'id' => 'id', + 'table' => 'views_test', + 'field' => 'id', + 'relationship' => 'none', + ), + 'age' => array( + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + ), + )); + + $view->set_display('default'); + $view->init_handlers(); + + $id_operators = $view->filter['id']->operators(); + $age_operators = $view->filter['age']->operators(); + + $this->assertFalse(isset($id_operators['empty'])); + $this->assertFalse(isset($id_operators['not empty'])); + $this->assertTrue(isset($age_operators['empty'])); + $this->assertTrue(isset($age_operators['not empty'])); + } + + protected function getGroupedExposedFilters() { + $filters = array( + 'age' => array( + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + 'exposed' => TRUE, + 'expose' => array( + 'operator' => 'age_op', + 'label' => 'age', + 'identifier' => 'age', + ), + 'is_grouped' => TRUE, + 'group_info' => array( + 'label' => 'age', + 'identifier' => 'age', + 'default_group' => 'All', + 'group_items' => array( + 1 => array( + 'title' => 'Age is 28', + 'operator' => '=', + 'value' => array('value' => 28), + ), + 2 => array( + 'title' => 'Age is between 26 and 29', + 'operator' => 'between', + 'value' => array( + 'min' => 26, + 'max' => 29, + ), + ), + 3 => array( + 'title' => 'Age is not between 26 and 29', + 'operator' => 'not between', + 'value' => array( + 'min' => 26, + 'max' => 29, + ), + ), + 4 => array( + 'title' => 'Age is empty', + 'operator' => 'empty', + ), + 5 => array( + 'title' => 'Age is not empty', + 'operator' => 'not empty', + ), + ), + ), + ), + ); + return $filters; + } + +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_string.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_string.test new file mode 100644 index 0000000..ee74a28 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_filter_string.test @@ -0,0 +1,810 @@ + 'Filter: String', + 'description' => 'Tests the core views_handler_filter_string handler.', + 'group' => 'Views Handlers', + ); + } + + function setUp() { + parent::setUp(); + $this->column_map = array( + 'views_test_name' => 'name', + ); + } + + function viewsData() { + $data = parent::viewsData(); + $data['views_test']['name']['filter']['allow empty'] = TRUE; + $data['views_test']['job']['filter']['allow empty'] = FALSE; + $data['views_test']['description'] = $data['views_test']['name']; + + return $data; + } + + protected function schemaDefinition() { + $schema = parent::schemaDefinition(); + $schema['views_test']['fields']['description'] = array( + 'description' => "A person's description", + 'type' => 'text', + 'not null' => FALSE, + 'size' => 'big', + ); + + return $schema; + } + + /** + * An extended test dataset. + */ + protected function dataSet() { + $dataset = parent::dataSet(); + $dataset[0]['description'] = 'John Winston Ono Lennon, MBE (9 October 1940 – 8 December 1980) was an English musician and singer-songwriter who rose to worldwide fame as one of the founding members of The Beatles, one of the most commercially successful and critically acclaimed acts in the history of popular music. Along with fellow Beatle Paul McCartney, he formed one of the most successful songwriting partnerships of the 20th century.'; + $dataset[1]['description'] = 'George Harrison,[1] MBE (25 February 1943 – 29 November 2001)[2] was an English rock guitarist, singer-songwriter, actor and film producer who achieved international fame as lead guitarist of The Beatles.'; + $dataset[2]['description'] = 'Richard Starkey, MBE (born 7 July 1940), better known by his stage name Ringo Starr, is an English musician, singer-songwriter, and actor who gained worldwide fame as the drummer for The Beatles.'; + $dataset[3]['description'] = 'Sir James Paul McCartney, MBE (born 18 June 1942) is an English musician, singer-songwriter and composer. Formerly of The Beatles (1960–1970) and Wings (1971–1981), McCartney is the most commercially successful songwriter in the history of popular music, according to Guinness World Records.[1]'; + $dataset[4]['description'] = NULL; + + return $dataset; + } + + protected function getBasicView() { + $view = parent::getBasicView(); + $view->display['default']->options['fields']['description'] = array( + 'id' => 'description', + 'table' => 'views_test', + 'field' => 'description', + 'relationship' => 'none', + ); + return $view; + } + + function testFilterStringEqual() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + 'operator' => '=', + 'value' => 'Ringo', + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'Ringo', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringGroupedExposedEqual() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Name, Operator: =, Value: Ringo + $filters['name']['group_info']['default_group'] = 1; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + + $resultset = array( + array( + 'name' => 'Ringo', + ), + ); + + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringNotEqual() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + 'operator' => '!=', + 'value' => array('value' => 'Ringo'), + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'John', + ), + array( + 'name' => 'George', + ), + array( + 'name' => 'Paul', + ), + array( + 'name' => 'Meredith', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringGroupedExposedNotEqual() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Name, Operator: !=, Value: Ringo + $filters['name']['group_info']['default_group'] = '2'; + + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + + $resultset = array( + array( + 'name' => 'John', + ), + array( + 'name' => 'George', + ), + array( + 'name' => 'Paul', + ), + array( + 'name' => 'Meredith', + ), + ); + + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringContains() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + 'operator' => 'contains', + 'value' => 'ing', + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'Ringo', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + + function testFilterStringGroupedExposedContains() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Name, Operator: contains, Value: ing + $filters['name']['group_info']['default_group'] = '3'; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + + $resultset = array( + array( + 'name' => 'Ringo', + ), + ); + + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + + function testFilterStringWord() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'description' => array( + 'id' => 'description', + 'table' => 'views_test', + 'field' => 'description', + 'relationship' => 'none', + 'operator' => 'word', + 'value' => 'actor', + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'George', + ), + array( + 'name' => 'Ringo', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + $view->destroy(); + + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'description' => array( + 'id' => 'description', + 'table' => 'views_test', + 'field' => 'description', + 'relationship' => 'none', + 'operator' => 'allwords', + 'value' => 'Richard Starkey', + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'Ringo', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + + function testFilterStringGroupedExposedWord() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Name, Operator: contains, Value: ing + $filters['name']['group_info']['default_group'] = '3'; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + + $resultset = array( + array( + 'name' => 'Ringo', + ), + ); + + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + $view->destroy(); + + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Description, Operator: contains, Value: actor + $filters['description']['group_info']['default_group'] = '1'; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'George', + ), + array( + 'name' => 'Ringo', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringStarts() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'description' => array( + 'id' => 'description', + 'table' => 'views_test', + 'field' => 'description', + 'relationship' => 'none', + 'operator' => 'starts', + 'value' => 'George', + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'George', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringGroupedExposedStarts() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Name, Operator: starts, Value: George + $filters['description']['group_info']['default_group'] = 2; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + + $resultset = array( + array( + 'name' => 'George', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringNotStarts() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'description' => array( + 'id' => 'description', + 'table' => 'views_test', + 'field' => 'description', + 'relationship' => 'none', + 'operator' => 'not_starts', + 'value' => 'George', + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'John', + ), + array( + 'name' => 'Ringo', + ), + array( + 'name' => 'Paul', + ), + // There is no Meredith returned because his description is empty + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringGroupedExposedNotStarts() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Name, Operator: not_starts, Value: George + $filters['description']['group_info']['default_group'] = 3; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + + $resultset = array( + array( + 'name' => 'John', + ), + array( + 'name' => 'Ringo', + ), + array( + 'name' => 'Paul', + ), + // There is no Meredith returned because his description is empty + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringEnds() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'description' => array( + 'id' => 'description', + 'table' => 'views_test', + 'field' => 'description', + 'relationship' => 'none', + 'operator' => 'ends', + 'value' => 'Beatles.', + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'George', + ), + array( + 'name' => 'Ringo', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringGroupedExposedEnds() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Descriptino, Operator: ends, Value: Beatles + $filters['description']['group_info']['default_group'] = 4; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + + $resultset = array( + array( + 'name' => 'George', + ), + array( + 'name' => 'Ringo', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringNotEnds() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'description' => array( + 'id' => 'description', + 'table' => 'views_test', + 'field' => 'description', + 'relationship' => 'none', + 'operator' => 'not_ends', + 'value' => 'Beatles.', + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'John', + ), + array( + 'name' => 'Paul', + ), + // There is no Meredith returned because his description is empty + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringGroupedExposedNotEnds() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Description, Operator: not_ends, Value: Beatles + $filters['description']['group_info']['default_group'] = 5; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + + $resultset = array( + array( + 'name' => 'John', + ), + array( + 'name' => 'Paul', + ), + // There is no Meredith returned because his description is empty + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringNot() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'description' => array( + 'id' => 'description', + 'table' => 'views_test', + 'field' => 'description', + 'relationship' => 'none', + 'operator' => 'not', + 'value' => 'Beatles.', + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'John', + ), + array( + 'name' => 'Paul', + ), + // There is no Meredith returned because his description is empty + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + + function testFilterStringGroupedExposedNot() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Description, Operator: not (does not contains), Value: Beatles + $filters['description']['group_info']['default_group'] = 6; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + + $resultset = array( + array( + 'name' => 'John', + ), + array( + 'name' => 'Paul', + ), + // There is no Meredith returned because his description is empty + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + + } + + function testFilterStringShorter() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + 'operator' => 'shorterthan', + 'value' => 5, + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'John', + ), + array( + 'name' => 'Paul', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringGroupedExposedShorter() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Name, Operator: shorterthan, Value: 5 + $filters['name']['group_info']['default_group'] = 4; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'John', + ), + array( + 'name' => 'Paul', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringLonger() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + 'operator' => 'longerthan', + 'value' => 7, + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'Meredith', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringGroupedExposedLonger() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Name, Operator: longerthan, Value: 4 + $filters['name']['group_info']['default_group'] = 5; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'Meredith', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + + function testFilterStringEmpty() { + $view = $this->getBasicView(); + + // Change the filtering + $view->display['default']->handler->override_option('filters', array( + 'description' => array( + 'id' => 'description', + 'table' => 'views_test', + 'field' => 'description', + 'relationship' => 'none', + 'operator' => 'empty', + ), + )); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'Meredith', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function testFilterStringGroupedExposedEmpty() { + $filters = $this->getGroupedExposedFilters(); + $view = $this->getBasicPageView(); + + // Filter: Description, Operator: empty, Value: + $filters['description']['group_info']['default_group'] = 7; + $view->set_display('page_1'); + $view->display['page_1']->handler->override_option('filters', $filters); + + $this->executeView($view); + $resultset = array( + array( + 'name' => 'Meredith', + ), + ); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + protected function getGroupedExposedFilters() { + $filters = array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + 'exposed' => TRUE, + 'expose' => array( + 'operator' => 'name_op', + 'label' => 'name', + 'identifier' => 'name', + ), + 'is_grouped' => TRUE, + 'group_info' => array( + 'label' => 'name', + 'identifier' => 'name', + 'default_group' => 'All', + 'group_items' => array( + 1 => array( + 'title' => 'Is Ringo', + 'operator' => '=', + 'value' => 'Ringo', + ), + 2 => array( + 'title' => 'Is not Ringo', + 'operator' => '!=', + 'value' => array('value' => 'Ringo'), + ), + 3 => array( + 'title' => 'Contains ing', + 'operator' => 'contains', + 'value' => 'ing', + ), + 4 => array( + 'title' => 'Shorter than 5 letters', + 'operator' => 'shorterthan', + 'value' => 5, + ), + 5 => array( + 'title' => 'Longer than 7 letters', + 'operator' => 'longerthan', + 'value' => 7, + ), + ), + ), + ), + 'description' => array( + 'id' => 'description', + 'table' => 'views_test', + 'field' => 'description', + 'relationship' => 'none', + 'exposed' => TRUE, + 'expose' => array( + 'operator' => 'description_op', + 'label' => 'description', + 'identifier' => 'description', + ), + 'is_grouped' => TRUE, + 'group_info' => array( + 'label' => 'description', + 'identifier' => 'description', + 'default_group' => 'All', + 'group_items' => array( + 1 => array( + 'title' => 'Contains the word: Actor', + 'operator' => 'word', + 'value' => 'actor', + ), + 2 => array( + 'title' => 'Starts with George', + 'operator' => 'starts', + 'value' => 'George', + ), + 3 => array( + 'title' => 'Not Starts with: George', + 'operator' => 'not_starts', + 'value' => 'George', + ), + 4 => array( + 'title' => 'Ends with: Beatles', + 'operator' => 'ends', + 'value' => 'Beatles.', + ), + 5 => array( + 'title' => 'Not Ends with: Beatles', + 'operator' => 'not_ends', + 'value' => 'Beatles.', + ), + 6 => array( + 'title' => 'Does not contain: Beatles', + 'operator' => 'not', + 'value' => 'Beatles.', + ), + 7 => array( + 'title' => 'Empty description', + 'operator' => 'empty', + 'value' => '', + ), + ), + ), + ), + ); + return $filters; + } + +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_sort.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_sort.test new file mode 100644 index 0000000..70f2aac --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_sort.test @@ -0,0 +1,121 @@ + 'Sort: generic', + 'description' => 'Test the core views_handler_sort handler.', + 'group' => 'Views Handlers', + ); + } + + /** + * Tests numeric ordering of the result set. + */ + public function testNumericOrdering() { + $view = $this->getBasicView(); + + // Change the ordering + $view->display['default']->handler->override_option('sorts', array( + 'age' => array( + 'order' => 'ASC', + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + ), + )); + + // Execute the view. + $this->executeView($view); + + // Verify the result. + $this->assertEqual(count($this->dataSet()), count($view->result), t('The number of returned rows match.')); + $this->assertIdenticalResultset($view, $this->orderResultSet($this->dataSet(), 'age'), array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + )); + + $view = $this->getBasicView(); + + // Reverse the ordering + $view->display['default']->handler->override_option('sorts', array( + 'age' => array( + 'order' => 'DESC', + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + ), + )); + + // Execute the view. + $this->executeView($view); + + // Verify the result. + $this->assertEqual(count($this->dataSet()), count($view->result), t('The number of returned rows match.')); + $this->assertIdenticalResultset($view, $this->orderResultSet($this->dataSet(), 'age', TRUE), array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + )); + } + + /** + * Tests string ordering of the result set. + */ + public function testStringOrdering() { + $view = $this->getBasicView(); + + // Change the ordering + $view->display['default']->handler->override_option('sorts', array( + 'name' => array( + 'order' => 'ASC', + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + ), + )); + + // Execute the view. + $this->executeView($view); + + // Verify the result. + $this->assertEqual(count($this->dataSet()), count($view->result), t('The number of returned rows match.')); + $this->assertIdenticalResultset($view, $this->orderResultSet($this->dataSet(), 'name'), array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + )); + + $view = $this->getBasicView(); + + // Reverse the ordering + $view->display['default']->handler->override_option('sorts', array( + 'name' => array( + 'order' => 'DESC', + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + ), + )); + + // Execute the view. + $this->executeView($view); + + // Verify the result. + $this->assertEqual(count($this->dataSet()), count($view->result), t('The number of returned rows match.')); + $this->assertIdenticalResultset($view, $this->orderResultSet($this->dataSet(), 'name', TRUE), array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + )); + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_sort_date.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_sort_date.test new file mode 100644 index 0000000..65a94e8 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_sort_date.test @@ -0,0 +1,198 @@ + 'Sort: date', + 'description' => 'Test the core views_handler_sort_date handler.', + 'group' => 'Views Handlers', + ); + } + + protected function expectedResultSet($granularity, $reverse = TRUE) { + $expected = array(); + if (!$reverse) { + switch ($granularity) { + case 'second': + $expected = array( + array('name' => 'John'), + array('name' => 'Paul'), + array('name' => 'Meredith'), + array('name' => 'Ringo'), + array('name' => 'George'), + ); + break; + case 'minute': + $expected = array( + array('name' => 'John'), + array('name' => 'Paul'), + array('name' => 'Ringo'), + array('name' => 'Meredith'), + array('name' => 'George'), + ); + break; + case 'hour': + $expected = array( + array('name' => 'John'), + array('name' => 'Ringo'), + array('name' => 'Paul'), + array('name' => 'Meredith'), + array('name' => 'George'), + ); + break; + case 'day': + $expected = array( + array('name' => 'John'), + array('name' => 'Ringo'), + array('name' => 'Paul'), + array('name' => 'Meredith'), + array('name' => 'George'), + ); + break; + case 'month': + $expected = array( + array('name' => 'John'), + array('name' => 'George'), + array('name' => 'Ringo'), + array('name' => 'Paul'), + array('name' => 'Meredith'), + ); + break; + case 'year': + $expected = array( + array('name' => 'John'), + array('name' => 'George'), + array('name' => 'Ringo'), + array('name' => 'Paul'), + array('name' => 'Meredith'), + ); + break; + } + } + else { + switch ($granularity) { + case 'second': + $expected = array( + array('name' => 'George'), + array('name' => 'Ringo'), + array('name' => 'Meredith'), + array('name' => 'Paul'), + array('name' => 'John'), + ); + break; + case 'minute': + $expected = array( + array('name' => 'George'), + array('name' => 'Ringo'), + array('name' => 'Meredith'), + array('name' => 'Paul'), + array('name' => 'John'), + ); + break; + case 'hour': + $expected = array( + array('name' => 'George'), + array('name' => 'Ringo'), + array('name' => 'Paul'), + array('name' => 'Meredith'), + array('name' => 'John'), + ); + break; + case 'day': + $expected = array( + array('name' => 'George'), + array('name' => 'John'), + array('name' => 'Ringo'), + array('name' => 'Paul'), + array('name' => 'Meredith'), + ); + break; + case 'month': + $expected = array( + array('name' => 'John'), + array('name' => 'George'), + array('name' => 'Ringo'), + array('name' => 'Paul'), + array('name' => 'Meredith'), + ); + break; + case 'year': + $expected = array( + array('name' => 'John'), + array('name' => 'George'), + array('name' => 'Ringo'), + array('name' => 'Paul'), + array('name' => 'Meredith'), + ); + break; + } + } + + return $expected; + } + + /** + * Tests numeric ordering of the result set. + */ + public function testDateOrdering() { + foreach (array('second', 'minute', 'hour', 'day', 'month', 'year') as $granularity) { + foreach (array(FALSE, TRUE) as $reverse) { + $view = $this->getBasicView(); + + // Change the fields. + $view->display['default']->handler->override_option('fields', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + ), + 'created' => array( + 'id' => 'created', + 'table' => 'views_test', + 'field' => 'created', + 'relationship' => 'none', + ), + )); + + // Change the ordering + $view->display['default']->handler->override_option('sorts', array( + 'created' => array( + 'id' => 'created', + 'table' => 'views_test', + 'field' => 'created', + 'relationship' => 'none', + 'granularity' => $granularity, + 'order' => $reverse ? 'DESC' : 'ASC', + ), + 'id' => array( + 'id' => 'id', + 'table' => 'views_test', + 'field' => 'id', + 'relationship' => 'none', + 'order' => 'ASC', + ), + )); + + // Execute the view. + $this->executeView($view); + + // Verify the result. + $this->assertEqual(count($this->dataSet()), count($view->result), t('The number of returned rows match.')); + $this->assertIdenticalResultset($view, $this->expectedResultSet($granularity, $reverse), array( + 'views_test_name' => 'name', + ), t('Result is returned correctly when ordering by granularity @granularity, @reverse.', array('@granularity' => $granularity, '@reverse' => $reverse ? t('reverse') : t('forward')))); + $view->destroy(); + unset($view); + } + } + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handler_sort_random.test b/sites/all/modules/contrib/views/tests/handlers/views_handler_sort_random.test new file mode 100644 index 0000000..19db8a9 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handler_sort_random.test @@ -0,0 +1,89 @@ + 'Sort: random', + 'description' => 'Test the core views_handler_sort_random handler.', + 'group' => 'Views Handlers', + ); + } + + /** + * Add more items to the test set, to make the order tests more robust. + */ + protected function dataSet() { + $data = parent::dataSet(); + for ($i = 0; $i < 50; $i++) { + $data[] = array( + 'name' => 'name_' . $i, + 'age' => $i, + 'job' => 'job_' . $i, + 'created' => rand(0, time()), + ); + } + return $data; + } + + /** + * Return a basic view with random ordering. + */ + protected function getBasicRandomView() { + $view = $this->getBasicView(); + + // Add a random ordering. + $view->display['default']->handler->override_option('sorts', array( + 'random' => array( + 'id' => 'random', + 'field' => 'random', + 'table' => 'views', + ), + )); + + return $view; + } + + /** + * Tests random ordering of the result set. + * + * @see DatabaseSelectTestCase::testRandomOrder() + */ + public function testRandomOrdering() { + // Execute a basic view first. + $view = $this->getBasicView(); + $this->executeView($view); + + // Verify the result. + $this->assertEqual(count($this->dataSet()), count($view->result), t('The number of returned rows match.')); + $this->assertIdenticalResultset($view, $this->dataSet(), array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + )); + + // Execute a random view, we expect the result set to be different. + $view_random = $this->getBasicRandomView(); + $this->executeView($view_random); + $this->assertEqual(count($this->dataSet()), count($view_random->result), t('The number of returned rows match.')); + $this->assertNotIdenticalResultset($view_random, $view->result, array( + 'views_test_name' => 'views_test_name', + 'views_test_age' => 'views_test_name', + )); + + // Execute a second random view, we expect the result set to be different again. + $view_random_2 = $this->getBasicRandomView(); + $this->executeView($view_random_2); + $this->assertEqual(count($this->dataSet()), count($view_random_2->result), t('The number of returned rows match.')); + $this->assertNotIdenticalResultset($view_random, $view->result, array( + 'views_test_name' => 'views_test_name', + 'views_test_age' => 'views_test_name', + )); + } +} diff --git a/sites/all/modules/contrib/views/tests/handlers/views_handlers.test b/sites/all/modules/contrib/views/tests/handlers/views_handlers.test new file mode 100644 index 0000000..f2faee3 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/handlers/views_handlers.test @@ -0,0 +1,81 @@ + 'Handlers', + 'description' => 'Tests generic handler functionality.', + 'group' => 'Views Handlers', + ); + } + + /** + * {@inheritdoc} + */ + protected function viewsData() { + $views_data = parent::viewsData(); + $views_data['views']['test_access'] = array( + 'title' => 'test access', + 'help' => '', + 'area' => array( + 'handler' => 'views_test_area_access', + ), + ); + + return $views_data; + } + + /** + * Tests access for handlers using an area handler. + */ + public function testHandlerAccess() { + $view = $this->getBasicView(); + + // add a test area + $view->display['default']->handler->override_option('header', array( + 'test_access' => array( + 'id' => 'test_access', + 'table' => 'views', + 'field' => 'test_access', + 'custom_access' => FALSE, + ), + )); + + $view->init_display(); + $view->init_handlers(); + $handlers = $view->display_handler->get_handlers('header'); + $this->assertEqual(0, count($handlers)); + + $view->destroy(); + + $view = $this->getBasicView(); + + // add a test area + $view->display['default']->handler->override_option('header', array( + 'test_access' => array( + 'id' => 'test_access', + 'table' => 'views', + 'field' => 'test_access', + 'custom_access' => TRUE, + ), + )); + + $view->init_display(); + $view->init_handlers(); + $handlers = $view->display_handler->get_handlers('header'); + $this->assertEqual(1, count($handlers)); + $this->assertTrue(isset($handlers['test_access'])); + } + +} diff --git a/sites/all/modules/contrib/views/tests/node/views_node_revision_relations.test b/sites/all/modules/contrib/views/tests/node/views_node_revision_relations.test new file mode 100644 index 0000000..6b38396 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/node/views_node_revision_relations.test @@ -0,0 +1,177 @@ + 'Tests basic node_revision integration', + 'description' => 'Tests the integration of node_revision table of node module', + 'group' => 'Views Modules', + ); + } + + /** + * Create a node with revision and rest result count for both views. + */ + public function testNodeRevisionRelationship() { + $node = $this->drupalCreateNode(); + // Create revision of the node. + $node_revision = clone $node; + $node_revision->revision = 1; + node_save($node_revision); + $column_map = array( + 'vid' => 'vid', + 'node_revision_nid' => 'node_revision_nid', + 'node_node_revision_nid' => 'node_node_revision_nid', + ); + + // Here should be two rows. + $view_nid = $this->test_view_node_revision_nid(); + $this->executeView($view_nid, array($node->nid)); + $resultset_nid = array( + array( + 'vid' => '1', + 'node_revision_nid' => '1', + 'node_node_revision_nid' => '1', + ), + array( + 'vid' => '2', + 'node_revision_nid' => '1', + 'node_node_revision_nid' => '1', + ), + ); + $this->assertIdenticalResultset($view_nid, $resultset_nid, $column_map); + + // There should be only one row with active revision 2. + $view_vid = $this->test_view_node_revision_vid(); + $this->executeView($view_vid, array($node->nid)); + $resultset_vid = array( + array( + 'vid' => '2', + 'node_revision_nid' => '1', + 'node_node_revision_nid' => '1', + ), + ); + $this->assertIdenticalResultset($view_vid, $resultset_vid, $column_map); + } + + /** + * Test view with default join on node.nid. + */ + function test_view_node_revision_nid() { + $view = new view(); + $view->name = 'test_node_revision_nid'; + $view->description = ''; + $view->tag = ''; + $view->base_table = 'node_revision'; + $view->human_name = 'Test node revision nid'; + $view->core = 7; + $view->api_version = '3.0'; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['use_more_always'] = FALSE; + $handler->display->display_options['access']['type'] = 'perm'; + $handler->display->display_options['access']['perm'] = 'view revisions'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['query']['type'] = 'views_query'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Relationship: Content revision: Content */ + $handler->display->display_options['relationships']['nid']['id'] = 'nid'; + $handler->display->display_options['relationships']['nid']['table'] = 'node_revision'; + $handler->display->display_options['relationships']['nid']['field'] = 'nid'; + $handler->display->display_options['relationships']['nid']['label'] = 'NID'; + $handler->display->display_options['relationships']['nid']['required'] = TRUE; + /* Field: Content revision: Vid */ + $handler->display->display_options['fields']['vid']['id'] = 'vid'; + $handler->display->display_options['fields']['vid']['table'] = 'node_revision'; + $handler->display->display_options['fields']['vid']['field'] = 'vid'; + /* Field: Content revision: Nid */ + $handler->display->display_options['fields']['nid_1']['id'] = 'nid_1'; + $handler->display->display_options['fields']['nid_1']['table'] = 'node_revision'; + $handler->display->display_options['fields']['nid_1']['field'] = 'nid'; + /* Field: Content: Nid */ + $handler->display->display_options['fields']['nid']['id'] = 'nid'; + $handler->display->display_options['fields']['nid']['table'] = 'node'; + $handler->display->display_options['fields']['nid']['field'] = 'nid'; + $handler->display->display_options['fields']['nid']['relationship'] = 'nid'; + /* Contextual filter: Content revision: Nid */ + $handler->display->display_options['arguments']['nid']['id'] = 'nid'; + $handler->display->display_options['arguments']['nid']['table'] = 'node_revision'; + $handler->display->display_options['arguments']['nid']['field'] = 'nid'; + $handler->display->display_options['arguments']['nid']['default_argument_type'] = 'fixed'; + $handler->display->display_options['arguments']['nid']['summary']['number_of_records'] = '0'; + $handler->display->display_options['arguments']['nid']['summary']['format'] = 'default_summary'; + $handler->display->display_options['arguments']['nid']['summary_options']['items_per_page'] = '25'; + + return $view; + } + + /** + * Test view with default join on node.vid. + */ + function test_view_node_revision_vid() { + $view = new view(); + $view->name = 'test_node_revision_vid'; + $view->description = ''; + $view->tag = ''; + $view->base_table = 'node_revision'; + $view->human_name = 'Test node revision vid'; + $view->core = 7; + $view->api_version = '3.0'; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['use_more_always'] = FALSE; + $handler->display->display_options['access']['type'] = 'perm'; + $handler->display->display_options['access']['perm'] = 'view revisions'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['query']['type'] = 'views_query'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Relationship: Content revision: Content */ + $handler->display->display_options['relationships']['vid']['id'] = 'vid'; + $handler->display->display_options['relationships']['vid']['table'] = 'node_revision'; + $handler->display->display_options['relationships']['vid']['field'] = 'vid'; + $handler->display->display_options['relationships']['vid']['label'] = 'VID'; + $handler->display->display_options['relationships']['vid']['required'] = TRUE; + /* Field: Content revision: Vid */ + $handler->display->display_options['fields']['vid']['id'] = 'vid'; + $handler->display->display_options['fields']['vid']['table'] = 'node_revision'; + $handler->display->display_options['fields']['vid']['field'] = 'vid'; + /* Field: Content revision: Nid */ + $handler->display->display_options['fields']['nid_1']['id'] = 'nid_1'; + $handler->display->display_options['fields']['nid_1']['table'] = 'node_revision'; + $handler->display->display_options['fields']['nid_1']['field'] = 'nid'; + /* Field: Content: Nid */ + $handler->display->display_options['fields']['nid']['id'] = 'nid'; + $handler->display->display_options['fields']['nid']['table'] = 'node'; + $handler->display->display_options['fields']['nid']['field'] = 'nid'; + $handler->display->display_options['fields']['nid']['relationship'] = 'vid'; + /* Contextual filter: Content revision: Nid */ + $handler->display->display_options['arguments']['nid']['id'] = 'nid'; + $handler->display->display_options['arguments']['nid']['table'] = 'node_revision'; + $handler->display->display_options['arguments']['nid']['field'] = 'nid'; + $handler->display->display_options['arguments']['nid']['default_argument_type'] = 'fixed'; + $handler->display->display_options['arguments']['nid']['summary']['number_of_records'] = '0'; + $handler->display->display_options['arguments']['nid']['summary']['format'] = 'default_summary'; + $handler->display->display_options['arguments']['nid']['summary_options']['items_per_page'] = '25'; + + return $view; + } +} diff --git a/sites/all/modules/contrib/views/tests/plugins/views_plugin_display.test b/sites/all/modules/contrib/views/tests/plugins/views_plugin_display.test new file mode 100644 index 0000000..df33a34 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/plugins/views_plugin_display.test @@ -0,0 +1,194 @@ + 'Display plugin', + 'description' => 'Tests the basic display plugin.', + 'group' => 'Views Plugins', + ); + } + + /** + * Tests the overriding of filter_groups. + */ + function testFilterGroupsOverriding() { + $view = $this->viewFilterGroupsUpdating(); + $view->init_display(); + + // mark is as overridden, yes FALSE, means overridden. + $view->display['page']->handler->set_override('filter_groups', FALSE); + $this->assertFalse($view->display['page']->handler->is_defaulted('filter_groups'), "Take sure that 'filter_groups' is marked as overridden."); + $this->assertFalse($view->display['page']->handler->is_defaulted('filters'), "Take sure that 'filters'' is marked as overridden."); + } + + /** + * Returns a test view for testFilterGroupsOverriding. + * + * @see testFilterGroupsOverriding + * @return view + */ + function viewFilterGroupsOverriding() { + $view = new view(); + $view->name = 'test_filter_group_override'; + $view->description = ''; + $view->tag = 'default'; + $view->base_table = 'node'; + $view->human_name = 'test_filter_group_override'; + $view->core = 7; + $view->api_version = '3.0'; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'perm'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['query']['type'] = 'views_query'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Field: Content: Title */ + $handler->display->display_options['fields']['title']['id'] = 'title'; + $handler->display->display_options['fields']['title']['table'] = 'node'; + $handler->display->display_options['fields']['title']['field'] = 'title'; + $handler->display->display_options['fields']['title']['label'] = ''; + $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE; + $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE; + /* Filter criterion: Content: Published */ + $handler->display->display_options['filters']['status']['id'] = 'status'; + $handler->display->display_options['filters']['status']['table'] = 'node'; + $handler->display->display_options['filters']['status']['field'] = 'status'; + $handler->display->display_options['filters']['status']['value'] = 1; + $handler->display->display_options['filters']['status']['group'] = 1; + $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE; + + /* Display: Page */ + $handler = $view->new_display('page', 'Page', 'page_1'); + $handler->display->display_options['path'] = 'test'; + + return $view; + } + + /** + * Based on a bug some filter_groups landed in the overridden display, even the filters weren't overridden. + * This caused multiple issues. + * Take sure that the value from the default display are used. + * + * @see http://drupal.org/node/1259608 + * @see views_plugin_display::init() + */ + function testFilterGroupsUpdating() { + $view = $this->viewFilterGroupsUpdating(); + $view->init_display(); + + $this->assertFalse($view->display['page']->handler->options['defaults']['filter_groups']); + $this->assertEqual($view->display['default']->handler->options['filter_groups'], $view->display['page']->handler->options['filter_groups'], 'Take sure the default options are used for the filter_groups'); + } + + /** + * Returns a test view for testFilterGroupUpdating. + * + * @see testFilterGroupUpdating + * + * @return view + */ + function viewFilterGroupsUpdating() { + $view = new view(); + $view->name = 'test_filter_groups'; + $view->description = ''; + $view->tag = 'default'; + $view->base_table = 'node'; + $view->human_name = 'test_filter_groups'; + $view->core = 7; + $view->api_version = '3.0'; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['title'] = 'test_filter_groups'; + $handler->display->display_options['access']['type'] = 'perm'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['query']['type'] = 'views_query'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['pager']['options']['items_per_page'] = '10'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'node'; + /* Field: Content: Title */ + $handler->display->display_options['fields']['title']['id'] = 'title'; + $handler->display->display_options['fields']['title']['table'] = 'node'; + $handler->display->display_options['fields']['title']['field'] = 'title'; + $handler->display->display_options['fields']['title']['label'] = ''; + $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE; + $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE; + /* Sort criterion: Content: Post date */ + $handler->display->display_options['sorts']['created']['id'] = 'created'; + $handler->display->display_options['sorts']['created']['table'] = 'node'; + $handler->display->display_options['sorts']['created']['field'] = 'created'; + $handler->display->display_options['sorts']['created']['order'] = 'DESC'; + $handler->display->display_options['filter_groups']['groups'] = array( + 1 => 'AND', + 2 => 'AND', + ); + /* Filter criterion: Content: Published */ + $handler->display->display_options['filters']['status']['id'] = 'status'; + $handler->display->display_options['filters']['status']['table'] = 'node'; + $handler->display->display_options['filters']['status']['field'] = 'status'; + $handler->display->display_options['filters']['status']['value'] = 1; + $handler->display->display_options['filters']['status']['group'] = 1; + $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE; + /* Filter criterion: Content: Nid */ + $handler->display->display_options['filters']['nid']['id'] = 'nid'; + $handler->display->display_options['filters']['nid']['table'] = 'node'; + $handler->display->display_options['filters']['nid']['field'] = 'nid'; + $handler->display->display_options['filters']['nid']['value']['value'] = '1'; + $handler->display->display_options['filters']['nid']['group'] = 2; + /* Filter criterion: Content: Nid */ + $handler->display->display_options['filters']['nid_1']['id'] = 'nid_1'; + $handler->display->display_options['filters']['nid_1']['table'] = 'node'; + $handler->display->display_options['filters']['nid_1']['field'] = 'nid'; + $handler->display->display_options['filters']['nid_1']['value']['value'] = '2'; + $handler->display->display_options['filters']['nid_1']['group'] = 2; + + /* Display: Page */ + $handler = $view->new_display('page', 'Page', 'page'); + $handler->display->display_options['filter_groups']['operator'] = 'OR'; + $handler->display->display_options['filter_groups']['groups'] = array( + 1 => 'OR', + 2 => 'OR', + ); + $handler->display->display_options['defaults']['filters'] = FALSE; + /* Filter criterion: Content: Published */ + $handler->display->display_options['filters']['status']['id'] = 'status'; + $handler->display->display_options['filters']['status']['table'] = 'node'; + $handler->display->display_options['filters']['status']['field'] = 'status'; + $handler->display->display_options['filters']['status']['value'] = 1; + $handler->display->display_options['filters']['status']['group'] = 1; + $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE; + /* Filter criterion: Content: Nid */ + $handler->display->display_options['filters']['nid']['id'] = 'nid'; + $handler->display->display_options['filters']['nid']['table'] = 'node'; + $handler->display->display_options['filters']['nid']['field'] = 'nid'; + $handler->display->display_options['filters']['nid']['value']['value'] = '1'; + $handler->display->display_options['filters']['nid']['group'] = 2; + /* Filter criterion: Content: Nid */ + $handler->display->display_options['filters']['nid_1']['id'] = 'nid_1'; + $handler->display->display_options['filters']['nid_1']['table'] = 'node'; + $handler->display->display_options['filters']['nid_1']['field'] = 'nid'; + $handler->display->display_options['filters']['nid_1']['value']['value'] = '2'; + $handler->display->display_options['filters']['nid_1']['group'] = 2; + $handler->display->display_options['path'] = 'test-filter-groups'; + + return $view; + } +} diff --git a/sites/all/modules/contrib/views/tests/styles/views_plugin_style.test b/sites/all/modules/contrib/views/tests/styles/views_plugin_style.test new file mode 100644 index 0000000..2bb2857 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/styles/views_plugin_style.test @@ -0,0 +1,264 @@ + 'Styles', + 'description' => 'Test general style functionality.', + 'group' => 'Views Plugins', + ); + } + + /** + * Tests the grouping legacy features of styles. + */ + function testGroupingLegacy() { + $view = $this->getBasicView(); + // Setup grouping by the job. + $view->init_display(); + $view->init_style(); + $view->style_plugin->options['grouping'] = 'job'; + + // Reduce the amount of items to make the test a bit easier. + // Set up the pager. + $view->display['default']->handler->override_option('pager', array( + 'type' => 'some', + 'options' => array('items_per_page' => 3), + )); + + // Add the job field . + $view->display['default']->handler->override_option('fields', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + ), + 'job' => array( + 'id' => 'job', + 'table' => 'views_test', + 'field' => 'job', + 'relationship' => 'none', + ), + )); + + // Now run the query and groupby the result. + $this->executeView($view); + + // This is the old way to call it. + $sets = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping']); + + $expected = array(); + // Use Job: as label, so be sure that the label is used for groupby as well. + $expected['Job: Singer'] = array(); + $expected['Job: Singer'][0] = new StdClass(); + $expected['Job: Singer'][0]->views_test_name = 'John'; + $expected['Job: Singer'][0]->views_test_job = 'Singer'; + $expected['Job: Singer'][0]->views_test_id = '1'; + $expected['Job: Singer'][1] = new StdClass(); + $expected['Job: Singer'][1]->views_test_name = 'George'; + $expected['Job: Singer'][1]->views_test_job = 'Singer'; + $expected['Job: Singer'][1]->views_test_id = '2'; + $expected['Job: Drummer'] = array(); + $expected['Job: Drummer'][2] = new StdClass(); + $expected['Job: Drummer'][2]->views_test_name = 'Ringo'; + $expected['Job: Drummer'][2]->views_test_job = 'Drummer'; + $expected['Job: Drummer'][2]->views_test_id = '3'; + + $this->assertEqual($sets, $expected, t('The style plugin should proper group the results with grouping by the rendered output.')); + + $expected = array(); + $expected['Job: Singer'] = array(); + $expected['Job: Singer']['group'] = 'Job: Singer'; + $expected['Job: Singer']['rows'][0] = new StdClass(); + $expected['Job: Singer']['rows'][0]->views_test_name = 'John'; + $expected['Job: Singer']['rows'][0]->views_test_job = 'Singer'; + $expected['Job: Singer']['rows'][0]->views_test_id = '1'; + $expected['Job: Singer']['rows'][1] = new StdClass(); + $expected['Job: Singer']['rows'][1]->views_test_name = 'George'; + $expected['Job: Singer']['rows'][1]->views_test_job = 'Singer'; + $expected['Job: Singer']['rows'][1]->views_test_id = '2'; + $expected['Job: Drummer'] = array(); + $expected['Job: Drummer']['group'] = 'Job: Drummer'; + $expected['Job: Drummer']['rows'][2] = new StdClass(); + $expected['Job: Drummer']['rows'][2]->views_test_name = 'Ringo'; + $expected['Job: Drummer']['rows'][2]->views_test_job = 'Drummer'; + $expected['Job: Drummer']['rows'][2]->views_test_id = '3'; + + // The newer api passes the value of the grouping as well. + $sets_new_rendered = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], TRUE); + $sets_new_value = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], FALSE); + + $this->assertEqual($sets_new_rendered, $expected, t('The style plugins should proper group the results with grouping by the rendered output.')); + + // Reorder the group structure to group by value. + $expected['Singer'] = $expected['Job: Singer']; + $expected['Drummer'] = $expected['Job: Drummer']; + unset($expected['Job: Singer']); + unset($expected['Job: Drummer']); + + $this->assertEqual($sets_new_value, $expected, t('The style plugins should proper group the results with grouping by the value.')); + } + + function testGrouping() { + $this->_testGrouping(FALSE); + $this->_testGrouping(TRUE); + } + + /** + * Tests the grouping features of styles. + */ + function _testGrouping($stripped = FALSE) { + $view = $this->getBasicView(); + // Setup grouping by the job and the age field. + $view->init_display(); + $view->init_style(); + $view->style_plugin->options['grouping'] = array( + array('field' => 'job'), + array('field' => 'age'), + ); + + // Reduce the amount of items to make the test a bit easier. + // Set up the pager. + $view->display['default']->handler->override_option('pager', array( + 'type' => 'some', + 'options' => array('items_per_page' => 3), + )); + + // Add the job and age field. + $view->display['default']->handler->override_option('fields', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + ), + 'job' => array( + 'id' => 'job', + 'table' => 'views_test', + 'field' => 'job', + 'relationship' => 'none', + ), + 'age' => array( + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + ), + )); + + // Now run the query and groupby the result. + $this->executeView($view); + + $expected = array(); + $expected['Job: Singer'] = array(); + $expected['Job: Singer']['group'] = 'Job: Singer'; + $expected['Job: Singer']['rows']['Age: 25'] = array(); + $expected['Job: Singer']['rows']['Age: 25']['group'] = 'Age: 25'; + $expected['Job: Singer']['rows']['Age: 25']['rows'][0] = new StdClass(); + $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_name = 'John'; + $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_job = 'Singer'; + $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_age = '25'; + $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_id = '1'; + $expected['Job: Singer']['rows']['Age: 27'] = array(); + $expected['Job: Singer']['rows']['Age: 27']['group'] = 'Age: 27'; + $expected['Job: Singer']['rows']['Age: 27']['rows'][1] = new StdClass(); + $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_name = 'George'; + $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_job = 'Singer'; + $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_age = '27'; + $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_id = '2'; + $expected['Job: Drummer'] = array(); + $expected['Job: Drummer']['group'] = 'Job: Drummer'; + $expected['Job: Drummer']['rows']['Age: 28'] = array(); + $expected['Job: Drummer']['rows']['Age: 28']['group'] = 'Age: 28'; + $expected['Job: Drummer']['rows']['Age: 28']['rows'][2] = new StdClass(); + $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_name = 'Ringo'; + $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_job = 'Drummer'; + $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_age = '28'; + $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_id = '3'; + + + // Alter the results to support the stripped case. + if ($stripped) { + + // Add some html to the result and expected value. + $rand = ''; + $view->result[0]->views_test_job .= $rand; + $expected['Job: Singer']['rows']['Age: 25']['rows'][0]->views_test_job = 'Singer' . $rand; + $expected['Job: Singer']['group'] = 'Job: Singer'; + $rand = ''; + $view->result[1]->views_test_job .= $rand; + $expected['Job: Singer']['rows']['Age: 27']['rows'][1]->views_test_job = 'Singer' . $rand; + $rand = ''; + $view->result[2]->views_test_job .= $rand; + $expected['Job: Drummer']['rows']['Age: 28']['rows'][2]->views_test_job = 'Drummer' . $rand; + $expected['Job: Drummer']['group'] = 'Job: Drummer'; + + $view->style_plugin->options['grouping'][0] = array('field' => 'job', 'rendered' => TRUE, 'rendered_strip' => TRUE); + $view->style_plugin->options['grouping'][1] = array('field' => 'age', 'rendered' => TRUE, 'rendered_strip' => TRUE); + } + + + // The newer api passes the value of the grouping as well. + $sets_new_rendered = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], TRUE); + + $this->assertEqual($sets_new_rendered, $expected, t('The style plugins should proper group the results with grouping by the rendered output.')); + + // Don't test stripped case, because the actual value is not stripped. + if (!$stripped) { + $sets_new_value = $view->style_plugin->render_grouping($view->result, $view->style_plugin->options['grouping'], FALSE); + + // Reorder the group structure to grouping by value. + $expected['Singer'] = $expected['Job: Singer']; + $expected['Singer']['rows']['25'] = $expected['Job: Singer']['rows']['Age: 25']; + $expected['Singer']['rows']['27'] = $expected['Job: Singer']['rows']['Age: 27']; + $expected['Drummer'] = $expected['Job: Drummer']; + $expected['Drummer']['rows']['28'] = $expected['Job: Drummer']['rows']['Age: 28']; + unset($expected['Job: Singer']); + unset($expected['Singer']['rows']['Age: 25']); + unset($expected['Singer']['rows']['Age: 27']); + unset($expected['Job: Drummer']); + unset($expected['Drummer']['rows']['Age: 28']); + + $this->assertEqual($sets_new_value, $expected, t('The style plugins should proper group the results with grouping by the value.')); + } + } + + /** + * Tests custom css classes. + */ + function testCustomRowClasses() { + $view = $this->getBasicView(); + + // Setup some random css class. + $view->init_display(); + $view->init_style(); + $random_name = drupal_html_class($this->randomName()); + $view->style_plugin->options['row_class'] = $random_name . " test-token-[name]"; + + $rendered_output = $view->preview(); + $this->storeViewPreview($rendered_output); + + $rows = $this->elements->body->div->div->div; + $count = 0; + foreach ($rows as $row) { + $attributes = $row->attributes(); + $class = (string) $attributes['class'][0]; + $this->assertTrue(strpos($class, $random_name) !== FALSE, 'Take sure that a custom css class is added to the output.'); + + // Check token replacement. + $name = drupal_html_class($view->field['name']->get_value($view->result[$count])); + $this->assertTrue(strpos($class, "test-token-$name") !== FALSE, 'Take sure that a token in custom css class is replaced.'); + + $count++; + } + } +} diff --git a/sites/all/modules/contrib/views/tests/styles/views_plugin_style_base.test b/sites/all/modules/contrib/views/tests/styles/views_plugin_style_base.test new file mode 100644 index 0000000..514077d --- /dev/null +++ b/sites/all/modules/contrib/views/tests/styles/views_plugin_style_base.test @@ -0,0 +1,33 @@ +loadHTML($output); + if ($htmlDom) { + // It's much easier to work with simplexml than DOM, luckily enough + // we can just simply import our DOM tree. + $this->elements = simplexml_import_dom($htmlDom); + } + } + +} diff --git a/sites/all/modules/contrib/views/tests/styles/views_plugin_style_jump_menu.test b/sites/all/modules/contrib/views/tests/styles/views_plugin_style_jump_menu.test new file mode 100644 index 0000000..dd4eca0 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/styles/views_plugin_style_jump_menu.test @@ -0,0 +1,151 @@ + 'Jump menu', + 'description' => 'Test jump menu style functionality.', + 'group' => 'Views Plugins', + ); + } + + + public function setUp() { + parent::setUp(); + $this->nodes = array(); + $this->nodes['page'][] = $this->drupalCreateNode(array('type' => 'page')); + $this->nodes['page'][] = $this->drupalCreateNode(array('type' => 'page')); + $this->nodes['story'][] = $this->drupalCreateNode(array('type' => 'story')); + $this->nodes['story'][] = $this->drupalCreateNode(array('type' => 'story')); + + $this->nodeTitles = array($this->nodes['page'][0]->title, $this->nodes['page'][1]->title, $this->nodes['story'][0]->title, $this->nodes['story'][1]->title); + } + + + /** + * Tests jump menues with more then one same path but maybe differnet titles. + */ + function testDuplicatePaths() { + $view = $this->getJumpMenuView(); + $view->set_display(); + $view->init_handlers(); + + // Setup a [path] which would leed to "duplicate" paths, but still the shouldn't be used for grouping. + $view->field['nothing']->options['alter']['text'] = '[path]'; + $view->preview(); + $form = $view->style_plugin->render($view->result); + + // As there is no grouping setup it should be 4 elements. + $this->assertEqual(count($form['jump']['#options']), 4 + 1); + + // Check that all titles are part of the form as well. + $options = array_values($form['jump']['#options']); + foreach ($options as $key => $title) { + // The first one is the choose label. + if ($key == 0) { + continue; + } + $this->assertTrue($this->nodeTitles[$key - 1] == trim($title), t('Title @title should appear on the jump list, as we do not filter', array('@title' => $title))); + } + } + + function getJumpMenuView() { + $view = new view; + $view->name = 'test_jump_menu'; + $view->description = ''; + $view->tag = 'default'; + $view->base_table = 'node'; + $view->human_name = 'test_jump_menu'; + $view->core = 7; + $view->api_version = '3.0'; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'perm'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['query']['type'] = 'views_query'; + $handler->display->display_options['query']['options']['query_comment'] = FALSE; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'jump_menu'; + $handler->display->display_options['style_options']['hide'] = 0; + $handler->display->display_options['style_options']['path'] = 'nothing'; + $handler->display->display_options['style_options']['default_value'] = 0; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Field: Content: Title */ + $handler->display->display_options['fields']['title']['id'] = 'title'; + $handler->display->display_options['fields']['title']['table'] = 'node'; + $handler->display->display_options['fields']['title']['field'] = 'title'; + $handler->display->display_options['fields']['title']['label'] = ''; + $handler->display->display_options['fields']['title']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['title']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['title']['alter']['absolute'] = 0; + $handler->display->display_options['fields']['title']['alter']['word_boundary'] = 0; + $handler->display->display_options['fields']['title']['alter']['ellipsis'] = 0; + $handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['title']['alter']['trim'] = 0; + $handler->display->display_options['fields']['title']['alter']['html'] = 0; + $handler->display->display_options['fields']['title']['hide_empty'] = 0; + $handler->display->display_options['fields']['title']['empty_zero'] = 0; + $handler->display->display_options['fields']['title']['link_to_node'] = 1; + /* Field: Content: Type */ + $handler->display->display_options['fields']['type']['id'] = 'type'; + $handler->display->display_options['fields']['type']['table'] = 'node'; + $handler->display->display_options['fields']['type']['field'] = 'type'; + $handler->display->display_options['fields']['type']['exclude'] = 1; + /* Field: Global: Custom text */ + $handler->display->display_options['fields']['nothing']['id'] = 'nothing'; + $handler->display->display_options['fields']['nothing']['table'] = 'views'; + $handler->display->display_options['fields']['nothing']['field'] = 'nothing'; + $handler->display->display_options['fields']['nothing']['alter']['text'] = '[type]'; + $handler->display->display_options['fields']['nothing']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['nothing']['alter']['absolute'] = 0; + $handler->display->display_options['fields']['nothing']['alter']['external'] = 0; + $handler->display->display_options['fields']['nothing']['alter']['replace_spaces'] = 0; + $handler->display->display_options['fields']['nothing']['alter']['trim_whitespace'] = 0; + $handler->display->display_options['fields']['nothing']['alter']['nl2br'] = 0; + $handler->display->display_options['fields']['nothing']['alter']['word_boundary'] = 1; + $handler->display->display_options['fields']['nothing']['alter']['ellipsis'] = 1; + $handler->display->display_options['fields']['nothing']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['nothing']['alter']['trim'] = 0; + $handler->display->display_options['fields']['nothing']['alter']['html'] = 0; + $handler->display->display_options['fields']['nothing']['element_label_colon'] = 1; + $handler->display->display_options['fields']['nothing']['element_default_classes'] = 1; + $handler->display->display_options['fields']['nothing']['hide_empty'] = 0; + $handler->display->display_options['fields']['nothing']['empty_zero'] = 0; + $handler->display->display_options['fields']['nothing']['hide_alter_empty'] = 0; + $handler->display->display_options['fields']['nothing']['exclude'] = 1; + + /* Sort criterion: Content: Post date */ + $handler->display->display_options['sorts']['created']['id'] = 'created'; + $handler->display->display_options['sorts']['created']['table'] = 'node'; + $handler->display->display_options['sorts']['created']['field'] = 'created'; + $handler->display->display_options['sorts']['created']['order'] = 'DESC'; + /* Filter criterion: Content: Published */ + $handler->display->display_options['filters']['status']['id'] = 'status'; + $handler->display->display_options['filters']['status']['table'] = 'node'; + $handler->display->display_options['filters']['status']['field'] = 'status'; + $handler->display->display_options['filters']['status']['value'] = 1; + $handler->display->display_options['filters']['status']['group'] = 0; + $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE; + + return $view; + } +} diff --git a/sites/all/modules/contrib/views/tests/styles/views_plugin_style_mapping.test b/sites/all/modules/contrib/views/tests/styles/views_plugin_style_mapping.test new file mode 100644 index 0000000..5785075 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/styles/views_plugin_style_mapping.test @@ -0,0 +1,144 @@ + 'Style: Mapping', + 'description' => 'Test mapping style functionality.', + 'group' => 'Views Plugins', + ); + } + + public function setUp() { + parent::setUp(); + + // Reset the plugin data. + views_fetch_plugin_data(NULL, NULL, TRUE); + } + + protected function viewsPlugins() { + return array( + 'style' => array( + 'test_mapping' => array( + 'title' => t('Field mapping'), + 'help' => t('Maps specific fields to specific purposes.'), + 'handler' => 'views_test_plugin_style_test_mapping', + 'path' => drupal_get_path('module', 'views_test') . '/test_plugins', + 'theme' => 'views_view_mapping_test', + 'theme path' => drupal_get_path('module', 'views_test'), + 'theme file' => 'views_test.module', + 'uses row plugin' => FALSE, + 'uses fields' => TRUE, + 'uses options' => TRUE, + 'uses grouping' => FALSE, + 'type' => 'normal', + ), + ), + ); + } + + /** + * Overrides ViewsTestCase::getBasicView(). + */ + protected function getBasicView() { + $view = parent::getBasicView(); + $view->display['default']->handler->override_option('style_plugin', 'test_mapping'); + $view->display['default']->handler->override_option('style_options', array( + 'mapping' => array( + 'name_field' => 'name', + 'numeric_field' => array( + 'age', + ), + 'title_field' => 'name', + 'toggle_numeric_field' => TRUE, + 'toggle_title_field' => TRUE, + ), + )); + $view->display['default']->handler->override_option('fields', array( + 'age' => array( + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + ), + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + ), + 'job' => array( + 'id' => 'job', + 'table' => 'views_test', + 'field' => 'job', + 'relationship' => 'none', + ), + )); + return $view; + } + + /** + * Verifies that the fields were mapped correctly. + */ + public function testMappedOutput() { + $view = $this->getBasicView(); + $output = $this->mappedOutputHelper($view); + $this->assertTrue(strpos($output, 'job') === FALSE, 'The job field is added to the view but not in the mapping.'); + + $view = $this->getBasicView(); + $view->display['default']->display_options['style_options']['mapping']['name_field'] = 'job'; + $output = $this->mappedOutputHelper($view); + $this->assertTrue(strpos($output, 'job') !== FALSE, 'The job field is added to the view and is in the mapping.'); + } + + /** + * Tests the mapping of fields. + * + * @param view $view + * The view to test. + * + * @return string + * The view rendered as HTML. + */ + protected function mappedOutputHelper($view) { + $rendered_output = $view->preview(); + $this->storeViewPreview($rendered_output); + $rows = $this->elements->body->div->div->div; + $data_set = $this->dataSet(); + + $count = 0; + foreach ($rows as $row) { + $attributes = $row->attributes(); + $class = (string) $attributes['class'][0]; + $this->assertTrue(strpos($class, 'views-row-mapping-test') !== FALSE, 'Make sure that each row has the correct CSS class.'); + + foreach ($row->div as $field) { + // Split up the field-level class, the first part is the mapping name + // and the second is the field ID. + $field_attributes = $field->attributes(); + $name = strtok((string) $field_attributes['class'][0], '-'); + $field_id = strtok('-'); + + // The expected result is the mapping name and the field value, + // separated by ':'. + $expected_result = $name . ':' . $data_set[$count][$field_id]; + $actual_result = (string) $field; + $this->assertIdentical($expected_result, $actual_result, format_string('The fields were mapped successfully: %name => %field_id', array('%name' => $name, '%field_id' => $field_id))); + } + + $count++; + } + + return $rendered_output; + } + +} diff --git a/sites/all/modules/contrib/views/tests/styles/views_plugin_style_unformatted.test b/sites/all/modules/contrib/views/tests/styles/views_plugin_style_unformatted.test new file mode 100644 index 0000000..0c0e882 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/styles/views_plugin_style_unformatted.test @@ -0,0 +1,53 @@ + 'Style: unformatted', + 'description' => 'Test unformatted style functionality.', + 'group' => 'Views Plugins', + ); + } + + /** + * Take sure that the default css classes works as expected. + */ + function testDefaultRowClasses() { + $view = $this->getBasicView(); + $rendered_output = $view->preview(); + $this->storeViewPreview($rendered_output); + + $rows = $this->elements->body->div->div->div; + $count = 0; + $count_result = count($view->result); + foreach ($rows as $row) { + $count++; + $attributes = $row->attributes(); + $class = (string) $attributes['class'][0]; + // Take sure that each row has a row css class. + $this->assertTrue(strpos($class, "views-row-$count") !== FALSE, 'Take sure that each row has a row css class.'); + // Take sure that the odd/even classes are set right. + $odd_even = $count % 2 == 0 ? 'even' : 'odd'; + $this->assertTrue(strpos($class, "views-row-$odd_even") !== FALSE, 'Take sure that the odd/even classes are set right.'); + + if ($count == 1) { + $this->assertTrue(strpos($class, "views-row-first") !== FALSE, 'Take sure that the first class is set right.'); + } + else if ($count == $count_result) { + $this->assertTrue(strpos($class, "views-row-last") !== FALSE, 'Take sure that the last class is set right.'); + + } + $this->assertTrue(strpos($class, 'views-row') !== FALSE, 'Take sure that the views row class is set right.'); + } + } + +} diff --git a/sites/all/modules/contrib/views/tests/taxonomy/views_handler_relationship_node_term_data.test b/sites/all/modules/contrib/views/tests/taxonomy/views_handler_relationship_node_term_data.test new file mode 100644 index 0000000..37e8aa6 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/taxonomy/views_handler_relationship_node_term_data.test @@ -0,0 +1,122 @@ + 'Tests handler relationship_node_term_data', + 'description' => 'Tests the taxonomy term on node relationship handler', + 'group' => 'Views Modules', + ); + } + + /** + * Returns a new term with random properties in vocabulary $vid. + */ + function createTerm($vocabulary) { + $term = new stdClass(); + $term->name = $this->randomName(); + $term->description = $this->randomName(); + // Use the first available text format. + $term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField(); + $term->vid = $vocabulary->vid; + taxonomy_term_save($term); + return $term; + } + + function setUp() { + parent::setUp(); + + //$web_user = $this->drupalCreateUser(array('create article content')); + //$this->drupalLogin($web_user); + + $vocabulary = taxonomy_vocabulary_machine_name_load('tags'); + $this->term_1 = $this->createTerm($vocabulary); + $this->term_2 = $this->createTerm($vocabulary); + + $node = array(); + $node['type'] = 'article'; + $node['field_tags'][LANGUAGE_NONE][]['tid'] = $this->term_1->tid; + $node['field_tags'][LANGUAGE_NONE][]['tid'] = $this->term_2->tid; + $this->node = $this->drupalCreateNode($node); + } + + function testViewsHandlerRelationshipNodeTermData() { + $view = $this->view_taxonomy_node_term_data(); + + $this->executeView($view, array($this->term_1->tid, $this->term_2->tid)); + $resultset = array( + array( + 'nid' => $this->node->nid, + ), + ); + $this->column_map = array('nid' => 'nid'); + debug($view->result); + $this->assertIdenticalResultset($view, $resultset, $this->column_map); + } + + function view_taxonomy_node_term_data() { + $view = new view(); + $view->name = 'test_taxonomy_node_term_data'; + $view->description = ''; + $view->tag = ''; + $view->base_table = 'node'; + $view->human_name = 'test_taxonomy_node_term_data'; + $view->core = 7; + $view->api_version = '3.0'; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'perm'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['query']['type'] = 'views_query'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'node'; + /* Relationship: Content: Taxonomy terms on node */ + $handler->display->display_options['relationships']['term_node_tid']['id'] = 'term_node_tid'; + $handler->display->display_options['relationships']['term_node_tid']['table'] = 'node'; + $handler->display->display_options['relationships']['term_node_tid']['field'] = 'term_node_tid'; + $handler->display->display_options['relationships']['term_node_tid']['label'] = 'Term #1'; + $handler->display->display_options['relationships']['term_node_tid']['vocabularies'] = array( + 'tags' => 0, + ); + /* Relationship: Content: Taxonomy terms on node */ + $handler->display->display_options['relationships']['term_node_tid_1']['id'] = 'term_node_tid_1'; + $handler->display->display_options['relationships']['term_node_tid_1']['table'] = 'node'; + $handler->display->display_options['relationships']['term_node_tid_1']['field'] = 'term_node_tid'; + $handler->display->display_options['relationships']['term_node_tid_1']['label'] = 'Term #2'; + $handler->display->display_options['relationships']['term_node_tid_1']['vocabularies'] = array( + 'tags' => 0, + ); + /* Contextual filter: Taxonomy term: Term ID */ + $handler->display->display_options['arguments']['tid']['id'] = 'tid'; + $handler->display->display_options['arguments']['tid']['table'] = 'taxonomy_term_data'; + $handler->display->display_options['arguments']['tid']['field'] = 'tid'; + $handler->display->display_options['arguments']['tid']['relationship'] = 'term_node_tid'; + $handler->display->display_options['arguments']['tid']['default_argument_type'] = 'fixed'; + $handler->display->display_options['arguments']['tid']['summary']['number_of_records'] = '0'; + $handler->display->display_options['arguments']['tid']['summary']['format'] = 'default_summary'; + $handler->display->display_options['arguments']['tid']['summary_options']['items_per_page'] = '25'; + /* Contextual filter: Taxonomy term: Term ID */ + $handler->display->display_options['arguments']['tid_1']['id'] = 'tid_1'; + $handler->display->display_options['arguments']['tid_1']['table'] = 'taxonomy_term_data'; + $handler->display->display_options['arguments']['tid_1']['field'] = 'tid'; + $handler->display->display_options['arguments']['tid_1']['relationship'] = 'term_node_tid_1'; + $handler->display->display_options['arguments']['tid_1']['default_argument_type'] = 'fixed'; + $handler->display->display_options['arguments']['tid_1']['summary']['number_of_records'] = '0'; + $handler->display->display_options['arguments']['tid_1']['summary']['format'] = 'default_summary'; + $handler->display->display_options['arguments']['tid_1']['summary_options']['items_per_page'] = '25'; + + return $view; + } +} \ No newline at end of file diff --git a/sites/all/modules/contrib/views/tests/test_handlers/views_test_area_access.inc b/sites/all/modules/contrib/views/tests/test_handlers/views_test_area_access.inc new file mode 100644 index 0000000..57a0014 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/test_handlers/views_test_area_access.inc @@ -0,0 +1,28 @@ +options['custom_access']; + } + + /** + * {@inheritdoc} + */ + function option_definition() { + $options = parent::option_definition(); + + $options['custom_access'] = array('default' => TRUE, 'bool' => TRUE); + + return $options; + } + +} diff --git a/sites/all/modules/contrib/views/tests/test_plugins/views_test_plugin_access_test_dynamic.inc b/sites/all/modules/contrib/views/tests/test_plugins/views_test_plugin_access_test_dynamic.inc new file mode 100644 index 0000000..cecec2f --- /dev/null +++ b/sites/all/modules/contrib/views/tests/test_plugins/views_test_plugin_access_test_dynamic.inc @@ -0,0 +1,26 @@ + FALSE, 'bool' => TRUE); + + return $options; + } + + function access($account) { + return !empty($this->options['access']) && isset($this->view->args[0]) && $this->view->args[0] == variable_get('test_dynamic_access_argument1', NULL) && isset($this->view->args[1]) && $this->view->args[1] == variable_get('test_dynamic_access_argument2', NULL); + } + + function get_access_callback() { + return array('views_test_test_dynamic_access_callback', array(!empty($options['access']), 1, 2)); + } +} diff --git a/sites/all/modules/contrib/views/tests/test_plugins/views_test_plugin_access_test_static.inc b/sites/all/modules/contrib/views/tests/test_plugins/views_test_plugin_access_test_static.inc new file mode 100644 index 0000000..187d6ea --- /dev/null +++ b/sites/all/modules/contrib/views/tests/test_plugins/views_test_plugin_access_test_static.inc @@ -0,0 +1,26 @@ + FALSE, 'bool' => TRUE); + + return $options; + } + + function access($account) { + return !empty($this->options['access']); + } + + function get_access_callback() { + return array('views_test_test_static_access_callback', array(!empty($options['access']))); + } +} diff --git a/sites/all/modules/contrib/views/tests/test_plugins/views_test_plugin_style_test_mapping.inc b/sites/all/modules/contrib/views/tests/test_plugins/views_test_plugin_style_test_mapping.inc new file mode 100644 index 0000000..b926787 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/test_plugins/views_test_plugin_style_test_mapping.inc @@ -0,0 +1,52 @@ + array( + '#title' => t('Title field'), + '#description' => t('Choose the field with the custom title.'), + '#toggle' => TRUE, + '#required' => TRUE, + ), + 'name_field' => array( + '#title' => t('Name field'), + '#description' => t('Choose the field with the custom name.'), + ), + 'numeric_field' => array( + '#title' => t('Numeric field'), + '#description' => t('Select one or more numeric fields.'), + '#multiple' => TRUE, + '#toggle' => TRUE, + '#filter' => 'filter_numeric_fields', + '#required' => TRUE, + ), + ); + } + + /** + * Restricts the allowed fields to only numeric fields. + * + * @param array $fields + * An array of field labels, keyed by the field ID. + */ + protected function filter_numeric_fields(&$fields) { + foreach ($this->view->field as $id => $field) { + if (!($field instanceof views_handler_field_numeric)) { + unset($fields[$id]); + } + } + } +} diff --git a/sites/all/modules/contrib/views/tests/user/views_handler_field_user_name.test b/sites/all/modules/contrib/views/tests/user/views_handler_field_user_name.test new file mode 100644 index 0000000..6ace471 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/user/views_handler_field_user_name.test @@ -0,0 +1,96 @@ + 'Tests user: name field', + 'description' => 'Tests the handler of the user: name field', + 'group' => 'Views Modules', + ); + } + + function testUserName() { + $view = $this->view_user_name(); + $view->init_display(); + $this->executeView($view); + + $view->row_index = 0; + + $view->field['name']->options['link_to_user'] = TRUE; + $username = $view->result[0]->users_name = $this->randomName(); + $view->result[0]->uid = 1; + $render = $view->field['name']->advanced_render($view->result[0]); + $this->assertTrue(strpos($render, $username) !== FALSE, 'If link to user is checked the username should be part of the output.'); + $this->assertTrue(strpos($render, 'user/1') !== FALSE, 'If link to user is checked the link to the user should appear as well.'); + + $view->field['name']->options['link_to_user'] = FALSE; + $username = $view->result[0]->users_name = $this->randomName(); + $view->result[0]->uid = 1; + $render = $view->field['name']->advanced_render($view->result[0]); + $this->assertIdentical($render, $username, 'If the user is not linked the username should be printed out for a normal user.'); + + $view->result[0]->uid = 0; + $anon_name = variable_get('anonymous', t('Anonymous')); + $view->result[0]->users_name = ''; + $render = $view->field['name']->advanced_render($view->result[0]); + $this->assertIdentical($render, $anon_name , 'For user0 it should use the default anonymous name by default.'); + + $view->field['name']->options['overwrite_anonymous'] = TRUE; + $anon_name = $view->field['name']->options['anonymous_text'] = $this->randomName(); + $render = $view->field['name']->advanced_render($view->result[0]); + $this->assertIdentical($render, $anon_name , 'For user0 it should use the configured anonymous text if overwrite_anonymous is checked.'); + + + } + function view_user_name() { + $view = new view; + $view->name = 'test_views_handler_field_user_name'; + $view->description = ''; + $view->tag = 'default'; + $view->base_table = 'users'; + $view->human_name = 'test_views_handler_field_user_name'; + $view->core = 7; + $view->api_version = '3.0'; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['query']['type'] = 'views_query'; + $handler->display->display_options['query']['options']['query_comment'] = FALSE; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Field: User: Name */ + $handler->display->display_options['fields']['name']['id'] = 'name'; + $handler->display->display_options['fields']['name']['table'] = 'users'; + $handler->display->display_options['fields']['name']['field'] = 'name'; + $handler->display->display_options['fields']['name']['label'] = ''; + $handler->display->display_options['fields']['name']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['name']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['name']['alter']['absolute'] = 0; + $handler->display->display_options['fields']['name']['alter']['word_boundary'] = 0; + $handler->display->display_options['fields']['name']['alter']['ellipsis'] = 0; + $handler->display->display_options['fields']['name']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['name']['alter']['trim'] = 0; + $handler->display->display_options['fields']['name']['alter']['html'] = 0; + $handler->display->display_options['fields']['name']['hide_empty'] = 0; + $handler->display->display_options['fields']['name']['empty_zero'] = 0; + $handler->display->display_options['fields']['name']['link_to_user'] = 1; + $handler->display->display_options['fields']['name']['overwrite_anonymous'] = 0; + + return $view; + } +} diff --git a/sites/all/modules/contrib/views/tests/user/views_user.test b/sites/all/modules/contrib/views/tests/user/views_user.test new file mode 100644 index 0000000..52c5026 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/user/views_user.test @@ -0,0 +1,143 @@ + 'Tests basic user integration', + 'description' => 'Tests the integration of user module', + 'group' => 'Views Modules', + ); + } + + + protected function setUp() { + parent::setUp(); + + $this->users[] = $this->drupalCreateUser(); + $this->users[] = user_load(1); + $this->nodes[] = $this->drupalCreateNode(array('uid' => $this->users[0]->uid)); + $this->nodes[] = $this->drupalCreateNode(array('uid' => 1)); + } + + /** + * Add a view which has no explicit relationship to the author and check the result. + * + * @todo: Remove the following comment once the relationship is required. + * One day a view will require the relationship so it should still work + */ + public function testRelationship() { + $view = $this->test_view_user_relationship(); + + $view->execute_display(); + $expected = array(); + for ($i = 0; $i <= 1; $i++) { + $expected[$i] = array( + 'node_title' => $this->nodes[$i]->title, + 'users_uid' => $this->nodes[$i]->uid, + 'users_name' => $this->users[$i]->name, + ); + } + $this->assertIdenticalResultset($view, $expected); + } + + function test_view_user_relationship() { + $view = new view; + $view->name = 'test_user_relationship'; + $view->description = ''; + $view->tag = 'default'; + $view->base_table = 'node'; + $view->human_name = 'test_user_relationship'; + $view->core = 7; + $view->api_version = '3.0-alpha1'; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['title'] = 'test_user_relationship'; + $handler->display->display_options['access']['type'] = 'perm'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['query']['type'] = 'views_query'; + $handler->display->display_options['query']['options']['query_comment'] = FALSE; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['pager']['options']['items_per_page'] = '10'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + $handler->display->display_options['row_options']['hide_empty'] = 0; + $handler->display->display_options['row_options']['default_field_elements'] = 1; + /* Field: Content: Title */ + $handler->display->display_options['fields']['title']['id'] = 'title'; + $handler->display->display_options['fields']['title']['table'] = 'node'; + $handler->display->display_options['fields']['title']['field'] = 'title'; + $handler->display->display_options['fields']['title']['label'] = ''; + $handler->display->display_options['fields']['title']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['title']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['title']['alter']['absolute'] = 0; + $handler->display->display_options['fields']['title']['alter']['word_boundary'] = 0; + $handler->display->display_options['fields']['title']['alter']['ellipsis'] = 0; + $handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['title']['alter']['trim'] = 0; + $handler->display->display_options['fields']['title']['alter']['html'] = 0; + $handler->display->display_options['fields']['title']['hide_empty'] = 0; + $handler->display->display_options['fields']['title']['empty_zero'] = 0; + $handler->display->display_options['fields']['title']['link_to_node'] = 1; + /* Field: User: Name */ + $handler->display->display_options['fields']['name']['id'] = 'name'; + $handler->display->display_options['fields']['name']['table'] = 'users'; + $handler->display->display_options['fields']['name']['field'] = 'name'; + $handler->display->display_options['fields']['name']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['name']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['name']['alter']['absolute'] = 0; + $handler->display->display_options['fields']['name']['alter']['external'] = 0; + $handler->display->display_options['fields']['name']['alter']['replace_spaces'] = 0; + $handler->display->display_options['fields']['name']['alter']['trim_whitespace'] = 0; + $handler->display->display_options['fields']['name']['alter']['nl2br'] = 0; + $handler->display->display_options['fields']['name']['alter']['word_boundary'] = 1; + $handler->display->display_options['fields']['name']['alter']['ellipsis'] = 1; + $handler->display->display_options['fields']['name']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['name']['alter']['trim'] = 0; + $handler->display->display_options['fields']['name']['alter']['html'] = 0; + $handler->display->display_options['fields']['name']['element_label_colon'] = 1; + $handler->display->display_options['fields']['name']['element_default_classes'] = 1; + $handler->display->display_options['fields']['name']['hide_empty'] = 0; + $handler->display->display_options['fields']['name']['empty_zero'] = 0; + $handler->display->display_options['fields']['name']['hide_alter_empty'] = 0; + $handler->display->display_options['fields']['name']['link_to_user'] = 1; + $handler->display->display_options['fields']['name']['overwrite_anonymous'] = 0; + /* Field: User: Uid */ + $handler->display->display_options['fields']['uid']['id'] = 'uid'; + $handler->display->display_options['fields']['uid']['table'] = 'users'; + $handler->display->display_options['fields']['uid']['field'] = 'uid'; + $handler->display->display_options['fields']['uid']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['uid']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['uid']['alter']['absolute'] = 0; + $handler->display->display_options['fields']['uid']['alter']['external'] = 0; + $handler->display->display_options['fields']['uid']['alter']['replace_spaces'] = 0; + $handler->display->display_options['fields']['uid']['alter']['trim_whitespace'] = 0; + $handler->display->display_options['fields']['uid']['alter']['nl2br'] = 0; + $handler->display->display_options['fields']['uid']['alter']['word_boundary'] = 1; + $handler->display->display_options['fields']['uid']['alter']['ellipsis'] = 1; + $handler->display->display_options['fields']['uid']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['uid']['alter']['trim'] = 0; + $handler->display->display_options['fields']['uid']['alter']['html'] = 0; + $handler->display->display_options['fields']['uid']['element_label_colon'] = 1; + $handler->display->display_options['fields']['uid']['element_default_classes'] = 1; + $handler->display->display_options['fields']['uid']['hide_empty'] = 0; + $handler->display->display_options['fields']['uid']['empty_zero'] = 0; + $handler->display->display_options['fields']['uid']['hide_alter_empty'] = 0; + $handler->display->display_options['fields']['uid']['link_to_user'] = 1; + + return $view; + } +} diff --git a/sites/all/modules/contrib/views/tests/user/views_user_argument_default.test b/sites/all/modules/contrib/views/tests/user/views_user_argument_default.test new file mode 100644 index 0000000..afb24d1 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/user/views_user_argument_default.test @@ -0,0 +1,90 @@ + 'Tests user argument default plugin', + 'description' => 'Tests user argument default plugin', + 'group' => 'Views Plugins', + ); + } + + public function test_plugin_argument_default_current_user() { + // Create a user to test. + $account = $this->drupalCreateUser(); + + // Switch the user, we have to check the global user too, because drupalLogin is only for the simpletest browser. + $this->drupalLogin($account); + global $user; + $admin = $user; + drupal_save_session(FALSE); + $user = $account; + + $view = $this->view_plugin_argument_default_current_user(); + + $view->set_display('default'); + $view->pre_execute(); + $view->init_handlers(); + + $this->assertEqual($view->argument['null']->get_default_argument(), $account->uid, 'Uid of the current user is used.'); + // Switch back. + $user = $admin; + drupal_save_session(TRUE); + } + + function view_plugin_argument_default_current_user() { + $view = new view; + $view->name = 'test_plugin_argument_default_current_user'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = '3.0-alpha1'; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['pager']['options']['items_per_page'] = '10'; + $handler->display->display_options['pager']['options']['offset'] = '0'; + $handler->display->display_options['pager']['options']['id'] = '0'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Field: Content: Title */ + $handler->display->display_options['fields']['title']['id'] = 'title'; + $handler->display->display_options['fields']['title']['table'] = 'node'; + $handler->display->display_options['fields']['title']['field'] = 'title'; + $handler->display->display_options['fields']['title']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['title']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['title']['alter']['trim'] = 0; + $handler->display->display_options['fields']['title']['alter']['word_boundary'] = 1; + $handler->display->display_options['fields']['title']['alter']['ellipsis'] = 1; + $handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['title']['alter']['html'] = 0; + $handler->display->display_options['fields']['title']['hide_empty'] = 0; + $handler->display->display_options['fields']['title']['empty_zero'] = 0; + $handler->display->display_options['fields']['title']['link_to_node'] = 0; + /* Argument: Global: Null */ + $handler->display->display_options['arguments']['null']['id'] = 'null'; + $handler->display->display_options['arguments']['null']['table'] = 'views'; + $handler->display->display_options['arguments']['null']['field'] = 'null'; + $handler->display->display_options['arguments']['null']['default_action'] = 'default'; + $handler->display->display_options['arguments']['null']['style_plugin'] = 'default_summary'; + $handler->display->display_options['arguments']['null']['default_argument_type'] = 'current_user'; + $handler->display->display_options['arguments']['null']['must_not_be'] = 0; + + return $view; + } +} diff --git a/sites/all/modules/contrib/views/tests/user/views_user_argument_validate.test b/sites/all/modules/contrib/views/tests/user/views_user_argument_validate.test new file mode 100644 index 0000000..6e157bf --- /dev/null +++ b/sites/all/modules/contrib/views/tests/user/views_user_argument_validate.test @@ -0,0 +1,115 @@ + 'Tests user argument validator', + 'description' => 'Tests user argument validator', + 'group' => 'Views Plugins', + ); + } + + function setUp() { + parent::setUp('views'); + $this->account = $this->drupalCreateUser(); + } + + function testArgumentValidateUserUid() { + $account = $this->account; + // test 'uid' case + $view = $this->view_argument_validate_user('uid'); + $view->set_display('default'); + $view->pre_execute(); + $view->init_handlers(); + $this->assertTrue($view->argument['null']->validate_arg($account->uid)); + // Reset safed argument validation. + $view->argument['null']->argument_validated = NULL; + // Fail for a string variable since type is 'uid' + $this->assertFalse($view->argument['null']->validate_arg($account->name)); + // Reset safed argument validation. + $view->argument['null']->argument_validated = NULL; + // Fail for a valid numeric, but for a user that doesn't exist + $this->assertFalse($view->argument['null']->validate_arg(32)); + } + + function testArgumentValidateUserName() { + $account = $this->account; + // test 'name' case + $view = $this->view_argument_validate_user('name'); + $view->set_display('default'); + $view->pre_execute(); + $view->init_handlers(); + $this->assertTrue($view->argument['null']->validate_arg($account->name)); + // Reset safed argument validation. + $view->argument['null']->argument_validated = NULL; + // Fail for a uid variable since type is 'name' + $this->assertFalse($view->argument['null']->validate_arg($account->uid)); + // Reset safed argument validation. + $view->argument['null']->argument_validated = NULL; + // Fail for a valid string, but for a user that doesn't exist + $this->assertFalse($view->argument['null']->validate_arg($this->randomName())); + } + + function testArgumentValidateUserEither() { + $account = $this->account; + // test 'either' case + $view = $this->view_argument_validate_user('either'); + $view->set_display('default'); + $view->pre_execute(); + $view->init_handlers(); + $this->assertTrue($view->argument['null']->validate_arg($account->name)); + // Reset safed argument validation. + $view->argument['null']->argument_validated = NULL; + // Fail for a uid variable since type is 'name' + $this->assertTrue($view->argument['null']->validate_arg($account->uid)); + // Reset safed argument validation. + $view->argument['null']->argument_validated = NULL; + // Fail for a valid string, but for a user that doesn't exist + $this->assertFalse($view->argument['null']->validate_arg($this->randomName())); + // Reset safed argument validation. + $view->argument['null']->argument_validated = NULL; + // Fail for a valid uid, but for a user that doesn't exist + $this->assertFalse($view->argument['null']->validate_arg(32)); + } + + function view_argument_validate_user($argtype) { + $view = new view; + $view->name = 'view_argument_validate_user'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 2; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Argument: Global: Null */ + $handler->display->display_options['arguments']['null']['id'] = 'null'; + $handler->display->display_options['arguments']['null']['table'] = 'views'; + $handler->display->display_options['arguments']['null']['field'] = 'null'; + $handler->display->display_options['arguments']['null']['style_plugin'] = 'default_summary'; + $handler->display->display_options['arguments']['null']['default_argument_type'] = 'fixed'; + $handler->display->display_options['arguments']['null']['validate']['type'] = 'user'; + $handler->display->display_options['arguments']['null']['validate_options']['type'] = $argtype; + $handler->display->display_options['arguments']['null']['must_not_be'] = 0; + + return $view; + } + +} diff --git a/sites/all/modules/contrib/views/tests/views_access.test b/sites/all/modules/contrib/views/tests/views_access.test new file mode 100644 index 0000000..f02eca9 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_access.test @@ -0,0 +1,285 @@ + 'Access', + 'description' => 'Tests pluggable access for views.', + 'group' => 'Views Plugins' + ); + } + + public function setUp() { + parent::setUp(); + + $this->admin_user = $this->drupalCreateUser(array('access all views')); + $this->web_user = $this->drupalCreateUser(); + $this->web_role = current($this->web_user->roles); + + $this->normal_role = $this->drupalCreateRole(array()); + $this->normal_user = $this->drupalCreateUser(array('views_test test permission')); + $this->normal_user->roles[$this->normal_role] = $this->normal_role; + // Reset the plugin data. + views_fetch_plugin_data(NULL, NULL, TRUE); + } + + function viewsPlugins() { + $plugins = array( + 'access' => array( + 'test_static' => array( + 'title' => t('Static test access plugin'), + 'help' => t('Provides a static test access plugin.'), + 'handler' => 'views_test_plugin_access_test_static', + 'path' => drupal_get_path('module', 'views_test') . '/test_plugins', + ), + 'test_dynamic' => array( + 'title' => t('Dynamic test access plugin'), + 'help' => t('Provides a dynamic test access plugin.'), + 'handler' => 'views_test_plugin_access_test_dynamic', + 'path' => drupal_get_path('module', 'views_test') . '/test_plugins', + ), + ), + ); + + return $plugins; + } + + /** + * Tests none access plugin. + */ + function testAccessNone() { + $view = $this->view_access_none(); + + $view->set_display('default'); + + $this->assertTrue($view->display_handler->access($this->admin_user), t('Admin-Account should be able to access the view everytime')); + $this->assertTrue($view->display_handler->access($this->web_user)); + $this->assertTrue($view->display_handler->access($this->normal_user)); + } + + /** + * Tests perm access plugin. + */ + function testAccessPerm() { + $view = $this->view_access_perm(); + + $view->set_display('default'); + $access_plugin = $view->display_handler->get_plugin('access'); + + $this->assertTrue($view->display_handler->access($this->admin_user), t('Admin-Account should be able to access the view everytime')); + $this->assertFalse($view->display_handler->access($this->web_user)); + $this->assertTrue($view->display_handler->access($this->normal_user)); + } + + /** + * Tests role access plugin. + */ + function testAccessRole() { + $view = $this->view_access_role(); + + $view->set_display('default'); + $access_plugin = $view->display_handler->get_plugin('access'); + + $this->assertTrue($view->display_handler->access($this->admin_user), t('Admin-Account should be able to access the view everytime')); + $this->assertFalse($view->display_handler->access($this->web_user)); + $this->assertTrue($view->display_handler->access($this->normal_user)); + } + + /** + * @todo Test abstract access plugin. + */ + + /** + * Tests static access check. + */ + function testStaticAccessPlugin() { + $view = $this->view_access_static(); + + $view->set_display('default'); + $access_plugin = $view->display_handler->get_plugin('access'); + + $this->assertFalse($access_plugin->access($this->normal_user)); + + $access_plugin->options['access'] = TRUE; + $this->assertTrue($access_plugin->access($this->normal_user)); + + // FALSE comes from hook_menu caching. + $expected_hook_menu = array( + 'views_test_test_static_access_callback', array(FALSE) + ); + $hook_menu = $view->execute_hook_menu('page_1'); + $this->assertEqual($expected_hook_menu, $hook_menu['test_access_static']['access arguments'][0]); + + $expected_hook_menu = array( + 'views_test_test_static_access_callback', array(TRUE) + ); + $this->assertTrue(views_access($expected_hook_menu)); + } + + /** + * Tests dynamic access plugin. + */ + function testDynamicAccessPlugin() { + $view = $this->view_access_dynamic(); + $argument1 = $this->randomName(); + $argument2 = $this->randomName(); + variable_set('test_dynamic_access_argument1', $argument1); + variable_set('test_dynamic_access_argument2', $argument2); + + $view->set_display('default'); + $access_plugin = $view->display_handler->get_plugin('access'); + + $this->assertFalse($access_plugin->access($this->normal_user)); + + $access_plugin->options['access'] = TRUE; + $this->assertFalse($access_plugin->access($this->normal_user)); + + $view->set_arguments(array($argument1, $argument2)); + $this->assertTrue($access_plugin->access($this->normal_user)); + + // FALSE comes from hook_menu caching. + $expected_hook_menu = array( + 'views_test_test_dynamic_access_callback', array(FALSE, 1, 2) + ); + $hook_menu = $view->execute_hook_menu('page_1'); + $this->assertEqual($expected_hook_menu, $hook_menu['test_access_dynamic']['access arguments'][0]); + + $expected_hook_menu = array( + 'views_test_test_dynamic_access_callback', array(TRUE, 1, 2) + ); + $this->assertTrue(views_access($expected_hook_menu, $argument1, $argument2)); + } + + function view_access_none() { + $view = new view; + $view->name = 'test_access_none'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 2; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + + return $view; + } + + function view_access_perm() { + $view = new view; + $view->name = 'test_access_perm'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 2; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'perm'; + $handler->display->display_options['access']['perm'] = 'views_test test permission'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + + return $view; + } + + function view_access_role() { + $view = new view; + $view->name = 'test_access_role'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 2; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'role'; + $handler->display->display_options['access']['role'] = array( + $this->normal_role => $this->normal_role, + ); + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + + return $view; + } + + function view_access_dynamic() { + $view = new view; + $view->name = 'test_access_dynamic'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 2; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'test_dynamic'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + + $handler = $view->new_display('page', 'Page', 'page_1'); + $handler->display->display_options['path'] = 'test_access_dynamic'; + + return $view; + } + + function view_access_static() { + $view = new view; + $view->name = 'test_access_static'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 2; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'test_static'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + + $handler = $view->new_display('page', 'Page', 'page_1'); + $handler->display->display_options['path'] = 'test_access_static'; + + return $view; + } +} diff --git a/sites/all/modules/contrib/views/tests/views_analyze.test b/sites/all/modules/contrib/views/tests/views_analyze.test new file mode 100644 index 0000000..c21a4bb --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_analyze.test @@ -0,0 +1,51 @@ + 'Views Analyze', + 'description' => 'Test the views analyze system.', + 'group' => 'Views', + ); + } + + public function setUp() { + parent::setUp('views_ui'); + module_enable(array('views_ui')); + // @TODO Figure out why it's required to clear the cache here. + views_module_include('views_default', TRUE); + views_get_all_views(TRUE); + menu_rebuild(); + + // Add an admin user will full rights; + $this->admin = $this->drupalCreateUser(array('administer views')); + } + + /** + * Tests that analyze works in general. + */ + function testAnalyzeBasic() { + $this->drupalLogin($this->admin); + // Enable the frontpage view and click the analyse button. + $view = views_get_view('frontpage'); + $view->save(); + + $this->drupalGet('admin/structure/views/view/frontpage/edit'); + $this->assertLink(t('analyze view')); + + // This redirects the user to the form. + $this->clickLink(t('analyze view')); + $this->assertText(t('View analysis')); + + // This redirects the user back to the main views edit page. + $this->drupalPost(NULL, array(), t('Ok')); + } +} diff --git a/sites/all/modules/contrib/views/tests/views_argument_default.test b/sites/all/modules/contrib/views/tests/views_argument_default.test new file mode 100644 index 0000000..9c0a7eb --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_argument_default.test @@ -0,0 +1,137 @@ + 'Argument_default', + 'description' => 'Tests pluggable argument_default for views.', + 'group' => 'Views Plugins' + ); + } + + public function setUp() { + parent::setUp('views'); + + $this->random = $this->randomString(); + } + + /** + * Tests the use of a default argument plugin that provides no options. + */ + function testArgumentDefaultNoOptions() { + module_enable(array('views_ui', 'views_test')); + $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration')); + $this->drupalLogin($admin_user); + + // The current_user plugin has no options form, and should pass validation. + $argument_type = 'current_user'; + $edit = array( + 'options[default_argument_type]' => $argument_type, + ); + $this->drupalPost('admin/structure/views/nojs/config-item/test_argument_default_current_user/default/argument/uid', $edit, t('Apply')); + + // Note, the undefined index error has two spaces after it. + $error = array( + '%type' => 'Notice', + '!message' => 'Undefined index: ' . $argument_type, + '%function' => 'views_handler_argument->options_validate()', + ); + $message = t('%type: !message in %function', $error); + $this->assertNoRaw($message, t('Did not find error message: !message.', array('!message' => $message))); + } + + /** + * Tests fixed default argument. + */ + function testArgumentDefaultFixed() { + $view = $this->view_argument_default_fixed(); + + $view->set_display('default'); + $view->pre_execute(); + $view->init_handlers(); + + $this->assertEqual($view->argument['null']->get_default_argument(), $this->random, 'Fixed argument should be used by default.'); + + $view->destroy(); + + // Make sure that a normal argument provided is used + $view = $this->view_argument_default_fixed(); + + $view->set_display('default'); + $random_string = $this->randomString(); + $view->execute_display('default', array($random_string)); + + $this->assertEqual($view->args[0], $random_string, 'Provided argument should be used.'); + } + + /** + * @todo Test php default argument. + */ + function testArgumentDefaultPhp() { + + } + + /** + * @todo Test node default argument. + */ + function testArgumentDefaultNode() { + + } + + function view_argument_default_fixed() { + $view = new view; + $view->name = 'test_argument_default_fixed'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 2; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['pager']['options']['items_per_page'] = '10'; + $handler->display->display_options['pager']['options']['offset'] = '0'; + $handler->display->display_options['pager']['options']['id'] = '0'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Field: Content: Title */ + $handler->display->display_options['fields']['title']['id'] = 'title'; + $handler->display->display_options['fields']['title']['table'] = 'node'; + $handler->display->display_options['fields']['title']['field'] = 'title'; + $handler->display->display_options['fields']['title']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['title']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['title']['alter']['trim'] = 0; + $handler->display->display_options['fields']['title']['alter']['word_boundary'] = 1; + $handler->display->display_options['fields']['title']['alter']['ellipsis'] = 1; + $handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['title']['alter']['html'] = 0; + $handler->display->display_options['fields']['title']['hide_empty'] = 0; + $handler->display->display_options['fields']['title']['empty_zero'] = 0; + $handler->display->display_options['fields']['title']['link_to_node'] = 0; + /* Argument: Global: Null */ + $handler->display->display_options['arguments']['null']['id'] = 'null'; + $handler->display->display_options['arguments']['null']['table'] = 'views'; + $handler->display->display_options['arguments']['null']['field'] = 'null'; + $handler->display->display_options['arguments']['null']['default_action'] = 'default'; + $handler->display->display_options['arguments']['null']['style_plugin'] = 'default_summary'; + $handler->display->display_options['arguments']['null']['default_argument_type'] = 'fixed'; + $handler->display->display_options['arguments']['null']['default_argument_options']['argument'] = $this->random; + $handler->display->display_options['arguments']['null']['must_not_be'] = 0; + + return $view; + } +} diff --git a/sites/all/modules/contrib/views/tests/views_argument_validator.test b/sites/all/modules/contrib/views/tests/views_argument_validator.test new file mode 100644 index 0000000..fb2e4f2 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_argument_validator.test @@ -0,0 +1,106 @@ + 'Argument validator', + 'group' => 'Views Plugins', + 'description' => 'Test argument validator tests.', + ); + } + + function testArgumentValidatePhp() { + $string = $this->randomName(); + $view = $this->view_test_argument_validate_php($string); + $view->set_display('default'); + $view->pre_execute(); + $view->init_handlers(); + $this->assertTrue($view->argument['null']->validate_arg($string)); + // Reset safed argument validation. + $view->argument['null']->argument_validated = NULL; + $this->assertFalse($view->argument['null']->validate_arg($this->randomName())); + } + + function testArgumentValidateNumeric() { + $view = $this->view_argument_validate_numeric(); + $view->set_display('default'); + $view->pre_execute(); + $view->init_handlers(); + $this->assertFalse($view->argument['null']->validate_arg($this->randomString())); + // Reset safed argument validation. + $view->argument['null']->argument_validated = NULL; + $this->assertTrue($view->argument['null']->validate_arg(12)); + } + + function view_test_argument_validate_php($string) { + $code = 'return $argument == \''. $string .'\';'; + $view = new view; + $view->name = 'view_argument_validate_numeric'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 2; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Argument: Global: Null */ + $handler->display->display_options['arguments']['null']['id'] = 'null'; + $handler->display->display_options['arguments']['null']['table'] = 'views'; + $handler->display->display_options['arguments']['null']['field'] = 'null'; + $handler->display->display_options['arguments']['null']['style_plugin'] = 'default_summary'; + $handler->display->display_options['arguments']['null']['default_argument_type'] = 'fixed'; + $handler->display->display_options['arguments']['null']['validate_type'] = 'php'; + $handler->display->display_options['arguments']['null']['validate_options']['code'] = $code; + $handler->display->display_options['arguments']['null']['must_not_be'] = 0; + + return $view; + } + + function view_argument_validate_numeric() { + $view = new view; + $view->name = 'view_argument_validate_numeric'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 2; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Argument: Global: Null */ + $handler->display->display_options['arguments']['null']['id'] = 'null'; + $handler->display->display_options['arguments']['null']['table'] = 'views'; + $handler->display->display_options['arguments']['null']['field'] = 'null'; + $handler->display->display_options['arguments']['null']['style_plugin'] = 'default_summary'; + $handler->display->display_options['arguments']['null']['default_argument_type'] = 'fixed'; + $handler->display->display_options['arguments']['null']['validate_type'] = 'numeric'; + $handler->display->display_options['arguments']['null']['must_not_be'] = 0; + + return $view; + } +} diff --git a/sites/all/modules/contrib/views/tests/views_basic.test b/sites/all/modules/contrib/views/tests/views_basic.test new file mode 100644 index 0000000..5fc60d8 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_basic.test @@ -0,0 +1,178 @@ + 'Basic query test', + 'description' => 'A basic query test for Views.', + 'group' => 'Views' + ); + } + + /** + * Tests a trivial result set. + */ + public function testSimpleResultSet() { + $view = $this->getBasicView(); + + // Execute the view. + $this->executeView($view); + + // Verify the result. + $this->assertEqual(5, count($view->result), t('The number of returned rows match.')); + $this->assertIdenticalResultset($view, $this->dataSet(), array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + )); + } + + /** + * Tests filtering of the result set. + */ + public function testSimpleFiltering() { + $view = $this->getBasicView(); + + // Add a filter. + $view->display['default']->handler->override_option('filters', array( + 'age' => array( + 'operator' => '<', + 'value' => array( + 'value' => '28', + 'min' => '', + 'max' => '', + ), + 'group' => '0', + 'exposed' => FALSE, + 'expose' => array( + 'operator' => FALSE, + 'label' => '', + ), + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + ), + )); + + // Execute the view. + $this->executeView($view); + + // Build the expected result. + $dataset = array( + array( + 'id' => 1, + 'name' => 'John', + 'age' => 25, + ), + array( + 'id' => 2, + 'name' => 'George', + 'age' => 27, + ), + array( + 'id' => 4, + 'name' => 'Paul', + 'age' => 26, + ), + ); + + // Verify the result. + $this->assertEqual(3, count($view->result), t('The number of returned rows match.')); + $this->assertIdenticalResultSet($view, $dataset, array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + )); + } + + /** + * Tests simple argument. + */ + public function testSimpleArgument() { + $view = $this->getBasicView(); + + // Add a argument. + $view->display['default']->handler->override_option('arguments', array( + 'age' => array( + 'default_action' => 'ignore', + 'style_plugin' => 'default_summary', + 'style_options' => array(), + 'wildcard' => 'all', + 'wildcard_substitution' => 'All', + 'title' => '', + 'breadcrumb' => '', + 'default_argument_type' => 'fixed', + 'default_argument' => '', + 'validate_type' => 'none', + 'validate_fail' => 'not found', + 'break_phrase' => 0, + 'not' => 0, + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'validate_user_argument_type' => 'uid', + 'validate_user_roles' => array( + '2' => 0, + ), + 'relationship' => 'none', + 'default_options_div_prefix' => '', + 'default_argument_user' => 0, + 'default_argument_fixed' => '', + 'default_argument_php' => '', + 'validate_argument_node_type' => array( + 'page' => 0, + 'story' => 0, + ), + 'validate_argument_node_access' => 0, + 'validate_argument_nid_type' => 'nid', + 'validate_argument_vocabulary' => array(), + 'validate_argument_type' => 'tid', + 'validate_argument_transform' => 0, + 'validate_user_restrict_roles' => 0, + 'validate_argument_php' => '', + ) + )); + + $saved_view = clone $view; + + // Execute with a view + $view->set_arguments(array(27)); + $this->executeView($view); + + // Build the expected result. + $dataset = array( + array( + 'id' => 2, + 'name' => 'George', + 'age' => 27, + ), + ); + + // Verify the result. + $this->assertEqual(1, count($view->result), t('The number of returned rows match.')); + $this->assertIdenticalResultSet($view, $dataset, array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + )); + + // Test "show all" if no argument is present. + $view = $saved_view; + $this->executeView($view); + + // Build the expected result. + $dataset = $this->dataSet(); + + $this->assertEqual(5, count($view->result), t('The number of returned rows match.')); + $this->assertIdenticalResultSet($view, $dataset, array( + 'views_test_name' => 'name', + 'views_test_age' => 'age', + )); + } +} diff --git a/sites/all/modules/contrib/views/tests/views_cache.test b/sites/all/modules/contrib/views/tests/views_cache.test new file mode 100644 index 0000000..28534a2 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_cache.test @@ -0,0 +1,308 @@ + 'Cache', + 'description' => 'Tests pluggable caching for views.', + 'group' => 'Views Plugins' + ); + } + + /** + * Build and return a basic view of the views_test table. + * + * @return view + */ + protected function getBasicView() { + views_include('view'); + + // Create the basic view. + $view = new view(); + $view->name = 'test_view'; + $view->add_display('default'); + $view->base_table = 'views_test'; + + // Set up the fields we need. + $display = $view->new_display('default', 'Master', 'default'); + $display->override_option('fields', array( + 'id' => array( + 'id' => 'id', + 'table' => 'views_test', + 'field' => 'id', + 'relationship' => 'none', + ), + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + ), + 'age' => array( + 'id' => 'age', + 'table' => 'views_test', + 'field' => 'age', + 'relationship' => 'none', + ), + )); + + // Set up the sort order. + $display->override_option('sorts', array( + 'id' => array( + 'order' => 'ASC', + 'id' => 'id', + 'table' => 'views_test', + 'field' => 'id', + 'relationship' => 'none', + ), + )); + + return $view; + } + + /** + * Tests time based caching. + * + * @see views_plugin_cache_time + */ + function testTimeCaching() { + // Create a basic result which just 2 results. + $view = $this->getBasicView(); + $view->set_display(); + $view->display_handler->override_option('cache', array( + 'type' => 'time', + 'results_lifespan' => '3600', + 'output_lifespan' => '3600', + )); + + $this->executeView($view); + // Verify the result. + $this->assertEqual(5, count($view->result), t('The number of returned rows match.')); + + // Add another man to the beatles. + $record = array( + 'name' => 'Rod Davis', + 'age' => 29, + 'job' => 'Banjo', + ); + drupal_write_record('views_test', $record); + + // The Result should be the same as before, because of the caching. + $view = $this->getBasicView(); + $view->set_display(); + $view->display_handler->override_option('cache', array( + 'type' => 'time', + 'results_lifespan' => '3600', + 'output_lifespan' => '3600', + )); + + $this->executeView($view); + // Verify the result. + $this->assertEqual(5, count($view->result), t('The number of returned rows match.')); + } + + /** + * Tests no caching. + * + * @see views_plugin_cache_time + */ + function testNoneCaching() { + // Create a basic result which just 2 results. + $view = $this->getBasicView(); + $view->set_display(); + $view->display_handler->override_option('cache', array( + 'type' => 'none', + )); + + $this->executeView($view); + // Verify the result. + $this->assertEqual(5, count($view->result), t('The number of returned rows match.')); + + // Add another man to the beatles. + $record = array( + 'name' => 'Rod Davis', + 'age' => 29, + 'job' => 'Banjo', + ); + + drupal_write_record('views_test', $record); + + // The Result changes, because the view is not cached. + $view = $this->getBasicView(); + $view->set_display(); + $view->display_handler->override_option('cache', array( + 'type' => 'none', + )); + + $this->executeView($view); + // Verify the result. + $this->assertEqual(6, count($view->result), t('The number of returned rows match.')); + } + + /** + * Tests css/js storage and restoring mechanism. + */ + function testHeaderStorage() { + // Create a view with output caching enabled. + // Some hook_views_pre_render in views_test.module adds the test css/js file. + // so they should be added to the css/js storage. + $view = $this->getBasicView(); + $view->init_display(); + $view->name = 'test_cache_header_storage'; + $view->display_handler->override_option('cache', array( + 'type' => 'time', + 'output_lifespan' => '3600', + )); + + $view->preview(); + $view->destroy(); + unset($view->pre_render_called); + drupal_static_reset('drupal_add_css'); + drupal_static_reset('drupal_add_js'); + + $view->init_display(); + $view->preview(); + $css = drupal_add_css(); + $css_path = drupal_get_path('module', 'views_test') . '/views_cache.test.css'; + $js_path = drupal_get_path('module', 'views_test') . '/views_cache.test.js'; + $js = drupal_add_js(); + + $this->assertTrue(isset($css[$css_path]), 'Make sure the css is added for cached views.'); + $this->assertTrue(isset($js[$js_path]), 'Make sure the js is added for cached views.'); + $this->assertFalse(!empty($view->pre_render_called), 'Make sure hook_views_pre_render is not called for the cached view.'); + $view->destroy(); + + // Now add some css/jss before running the view. + // Make sure that this css is not added when running the cached view. + $view->name = 'test_cache_header_storage_2'; + + $system_css_path = drupal_get_path('module', 'system') . '/system.maintenance.css'; + drupal_add_css($system_css_path); + $system_js_path = drupal_get_path('module', 'system') . '/system.cron.js'; + drupal_add_js($system_js_path); + + $view->init_display(); + $view->preview(); + $view->destroy(); + drupal_static_reset('drupal_add_css'); + drupal_static_reset('drupal_add_js'); + + $view->init_display(); + $view->preview(); + + $css = drupal_add_css(); + $js = drupal_add_js(); + + $this->assertFalse(isset($css[$system_css_path]), 'Make sure that unrelated css is not added.'); + $this->assertFalse(isset($js[$system_js_path]), 'Make sure that unrelated js is not added.'); + + } + + /** + * Check that HTTP headers are cached for views. + */ + function testHttpHeadersCaching() { + // Create a few nodes to appear in RSS feed. + for ($i = 0; $i < 5; $i++) { + $this->drupalCreateNode(); + } + + // Make the first request and check returned content type. + $this->drupalGet('test_feed_http_headers_caching'); + $first_content = $this->drupalGetContent(); + $first_content_type = $this->drupalGetHeader('content-type'); + $expected_type = 'application/rss+xml'; + $this->assertIdentical(0, strpos(trim($first_content_type), $expected_type), t('Expected content type returned.')); + + // Check that we have 5 items in RSS feed returned by the first request. + $xml = simplexml_load_string($first_content); + $items = $xml->xpath('/rss/channel/item'); + $this->assertEqual(5, count($items), t('The number of RSS feed items matched.')); + + // Create another node to be sure we get cached results on the second + // request. + $this->drupalCreateNode(); + + // Make the second request, check content type and content matching. + $this->drupalGet('test_feed_http_headers_caching'); + $second_content = $this->drupalGetContent(); + $this->assertEqual($first_content, $second_content, t('The second result fetched from cache.')); + $second_content_type = $this->drupalGetHeader('content-type'); + $this->assertEqual($first_content_type, $second_content_type, t('Content types of responses are equal.')); + } + + /** + * Test caching of different exposed filter values with the same view result. + * + * Make sure the output is different. + */ + function testExposedFilterSameResultsCaching() { + // Create the view with time-based cache with hour lifetimes and add exposed + // filter to it with "Starts with" operator. + $view = $this->getBasicView(); + $view->set_display(); + $view->display_handler->override_option('cache', array( + 'type' => 'time', + 'results_lifespan' => '3600', + 'output_lifespan' => '3600', + )); + $view->display_handler->override_option('filters', array( + 'name' => array( + 'id' => 'name', + 'table' => 'views_test', + 'field' => 'name', + 'relationship' => 'none', + 'operator' => 'starts', + 'exposed' => TRUE, + 'expose' => array( + 'operator_id' => 'name_op', + 'operator' => 'name_op', + 'identifier' => 'name', + ), + ), + )); + + // Clone the view before setting exposed input. + $clone = $view->copy(); + + // Pass "Rin" to the exposed filter and check that only one row returned. + $view->set_exposed_input(array( + 'name' => 'Rin', + )); + $this->executeView($view); + $first_result = $view->result; + $first_output = $view->render(); + $this->assertEqual(1, count($first_result), t('The number of rows returned by the first view match.')); + + // Pass full "Ringo" to the exposed filter at the second time and make sure + // results are the same. + $clone->set_exposed_input(array( + 'name' => 'Ringo', + )); + $this->executeView($clone); + $second_result = $clone->result; + $second_output = $clone->render(); + $this->assertEqual($first_result, $second_result, t('Results of both views are the same.')); + + // Check that output is not the same and it contains full "Ringo" word in + // default value of exposed input. + $this->assertNotEqual($first_output, $second_output, t('Output of the second view is different.')); + $this->drupalSetContent($second_output); + $element = $this->xpath('//input[@name="name" and @value="Ringo"]'); + $this->assertTrue(!empty($element), t('Input field of exposed filter has the second value.')); + + $view->destroy(); + $clone->destroy(); + } + +} diff --git a/sites/all/modules/contrib/views/tests/views_cache.test.css b/sites/all/modules/contrib/views/tests/views_cache.test.css new file mode 100644 index 0000000..8dd17c1 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_cache.test.css @@ -0,0 +1,5 @@ +/** + * @file + * Just a placeholder file for the test. + * @see ViewsCacheTest::testHeaderStorage + */ diff --git a/sites/all/modules/contrib/views/tests/views_cache.test.js b/sites/all/modules/contrib/views/tests/views_cache.test.js new file mode 100644 index 0000000..8dd17c1 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_cache.test.js @@ -0,0 +1,5 @@ +/** + * @file + * Just a placeholder file for the test. + * @see ViewsCacheTest::testHeaderStorage + */ diff --git a/sites/all/modules/contrib/views/tests/views_exposed_form.test b/sites/all/modules/contrib/views/tests/views_exposed_form.test new file mode 100644 index 0000000..72baf2c --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_exposed_form.test @@ -0,0 +1,170 @@ + 'Exposed forms', + 'description' => 'Test exposed forms functionality.', + 'group' => 'Views Plugins', + ); + } + + public function setUp() { + parent::setUp('views_ui'); + module_enable(array('views_ui')); + // @TODO Figure out why it's required to clear the cache here. + views_module_include('views_default', TRUE); + views_get_all_views(TRUE); + menu_rebuild(); + } + + /** + * Tests, whether and how the reset button can be renamed. + */ + public function testRenameResetButton() { + $account = $this->drupalCreateUser(); + $this->drupalLogin($account); + // Create some random nodes. + for ($i = 0; $i < 5; $i++) { + $this->drupalCreateNode(); + } + // Look at the page and check the label "reset". + $this->drupalGet('test_rename_reset_button'); + // Rename the label of the reset button. + $view = views_get_view('test_rename_reset_button'); + $view->set_display('default'); + + $exposed_form = $view->display_handler->get_option('exposed_form'); + $exposed_form['options']['reset_button_label'] = $expected_label = $this->randomName(); + $exposed_form['options']['reset_button'] = TRUE; + $view->display_handler->set_option('exposed_form', $exposed_form); + $view->save(); + + views_invalidate_cache(); + + // Look whether ther reset button label changed. + $this->drupalGet('test_rename_reset_button'); + + $this->helperButtonHasLabel('edit-reset', $expected_label); + } + + /** + * Tests the admin interface of exposed filter and sort items. + */ + function testExposedAdminUi() { + $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration')); + $this->drupalLogin($admin_user); + menu_rebuild(); + $edit = array(); + + $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type'); + // Be sure that the button is called exposed. + $this->helperButtonHasLabel('edit-options-expose-button-button', t('Expose filter')); + + // The first time the filter UI is displayed, the operator and the + // value forms should be shown. + $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists'); + $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists'); + $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists'); + $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists'); + + // Click the Expose filter button. + $this->drupalPost('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type', $edit, t('Expose filter')); + // Check the label of the expose button. + $this->helperButtonHasLabel('edit-options-expose-button-button', t('Hide filter')); + // Check the label of the grouped exposed button + $this->helperButtonHasLabel('edit-options-group-button-button', t('Grouped filters')); + + // After Expose the filter, Operator and Value should be still here + $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists'); + $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists'); + $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists'); + $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists'); + + // Check the validations of the filter handler. + $edit = array(); + $edit['options[expose][identifier]'] = ''; + $this->drupalPost(NULL, $edit, t('Apply')); + $this->assertText(t('The identifier is required if the filter is exposed.')); + + $edit = array(); + $edit['options[expose][identifier]'] = 'value'; + $this->drupalPost(NULL, $edit, t('Apply')); + $this->assertText(t('This identifier is not allowed.')); + + // Now check the sort criteria. + $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/sort/created'); + $this->helperButtonHasLabel('edit-options-expose-button-button', t('Expose sort')); + $this->assertNoFieldById('edit-options-expose-label', '', t('Make sure no label field is shown')); + + // Click the Grouped Filters button. + $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type'); + $this->drupalPost(NULL, array(), t('Grouped filters')); + + // After click on 'Grouped Filters' standard operator and value should not be displayed + $this->assertNoFieldById('edit-options-operator-in', '', 'Operator In not exists'); + $this->assertNoFieldById('edit-options-operator-not-in', '', 'Operator Not In not exists'); + $this->assertNoFieldById('edit-options-value-page', '', 'Checkbox for Page not exists'); + $this->assertNoFieldById('edit-options-value-article', '', 'Checkbox for Article not exists'); + + + // Check that after click on 'Grouped Filters', a new button is shown to + // add more items to the list. + $this->helperButtonHasLabel('edit-options-group-info-add-group', t('Add another item')); + + // Create a grouped filter + $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type'); + $edit = array(); + $edit["options[group_info][group_items][1][title]"] = 'Is Article'; + $edit["options[group_info][group_items][1][value][article]"] = 'article'; + + $edit["options[group_info][group_items][2][title]"] = 'Is Page'; + $edit["options[group_info][group_items][2][value][page]"] = TRUE; + + $edit["options[group_info][group_items][3][title]"] = 'Is Page and Article'; + $edit["options[group_info][group_items][3][value][article]"] = TRUE; + $edit["options[group_info][group_items][3][value][page]"] = TRUE; + $this->drupalPost(NULL, $edit, t('Apply')); + + // Validate that all the titles are defined for each group + $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type'); + $edit = array(); + $edit["options[group_info][group_items][1][title]"] = 'Is Article'; + $edit["options[group_info][group_items][1][value][article]"] = TRUE; + + // This should trigger an error + $edit["options[group_info][group_items][2][title]"] = ''; + $edit["options[group_info][group_items][2][value][page]"] = TRUE; + + $edit["options[group_info][group_items][3][title]"] = 'Is Page and Article'; + $edit["options[group_info][group_items][3][value][article]"] = TRUE; + $edit["options[group_info][group_items][3][value][page]"] = TRUE; + $this->drupalPost(NULL, $edit, t('Apply')); + $this->assertRaw(t('The title is required if value for this item is defined.'), t('Group items should have a title')); + + // Un-Expose the filter + $this->drupalGet('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/filter/type'); + $this->drupalPost(NULL, array(), t('Hide filter')); + + // After Un-Expose the filter, Operator and Value should be shown again + $this->assertFieldById('edit-options-operator-in', '', 'Operator In exists after hide filter'); + $this->assertFieldById('edit-options-operator-not-in', '', 'Operator Not In exists after hide filter'); + $this->assertFieldById('edit-options-value-page', '', 'Checkbox for Page exists after hide filter'); + $this->assertFieldById('edit-options-value-article', '', 'Checkbox for Article exists after hide filter'); + + // Click the Expose sort button. + $edit = array(); + $this->drupalPost('admin/structure/views/nojs/config-item/test_exposed_admin_ui/default/sort/created', $edit, t('Expose sort')); + // Check the label of the expose button. + $this->helperButtonHasLabel('edit-options-expose-button-button', t('Hide sort')); + $this->assertFieldById('edit-options-expose-label', '', t('Make sure a label field is shown')); + } +} diff --git a/sites/all/modules/contrib/views/tests/views_glossary.test b/sites/all/modules/contrib/views/tests/views_glossary.test new file mode 100644 index 0000000..0fe0fba --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_glossary.test @@ -0,0 +1,60 @@ + 'Glossary Test', + 'description' => 'Tests glossary functionality of views.', + 'group' => 'Views', + ); + } + + public function setUp() { + parent::setUp('views'); + } + + /** + * Tests the default glossary view. + */ + public function testGlossaryView() { + // create a contentype and add some nodes, with a non random title. + $type = $this->drupalCreateContentType(); + $nodes_per_char = array( + 'd' => 1, + 'r' => 4, + 'u' => 10, + 'p' => 2, + 'a' => 3, + 'l' => 6, + ); + foreach ($nodes_per_char as $char => $count) { + $setting = array( + 'type' => $type->type + ); + for ($i = 0; $i < $count; $i++) { + $node = $setting; + $node['title'] = $char . $this->randomString(3); + $this->drupalCreateNode($node); + } + } + + // Execute glossary view + $view = views_get_view('glossary'); + $view->set_display('attachment'); + $view->execute_display('attachment'); + + // Check that the amount of nodes per char. + $result_nodes_per_char = array(); + foreach ($view->result as $item) { + $this->assertEqual($nodes_per_char[$item->title_truncated], $item->num_records); + } + } +} diff --git a/sites/all/modules/contrib/views/tests/views_groupby.test b/sites/all/modules/contrib/views/tests/views_groupby.test new file mode 100644 index 0000000..4e09256 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_groupby.test @@ -0,0 +1,352 @@ + 'Groupby', + 'description' => 'Tests aggregate functionality of views, for example count.', + 'group' => 'Views', + ); + + } + + /** + * Tests aggregate count feature. + */ + public function testAggregateCount() { + // Create 2 nodes of type1 and 3 nodes of type2 + $type1 = $this->drupalCreateContentType(); + $type2 = $this->drupalCreateContentType(); + + $node_1 = array( + 'type' => $type1->type, + ); + $this->drupalCreateNode($node_1); + $this->drupalCreateNode($node_1); + $this->drupalCreateNode($node_1); + $this->drupalCreateNode($node_1); + + $node_2 = array( + 'type' => $type2->type, + ); + $this->drupalCreateNode($node_2); + $this->drupalCreateNode($node_2); + $this->drupalCreateNode($node_2); + + $view = $this->viewsAggregateCountView(); + $output = $view->execute_display(); + + $this->assertEqual(count($view->result), 2, 'Make sure the count of items is right.'); + + $types = array(); + foreach ($view->result as $item) { + // num_records is a alias for nid. + $types[$item->node_type] = $item->num_records; + } + + $this->assertEqual($types[$type1->type], 4); + $this->assertEqual($types[$type2->type], 3); + } + + //public function testAggregateSum() { + //} + + public function viewsAggregateCountView() { + $view = new view; + $view->name = 'aggregate_count'; + $view->description = ''; + $view->tag = ''; + $view->base_table = 'node'; + $view->human_name = ''; + $view->core = 7; + $view->api_version = '3.0'; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['group_by'] = TRUE; + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['query']['type'] = 'views_query'; + $handler->display->display_options['query']['options']['query_comment'] = FALSE; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'some'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Field: Content: Title */ + $handler->display->display_options['fields']['nid']['id'] = 'nid'; + $handler->display->display_options['fields']['nid']['table'] = 'node'; + $handler->display->display_options['fields']['nid']['field'] = 'title'; + $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1; + $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1; + $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['nid']['alter']['trim'] = 0; + $handler->display->display_options['fields']['nid']['alter']['html'] = 0; + $handler->display->display_options['fields']['nid']['hide_empty'] = 0; + $handler->display->display_options['fields']['nid']['empty_zero'] = 0; + $handler->display->display_options['fields']['nid']['link_to_node'] = 0; + /* Contextual filter: Content: Type */ + $handler->display->display_options['arguments']['type']['id'] = 'type'; + $handler->display->display_options['arguments']['type']['table'] = 'node'; + $handler->display->display_options['arguments']['type']['field'] = 'type'; + $handler->display->display_options['arguments']['type']['default_action'] = 'summary'; + $handler->display->display_options['arguments']['type']['default_argument_type'] = 'fixed'; + $handler->display->display_options['arguments']['type']['summary']['format'] = 'default_summary'; + + + return $view; + } + + /** + * @param string|null $group_by + * (optional) Which group_by function should be used, for example sum or + * count. If omitted, the aggregation is tested with no group function. + * @param array|null $values + * (optional) Expected values. + */ + function GroupByTestHelper($group_by = NULL, $values = NULL) { + // Create 4 nodes of type1 and 3 nodes of type2 + $type1 = $this->drupalCreateContentType(); + $type2 = $this->drupalCreateContentType(); + + $node_1 = array( + 'type' => $type1->type, + ); + // Nids from 1 to 4. + $this->drupalCreateNode($node_1); + $this->drupalCreateNode($node_1); + $this->drupalCreateNode($node_1); + $this->drupalCreateNode($node_1); + $node_2 = array( + 'type' => $type2->type, + ); + // Nids from 5 to 7. + $this->drupalCreateNode($node_2); + $this->drupalCreateNode($node_2); + $this->drupalCreateNode($node_2); + + $view = $this->viewsGroupByViewHelper($group_by); + $output = $view->execute_display(); + + $this->assertEqual(count($view->result), 2, 'Make sure the count of items is right.'); + + $results = array(); + // There's no need for a function in order to have aggregation. + if (empty($group_by)) { + $types = array($type1->type, $type2->type); + $results = array_map(function ($item) { return $item->node_type; }, $view->result); + sort($types); + sort($results); + $this->assertIdentical($results, $types); + // Exit here with no aggregation function. + return; + } + + // Group by nodetype to identify the right count. + foreach ($view->result as $item) { + $results[$item->node_type] = $item->nid; + } + $this->assertEqual($results[$type1->type], $values[0]); + $this->assertEqual($results[$type2->type], $values[1]); + } + + function viewsGroupByViewHelper($group_by = NULL) { + $view = new view; + $view->name = 'group_by_count'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 2; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['group_by'] = TRUE; + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'some'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + + // The test view has 2 fields ('nid' and 'type'). Don't add 'nid' when + // having no aggregation function. We just want to aggregate on node type. + if (!empty($group_by)) { + /* Field: Content: Nid */ + $handler->display->display_options['fields']['nid']['id'] = 'nid'; + $handler->display->display_options['fields']['nid']['table'] = 'node'; + $handler->display->display_options['fields']['nid']['field'] = 'nid'; + $handler->display->display_options['fields']['nid']['group_type'] = $group_by; + $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['nid']['alter']['trim'] = 0; + $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1; + $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1; + $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['nid']['alter']['html'] = 0; + $handler->display->display_options['fields']['nid']['hide_empty'] = 0; + $handler->display->display_options['fields']['nid']['empty_zero'] = 0; + $handler->display->display_options['fields']['nid']['link_to_node'] = 0; + } + + /* Field: Content: Type */ + $handler->display->display_options['fields']['type']['id'] = 'type'; + $handler->display->display_options['fields']['type']['table'] = 'node'; + $handler->display->display_options['fields']['type']['field'] = 'type'; + $handler->display->display_options['fields']['type']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['type']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['type']['alter']['trim'] = 0; + $handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1; + $handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1; + $handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['type']['alter']['html'] = 0; + $handler->display->display_options['fields']['type']['hide_empty'] = 0; + $handler->display->display_options['fields']['type']['empty_zero'] = 0; + $handler->display->display_options['fields']['type']['link_to_node'] = 0; + + return $view; + } + + public function testGroupByCount() { + $this->GroupByTestHelper('count', array(4, 3)); + } + + function testGroupBySum() { + $this->GroupByTestHelper('sum', array(10, 18)); + } + + + function testGroupByAverage() { + $this->GroupByTestHelper('avg', array(2.5, 6)); + } + + function testGroupByMin() { + $this->GroupByTestHelper('min', array(1, 5)); + } + + function testGroupByMax() { + $this->GroupByTestHelper('max', array(4, 7)); + } + + function testGroupByNone() { + $this->GroupByTestHelper(); + } + + public function testGroupByCountOnlyFilters() { + // Check if GROUP BY and HAVING are included when a view + // Doesn't display SUM, COUNT, MAX... functions in SELECT statment + + $type1 = $this->drupalCreateContentType(); + + $node_1 = array( + 'type' => $type1->type, + ); + for ($x = 0; $x < 10; $x++) { + $this->drupalCreateNode($node_1); + } + + $view = $this->viewsGroupByCountViewOnlyFilters(); + $output = $view->execute_display(); + + $this->assertTrue(strpos($view->build_info['query'], 'GROUP BY'), t('Make sure that GROUP BY is in the query')); + $this->assertTrue(strpos($view->build_info['query'], 'HAVING'), t('Make sure that HAVING is in the query')); + } + + function viewsGroupByCountViewOnlyFilters() { + $view = new view; + $view->name = 'group_by_in_filters'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 2; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['group_by'] = TRUE; + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'some'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Field: Nodo: Tipo */ + $handler->display->display_options['fields']['type']['id'] = 'type'; + $handler->display->display_options['fields']['type']['table'] = 'node'; + $handler->display->display_options['fields']['type']['field'] = 'type'; + $handler->display->display_options['fields']['type']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['type']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['type']['alter']['trim'] = 0; + $handler->display->display_options['fields']['type']['alter']['word_boundary'] = 1; + $handler->display->display_options['fields']['type']['alter']['ellipsis'] = 1; + $handler->display->display_options['fields']['type']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['type']['alter']['html'] = 0; + $handler->display->display_options['fields']['type']['hide_empty'] = 0; + $handler->display->display_options['fields']['type']['empty_zero'] = 0; + $handler->display->display_options['fields']['type']['link_to_node'] = 0; + /* Filtrar: Nodo: Nid */ + $handler->display->display_options['filters']['nid']['id'] = 'nid'; + $handler->display->display_options['filters']['nid']['table'] = 'node'; + $handler->display->display_options['filters']['nid']['field'] = 'nid'; + $handler->display->display_options['filters']['nid']['group_type'] = 'count'; + $handler->display->display_options['filters']['nid']['operator'] = '>'; + $handler->display->display_options['filters']['nid']['value']['value'] = '3'; + + return $view; + } +} + +/** + * Tests UI of aggregate functionality.. + */ +class viewsUiGroupbyTestCase extends DrupalWebTestCase { + function setUp() { + // Enable views_ui. + parent::setUp('views_ui', 'views_test'); + + // Create and log in a user with administer views permission. + $views_admin = $this->drupalCreateUser(array('administer views', 'administer blocks', 'bypass node access', 'access user profiles', 'view revisions')); + $this->drupalLogin($views_admin); + } + + public static function getInfo() { + return array( + 'name' => 'Groupby UI', + 'description' => 'Tests UI of aggregate functionality.', + 'group' => 'Views UI', + ); + } + + /** + * Tests whether basic saving works. + * + * @todo: this should check the change of the settings as well. + */ + function testGroupBySave() { + $this->drupalGet('admin/structure/views/view/test_views_groupby_save/edit'); + + $edit = array( + 'group_by' => TRUE, + ); + $this->drupalPost('admin/structure/views/nojs/display/test_views_groupby_save/default/group_by', $edit, t('Apply')); + + $this->drupalGet('admin/structure/views/view/test_views_groupby_save/edit'); + $this->drupalPost('admin/structure/views/view/test_views_groupby_save/edit', array(), t('Save')); + + $this->drupalGet('admin/structure/views/nojs/display/test_views_groupby_save/default/group_by'); + } +} diff --git a/sites/all/modules/contrib/views/tests/views_handlers.test b/sites/all/modules/contrib/views/tests/views_handlers.test new file mode 100644 index 0000000..d54a7df --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_handlers.test @@ -0,0 +1,150 @@ + 'Handlers test', + 'description' => 'test abstract handler definitions', + 'group' => 'Views', + ); + } + + protected function setUp() { + parent::setUp('views', 'views_ui'); + module_enable(array('views_ui')); + } + + function testFilterInOperatorUi() { + $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration')); + $this->drupalLogin($admin_user); + menu_rebuild(); + + $path = 'admin/structure/views/nojs/config-item/test_filter_in_operator_ui/default/filter/type'; + $this->drupalGet($path); + $this->assertFieldByName('options[expose][reduce]', FALSE); + + $edit = array( + 'options[expose][reduce]' => TRUE, + ); + $this->drupalPost($path, $edit, t('Apply')); + $this->drupalGet($path); + $this->assertFieldByName('options[expose][reduce]', TRUE); + } + + /** + * Tests views_break_phrase_string function. + */ + function test_views_break_phrase_string() { + $empty_stdclass = new stdClass(); + $empty_stdclass->operator = 'or'; + $empty_stdclass->value = array(); + + $null = NULL; + // check defaults + $this->assertEqual($empty_stdclass, views_break_phrase_string('', $null)); + + $handler = views_get_handler('node', 'title', 'argument'); + $this->assertEqual($handler, views_break_phrase_string('', $handler)); + + // test ors + $handler = views_break_phrase_string('word1 word2+word'); + $this->assertEqualValue(array('word1', 'word2', 'word'), $handler); + $this->assertEqual('or', $handler->operator); + $handler = views_break_phrase_string('word1+word2+word'); + $this->assertEqualValue(array('word1', 'word2', 'word'), $handler); + $this->assertEqual('or', $handler->operator); + $handler = views_break_phrase_string('word1 word2 word'); + $this->assertEqualValue(array('word1', 'word2', 'word'), $handler); + $this->assertEqual('or', $handler->operator); + $handler = views_break_phrase_string('word-1+word-2+word'); + $this->assertEqualValue(array('word-1', 'word-2', 'word'), $handler); + $this->assertEqual('or', $handler->operator); + $handler = views_break_phrase_string('wõrd1+wõrd2+wõrd'); + $this->assertEqualValue(array('wõrd1', 'wõrd2', 'wõrd'), $handler); + $this->assertEqual('or', $handler->operator); + + // test ands. + $handler = views_break_phrase_string('word1,word2,word'); + $this->assertEqualValue(array('word1', 'word2', 'word'), $handler); + $this->assertEqual('and', $handler->operator); + $handler = views_break_phrase_string('word1 word2,word'); + $this->assertEqualValue(array('word1 word2', 'word'), $handler); + $this->assertEqual('and', $handler->operator); + $handler = views_break_phrase_string('word1,word2 word'); + $this->assertEqualValue(array('word1', 'word2 word'), $handler); + $this->assertEqual('and', $handler->operator); + $handler = views_break_phrase_string('word-1,word-2,word'); + $this->assertEqualValue(array('word-1', 'word-2', 'word'), $handler); + $this->assertEqual('and', $handler->operator); + $handler = views_break_phrase_string('wõrd1,wõrd2,wõrd'); + $this->assertEqualValue(array('wõrd1', 'wõrd2', 'wõrd'), $handler); + $this->assertEqual('and', $handler->operator); + + // test a single word + $handler = views_break_phrase_string('word'); + $this->assertEqualValue(array('word'), $handler); + $this->assertEqual('and', $handler->operator); + } + + /** + * Tests views_break_phrase function. + */ + function test_views_break_phrase() { + $empty_stdclass = new stdClass(); + $empty_stdclass->operator = 'or'; + $empty_stdclass->value = array(); + + $null = NULL; + // check defaults + $this->assertEqual($empty_stdclass, views_break_phrase('', $null)); + + $handler = views_get_handler('node', 'title', 'argument'); + $this->assertEqual($handler, views_break_phrase('', $handler)); + + // Generate three random numbers which can be used below; + $n1 = rand(0, 100); + $n2 = rand(0, 100); + $n3 = rand(0, 100); + // test ors + $this->assertEqualValue(array($n1, $n2, $n3), views_break_phrase("$n1 $n2+$n3", $handler)); + $this->assertEqual('or', $handler->operator); + $this->assertEqualValue(array($n1, $n2, $n3), views_break_phrase("$n1+$n2+$n3", $handler)); + $this->assertEqual('or', $handler->operator); + $this->assertEqualValue(array($n1, $n2, $n3), views_break_phrase("$n1 $n2 $n3", $handler)); + $this->assertEqual('or', $handler->operator); + $this->assertEqualValue(array($n1, $n2, $n3), views_break_phrase("$n1 $n2++$n3", $handler)); + $this->assertEqual('or', $handler->operator); + + // test ands. + $this->assertEqualValue(array($n1, $n2, $n3), views_break_phrase("$n1,$n2,$n3", $handler)); + $this->assertEqual('and', $handler->operator); + $this->assertEqualValue(array($n1, $n2, $n3), views_break_phrase("$n1,,$n2,$n3", $handler)); + $this->assertEqual('and', $handler->operator); + } + + /** + * Check to see if two values are equal. + * + * @param $first + * The first value to check. + * @param views_handler $handler + * @param string $message + * The message to display along with the assertion. + * @param string $group + * The type of assertion - examples are "Browser", "PHP". + * + * @return bool + * TRUE if the assertion succeeded, FALSE otherwise. + */ + protected function assertEqualValue($first, $handler, $message = '', $group = 'Other') { + return $this->assert($first == $handler->value, $message ? $message : t('First value is equal to second value'), $group); + } +} diff --git a/sites/all/modules/contrib/views/tests/views_module.test b/sites/all/modules/contrib/views/tests/views_module.test new file mode 100644 index 0000000..e160964 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_module.test @@ -0,0 +1,241 @@ + 'Tests views.module', + 'description' => 'Tests some basic functions of views.module', + 'group' => 'Views', + ); + } + + public function setUp() { + parent::setUp(); + drupal_theme_rebuild(); + } + + public function viewsData() { + $data = parent::viewsData(); + $data['views_test_previous'] = array(); + $data['views_test_previous']['id']['field']['moved to'] = array('views_test', 'id'); + $data['views_test_previous']['id']['filter']['moved to'] = array('views_test', 'id'); + $data['views_test']['age_previous']['field']['moved to'] = array('views_test', 'age'); + $data['views_test']['age_previous']['sort']['moved to'] = array('views_test', 'age'); + $data['views_test_previous']['name_previous']['field']['moved to'] = array('views_test', 'name'); + $data['views_test_previous']['name_previous']['argument']['moved to'] = array('views_test', 'name'); + + return $data; + } + + public function test_views_trim_text() { + // Test unicode, @see http://drupal.org/node/513396#comment-2839416 + $text = array( + 'Tuy nhiên, những hi vọng', + 'Giả sử chúng tôi có 3 Apple', + 'siêu nhỏ này là bộ xử lý', + 'Di động của nhà sản xuất Phần Lan', + 'khoảng cách từ đại lí đến', + 'của hãng bao gồm ba dòng', + 'сд асд асд ас', + 'асд асд асд ас' + ); + // Just test maxlength without word boundry. + $alter = array( + 'max_length' => 10, + ); + $expect = array( + 'Tuy nhiên,', + 'Giả sử chú', + 'siêu nhỏ n', + 'Di động củ', + 'khoảng các', + 'của hãng b', + 'сд асд асд', + 'асд асд ас', + ); + + foreach ($text as $key => $line) { + $result_text = views_trim_text($alter, $line); + $this->assertEqual($result_text, $expect[$key]); + } + + // Test also word_boundary + $alter['word_boundary'] = TRUE; + $expect = array( + 'Tuy nhiên', + 'Giả sử', + 'siêu nhỏ', + 'Di động', + 'khoảng', + 'của hãng', + 'сд асд', + 'асд асд', + ); + + foreach ($text as $key => $line) { + $result_text = views_trim_text($alter, $line); + $this->assertEqual($result_text, $expect[$key]); + } + } + + /** + * Tests the dynamic includes of templates via module feature. + */ + function testModuleTemplates() { + $views_status = variable_get('views_defaults', array()); + $views_status['frontpage'] = FALSE; // false is enabled + variable_set('views_defaults', $views_status); + + $existing = array(); + $type = array(); + $theme = array(); + $path = array(); + $registry = views_theme($existing, $type, $theme, $path); + $this->assertTrue(isset($registry['views_view__frontpage'])); + } + + /** + * Tests the views_get_handler method. + */ + function testviews_get_handler() { + $types = array('field', 'area', 'filter'); + foreach ($types as $type) { + $handler = views_get_handler($this->randomName(), $this->randomName(), $type); + $this->assertEqual('views_handler_' . $type . '_broken', get_class($handler), t('Make sure that a broken handler of type: @type are created', array('@type' => $type))); + } + + $views_data = $this->viewsData(); + $test_tables = array('views_test' => array('id', 'name')); + foreach ($test_tables as $table => $fields) { + foreach ($fields as $field) { + $data = $views_data[$table][$field]; + foreach ($data as $id => $field_data) { + if (!in_array($id, array('title', 'help'))) { + $handler = views_get_handler($table, $field, $id); + $this->assertInstanceHandler($handler, $table, $field, $id); + } + } + } + } + + // Test the automatic conversion feature. + + // Test the automatic table renaming. + $handler = views_get_handler('views_test_previous', 'id', 'field'); + $this->assertInstanceHandler($handler, 'views_test', 'id', 'field'); + $handler = views_get_handler('views_test_previous', 'id', 'filter'); + $this->assertInstanceHandler($handler, 'views_test', 'id', 'filter'); + + // Test the automatic field renaming. + $handler = views_get_handler('views_test', 'age_previous', 'field'); + $this->assertInstanceHandler($handler, 'views_test', 'age', 'field'); + $handler = views_get_handler('views_test', 'age_previous', 'sort'); + $this->assertInstanceHandler($handler, 'views_test', 'age', 'sort'); + + // Test the automatic table and field renaming. + $handler = views_get_handler('views_test_previous', 'name_previous', 'field'); + $this->assertInstanceHandler($handler, 'views_test', 'name', 'field'); + $handler = views_get_handler('views_test_previous', 'name_previous', 'argument'); + $this->assertInstanceHandler($handler, 'views_test', 'name', 'argument'); + + // Test the override handler feature. + $handler = views_get_handler('views_test', 'job', 'filter', 'views_handler_filter'); + $this->assertEqual('views_handler_filter', get_class($handler)); + } + + /** + * Tests views_fetch_data(). + */ + function testFetchData() { + + // Make sure we start with a empty cache. + $this->resetStaticViewsDataCache(); + cache_clear_all('*', 'cache_views', TRUE); + variable_set('views_test_views_data_count', 0); + + // Request info about an existing table. + $this->assertTrue(views_fetch_data('views_test'), 'Data about existing table returned'); + // This should have triggered a views data rebuild, and written a cache + // entry for all tables and the requested table but no other tables. + $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once'); + $this->assertTrue(cache_get('views_data:en', 'cache_views'), 'Cache for all tables written.'); + $this->assertTrue(cache_get('views_data:views_test:en', 'cache_views'), 'Cache for requested table written.'); + $this->assertFalse(cache_get('views_data:views_test_previous:en', 'cache_views'), 'No Cache written for not requested table.'); + $this->assertTrue(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is fully loaded'); + + $this->resetStaticViewsDataCache(); + + // Request the same table again. + $this->assertTrue(views_fetch_data('views_test'), 'Data about existing table returned'); + $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once'); + $this->assertFalse(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is not fully loaded'); + + $this->resetStaticViewsDataCache(); + + // Request a missing table, this should load the full cache from cache but + // not rebuilt. + $this->assertFalse(views_fetch_data('views_test_missing'), 'No data about missing table returned'); + $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once'); + $this->assertTrue(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is fully loaded'); + + $this->resetStaticViewsDataCache(); + + // Request the same empty table again, this should load only that (empty) + // cache for that table. + $this->assertFalse(views_fetch_data('views_test_missing'), 'No data about missing table returned'); + $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once'); + $this->assertFalse(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is not fully loaded'); + + + // Test if the cache consistency is ensured. There was an issue where + // calling _views_fetch_data() first with a table would prevent the function + // from properly rebuilt a missing the general cache entry. + // See https://www.drupal.org/node/2475669 for details. + // Make sure we start with a empty cache. + $this->resetStaticViewsDataCache(); + cache_clear_all('*', 'cache_views', TRUE); + + // Prime the static cache of _views_fetch_data() by calling it with a table + // first. + views_fetch_data('views_test'); + // Now remove the general cache. + cache_clear_all('views_data:en', 'cache_views'); + // Reset the static cache to see if fetches from the persistent cache + // properly rebuild the static cache. + $this->resetStaticViewsDataCache(); + // Prime the static cache of _views_fetch_data() by calling it with a table + // first. + views_fetch_data('views_test'); + // Fetch the general cache, which was deleted, an see if it is rebuild + // properly. + views_fetch_data(); + $this->assertTrue(cache_get('views_data:en', 'cache_views'), 'Cache for all tables was properly rebuild.'); + } + + /** + * Ensure that a certain handler is a instance of a certain table/field. + */ + function assertInstanceHandler($handler, $table, $field, $id) { + $table_data = views_fetch_data($table); + $field_data = $table_data[$field][$id]; + + $this->assertEqual($field_data['handler'], get_class($handler)); + } + + /** + * Resets the views data cache. + */ + protected function resetStaticViewsDataCache() { + drupal_static_reset('_views_fetch_data_cache'); + drupal_static_reset('_views_fetch_data_recursion_protected'); + drupal_static_reset('_views_fetch_data_fully_loaded'); + } +} diff --git a/sites/all/modules/contrib/views/tests/views_pager.test b/sites/all/modules/contrib/views/tests/views_pager.test new file mode 100644 index 0000000..fef4915 --- /dev/null +++ b/sites/all/modules/contrib/views/tests/views_pager.test @@ -0,0 +1,496 @@ + 'Pager', + 'description' => 'Test the pluggable pager system', + 'group' => 'Views Plugins', + ); + } + + public function setUp() { + parent::setUp('views', 'views_ui', 'views_test'); + } + + /** + * Pagers was sometimes not stored. + * + * @see http://drupal.org/node/652712 + */ + public function testStorePagerSettings() { + $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration')); + $this->drupalLogin($admin_user); + // Test behaviour described in http://drupal.org/node/652712#comment-2354918. + + $this->drupalGet('admin/structure/views/view/frontpage/edit'); + + + $edit = array( + 'pager_options[items_per_page]' => 20, + ); + $this->drupalPost('admin/structure/views/nojs/display/frontpage/default/pager_options', $edit, t('Apply')); + $this->assertText('20 items'); + + // Change type and check whether the type is new type is stored. + $edit = array(); + $edit = array( + 'pager[type]' => 'mini', + ); + $this->drupalPost('admin/structure/views/nojs/display/frontpage/default/pager', $edit, t('Apply')); + $this->drupalGet('admin/structure/views/view/frontpage/edit'); + $this->assertText('Mini', 'Changed pager plugin, should change some text'); + + // Test behaviour described in http://drupal.org/node/652712#comment-2354400 + $view = $this->viewsStorePagerSettings(); + // Make it editable in the admin interface. + $view->save(); + + $this->drupalGet('admin/structure/views/view/test_store_pager_settings/edit'); + + $edit = array(); + $edit = array( + 'pager[type]' => 'full', + ); + $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/default/pager', $edit, t('Apply')); + $this->drupalGet('admin/structure/views/view/test_store_pager_settings/edit'); + $this->assertText('Full'); + + $edit = array( + 'pager_options[items_per_page]' => 20, + ); + $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/default/pager_options', $edit, t('Apply')); + $this->assertText('20 items'); + + // add new display and test the settings again, by override it. + $edit = array( ); + // Add a display and override the pager settings. + $this->drupalPost('admin/structure/views/view/test_store_pager_settings/edit', $edit, t('Add Page')); + $edit = array( + 'override[dropdown]' => 'page_1', + ); + $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/page_1/pager', $edit, t('Apply')); + + $edit = array( + 'pager[type]' => 'mini', + ); + $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/page_1/pager', $edit, t('Apply')); + $this->drupalGet('admin/structure/views/view/test_store_pager_settings/edit'); + $this->assertText('Mini', 'Changed pager plugin, should change some text'); + + $edit = array( + 'pager_options[items_per_page]' => 10, + ); + $this->drupalPost('admin/structure/views/nojs/display/test_store_pager_settings/default/pager_options', $edit, t('Apply')); + $this->assertText('20 items'); + + } + + public function viewsStorePagerSettings() { + $view = new view; + $view->name = 'test_store_pager_settings'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 3; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'none'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'node'; + return $view; + } + + /** + * Tests the none-pager-query. + */ + public function testNoLimit() { + // Create 11 nodes and make sure that everyone is returned. + // We create 11 nodes, because the default pager plugin had 10 items per page. + for ($i = 0; $i < 11; $i++) { + $this->drupalCreateNode(); + } + $view = $this->viewsPagerNoLimit(); + $view->set_display('default'); + $this->executeView($view); + $this->assertEqual(count($view->result), 11, 'Make sure that every item is returned in the result'); + + $view->destroy(); + + // Setup and test a offset. + $view = $this->viewsPagerNoLimit(); + $view->set_display('default'); + + $pager = array( + 'type' => 'none', + 'options' => array( + 'offset' => 3, + ), + ); + $view->display_handler->set_option('pager', $pager); + $this->executeView($view); + + $this->assertEqual(count($view->result), 8, 'Make sure that every item beside the first three is returned in the result'); + + // Check some public functions. + $this->assertFalse($view->query->pager->use_pager()); + $this->assertFalse($view->query->pager->use_count_query()); + $this->assertEqual($view->query->pager->get_items_per_page(), 0); + } + + public function viewsPagerNoLimit() { + $view = new view; + $view->name = 'test_pager_none'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version =3; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'none'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'node'; + return $view; + } + + public function testViewTotalRowsWithoutPager() { + $this->createNodes(23); + + $view = $this->viewsPagerNoLimit(); + $view->get_total_rows = TRUE; + $view->set_display('default'); + $this->executeView($view); + + $this->assertEqual($view->total_rows, 23, "'total_rows' is calculated when pager type is 'none' and 'get_total_rows' is TRUE."); + } + + public function createNodes($count) { + if ($count >= 0) { + for ($i = 0; $i < $count; $i++) { + $this->drupalCreateNode(); + } + } + } + + /** + * Tests the some pager plugin. + */ + public function testLimit() { + // Create 11 nodes and make sure that everyone is returned. + // We create 11 nodes, because the default pager plugin had 10 items per page. + for ($i = 0; $i < 11; $i++) { + $this->drupalCreateNode(); + } + $view = $this->viewsPagerLimit(); + $view->set_display('default'); + $this->executeView($view); + $this->assertEqual(count($view->result), 5, 'Make sure that only a certain count of items is returned'); + $view->destroy(); + + // Setup and test a offset. + $view = $this->viewsPagerLimit(); + $view->set_display('default'); + + $pager = array( + 'type' => 'none', + 'options' => array( + 'offset' => 8, + 'items_per_page' => 5, + ), + ); + $view->display_handler->set_option('pager', $pager); + $this->executeView($view); + $this->assertEqual(count($view->result), 3, 'Make sure that only a certain count of items is returned'); + + // Check some public functions. + $this->assertFalse($view->query->pager->use_pager()); + $this->assertFalse($view->query->pager->use_count_query()); + } + + public function viewsPagerLimit() { + $view = new view; + $view->name = 'test_pager_some'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 3; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'some'; + $handler->display->display_options['pager']['options']['offset'] = 0; + $handler->display->display_options['pager']['options']['items_per_page'] = 5; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'node'; + return $view; + } + + /** + * Tests the normal pager. + */ + public function testNormalPager() { + // Create 11 nodes and make sure that everyone is returned. + // We create 11 nodes, because the default pager plugin had 10 items per page. + for ($i = 0; $i < 11; $i++) { + $this->drupalCreateNode(); + } + $view = $this->viewsPagerFull(); + $view->set_display('default'); + $this->executeView($view); + $this->assertEqual(count($view->result), 5, 'Make sure that only a certain count of items is returned'); + $view->destroy(); + + // Setup and test a offset. + $view = $this->viewsPagerFull(); + $view->set_display('default'); + + $pager = array( + 'type' => 'full', + 'options' => array( + 'offset' => 8, + 'items_per_page' => 5, + ), + ); + $view->display_handler->set_option('pager', $pager); + $this->executeView($view); + $this->assertEqual(count($view->result), 3, 'Make sure that only a certain count of items is returned'); + + // Test items per page = 0 + $view = $this->viewPagerFullZeroItemsPerPage(); + $view->set_display('default'); + $this->executeView($view); + + $this->assertEqual(count($view->result), 11, 'All items are return'); + + // TODO test number of pages. + + // Test items per page = 0. + $view->destroy(); + + // Setup and test a offset. + $view = $this->viewsPagerFull(); + $view->set_display('default'); + + $pager = array( + 'type' => 'full', + 'options' => array( + 'offset' => 0, + 'items_per_page' => 0, + ), + ); + + $view->display_handler->set_option('pager', $pager); + $this->executeView($view); + $this->assertEqual($view->query->pager->get_items_per_page(), 0); + $this->assertEqual(count($view->result), 11); + } + + function viewPagerFullZeroItemsPerPage() { + $view = new view; + $view->name = 'view_pager_full_zero_items_per_page'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 3; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['pager']['options']['items_per_page'] = '0'; + $handler->display->display_options['pager']['options']['offset'] = '0'; + $handler->display->display_options['pager']['options']['id'] = '0'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + /* Field: Content: Title */ + $handler->display->display_options['fields']['title']['id'] = 'title'; + $handler->display->display_options['fields']['title']['table'] = 'node'; + $handler->display->display_options['fields']['title']['field'] = 'title'; + $handler->display->display_options['fields']['title']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['title']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['title']['alter']['trim'] = 0; + $handler->display->display_options['fields']['title']['alter']['word_boundary'] = 1; + $handler->display->display_options['fields']['title']['alter']['ellipsis'] = 1; + $handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['title']['alter']['html'] = 0; + $handler->display->display_options['fields']['title']['hide_empty'] = 0; + $handler->display->display_options['fields']['title']['empty_zero'] = 0; + $handler->display->display_options['fields']['title']['link_to_node'] = 0; + + return $view; + } + + function viewsPagerFull() { + $view = new view; + $view->name = 'test_pager_full'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 3; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['pager']['options']['items_per_page'] = '5'; + $handler->display->display_options['pager']['options']['offset'] = '0'; + $handler->display->display_options['pager']['options']['id'] = '0'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'node'; + + return $view; + } + + function viewsPagerFullFields() { + $view = new view; + $view->name = 'test_pager_full'; + $view->description = ''; + $view->tag = ''; + $view->view_php = ''; + $view->base_table = 'node'; + $view->is_cacheable = FALSE; + $view->api_version = 3; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'full'; + $handler->display->display_options['pager']['options']['items_per_page'] = '5'; + $handler->display->display_options['pager']['options']['offset'] = '0'; + $handler->display->display_options['pager']['options']['id'] = '0'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + $handler->display->display_options['fields']['title']['id'] = 'title'; + $handler->display->display_options['fields']['title']['table'] = 'node'; + $handler->display->display_options['fields']['title']['field'] = 'title'; + $handler->display->display_options['fields']['title']['alter']['alter_text'] = 0; + $handler->display->display_options['fields']['title']['alter']['make_link'] = 0; + $handler->display->display_options['fields']['title']['alter']['trim'] = 0; + $handler->display->display_options['fields']['title']['alter']['word_boundary'] = 1; + $handler->display->display_options['fields']['title']['alter']['ellipsis'] = 1; + $handler->display->display_options['fields']['title']['alter']['strip_tags'] = 0; + $handler->display->display_options['fields']['title']['alter']['html'] = 0; + $handler->display->display_options['fields']['title']['hide_empty'] = 0; + $handler->display->display_options['fields']['title']['empty_zero'] = 0; + $handler->display->display_options['fields']['title']['link_to_node'] = 0; + return $view; + } + + /** + * Tests the minipager. + */ + public function testMiniPager() { + // the functionality is the same as the normal pager, so i don't know what to test here. + } + + /** + * Tests rendering with NULL pager. + */ + public function testRenderNullPager() { + // Create 11 nodes and make sure that everyone is returned. + // We create 11 nodes, because the default pager plugin had 10 items per page. + for ($i = 0; $i < 11; $i++) { + $this->drupalCreateNode(); + } + $view = $this->viewsPagerFullFields(); + $view->set_display('default'); + $this->executeView($view); + $view->use_ajax = TRUE; // force the value again here + $view->query->pager = NULL; + $output = $view->render(); + $this->assertEqual(preg_match('/