Flags :
- bookmarks OK - flaglist DEV Signed-off-by: bachy <git@g-u-i.net>
This commit is contained in:
parent
8e956c857b
commit
d77d7e5ef7
5
js/materio_flag-ck.js
Normal file
5
js/materio_flag-ck.js
Normal file
File diff suppressed because one or more lines are too long
36
js/materio_flag.js
Normal file
36
js/materio_flag.js
Normal file
@ -0,0 +1,36 @@
|
||||
// @codekit-prepend "gui.js"
|
||||
|
||||
(function($) {
|
||||
|
||||
MaterioFlag = function(){
|
||||
|
||||
|
||||
/**
|
||||
* init()
|
||||
*/
|
||||
function init(){
|
||||
trace('init MaterioFlag');
|
||||
$(document).bind('flagGlobalAfterLinkUpdate', onFlaging);
|
||||
};
|
||||
|
||||
function onFlaging(event){
|
||||
trace('onFlaging', event);
|
||||
|
||||
$.getJSON('/materioflag/refresh/block/bookmarks', function(json){
|
||||
trace('json', json);
|
||||
$('#block-materio-flag-materio-flag-mybookmarks').replaceWith(json.block);
|
||||
Drupal.flagLink('#block-materio-flag-materio-flag-mybookmarks');
|
||||
// TODO: update flags stars on search results after unflaging from block
|
||||
$.event.trigger('mybookmarks-block-updated');
|
||||
});
|
||||
|
||||
};
|
||||
|
||||
init();
|
||||
|
||||
|
||||
};
|
||||
|
||||
var materioflag = new MaterioFlag();
|
||||
|
||||
})(jQuery);
|
6
materio_flag.info
Normal file
6
materio_flag.info
Normal file
@ -0,0 +1,6 @@
|
||||
name = Materio Flag
|
||||
description = Provides bookmark flag (and maybe folders).
|
||||
core = 7.x
|
||||
dependencies[] = flag
|
||||
package = Materio
|
||||
|
72
materio_flag.install
Normal file
72
materio_flag.install
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* The Flag Bookmark module install hooks.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_install().
|
||||
*/
|
||||
function materio_flag_install() {
|
||||
// Everything is handled by hook_enable().
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_enable().
|
||||
*
|
||||
* We create the demonstration flag on enable, so hook implementations in flag
|
||||
* module will fire correctly, as the APIs are not available on install.
|
||||
*/
|
||||
function materio_flag_enable() {
|
||||
// Load the flag API in case we want to use it when enabling.
|
||||
include_once(drupal_get_path('module', 'flag') . '/flag.module');
|
||||
|
||||
if (!flag_get_flags()) {
|
||||
// Install a demonstration flag only if no flag exists. This is to prevent
|
||||
// a case where a disables and enables the module, and the demonstration
|
||||
// flag is overwritten or re-created.
|
||||
$flag = flag_flag::factory_by_entity_type('node');
|
||||
$configuration = array(
|
||||
'name' => 'bookmarks',
|
||||
'global' => 0,
|
||||
'show_on_page' => 1,
|
||||
'show_on_teaser' => 0,
|
||||
'show_on_form' => 0,
|
||||
// The following UI labels aren't wrapped in t() because they are written
|
||||
// to the DB in English. They are passed to t() later, thus allowing for
|
||||
// multilingual sites.
|
||||
'title' => 'Bookmarks',
|
||||
'flag_short' => 'Bookmark this',
|
||||
'flag_long' => 'Add this post to your bookmarks',
|
||||
'flag_message' => 'This post has been added to your bookmarks',
|
||||
'unflag_short' => 'Unbookmark this',
|
||||
'unflag_long' => 'Remove this post from your bookmarks',
|
||||
'unflag_message' => 'This post has been removed from your bookmarks',
|
||||
'types' => _materio_flag_install_get_suggested_node_types(),
|
||||
);
|
||||
$flag->form_input($configuration);
|
||||
$flag->save();
|
||||
|
||||
// Clear the flag cache so the new permission is seen by core.
|
||||
drupal_static_reset('flag_get_flags');
|
||||
|
||||
// Grant permissions.
|
||||
$permissions = array('flag bookmarks', 'unflag bookmarks');
|
||||
user_role_grant_permissions(DRUPAL_AUTHENTICATED_RID, $permissions);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns some node types to which the demonstration 'bookmarks' flag will apply.
|
||||
*/
|
||||
function _materio_flag_install_get_suggested_node_types() {
|
||||
$preferred = array('page', 'article', 'story', 'forum', 'blog');
|
||||
$existing = array_intersect($preferred, array_keys(node_type_get_types()));
|
||||
if (!$existing) {
|
||||
// As a last resort, take the first preference.
|
||||
return array($preferred[0]);
|
||||
}
|
||||
return $existing;
|
||||
}
|
||||
|
299
materio_flag.module
Normal file
299
materio_flag.module
Normal file
@ -0,0 +1,299 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* The Flag bookmark module.
|
||||
*
|
||||
* This module creates a default Flag when enabled.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_permission().
|
||||
*/
|
||||
function materio_flag_permission() {
|
||||
return array(
|
||||
/*
|
||||
*'administer my module' => array(
|
||||
* 'title' => t('Administer my module'),
|
||||
* 'description' => t('Perform administration tasks for my module.'),
|
||||
*),
|
||||
*/
|
||||
'access mybookmarks block' => array(
|
||||
'title' => t('Show my bookmarks block'),
|
||||
'description' => t('access own bookmarks block'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_menu().
|
||||
*/
|
||||
function materio_flag_menu() {
|
||||
$items = array();
|
||||
|
||||
$base = array(
|
||||
'type' => MENU_CALLBACK,
|
||||
'file' => 'materio_flag.pages.inc',
|
||||
);
|
||||
|
||||
|
||||
$items['materioflag/refresh/block/bookmarks'] = $base+array(
|
||||
'access arguments' => array('access mybookmarks block'),
|
||||
'page callback' => 'materio_flag_refresh_block_bookmarks',
|
||||
// 'page arguments' => array(),
|
||||
);
|
||||
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_block_info().
|
||||
*/
|
||||
function materio_flag_block_info() {
|
||||
|
||||
$blocks['materio_flag_mybookmarks'] = array(
|
||||
'info' => t('My bookmarks'),
|
||||
'cache' => DRUPAL_NO_CACHE
|
||||
);
|
||||
|
||||
$blocks['materio_flag_mylists'] = array(
|
||||
'info' => t('My Materio flag lists'),
|
||||
'cache' => DRUPAL_NO_CACHE
|
||||
);
|
||||
|
||||
|
||||
return $blocks;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_block_view().
|
||||
*/
|
||||
function materio_flag_block_view($delta = '') {
|
||||
global $user;
|
||||
|
||||
$block = array();
|
||||
|
||||
switch ($delta) {
|
||||
case 'materio_flag_mybookmarks':
|
||||
if(user_access('access own bookmarks block')){
|
||||
$userflags = flag_get_user_flags('node');
|
||||
// dsm($userflags, 'userflags');
|
||||
if(isset($userflags['bookmarks'])){
|
||||
$userbookmarks = array();
|
||||
foreach ($userflags['bookmarks'] as $nid => $flag) {
|
||||
$userbookmarks[] = node_load($nid);
|
||||
}
|
||||
$block['subject'] = t('My bookmarks (@len)', array("@len"=>count($userbookmarks)));
|
||||
$block['content'] = theme('materio_flag_mybookmarks_block', array("bookmarks"=>$userbookmarks, "viewmode"=>"bookmark"));
|
||||
}else{
|
||||
$block['subject'] = t('My bookmarks');
|
||||
$block['content'] = t('No bookmarks yet. Add bookmarks on clicking on results star');
|
||||
}
|
||||
drupal_add_js(drupal_get_path('module', 'materio_flag').'/js/materio_flag-ck.js');
|
||||
}
|
||||
break;
|
||||
case 'materio_flag_mylists':
|
||||
if(user_access('create flag lists')){
|
||||
$flags = flag_lists_get_user_flags(NULL, $user);
|
||||
// $userflags = flag_get_user_flags('node');
|
||||
// dsm($userflags, 'userflags');
|
||||
// dsm($flags, 'flags');
|
||||
|
||||
foreach ($flags as $name => $flag) {
|
||||
$flaged_content = flag_lists_get_flagged_content($flag->fid, $user->uid);
|
||||
// dsm($flaged_content, 'flaged_content');
|
||||
$fcn = array();
|
||||
foreach ($flaged_content as $entity) {
|
||||
if($entity->entity_type == 'node'){
|
||||
$fcn[] = node_load($entity->entity_id);
|
||||
}
|
||||
}
|
||||
$lists[$name] = array(
|
||||
'list' => $flag,
|
||||
'content' => $fcn,
|
||||
);
|
||||
}
|
||||
|
||||
if(isset($lists)){
|
||||
$block['subject'] = t('My !listname', array('!listname'=>variable_get('flag_lists_name', 'list')));
|
||||
$block['content'] = theme('materio_flag_mylists_block', array("lists"=>$lists, "viewmode"=>"bookmark"));
|
||||
// $block['content'] = theme('flag_lists_user_page', array('uid' => $user->uid));
|
||||
}else{
|
||||
|
||||
$block['subject'] = t('My !listname', array('!listname'=>variable_get('flag_lists_name', 'list')));
|
||||
$block['content'] = t('No !listname yet. Add !listname on clicking on results star', array('!listname'=>variable_get('flag_lists_name', 'list')));
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
return $block;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_entity_info_alter().
|
||||
*/
|
||||
function materio_flag_entity_info_alter(&$entity_info) {
|
||||
// Set the controller class for nodes to an alternate implementation of the
|
||||
// DrupalEntityController interface.
|
||||
/*
|
||||
*$entity_info['node']['controller class'] = 'MyCustomNodeController';
|
||||
*/
|
||||
/* Your code here */
|
||||
$entity_info['node']['view modes']['bookmark'] = array(
|
||||
'label' => t('Bookmark'),
|
||||
'custom settings' => TRUE,
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_prepare_view().
|
||||
*/
|
||||
function materio_flag_entity_prepare_view($entities, $type) {
|
||||
// Load a specific node into the user object for later theming.
|
||||
/*
|
||||
*if ($type == 'user') {
|
||||
* $nodes = mymodule_get_user_nodes(array_keys($entities));
|
||||
* foreach ($entities as $uid => $entity) {
|
||||
* $entity->user_node = $nodes[$uid];
|
||||
* }
|
||||
*}
|
||||
*/
|
||||
/* Your code here */
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_entity_view().
|
||||
*
|
||||
* Note this is broken for taxonomy terms. @see http://drupal.org/node/1067120
|
||||
*/
|
||||
function materio_flag_entity_view($entity, $type, $view_mode, $langcode) {
|
||||
if($type == 'node'){
|
||||
// dsm($entity, 'entity');
|
||||
// $userlists = flag_lists_get_user_flags();
|
||||
// if (isset($userlists)) {
|
||||
// $entity->content['links']['flaglists'] = array(
|
||||
// '#theme' => 'links',
|
||||
// '#links' => $userlists,
|
||||
// '#attributes' => array('class' => array('links', 'inline')),
|
||||
// );
|
||||
// }
|
||||
|
||||
// $create = theme('flag_lists_list', array(
|
||||
// 'node' => $entity,
|
||||
// 'create' => 0,
|
||||
// 'ops' => 0,
|
||||
// 'use_flags' => 1)
|
||||
// );
|
||||
// dsm($create, 'create');
|
||||
|
||||
$flaglists_links = theme('materio_flag_mylists_entity_links', array(
|
||||
'node' => $entity,
|
||||
'create' => 0,
|
||||
'ops' => 0,
|
||||
'use_flags' => 1)
|
||||
);
|
||||
|
||||
$entity->content['links']['flaglistslinks'] = array('#markup' => $flaglists_links,"#html"=>true);
|
||||
|
||||
// dsm($entity, 'entity');
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Implements hook_theme().
|
||||
*/
|
||||
function materio_flag_theme($existing, $type, $theme, $path) {
|
||||
return array(
|
||||
'materio_flag_mybookmarks_block' => array(
|
||||
'arguments' => array(),
|
||||
'template' => 'materio-flag-mybookmarks-block',
|
||||
'path' => drupal_get_path('module', 'materio_flag').'/templates',
|
||||
),
|
||||
'materio_flag_mylists_block' => array(
|
||||
'arguments' => array(),
|
||||
'template' => 'materio-flag-mylists-block',
|
||||
'path' => drupal_get_path('module', 'materio_flag').'/templates',
|
||||
),
|
||||
'materio_flag_mylists_entity_links' => array(
|
||||
'variables' => array('node' => NULL, 'create' => NULL, 'ops' => NULL, 'use_flags' => NULL),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
function template_preprocess_materio_flag_mybookmarks_block($vars){
|
||||
// dsm($vars, 'vars');
|
||||
}
|
||||
|
||||
|
||||
function template_preprocess_materio_flag_mylists_block($vars){
|
||||
// dsm($vars, 'vars');
|
||||
}
|
||||
|
||||
function theme_materio_flag_mylists_entity_links($vars){
|
||||
// dsm($vars, 'vars');
|
||||
return;
|
||||
|
||||
$node = $vars['node'];
|
||||
$create = $vars['create'];
|
||||
$ops = $vars['ops'];
|
||||
$use_flags = $vars['use_flags'];
|
||||
|
||||
$items = array();
|
||||
|
||||
// Make sure we have a node.
|
||||
if (is_object($node) && user_access('create flag lists')) {
|
||||
$content_type = $node->type;
|
||||
$entity_id = $node->nid;
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
|
||||
// Do we have a list template for this node type, or are we s
|
||||
if (!flag_lists_template_exists($content_type) && !$use_flags) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
global $user;
|
||||
if ($flags = flag_lists_get_user_flags($content_type, $user, $use_flags)) {
|
||||
// Build the list of lists for this node.
|
||||
foreach ($flags as $flag) {
|
||||
if ($flag->module == 'flag_lists') {
|
||||
$action = _flag_lists_is_flagged($flag, $entity_id, $user->uid, 0) ? 'unflag' : 'flag';
|
||||
}
|
||||
else {
|
||||
$action = $flag->is_flagged($entity_id) ? 'unflag' : 'flag';;
|
||||
}
|
||||
|
||||
// Do we need the ops?
|
||||
if ($ops && $flag->module == 'flag_lists') {
|
||||
$ops_links = theme('flag_lists_ops', array('flag' => $flag));
|
||||
$link = $flag->theme($action, $entity_id) . $ops_links;
|
||||
}
|
||||
else {
|
||||
$link = $flag->theme($action, $entity_id);
|
||||
}
|
||||
|
||||
// If it's a list, fix the link.
|
||||
if ($flag->module == 'flag_lists') {
|
||||
flag_lists_fix_link($link, $action);
|
||||
}
|
||||
$items[] = $link;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
18
materio_flag.pages.inc
Normal file
18
materio_flag.pages.inc
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
function materio_flag_refresh_block_bookmarks(){
|
||||
$rep = array();
|
||||
|
||||
|
||||
// $block_content = module_invoke('block', 'block_view', 'materio_flag_mybookmarks');
|
||||
|
||||
$block = block_load('materio_flag', 'materio_flag_mybookmarks');
|
||||
$block_content = _block_render_blocks(array($block));
|
||||
$build = _block_get_renderable_array($block_content);
|
||||
$block_rendered = drupal_render($build);
|
||||
|
||||
$rep['block'] = $block_rendered;
|
||||
|
||||
|
||||
drupal_json_output($rep);
|
||||
}
|
4
templates/materio-flag-mybookmarks-block.tpl.php
Normal file
4
templates/materio-flag-mybookmarks-block.tpl.php
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
<section class="mybookmarks">
|
||||
<?php print render(entity_view('node', $bookmarks, $viewmode)); ?>
|
||||
</section>
|
6
templates/materio-flag-mylists-block.tpl.php
Normal file
6
templates/materio-flag-mylists-block.tpl.php
Normal file
@ -0,0 +1,6 @@
|
||||
<section class="mylists">
|
||||
<?php foreach ($lists as $name => $list): ?>
|
||||
<h2 class="listname"><?php print $list['list']->title; ?></h2>
|
||||
<div class="flaged"> <?php print render(entity_view('node', $list['content'], $viewmode)); ?> </div>
|
||||
<?php endforeach; ?>
|
||||
</section>
|
Loading…
x
Reference in New Issue
Block a user