/[drupal]/contributions/modules/ubercart/uc_repeater/uc_repeater.module
ViewVC logotype

Contents of /contributions/modules/ubercart/uc_repeater/uc_repeater.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Thu Jul 10 12:41:04 2008 UTC (16 months, 2 weeks ago) by islandusurper
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--2
Changes since 1.4: +37 -36 lines
File MIME type: text/x-php
Begin the Ubercart 6.x-2.x branch.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Allows a multisite setup to share changes to the catalog.
7 */
8
9 function uc_repeater_menu($may_cache) {
10 $items = array();
11
12 if ($may_cache) {
13 $items[] = array('path' => 'admin/store/settings/repeater',
14 'title' => t('Repeater settings'),
15 'access' => user_access('administer catalog repeater'),
16 'callback' => 'uc_repeater_settings_overview',
17 'type' => MENU_NORMAL_ITEM,
18 );
19 $items[] = array('path' => 'admin/store/settings/repeater/overview',
20 'title' => t('Overview'),
21 'type' => MENU_DEFAULT_LOCAL_TASK,
22 'weight' => -10,
23 );
24 $items[] = array('path' => 'admin/store/settings/repeater/edit',
25 'title' => t('Edit'),
26 'callback' => 'drupal_get_form',
27 'callback arguments' => array('uc_repeater_admin_settings'),
28 'type' => MENU_LOCAL_TASK,
29 );
30 }
31 else {
32 $items[] = array('path' => 'repeater/import',
33 'access' => TRUE,
34 'callback' => 'uc_repeater_import',
35 'type' => MENU_CALLBACK,
36 );
37 }
38
39 return $items;
40 }
41
42 function uc_repeater_perm() {
43 return array('administer catalog repeater');
44 }
45
46 function uc_repeater_form_alter($form_id, &$form) {
47 $node = $form['#node'];
48 if (is_object($node) && $form_id == $node->type .'_node_form' && in_array($node->type, module_invoke_all('product_types'))) {
49 $form['uc_repeater_export'] = array('#type' => 'checkbox',
50 '#title' => t('Export to other sites'),
51 '#default_value' => 0,
52 );
53 }
54 }
55
56 function uc_repeater_nodeapi(&$node, $op, $arg3 = null, $arg4 = null) {
57 if (in_array($node->type, module_invoke_all('product_types'))) {
58 switch ($op) {
59 case 'insert':
60 case 'update':
61 if ($node->uc_repeater_export) {
62 // Reset cached nodes to send the new node content.
63 node_load(0, null, true);
64 uc_repeater_export($node->nid);
65 }
66 break;
67 }
68 }
69 }
70
71 function uc_repeater_settings_overview() {
72 $slaves = variable_get('uc_repeater_slaves', array());
73 $masters = variable_get('uc_repeater_masters', array());
74 $sections = array(
75 array(
76 'edit' => 'admin/store/settings/repeater/edit',
77 'title' => t('Slave sites'),
78 'items' => (count($slaves) ? $slaves : array(t('None'))),
79 ),
80 array(
81 'edit' => 'admin/store/settings/repeater/edit',
82 'title' => t('Master sites'),
83 'items' => (count($masters) ? $masters : array(t('None'))),
84 ),
85 );
86 return theme('uc_settings_overview', $sections);
87 }
88
89 function uc_repeater_admin_settings() {
90 $form = array(
91 '#description' => t('In each field enter the host name and path to the Drupal installation of the sites that interact with this site. Do not include a trailing slash. E.g.: www.othersite.com<br />copy.yoursite.net/store<br />etc.'),
92 );
93
94 $form['uc_repeater_slaves'] = array('#type' => 'textarea',
95 '#title' => t('Slave sites'),
96 '#default_value' => implode("\n", variable_get('uc_repeater_slaves', array())),
97 '#description' => t('These sites will receive catalog changes from this site. They must recognize this site as a master (see below).'),
98 );
99 $form['uc_repeater_masters'] = array('#type' => 'textarea',
100 '#title' => t('Master sites'),
101 '#default_value' => implode("\n", variable_get('uc_repeater_masters', array())),
102 '#description' => t('These sites send catalog changes to this site.'),
103 );
104
105 $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration') );
106 $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults') );
107
108 if (!empty($_POST) && form_get_errors()) {
109 drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
110 }
111
112 return $form;
113 }
114
115 function uc_repeater_admin_settings_submit($form_id, $form_values) {
116 if ($form_values['op'] == t('Reset to defaults')) {
117 variable_del('uc_repeater_slaves');
118 variable_del('uc_repeater_masters');
119 drupal_set_message(t('The configuration options have been reset to their default values.'));
120 }
121 else {
122 foreach (array('uc_repeater_slaves', 'uc_repeater_masters') as $var) {
123 $values = array_filter(explode("\n", trim($form_values[$var])));
124 foreach ($values as $key => $input) {
125 $url = explode(' ', trim($input), 2);
126 if ($url[0]) {
127 $site = parse_url($url[0]);
128 $values[$key] = $site['host'] . (isset($site['port']) ? ':'. $site['port'] : '') . $site['path'];
129 }
130 }
131 variable_set($var, $values);
132 }
133 drupal_set_message(t('The configuration options have been saved.'));
134 }
135 }
136
137 /**
138 * Pushes new products to other sites.
139 */
140 function uc_repeater_export($nids = array()) {
141 if (count($nids)) {
142 if (count($nids) == 1) {
143 // Most awesome use of casting ever: wrap in an array if not an array.
144 $nids = (array)$nids;
145 }
146 //drupal_set_message('<pre>'. print_r($nids, true) .'</pre>');
147 $xml = uc_importer_export($nids);
148 //drupal_set_message('<pre>'. htmlentities($xml) .'</pre>');
149 if ($xml) {
150 $responses = array();
151 foreach (variable_get('uc_repeater_slaves', array()) as $site) {
152 $responses[$site] = drupal_http_request('http://'. $site .'/repeater/import/'. md5($_SERVER['SERVER_NAME'] . substr(base_path(), 0, -1)) .'/'. md5($xml), array('Content-Type' => 'text/xml'), 'POST', $xml);
153 //watchdog('uc_repeater', htmlentities($xml) ."<br/>$site:<pre>". print_r($responses[$site], true) ."</pre>", WATCHDOG_NOTICE);
154 }
155 }
156 }
157 }
158
159 function uc_repeater_import($site_hash = null, $xml_hash = null) {
160 $valid_site = false;
161 foreach (variable_get('uc_repeater_masters', array()) as $site) {
162 if (md5($site) == $site_hash) {
163 $valid_site = true;
164 }
165 }
166
167 if ($valid_site) {
168 $xml = file_get_contents('php://input');
169 }
170 else {
171 die("Yeah, that's not a good way to get me to do things.");
172 }
173 if (!$xml) {
174 die('Needs must have POST data. And as a hint, it should be XML.');
175 }
176 else if (md5($xml) != $xml_hash) {
177 die("You really don't know how this site works, do you?");
178 }
179 /* $dom = new DOMDocument();
180 $dom->loadXML($xml);
181 if (!$dom->schemaValidate('http://www.ubercart.org/files/store.xsd')) {
182 die("Did not validate against the schema.<br /><pre>". htmlentities($xml) .'</pre>');
183 } */
184 uc_importer_import($xml);
185 die("Maybe a successful import. :P");
186 }

  ViewVC Help
Powered by ViewVC 1.1.2