View source
<?php
class ShareaholicContentSettings {
public static function schema() {
$schema['shareaholic_content_settings'] = array(
'description' => 'Stores shareaholic specific settings for nodes.',
'fields' => array(
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
'description' => 'The {node}.nid to store settings.',
),
'settings' => array(
'type' => 'text',
'size' => 'medium',
'not null' => TRUE,
'serialize' => TRUE,
'description' => 'The settings object for a node',
),
),
'primary key' => array(
'nid',
),
'foreign keys' => array(
'dnv_node' => array(
'table' => 'node',
'columns' => array(
'nid' => 'nid',
),
),
),
);
return $schema;
}
public static function update($node) {
if (!db_table_exists('shareaholic_content_settings')) {
return;
}
if (db_select('shareaholic_content_settings', 'settings')
->fields('settings')
->condition('nid', $node->nid, '=')
->execute()
->fetchAssoc()) {
db_update('shareaholic_content_settings')
->fields(array(
'settings' => self::serialize_settings($node),
))
->condition('nid', $node->nid)
->execute();
}
else {
self::insert($node);
}
}
public static function insert($node) {
if (!db_table_exists('shareaholic_content_settings')) {
return;
}
if (isset($node->shareaholic_options)) {
db_insert('shareaholic_content_settings')
->fields(array(
'nid' => $node->nid,
'settings' => self::serialize_settings($node),
))
->execute();
}
}
public static function load($nodes, $types) {
if (!db_table_exists('shareaholic_content_settings')) {
return;
}
$result = db_query('SELECT * FROM {shareaholic_content_settings} WHERE nid IN(:nids)', array(
':nids' => array_keys($nodes),
))
->fetchAllAssoc('nid');
foreach ($nodes as &$node) {
if (isset($result[$node->nid]->settings)) {
$settings = self::unserialize_settings($result[$node->nid]);
$node->shareaholic_options['shareaholic_exclude_from_recommendations'] = isset($settings['exclude_from_recommendations']) && $settings['exclude_from_recommendations'];
$node->shareaholic_options['shareaholic_hide_recommendations'] = isset($settings['hide_recommendations']) && $settings['hide_recommendations'];
$node->shareaholic_options['shareaholic_hide_share_buttons'] = isset($settings['hide_share_buttons']) && $settings['hide_share_buttons'];
$node->shareaholic_options['shareaholic_exclude_og_tags'] = isset($settings['exclude_og_tags']) && $settings['exclude_og_tags'];
}
}
}
public static function delete($node) {
if (!db_table_exists('shareaholic_content_settings')) {
return;
}
db_delete('shareaholic_content_settings')
->condition('nid', $node->nid)
->execute();
}
private static function serialize_settings($node) {
$settings = array(
'exclude_from_recommendations' => $node->shareaholic_options['shareaholic_exclude_from_recommendations'],
'hide_recommendations' => $node->shareaholic_options['shareaholic_hide_recommendations'],
'hide_share_buttons' => $node->shareaholic_options['shareaholic_hide_share_buttons'],
'exclude_og_tags' => $node->shareaholic_options['shareaholic_exclude_og_tags'],
);
return serialize($settings);
}
private static function unserialize_settings($node) {
return unserialize($node->settings);
}
}