| [ Index ] |
|
Code source de b2evolution 2.1.0-beta |
1 <?php 2 /** 3 * This file implements the Widget class. 4 * 5 * This file is part of the evoCore framework - {@link http://evocore.net/} 6 * See also {@link http://sourceforge.net/projects/evocms/}. 7 * 8 * @copyright (c)2003-2007 by Francois PLANQUE - {@link http://fplanque.net/} 9 * 10 * {@internal License choice 11 * - If you have received this file as part of a package, please find the license.txt file in 12 * the same folder or the closest folder above for complete license terms. 13 * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/) 14 * then you must choose one of the following licenses before using the file: 15 * - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php 16 * - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php 17 * }} 18 * 19 * @package evocore 20 * 21 * {@internal Below is a list of authors who have contributed to design/coding of this file: }} 22 * @author fplanque: Francois PLANQUE. 23 * 24 * @version $Id: _widget.class.php,v 1.9 2007/11/04 01:10:57 fplanque Exp $ 25 */ 26 if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' ); 27 28 /** 29 * ComponentWidget Class 30 * 31 * A ComponentWidget is a displayable entity that can be placed into a Container on a web page. 32 * 33 * @package evocore 34 */ 35 class ComponentWidget extends DataObject 36 { 37 var $coll_ID; 38 /** 39 * Container name 40 */ 41 var $sco_name; 42 var $order; 43 var $type; 44 var $code; 45 var $params; 46 47 /** 48 * Array of params which have been customized for this widget instance 49 * 50 * This is saved to the DB as a serialized string ($params) 51 */ 52 var $param_array = NULL; 53 54 /** 55 * Array of params used during display() 56 */ 57 var $disp_params; 58 59 /** 60 * Lazy instantiated 61 * (false if this Widget is not handled by a Plugin) 62 * @see get_Plugin() 63 * @var Plugin 64 */ 65 var $Plugin; 66 67 68 /** 69 * Constructor 70 * 71 * @param object data row from db 72 */ 73 function ComponentWidget( $db_row = NULL, $type = 'core', $code = NULL ) 74 { 75 // Call parent constructor: 76 parent::DataObject( 'T_widget', 'wi_', 'wi_ID' ); 77 78 if( is_null($db_row) ) 79 { // We are creating an object here: 80 $this->set( 'type', $type ); 81 $this->set( 'code', $code ); 82 } 83 else 84 { // Wa are loading an object: 85 $this->ID = $db_row->wi_ID; 86 $this->coll_ID = $db_row->wi_coll_ID; 87 $this->sco_name = $db_row->wi_sco_name; 88 $this->type = $db_row->wi_type; 89 $this->code = $db_row->wi_code; 90 $this->params = $db_row->wi_params; 91 $this->order = $db_row->wi_order; 92 } 93 } 94 95 96 /** 97 * Get ref to Plugin handling this Widget 98 * 99 * @return Plugin 100 */ 101 function & get_Plugin() 102 { 103 global $Plugins; 104 105 if( is_null( $this->Plugin ) ) 106 { 107 if( $this->type != 'plugin' ) 108 { 109 $this->Plugin = false; 110 } 111 else 112 { 113 $this->Plugin = & $Plugins->get_by_code( $this->code ); 114 } 115 } 116 117 return $this->Plugin; 118 } 119 120 121 /** 122 * Get name of widget 123 * 124 * Should be overriden by core widgets 125 */ 126 function get_name() 127 { 128 if( $this->type == 'plugin' ) 129 { 130 // Make sure Plugin is loaded: 131 if( $this->get_Plugin() ) 132 { 133 return $this->Plugin->name; 134 } 135 return T_('Inactive / Uninstalled plugin'); 136 } 137 138 return T_('Unknown'); 139 } 140 141 142 /** 143 * Get desc of widget 144 * 145 * Should be overriden by core widgets 146 */ 147 function get_desc() 148 { 149 if( $this->type == 'plugin' ) 150 { 151 // Make sure Plugin is loaded: 152 if( $this->get_Plugin() ) 153 { 154 return $this->Plugin->short_desc; 155 } 156 return T_('Inactive / Uninstalled plugin'); 157 } 158 159 return T_('Unknown'); 160 } 161 162 163 /** 164 * Get definitions for editable params 165 * 166 * @see Plugin::GetDefaultSettings() 167 * @param local params like 'for_editing' => true 168 */ 169 function get_param_definitions( $params ) 170 { 171 if( $this->type == 'plugin' ) 172 { 173 // Make sure Plugin is loaded: 174 if( $this->get_Plugin() ) 175 { 176 return $this->Plugin->get_widget_param_definitions( $params ); 177 } 178 } 179 180 return array(); 181 } 182 183 184 /** 185 * Load param array 186 */ 187 function load_param_array() 188 { 189 if( is_null( $this->param_array ) ) 190 { // Param array has not been loaded yet 191 $this->param_array = @unserialize( $this->params ); 192 193 if( empty( $this->param_array ) ) 194 { // No saved param values were found: 195 $this->param_array = array(); 196 } 197 } 198 } 199 200 201 /** 202 * param value 203 * 204 */ 205 function get_param( $parname ) 206 { 207 $this->load_param_array(); 208 209 if( isset( $this->param_array[$parname] ) ) 210 { // We have a value for this param: 211 return $this->param_array[$parname]; 212 } 213 214 // Try default values: 215 $params = $this->get_param_definitions( NULL ); 216 if( isset( $params[$parname]['defaultvalue'] ) ) 217 { // We ahve a default value: 218 return $params[$parname]['defaultvalue'] ; 219 } 220 221 return NULL; 222 } 223 224 225 /** 226 * Set param value 227 * 228 * @param string parameter name 229 * @param mixed parameter value 230 * @return boolean true, if a value has been set; false if it has not changed 231 */ 232 function set( $parname, $parvalue ) 233 { 234 $params = $this->get_param_definitions( NULL ); 235 236 if( isset( $params[$parname] ) ) 237 { // This is a widget specifc param: 238 $this->param_array[$parname] = $parvalue; 239 // This is what'll be saved to the DB: 240 $this->set_param( 'params', 'string', serialize($this->param_array) ); 241 return; 242 } 243 244 switch( $parname ) 245 { 246 default: 247 return parent::set_param( $parname, 'string', $parvalue ); 248 } 249 } 250 251 252 /** 253 * Prepare display params 254 */ 255 function init_display( $params ) 256 { 257 // Generate widget defaults array: 258 $widget_defaults = array(); 259 $defs = $this->get_param_definitions( array() ); 260 foreach( $defs as $parname => $parmeta ) 261 { 262 if( isset( $parmeta['defaultvalue'] ) ) 263 { 264 $widget_defaults[ $parname ] = $parmeta['defaultvalue']; 265 } 266 else 267 { 268 $widget_defaults[ $parname ] = NULL; 269 } 270 } 271 272 // Load DB configuration: 273 $this->load_param_array(); 274 275 // Merge basic defaults < widget defaults < container params < DB params 276 // note: when called with skin_widget it falls back to basic defaults < widget defaults < calltime params < array() 277 $params = array_merge( array( 278 'block_start' => '<div class="$wi_class$">', 279 'block_end' => '</div>', 280 'block_display_title' => true, 281 'block_title_start' => '<h3>', 282 'block_title_end' => '</h3>', 283 'collist_start' => '', 284 'collist_end' => '', 285 'coll_start' => '<h4>', 286 'coll_end' => '</h4>', 287 'list_start' => '<ul>', 288 'list_end' => '</ul>', 289 'item_start' => '<li>', 290 'item_end' => '</li>', 291 'link_default_class' => 'default', 292 'item_text_start' => '', 293 'item_text_end' => '', 294 'item_selected_start' => '<li class="selected">', 295 'item_selected_end' => '</li>', 296 'link_selected_class' => 'selected', 297 'link_type' => 'canonic', // 'canonic' | 'context' (context will regenrate URL injecting/replacing a single filter) 298 'item_selected_text_start' => '', 299 'item_selected_text_end' => '', 300 'group_start' => '<ul>', 301 'group_end' => '</ul>', 302 'notes_start' => '<div class="notes">', 303 'notes_end' => '</div>', 304 ), $widget_defaults, $params, $this->param_array ); 305 306 307 // Customize params to the current widget: 308 $this->disp_params = str_replace( '$wi_class$', 'widget_'.$this->type.'_'.$this->code, $params ); 309 } 310 311 312 /** 313 * Display the widget! 314 * 315 * Should be overriden by core widgets 316 * 317 * @todo fp> handle custom params for each widget 318 * 319 * @param array MUST contain at least the basic display params 320 */ 321 function display( $params ) 322 { 323 global $Blog; 324 global $Plugins; 325 global $rsc_url; 326 327 $this->init_display( $params ); 328 329 switch( $this->type ) 330 { 331 case 'plugin': 332 // Call plugin (will return false if Plugin is not enabled): 333 if( $Plugins->call_by_code( $this->code, $this->disp_params ) ) 334 { 335 return true; 336 } 337 break; 338 } 339 340 echo "Widget $this->type : $this->code did not provide a display() method! "; 341 342 return false; 343 } 344 345 346 /** 347 * Note: a container can prevent display of titles with 'block_display_title' 348 * This is useful for the lists in the headers 349 * fp> I'm not sur if this param should be overridable by widgets themselves (priority problem) 350 * Maybe an "auto" setting. 351 * 352 * @protected 353 */ 354 function disp_title( $title = NULL ) 355 { 356 if( is_null($title) ) 357 { 358 $title = & $this->disp_params['title']; 359 } 360 361 if( $this->disp_params['block_display_title'] && !empty( $title ) ) 362 { 363 echo $this->disp_params['block_title_start']; 364 echo format_to_output( $title ); 365 echo $this->disp_params['block_title_end']; 366 } 367 } 368 369 370 /** 371 * List of items 372 * 373 * @param array MUST contain at least the basic display params 374 * @param string 'pages' or 'posts' 375 */ 376 function disp_item_list( $what ) 377 { 378 global $Blog; 379 380 // Create ItemList 381 // Note: we pass a widget specific prefix in order to make sure to never interfere with the mainlist 382 $ItemList = & new ItemListLight( $Blog, NULL, NULL, 20, 'ItemCacheLight', $this->code.'_' ); 383 // Filter list: 384 if( $what == 'pages' ) 385 { 386 $ItemList->set_filters( array( 387 'types' => '1000', // Restrict to type 1000 (pages) 388 'orderby' => 'title', 389 'order' => 'ASC', 390 'unit' => 'all', // We want to advertise all items (not just a page or a day) 391 ), false ); 392 } 393 else 394 { // post list 395 $ItemList->set_filters( array( 396 'unit' => 'all', // We want to advertise all items (not just a page or a day) 397 ) ); 398 } 399 // Run the query: 400 $ItemList->query(); 401 402 if( ! $ItemList->result_num_rows ) 403 { // Nothing to display: 404 return; 405 } 406 407 echo $this->disp_params['block_start']; 408 409 if( $what == 'pages' ) 410 { 411 $this->disp_title( T_('Info pages') ); 412 } 413 else 414 { 415 $this->disp_title( T_('Contents') ); 416 } 417 418 echo $this->disp_params['list_start']; 419 420 while( $Item = & $ItemList->get_item() ) 421 { 422 echo $this->disp_params['item_start']; 423 $Item->title( array( 424 'link_type' => 'permalink', 425 ) ); 426 echo $this->disp_params['item_end']; 427 } 428 429 echo $this->disp_params['list_end']; 430 431 echo $this->disp_params['block_end']; 432 } 433 434 435 /** 436 * List of items by category 437 * 438 * @param array MUST contain at least the basic display params 439 */ 440 function disp_cat_item_list( $link_type = 'linkto_url' ) 441 { 442 global $BlogCache, $Blog; 443 444 $linkblog = $Blog->get('links_blog_ID'); 445 446 if( ! $linkblog ) 447 { // No linkblog blog requested for this blog 448 return; 449 } 450 451 // Load the linkblog blog: 452 $link_Blog = & $BlogCache->get_by_ID( $linkblog, false ); 453 454 if( empty($link_Blog) ) 455 { 456 echo $this->disp_params['block_start']; 457 echo T_('The requested Blog doesn\'t exist any more!'); 458 echo $this->disp_params['block_end']; 459 return; 460 } 461 462 463 # This is the list of categories to restrict the linkblog to (cats will be displayed recursively) 464 # Example: $linkblog_cat = '4,6,7'; 465 $linkblog_cat = ''; 466 467 # This is the array if categories to restrict the linkblog to (non recursive) 468 # Example: $linkblog_catsel = array( 4, 6, 7 ); 469 $linkblog_catsel = array(); 470 471 // Compile cat array stuff: 472 $linkblog_cat_array = array(); 473 $linkblog_cat_modifier = ''; 474 compile_cat_array( $linkblog_cat, $linkblog_catsel, /* by ref */ $linkblog_cat_array, /* by ref */ $linkblog_cat_modifier, $linkblog ); 475 476 $LinkblogList = & new ItemListLight( $link_Blog ); 477 478 $LinkblogList->set_filters( array( 479 'cat_array' => $linkblog_cat_array, 480 'cat_modifier' => $linkblog_cat_modifier, 481 'orderby' => 'main_cat_ID title', 482 'order' => 'ASC', 483 'unit' => 'all', 484 ) ); 485 486 // Run the query: 487 $LinkblogList->query(); 488 489 if( ! $LinkblogList->get_num_rows() ) 490 { // empty list: 491 return; 492 } 493 494 echo $this->disp_params['block_start']; 495 496 $this->disp_title( T_('Linkblog') ); 497 498 echo $this->disp_params['list_start']; 499 500 /** 501 * @var ItemLight 502 */ 503 while( $Item = & $LinkblogList->get_category_group() ) 504 { 505 // Open new cat: 506 echo $this->disp_params['item_start']; 507 $Item->main_category(); 508 echo $this->disp_params['group_start']; 509 510 while( $Item = & $LinkblogList->get_item() ) 511 { 512 echo $this->disp_params['item_start']; 513 514 $Item->title( array( 515 'link_type' => $link_type, 516 ) ); 517 518 /* 519 $Item->content_teaser( array( 520 'before' => '', 521 'after' => ' ', 522 'disppage' => 1, 523 'stripteaser' => false, 524 ) ); 525 526 $Item->more_link( array( 527 'before' => '', 528 'after' => '', 529 'link_text' => T_('more').' »', 530 ) ); 531 */ 532 533 534 echo $this->disp_params['item_end']; 535 } 536 537 // Close cat 538 echo $this->disp_params['group_end']; 539 echo $this->disp_params['item_end']; 540 } 541 542 // Close the global list 543 echo $this->disp_params['list_end']; 544 545 echo $this->disp_params['block_end']; 546 } 547 548 549 /** 550 * List of collections/blogs 551 * 552 * @param array MUST contain at least the basic display params 553 */ 554 function disp_coll_list( $filter = 'public' ) 555 { 556 /** 557 * @var Blog 558 */ 559 global $Blog; 560 561 echo $this->disp_params['block_start']; 562 563 $this->disp_title( T_('Blogs') ); 564 565 echo $this->disp_params['list_start']; 566 567 /** 568 * @var BlogCache 569 */ 570 $BlogCache = & get_Cache( 'BlogCache' ); 571 572 if( $filter == 'owner' ) 573 { // Load blogs of same owner 574 $blog_array = $BlogCache->load_owner_blogs( $Blog->owner_user_ID, 'ID' ); 575 } 576 else 577 { // Load all public blogs 578 $blog_array = $BlogCache->load_public( 'ID' ); 579 } 580 581 foreach( $blog_array as $l_blog_ID ) 582 { // Loop through all public blogs: 583 584 $l_Blog = & $BlogCache->get_by_ID( $l_blog_ID ); 585 586 if( $Blog && $l_blog_ID == $Blog->ID ) 587 { // This is the blog being displayed on this page: 588 echo $this->disp_params['item_selected_start']; 589 $link_class = $this->disp_params['link_selected_class']; 590 } 591 else 592 { 593 echo $this->disp_params['item_start']; 594 $link_class = $this->disp_params['link_default_class'];; 595 } 596 597 echo '<a href="'.$l_Blog->gen_blogurl().'" class="'.$link_class.'" title="' 598 .$l_Blog->dget( 'name', 'htmlattr' ).'">'; 599 600 if( $Blog && $l_blog_ID == $Blog->ID ) 601 { // This is the blog being displayed on this page: 602 echo $this->disp_params['item_selected_text_start']; 603 echo $l_Blog->dget( 'shortname', 'htmlbody' ); 604 echo $this->disp_params['item_selected_text_end']; 605 echo '</a>'; 606 echo $this->disp_params['item_selected_end']; 607 } 608 else 609 { 610 echo $this->disp_params['item_text_start']; 611 echo $l_Blog->dget( 'shortname', 'htmlbody' ); 612 echo $this->disp_params['item_text_end']; 613 echo '</a>'; 614 echo $this->disp_params['item_end']; 615 } 616 } 617 618 echo $this->disp_params['list_end']; 619 620 echo $this->disp_params['block_end']; 621 } 622 623 624 /** 625 * Insert object into DB based on previously recorded changes. 626 * 627 * @return boolean true on success 628 */ 629 function dbinsert() 630 { 631 global $DB; 632 633 if( $this->ID != 0 ) die( 'Existing object cannot be inserted!' ); 634 635 $DB->begin(); 636 637 $order_max = $DB->get_var( 638 'SELECT MAX(wi_order) 639 FROM T_widget 640 WHERE wi_coll_ID = '.$this->coll_ID.' 641 AND wi_sco_name = '.$DB->quote($this->sco_name), 0, 0, 'Get current max order' ); 642 643 $this->set( 'order', $order_max+1 ); 644 645 $res = parent::dbinsert(); 646 647 $DB->commit(); 648 649 return $res; 650 } 651 } 652 653 654 /* 655 * $Log: _widget.class.php,v $ 656 * Revision 1.9 2007/11/04 01:10:57 fplanque 657 * skin cleanup continued 658 * 659 * Revision 1.8 2007/11/03 04:56:04 fplanque 660 * permalink / title links cleanup 661 * 662 * Revision 1.7 2007/09/24 12:08:24 yabs 663 * minor bug fix 664 * 665 * Revision 1.6 2007/09/23 18:57:15 fplanque 666 * filter handling fixes 667 * 668 * Revision 1.5 2007/09/17 18:03:52 blueyed 669 * Fixed cases for no $Blog, e.g. with contact.php 670 * 671 * Revision 1.4 2007/09/04 19:48:33 fplanque 672 * small fixes 673 * 674 * Revision 1.3 2007/06/30 20:37:50 fplanque 675 * fixes 676 * 677 * Revision 1.2 2007/06/29 00:25:02 fplanque 678 * minor 679 * 680 * Revision 1.1 2007/06/25 11:01:57 fplanque 681 * MODULES (refactored MVC) 682 * 683 * Revision 1.11 2007/06/23 00:12:38 fplanque 684 * cleanup 685 * 686 * Revision 1.10 2007/06/21 23:28:18 blueyed 687 * todos 688 * 689 * Revision 1.9 2007/06/21 00:44:36 fplanque 690 * linkblog now a widget 691 * 692 * Revision 1.8 2007/06/20 23:12:51 fplanque 693 * "Who's online" moved to a plugin 694 * 695 * Revision 1.7 2007/06/20 21:42:13 fplanque 696 * implemented working widget/plugin params 697 * 698 * Revision 1.6 2007/06/20 14:25:00 fplanque 699 * fixes 700 * 701 * Revision 1.5 2007/06/20 13:19:29 fplanque 702 * Free html widget 703 * 704 * Revision 1.4 2007/06/20 00:48:18 fplanque 705 * some real life widget settings 706 * 707 * Revision 1.3 2007/06/19 20:42:53 fplanque 708 * basic demo of widget params handled by autoform_* 709 * 710 * Revision 1.2 2007/06/19 00:03:26 fplanque 711 * doc / trying to make sense of automatic settings forms generation. 712 * 713 * Revision 1.1 2007/06/18 21:25:47 fplanque 714 * one class per core widget 715 * 716 * Revision 1.26 2007/05/28 15:18:30 fplanque 717 * cleanup 718 * 719 * Revision 1.25 2007/05/28 01:36:24 fplanque 720 * enhanced blog list widget 721 * 722 * Revision 1.24 2007/05/09 01:58:57 fplanque 723 * Widget to display other blogs from same owner 724 * 725 * Revision 1.23 2007/05/09 01:00:24 fplanque 726 * optimized querying for blog lists 727 * 728 * Revision 1.22 2007/05/08 00:42:07 fplanque 729 * public blog list as a widget 730 * 731 * Revision 1.21 2007/05/07 23:26:19 fplanque 732 * public blog list as a widget 733 * 734 * Revision 1.20 2007/04/26 00:11:06 fplanque 735 * (c) 2007 736 * 737 * Revision 1.19 2007/03/27 18:00:13 blueyed 738 * Fixed E_FATAL: "Cannot redeclare ComponentWidget::$order"; doc 739 * 740 * Revision 1.18 2007/03/26 17:12:40 fplanque 741 * allow moving of widgets 742 * 743 * Revision 1.17 2007/03/26 14:21:30 fplanque 744 * better defaults for pages implementation 745 * 746 * Revision 1.16 2007/03/26 12:59:18 fplanque 747 * basic pages support 748 * 749 * Revision 1.15 2007/03/25 13:19:17 fplanque 750 * temporarily disabled dynamic and static urls. 751 * may become permanent in favor of a caching mechanism. 752 * 753 * Revision 1.14 2007/03/04 21:46:39 fplanque 754 * category directory / albums 755 * 756 * Revision 1.13 2007/02/05 00:35:43 fplanque 757 * small adjustments 758 * 759 * Revision 1.12 2007/01/25 13:41:50 fplanque 760 * wording 761 * 762 * Revision 1.11 2007/01/14 03:24:30 fplanque 763 * widgets complete proof of concept with multiple skins 764 * 765 * Revision 1.10 2007/01/14 01:32:11 fplanque 766 * more widgets supported! :) 767 * 768 * Revision 1.9 2007/01/13 22:28:12 fplanque 769 * doc 770 * 771 * Revision 1.8 2007/01/13 18:40:33 fplanque 772 * SkinTag/Widget plugins now get displayed inside of the containers. 773 * next step: adapt all default skins to use this. 774 * 775 * Revision 1.7 2007/01/13 14:35:42 blueyed 776 * todo: $Plugin should be a ref?! 777 * 778 * Revision 1.6 2007/01/13 04:10:44 fplanque 779 * implemented "add" support for plugin widgets 780 * 781 * Revision 1.5 2007/01/12 02:40:26 fplanque 782 * widget default params proof of concept 783 * (param customization to be done) 784 * 785 * Revision 1.4 2007/01/11 20:44:19 fplanque 786 * skin containers proof of concept 787 * (no params handling yet though) 788 * 789 * Revision 1.3 2007/01/11 02:57:25 fplanque 790 * implemented removing widgets from containers 791 * 792 * Revision 1.2 2007/01/08 23:45:48 fplanque 793 * A little less rough widget manager... 794 * (can handle multiple instances of same widget and remembers order) 795 * 796 * Revision 1.1 2007/01/08 21:55:42 fplanque 797 * very rough widget handling 798 */ 799 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Thu Nov 29 23:58:50 2007 | par Balluche grâce à PHPXref 0.7 |
|