| 1 |
<?php |
<?php |
| 2 |
// $Id: og_collections.module,v 1.2.2.7 2008/04/05 20:30:34 sdboyer Exp $ |
// $Id: og_collections.module,v 1.2.2.8 2008/04/06 00:08:09 sdboyer Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file og_collections.module |
* @file og_collections.module |
| 15 |
class og_collection { |
class og_collection { |
| 16 |
public $version, $grouptype; |
public $version, $grouptype; |
| 17 |
private $modified; |
private $modified; |
| 18 |
// make usedefault private? |
// will $modified help? |
| 19 |
|
|
| 20 |
// saves changes to the default collection |
// saves changes to the default collection |
| 21 |
function __construct($grouptype, $pcpid = NULL) { |
function __construct() { |
| 22 |
if (is_null($pcpid)) { |
$this->grouptype = 'default'; |
| 23 |
$this->og_collection_load_all($grouptype); |
$result = db_fetch_array(db_query("SELECT * FROM {og_collections} WHERE grouptype = 'default'")); |
| 24 |
|
$this->version = $result['version']; |
| 25 |
|
$pcpanel_data = array_filter($result, !in_array('grouptype', 'version', 'defaults')); |
| 26 |
|
if (!empty($pcpanel_data)) { |
| 27 |
|
$this->load($pcpanel_data); |
| 28 |
} |
} |
| 29 |
else { |
else { |
| 30 |
$this->og_collection_load($grouptype, $pcpid); |
throw new og_collections_exception('nopcpanel'); |
| 31 |
} |
} |
| 32 |
} |
} |
| 33 |
|
|
| 34 |
// reduce to single, using private variable and sleep function. |
// reduce to single, using private variable and sleep function. |
| 35 |
function og_collection_save_all() { // @TODO add some verification? |
function save_all() { // @TODO add some verification? |
| 36 |
foreach ($this as $member => $value) { |
foreach ($this as $member => $value) { |
| 37 |
if ($member == ('version' || 'grouptype')) { |
if ($member == ('version' || 'grouptype')) { |
| 38 |
continue; |
continue; |
| 39 |
} |
} |
| 40 |
$this->og_collection_save($value); |
$this->save($value); |
| 41 |
} |
} |
| 42 |
} |
} |
| 43 |
|
|
| 44 |
// saves an individual collection |
// saves an individual collection |
| 45 |
function og_collection_save($pcpid) { |
function save($pcpid) { |
| 46 |
db_query("UPDATE {og_collections} SET pcpid_%d = '%s' WHERE grouptype = '%s'", $pcpid, serialize($this->$pcpid), $this->grouptype); |
db_query("UPDATE {og_collections} SET pcpid_%d = '%s' WHERE grouptype = '%s'", $pcpid, serialize($this->$pcpid), $this->grouptype); |
| 47 |
} |
} |
| 48 |
|
|
| 49 |
// loads the relevant pcpanel data into the default collection |
// loads the relevant pcpanel data into the default collection |
| 50 |
function og_collection_load_all($grouptype = 'default') { |
protected function load($pcpanel_data) { |
| 51 |
$result = db_fetch_array(db_query("SELECT * FROM {og_collections} WHERE grouptype = '%s'", $grouptype)); |
foreach ($pcpanel_data as $pcpname => $data) { |
| 52 |
foreach ($result as $field => $settings) { |
$this->$pcpname = new og_pcpanel(); |
| 53 |
if ($field == ('version' || 'grouptype')) { |
$settings = unserialize($data); |
| 54 |
continue; |
$this->$pcpname->og_pcpanel_load($settings['did']); |
|
} |
|
|
$pcpid = (int) substr($field, 6); |
|
|
unserialize($settings); |
|
|
$this->$pcpid = new og_pcpanel(); |
|
|
$this->$pcpid->og_pcpanel_load($settings['did']); |
|
| 55 |
foreach ($settings as $setting => $val) { |
foreach ($settings as $setting => $val) { |
| 56 |
$this->$pcpid->$setting = $val; |
$this->$pcpname->$setting = $val; |
| 57 |
} |
} |
| 58 |
} |
} |
| 59 |
} |
} |
| 60 |
|
|
| 61 |
function og_collection_load($grouptype, $pcpid) { |
public static function load_single($pcpname, $grouptype = 'default') { |
| 62 |
$result = unserialize(db_result(db_query("SELECT pcpid_%d FROM {og_collections} WHERE grouptype = '%s'", $pcpid, $grouptype))); |
return $result = unserialize(db_result(db_query("SELECT '%s' FROM {og_collections} WHERE grouptype = '%s'", $pcpname, $grouptype))); |
|
foreach ($result as $setting => $val) { |
|
|
$this->$pcpid->$setting = $val; |
|
|
} |
|
| 63 |
} |
} |
| 64 |
|
|
| 65 |
// performs the instantiation routine |
// performs the collection instantiation routine |
| 66 |
function og_collection_instantiate($nid) { |
function instantiate($nid) { |
| 67 |
|
|
| 68 |
} |
} |
| 69 |
|
|
| 70 |
// @TODO need to do this. This will need to record version data on not only the pcpanel's abstracted data, but the displays themselves... |
// @TODO need to do this. This will need to record version data on not only the pcpanel's abstracted data, but the displays themselves... |
| 71 |
function og_collection_new_version() { |
function new_version() { |
| 72 |
|
|
| 73 |
} |
} |
| 74 |
|
|
| 75 |
|
function __clone() { |
| 76 |
|
|
| 77 |
|
} |
| 78 |
} // end og_collection class declaration |
} // end og_collection class declaration |
| 79 |
|
|
| 80 |
class og_typed_collection extends og_collection { |
class og_collection_typed extends og_collection { |
| 81 |
|
|
| 82 |
|
private $defaults; |
| 83 |
|
|
| 84 |
|
function __construct($grouptype) { |
| 85 |
|
$this->grouptype = $grouptype; |
| 86 |
|
$result = db_fetch_array(db_query("SELECT * FROM {og_collections} WHERE grouptype = '%s'", $grouptype)); |
| 87 |
|
$this->version = $result['version']; |
| 88 |
|
|
| 89 |
|
// this is where we load the most up-to-date version of the pcpanels for those which are using the default collection's did |
| 90 |
|
$this->defaults = unserialize($result['defaults']); |
| 91 |
|
if (!empty($defaults)) { |
| 92 |
|
foreach ($defaults as $pcpname) { |
| 93 |
|
$this->$pcpname = new og_pcpanel(); |
| 94 |
|
$this->$pcpname->og_pcpanel_load(db_result(db_query("SELECT did FROM {og_collections_pcpanels} WHERE pcpname = '%s' AND grouptype = 'default'", $pcpname))); |
| 95 |
|
foreach (unserialize($result[$pcpname]) as $setting => $val) { |
| 96 |
|
$this->$pcpname->$setting = $val; |
| 97 |
|
} |
| 98 |
|
} |
| 99 |
|
$result = array_diff_keys($result, array_flip($defaults)); |
| 100 |
|
} |
| 101 |
|
$pcpanel_data = array_filter($result, !in_array('grouptype', 'version', 'defaults')); |
| 102 |
|
if (!empty($pcpanel_data)) { // loads any pcpanels that aren't reliant on the default |
| 103 |
|
parent::load($pcpanel_data); |
| 104 |
|
} |
| 105 |
|
} |
| 106 |
|
|
| 107 |
|
function revert_pcpanel() { |
| 108 |
|
|
| 109 |
|
} |
| 110 |
|
|
| 111 |
} |
} |
| 112 |
|
|
| 113 |
class og_pcpanel { |
class og_pcpanel { |
| 114 |
|
|
| 115 |
public $default, |
public $default, $enabled, $weight, $did, $page_title, $path, $published, $show_blocks, $display, $context, $content_types, $version; |
| 116 |
$enabled, |
private $pcpname, $grouptype; |
|
$weight, |
|
|
$page_title, |
|
|
$path, |
|
|
$published, |
|
|
$show_blocks, |
|
|
$did, |
|
|
$display, |
|
|
$context, |
|
|
$content_types, |
|
|
$grouptype, |
|
|
$version; |
|
|
private $sleep_container, $pcpid; |
|
| 117 |
// @FIXME This is an ugly hack way of accomplishing this, surely something in php's OOP would accomodate a better method? Can't figure out how, though... |
// @FIXME This is an ugly hack way of accomplishing this, surely something in php's OOP would accomodate a better method? Can't figure out how, though... |
| 118 |
/** |
|
| 119 |
* |
function __construct($op = FALSE) { |
| 120 |
* @return |
if ($op == 'new') { |
| 121 |
* @param $new Object[optional] |
$this->pcpid = db_next_id("{og_collections_pcpanel}_pcpid"); |
| 122 |
*/ |
$display = panels_save_display(panels_new_display()); |
| 123 |
function __construct($new = FALSE) { |
$this->did = $display->did; |
| 124 |
if ($new === 'add_new') { |
$this->display = $display; |
| 125 |
$this->add_new(); |
$this->grouptype = 'default'; |
| 126 |
|
db_query("INSERT INTO {og_collections_pcpanel} (did, pcpid, grouptype, version) VALUES (%d, %d, '%s', %d", $this->did, $this->pcpid, $this->grouptype, 0); |
| 127 |
|
} |
| 128 |
|
elseif ($op == 'export') { |
| 129 |
|
|
| 130 |
} |
} |
| 131 |
} |
} |
| 132 |
|
|
| 133 |
// There remain some oustanding issues with PHP's __sleep() magic method related to including protected/private variables. |
// There remain some oustanding issues with PHP's __sleep() magic method related to including protected/private variables. |
| 134 |
// See http://us.php.net/manual/en/language.oop5.magic.php#81492 for workaround details. |
// See http://us.php.net/manual/en/language.oop5.magic.php#81492 for workaround details. |
| 135 |
function __sleep() { |
function __sleep() { |
| 136 |
$this->sleep_container = $this->display; |
return array('default', 'enabled', 'weight', 'did', 'page_title', 'path', 'published', 'show_blocks', 'pcpid'); |
|
return array('default', 'enabled', 'weight', 'page_title', 'path', 'published', 'show_blocks', 'did'); |
|
| 137 |
} |
} |
| 138 |
|
|
| 139 |
// Doing it this way requires some double query-ness. Is this avoidable? |
// Doing it this way requires some double query-ness. Is this avoidable? |
| 140 |
function __wakeup() { |
function __wakeup() { |
| 141 |
$this->og_pcpanel_load(); |
$this->load(); |
| 142 |
} |
} |
| 143 |
|
|
|
// Adds a new pcpanel. ONLY called by the constructor, never directly |
|
|
private function og_pcpanel_new() { |
|
|
$this->pcpid = db_next_id("{og_collections_pcpanel}_pcpid"); |
|
|
$display = panels_save_display(panels_new_display()); |
|
|
$this->did = $display->did; |
|
|
$this->display = $display; |
|
|
$this->grouptype = 'default'; |
|
|
db_query("INSERT INTO {og_collections_pcpanel} (did, pcpid, grouptype, version) VALUES (%d, %d, '%s', %d", $this->did, $this->pcpid, $this->grouptype, 0); |
|
|
} |
|
|
|
|
| 144 |
// Saves the pcpanel data into the relevant collections & pcpanel fields |
// Saves the pcpanel data into the relevant collections & pcpanel fields |
| 145 |
function og_pcpanel_save() { |
function save() { |
| 146 |
db_query("UPDATE {og_collections_pcpanel} SET pcpid = %d, grouptype = '%s', version = %d WHERE did = %d", $this->pcpid, $this->grouptype, $this->version, $this->did); |
db_query("UPDATE {og_collections_pcpanel} SET pcpid = %d, grouptype = '%s', version = %d WHERE did = %d", $this->pcpid, $this->grouptype, $this->version, $this->did); |
| 147 |
} |
} |
| 148 |
|
|
| 149 |
// loads the pcpanel fields that are in the pcpanel table and stores them in the current object |
// loads the pcpanel fields that are in the pcpanel table and stores them in the current object |
| 150 |
function og_pcpanel_load() { |
function load() { |
| 151 |
if ($did = $this->did) { // __get snags the did if it's not loaded. wraps the whole function, b/c if there's no did, we can't load |
if ($did = $this->did) { // __get snags the did if it's not loaded. wraps the whole function, b/c if there's no did, we can't load |
| 152 |
$result = db_fetch_object(db_query("SELECT * FROM {og_collections_pcpanel} WHERE did = %d", $this->did)); |
$result = db_fetch_array(db_query("SELECT * FROM {og_collections_pcpanel} WHERE did = %d", $this->did)); |
| 153 |
$this->pcpid = $result->pcpid; |
$this->pcpid = $result['pcpid']; |
| 154 |
$this->grouptype = $result->grouptype; |
$this->grouptype = $result['grouptype']; |
| 155 |
$this->version = $result->version; |
$this->version = $result['version']; |
| 156 |
|
$this->pcpname = $result['pcpname']; |
| 157 |
} |
} |
| 158 |
} |
} |
| 159 |
|
|
| 160 |
function og_pcpanel_fill() { |
function fill() { |
| 161 |
$pcpid = $this->pcpid; |
$data = og_collection::load_single($this->pcpname, $this->grouptype); // TODO kind of an inelegant hack. Any better way of doing this? |
| 162 |
$collection = new og_collection($this->grouptype, $pcpid); // TODO kind of an inelegant hack. Any better way of doing this? |
foreach ($data as $setting => $value) { |
| 163 |
$this = $collection->$pcpid; |
$this->$setting = $value; |
| 164 |
|
} |
| 165 |
} |
} |
| 166 |
|
|
| 167 |
|
// magic method for snaggign whichever members |
| 168 |
function __get($member) { |
function __get($member) { |
| 169 |
switch ($member) { |
switch ($member) { |
| 170 |
case 'did': |
case 'did': |
| 171 |
if (isset($this->pcpid, $this->grouptype)) { |
if (isset($this->pcpid, $this->grouptype)) { // @FIXME need to expand this to include the pcpname |
| 172 |
return db_result(db_query("SELECT did FROM {og_collections_pcpanel} WHERE pcpid = %d AND grouptype = '%s'", $this->pcpid, $this->grouptype)); |
return $this->did = db_result(db_query("SELECT did FROM {og_collections_pcpanel} WHERE pcpid = %d AND grouptype = '%s'", $this->pcpid, $this->grouptype)); |
| 173 |
} |
} |
| 174 |
else { |
else { |
| 175 |
$exception = 'Not enough information to get retrieve pcpanel data (missing pcpid or grouptype).'; // TODO what about default? |
$exception = 'Not enough information to get retrieve pcpanel data (missing pcpid or grouptype).'; // TODO what about default? |
| 178 |
|
|
| 179 |
case 'pcpid': |
case 'pcpid': |
| 180 |
case 'grouptype': |
case 'grouptype': |
| 181 |
|
case 'pcpname': |
| 182 |
if (isset($this->did)) { |
if (isset($this->did)) { |
| 183 |
return db_result(db_query("SELECT '%s' FROM {og_collections_pcpanel} WHERE did = %d", $member, $this->did)); |
return $this->$member = db_result(db_query("SELECT '%s' FROM {og_collections_pcpanel} WHERE did = %d", $member, $this->did)); |
| 184 |
} |
} |
| 185 |
else { |
else { |
| 186 |
$exception = 'Not enough information to get retrieve pcpanel data (missing did).'; |
$exception = 'Not enough information to get retrieve pcpanel data (missing did).'; |
| 188 |
break; |
break; |
| 189 |
|
|
| 190 |
case 'display': |
case 'display': |
| 191 |
|
return $this->display = panels_load_display($this->did); |
|
break; |
|
| 192 |
|
|
| 193 |
case 'context': |
case 'context': |
| 194 |
|
$this->display->context = array('og_panels' => panels_context_create_empty('group')); |
| 195 |
break; |
return $this->context = $this->display->context; |
| 196 |
|
|
| 197 |
case 'content_types': |
case 'content_types': |
| 198 |
|
$this->display->content_types = panels_common_get_allowed_types('og_panels', $this->context); |
| 199 |
|
return $this->content_types = $this->display->content_types; |
| 200 |
|
|
| 201 |
|
default: |
| 202 |
|
$exception = 'Invalid member requested.'; |
| 203 |
break; |
break; |
| 204 |
} |
} |
| 205 |
if (isset($exception)) { |
if (isset($exception)) { |
| 208 |
} |
} |
| 209 |
} // end og_pcpanel class declaration |
} // end og_pcpanel class declaration |
| 210 |
|
|
| 211 |
|
class og_pcpanel_new extends og_pcpanel { |
| 212 |
|
|
| 213 |
|
} |
| 214 |
|
|
| 215 |
|
class og_pcpanel_export extends og_pcpanel { |
| 216 |
|
|
| 217 |
|
} |
| 218 |
|
|
| 219 |
|
|
| 220 |
/** |
/** |
| 221 |
* Implementation of hook_help() |
* Implementation of hook_help() |
| 222 |
*/ |
*/ |
| 372 |
function og_collections_admin_collectioncfg($type) { |
function og_collections_admin_collectioncfg($type) { |
| 373 |
$group_types = og_collections_group_types(TRUE); |
$group_types = og_collections_group_types(TRUE); |
| 374 |
drupal_set_title("'". $group_types[$type] ."' Collection"); |
drupal_set_title("'". $group_types[$type] ."' Collection"); |
| 375 |
$result = db_fetch_array(db_query("SELECT * FROM {og_collections} WHERE grouptype = 'default'")); |
if (og_collections_check_for_pcpanels()) { |
|
if (count($result) < 3) { |
|
| 376 |
drupal_set_message("You need to create at least one pre-configured panel for OG Collections to operate properly. Use this form to create your first one."); |
drupal_set_message("You need to create at least one pre-configured panel for OG Collections to operate properly. Use this form to create your first one."); |
| 377 |
drupal_get_destination(); |
drupal_get_destination(); |
| 378 |
drupal_goto('admin/og/og_collections/pcpanel/form'); |
drupal_goto('admin/og/og_collections/pcpanel/form'); |
| 379 |
} |
} |
| 380 |
$collectionsetting = variable_get('og_collections_settings', FALSE); |
$collectionsetting = variable_get('og_collections_settings', FALSE); |
| 381 |
// TODO Need to improve helptext considerably |
// TODO Need to improve helptext considerably |
| 399 |
* |
* |
| 400 |
*/ |
*/ |
| 401 |
function og_collections_collectioncfg_table($type) { |
function og_collections_collectioncfg_table($type) { |
| 402 |
$collectioncfg = og_collections_load_collectioncfg($type); |
$collectioncfg = $type == 'default' ? new og_collection() : new og_collection_typed($type); |
| 403 |
$form['typevar'] = array('#type' => 'hidden', '#value' => $type); |
$form['typevar'] = array('#type' => 'hidden', '#value' => $type); |
| 404 |
// $form['#action'] = arg(3) ? url('admin/og/og_collections/'. $theme_key) : url('admin/build/block'); |
// $form['#action'] = arg(3) ? url('admin/og/og_collections/'. $theme_key) : url('admin/build/block'); |
| 405 |
$form['#tree'] = TRUE; |
$form['#tree'] = TRUE; |
| 1061 |
// bad $op |
// bad $op |
| 1062 |
return FALSE; |
return FALSE; |
| 1063 |
} |
} |
| 1064 |
|
$op = strtoupper($op); |
| 1065 |
switch ($GLOBALS['db_type']) { |
switch ($GLOBALS['db_type']) { |
| 1066 |
case 'mysql': |
case 'mysql': |
| 1067 |
case 'mysqli': |
case 'mysqli': |
| 1068 |
if ($op == 'add') { |
if ($op == 'add') { |
| 1069 |
db_query("ALTER TABLE {og_collections} ADD COLUMN $fieldname LONGTEXT NOT NULL DEFAULT '%s'", serialize(array())); |
db_query("ALTER TABLE {og_collections} ADD COLUMN $fieldname longtext"); |
| 1070 |
} |
} |
| 1071 |
elseif ($op == 'drop') { |
elseif ($op == 'drop') { |
| 1072 |
db_query("ALTER TABLE {og_collections} DROP COLUMN $fieldname"); |
db_query("ALTER TABLE {og_collections} DROP COLUMN $fieldname"); |
| 1075 |
|
|
| 1076 |
case 'pgsql': |
case 'pgsql': |
| 1077 |
if ($op == 'add') { |
if ($op == 'add') { |
| 1078 |
db_add_column($ret, 'og_collections', $fieldname, 'TEXT', array('not null' => TRUE, 'default' => serialize(array()))); |
db_add_column($ret, 'og_collections', $fieldname, 'text'); |
| 1079 |
} |
} |
| 1080 |
elseif ($op == 'drop') { |
elseif ($op == 'drop') { |
| 1081 |
db_query("ALTER TABLE {og_collections} DROP $fieldname"); |
db_query("ALTER TABLE {og_collections} DROP $fieldname"); |
| 1269 |
} |
} |
| 1270 |
} |
} |
| 1271 |
|
|
| 1272 |
|
function og_collections_check_for_pcpanels() { |
| 1273 |
|
$result = db_fetch_array(db_query("SELECT * FROM {og_collections} WHERE grouptype = 'default'")); |
| 1274 |
|
$pcpanels = array_filter(array_diff_key($result, array_flip(array('grouptype', 'version', 'defaults')))); |
| 1275 |
|
if (empty($pcpanels)) { |
| 1276 |
|
return TRUE; |
| 1277 |
|
} |
| 1278 |
|
return FALSE; |
| 1279 |
|
} |
| 1280 |
|
|
| 1281 |
/** |
/** |
| 1282 |
* Return a list of fields and their db_query variable strings |
* Return a list of fields and their db_query variable strings |
| 1283 |
*/ |
*/ |
| 1293 |
"weight" => "%d", |
"weight" => "%d", |
| 1294 |
); |
); |
| 1295 |
} |
} |
| 1296 |
|
|
| 1297 |
|
/** |
| 1298 |
|
* Flexible callback for array_filter |
| 1299 |
|
* |
| 1300 |
|
* @param mixed $val |
| 1301 |
|
* The current value being passed in by array_filter |
| 1302 |
|
* @param array $array |
| 1303 |
|
* The array of values that $val should be compared against |
| 1304 |
|
* @param bool $matches_desired |
| 1305 |
|
* Whether we want matches or NOT matches; i.e., whether a match should return TRUE or FALSE |
| 1306 |
|
* @return bool $match |
| 1307 |
|
* TRUE keeps the value in the returned array, FALSE drops it. |
| 1308 |
|
* $val, $array, $matches_desired = TRUE |
| 1309 |
|
*/ |
| 1310 |
|
function _og_collections_filter() { |
| 1311 |
|
$args = func_get_args(); |
| 1312 |
|
$match = in_array($val, $array); |
| 1313 |
|
return $matches_desired ? $match : !$match; |
| 1314 |
|
} |
| 1315 |
|
|