00001 <?php
00021 class Post extends QueryRecord implements IsContent
00022 {
00023
00024 static $post_status_list = array();
00025 static $post_type_list_active = array();
00026 static $post_type_list_all = array();
00027
00028 private $tags = null;
00029 private $comments_object = null;
00030 private $author_object = null;
00031 private $tokens = null;
00032
00033 private $inforecords = null;
00034
00035 protected $url_args;
00036
00042 public static function list_active_post_types( $refresh = false )
00043 {
00044 if ( ( ! $refresh ) && ( ! empty( self::$post_type_list_active ) ) ) {
00045 return self::$post_type_list_active;
00046 }
00047 self::$post_type_list_active['any'] = 0;
00048 $sql = 'SELECT * FROM {posttype} WHERE active = 1 ORDER BY id ASC';
00049 $results = DB::get_results( $sql );
00050 foreach ( $results as $result ) {
00051 self::$post_type_list_active[$result->name] = $result->id;
00052 }
00053 return self::$post_type_list_active;
00054 }
00055
00061 public static function list_all_post_types( $refresh = false )
00062 {
00063 if ( ( ! $refresh ) && ( ! empty( self::$post_type_list_all ) ) ) {
00064 return self::$post_type_list_all;
00065 }
00066 self::$post_type_list_all['any'] = 0;
00067 $sql = 'SELECT * FROM {posttype} ORDER BY id ASC';
00068 $results = DB::get_results( $sql );
00069 foreach ( $results as $result ) {
00070 self::$post_type_list_all[$result->name] = array(
00071 'id' => $result->id,
00072 'active' => $result->active
00073 );
00074 }
00075 return self::$post_type_list_all;
00076 }
00077
00083 public static function activate_post_type( $type )
00084 {
00085 $all_post_types = Post::list_all_post_types( true );
00086
00087
00088 if ( array_key_exists( $type, $all_post_types ) ) {
00089 if ( ! $all_post_types[$type]['active'] == 1 ) {
00090
00091 $sql = 'UPDATE {posttype} SET active = 1 WHERE id = ' . $all_post_types[$type]['id'];
00092 DB::query( $sql );
00093 }
00094 return true;
00095 }
00096 else {
00097 return false;
00098 }
00099 }
00100
00106 public static function deactivate_post_type( $type )
00107 {
00108 $active_post_types = Post::list_active_post_types( false );
00109
00110 if ( array_key_exists( $type, $active_post_types ) ) {
00111
00112 $sql = 'UPDATE {posttype} SET active = 0 WHERE id = ' . $active_post_types[$type];
00113 DB::query( $sql );
00114 return true;
00115 }
00116 return false;
00117 }
00118
00125 public static function list_post_statuses( $all = true, $refresh = false )
00126 {
00127 $statuses = array();
00128 $statuses['any'] = 0;
00129 if ( $refresh || empty( self::$post_status_list ) ) {
00130 $sql = 'SELECT * FROM {poststatus} ORDER BY id ASC';
00131 $results = DB::get_results( $sql );
00132 self::$post_status_list = $results;
00133 }
00134 foreach ( self::$post_status_list as $status ) {
00135 if ( $all instanceof Post ) {
00136 if( ! $status->internal || $status->id == $all->status ) {
00137 $statuses[$status->name] = $status->id;
00138 }
00139 }
00140 elseif ( $all ) {
00141 $statuses[$status->name] = $status->id;
00142 }
00143 elseif ( ! $status->internal ) {
00144 $statuses[$status->name] = $status->id;
00145 }
00146 }
00147 return $statuses;
00148 }
00149
00155 public static function status( $name )
00156 {
00157 $statuses = Post::list_post_statuses();
00158 if ( is_numeric( $name ) && ( FALSE !== in_array( $name, $statuses ) ) ) {
00159 return $name;
00160 }
00161 if ( isset( $statuses[strtolower( $name )] ) ) {
00162 return $statuses[strtolower( $name )];
00163 }
00164 return false;
00165 }
00166
00172 public static function status_name( $status )
00173 {
00174 $statuses = array_flip( Post::list_post_statuses() );
00175 if ( is_numeric( $status ) && isset( $statuses[$status] ) ) {
00176 return $statuses[$status];
00177 }
00178 if ( FALSE !== in_array( $status, $statuses ) ) {
00179 return $status;
00180 }
00181 return '';
00182 }
00183
00189 public static function type( $name )
00190 {
00191 $types = Post::list_active_post_types();
00192 if ( is_numeric( $name ) && ( FALSE !== in_array( $name, $types ) ) ) {
00193 return $name;
00194 }
00195 if ( isset( $types[strtolower( $name )] ) ) {
00196 return $types[strtolower( $name )];
00197 }
00198 return false;
00199 }
00200
00206 public static function type_name( $type )
00207 {
00208 $types = array_flip( Post::list_active_post_types() );
00209 if ( is_numeric( $type ) && isset( $types[$type] ) ) {
00210 return $types[$type];
00211 }
00212 if ( FALSE !== in_array( $type, $types ) ) {
00213 return $type;
00214 }
00215 return '';
00216 }
00217
00224 public static function add_new_type( $type, $active = true )
00225 {
00226
00227 $types = self::list_all_post_types( true );
00228
00229 if ( ! array_key_exists( $type, $types ) ) {
00230
00231 DB::query( 'INSERT INTO {posttype} (name, active) VALUES (?, ?)', array( $type, $active ) );
00232 }
00233 elseif ( $types[$type]['active'] == 0 ) {
00234
00235 self::activate_post_type( $type );
00236 }
00237 ACL::create_token( 'post_' . Utils::slugify($type), _t('Permissions to posts of type "%s"', array($type) ), _t('Content'), TRUE );
00238
00239
00240
00241 $types = self::list_active_post_types( true );
00242 $types = self::list_all_post_types( true );
00243 }
00244
00251 public static function add_new_status( $status, $internal = false )
00252 {
00253
00254 $statuses = self::list_post_statuses( true );
00255 if ( ! array_key_exists( $status, $statuses ) ) {
00256
00257 $internal = intval( $internal );
00258 DB::query( 'INSERT INTO {poststatus} (name, internal) VALUES (?, ?)', array( $status, $internal ) );
00259
00260
00261 $statuses = self::list_post_statuses( true, true );
00262 }
00263 }
00264
00269 public static function default_fields()
00270 {
00271 return array(
00272 'id' => 0,
00273 'slug' => '',
00274 'title' => '',
00275 'guid' => '',
00276 'content' => '',
00277 'cached_content' => '',
00278 'user_id' => 0,
00279 'status' => Post::status( 'draft' ),
00280 'pubdate' => HabariDateTime::date_create(),
00281 'updated' => HabariDateTime::date_create(),
00282 'modified' => HabariDateTime::date_create(),
00283 'content_type' => Post::type( 'entry' )
00284 );
00285 }
00286
00291 public function __construct( $paramarray = array() )
00292 {
00293
00294 $this->fields = array_merge(
00295 self::default_fields(),
00296 $this->fields
00297 );
00298
00299 parent::__construct( $paramarray );
00300 if ( isset( $this->fields['tags'] ) ) {
00301 $this->tags = $this->parsetags( $this->fields['tags'] );
00302 unset( $this->fields['tags'] );
00303 }
00304
00305 $this->exclude_fields( 'id' );
00306
00307 }
00308
00319 static function get( $paramarray = array() )
00320 {
00321
00322 $defaults = array (
00323 'where' => array(
00324 array(
00325 'status' => Post::status( 'published' ),
00326 ),
00327 ),
00328 'fetch_fn' => 'get_row',
00329 );
00330 foreach ( $defaults['where'] as $index => $where ) {
00331 $defaults['where'][$index] = array_merge( $where, Utils::get_params( $paramarray ) );
00332 }
00333
00334 $defaults['limit'] = 1;
00335
00336 return Posts::get( $defaults );
00337 }
00338
00345 static function create( $paramarray )
00346 {
00347 $post = new Post( $paramarray );
00348 $post->insert();
00349 return $post;
00350 }
00351
00357 private function setslug()
00358 {
00359
00360
00361 if ( isset( $this->newfields['slug']) && $this->newfields['slug'] != '' ) {
00362 $value = $this->newfields['slug'];
00363 }
00364
00365 elseif ( isset( $this->newfields['slug']) && $this->newfields['slug'] == '' ) {
00366 if ( $this->fields['status'] == Post::status( 'draft' ) || ( $this->fields['status'] != Post::status( 'draft' ) && $this->newfields['status'] != Post::status( 'draft' ) ) ) {
00367 if ( isset( $this->newfields['title'] ) && $this->newfields['title'] != '' ) {
00368 $value = $this->newfields['title'];
00369 }
00370 else {
00371 $value = $this->fields['title'];
00372 }
00373 }
00374 }
00375
00376 elseif ( $this->fields['slug'] != '' ) {
00377 $value = $this->fields['slug'];
00378 }
00379
00380 elseif ( isset( $this->newfields['title'] ) && $this->newfields['title'] != '' ) {
00381 $value = $this->newfields['title'];
00382 }
00383
00384 elseif ( $this->fields['title'] != '' ) {
00385 $value = $this->fields['title'];
00386 }
00387
00388 else {
00389 $value = 'Post';
00390 }
00391
00392
00393 $slug = Plugins::filter( 'post_setslug', $value );
00394 $slug = Utils::slugify( $slug );
00395 $postfix = '';
00396 $postfixcount = 0;
00397 do {
00398 if ( ! $slugcount = DB::get_row( 'SELECT COUNT(slug) AS ct FROM {posts} WHERE slug = ?;', array( $slug . $postfix ) ) ) {
00399 Utils::debug( DB::get_errors() );
00400 exit;
00401 }
00402 if ( $slugcount->ct != 0 ) {
00403 $postfix = "-" . ( ++$postfixcount );
00404 }
00405 } while ( $slugcount->ct != 0 );
00406
00407 return $this->newfields['slug'] = $slug . $postfix;
00408 }
00409
00413 private function setguid()
00414 {
00415 if ( ! isset( $this->newfields['guid'] )
00416 || ( $this->newfields['guid'] == '' )
00417 || ( $this->newfields['guid'] == '//?p=' )
00418 ) {
00419 $result = 'tag:' . Site::get_url( 'hostname' ) . ',' . date( 'Y' ) . ':' . rawurlencode($this->setslug()) . '/' . time();
00420 $this->newfields['guid'] = $result;
00421 }
00422 return $this->newfields['guid'];
00423 }
00424
00431 private function setstatus( $value )
00432 {
00433 $statuses = Post::list_post_statuses();
00434 $fieldname = isset( $this->fields['status'] ) ? 'newfields' : 'fields';
00435 if ( is_numeric( $value ) && in_array( $value, $statuses ) ) {
00436 return $this->{"$fieldname"}['status'] = $value;
00437 }
00438 elseif ( array_key_exists( $value, $statuses ) ) {
00439 return $this->{"$fieldname"}['status'] = Post::status( $value );
00440 }
00441
00442 return false;
00443 }
00444
00451 private static function parsetags( $tags )
00452 {
00453 if ( is_string( $tags ) ) {
00454 if ( '' === $tags ) {
00455 return array();
00456 }
00457
00458 $rez = array( '\\"'=>':__unlikely_quote__:', '\\\''=>':__unlikely_apos__:' );
00459 $zer = array( ':__unlikely_quote__:'=>'"', ':__unlikely_apos__:'=>"'" );
00460
00461 $tagstr = str_replace( array_keys( $rez ), $rez, $tags );
00462
00463 preg_match_all( '/((("|((?<= )|^)\')\\S([^\\3]*?)\\3((?=[\\W])|$))|[^,])+/u', $tagstr, $matches );
00464
00465 $tags = array_map( 'trim', $matches[0] );
00466 $tags = preg_replace( array_fill( 0, count( $tags ), '/^(["\'])(((?!").)+)(\\1)$/'), '$2', $tags );
00467
00468 $tags = str_replace( array_keys( $zer ), $zer, $tags );
00469
00470 return $tags;
00471 }
00472 elseif ( is_array( $tags ) ) {
00473 return $tags;
00474 }
00475 }
00476
00480 private function save_tags()
00481 {
00482 return Tags::save_associations( $this->tags, $this->id );
00483 }
00484
00489 public function insert()
00490 {
00491 $this->newfields['updated'] = HabariDateTime::date_create();
00492 $this->newfields['modified'] = $this->newfields['updated'];
00493 $this->setguid();
00494
00495 $allow = true;
00496 $allow = Plugins::filter( 'post_insert_allow', $allow, $this );
00497 if ( ! $allow ) {
00498 return;
00499 }
00500 Plugins::act( 'post_insert_before', $this );
00501
00502
00503 foreach ( $this->fields as $fieldname => $value ) {
00504 Plugins::act( 'post_update_' . $fieldname, $this, ( $this->id == 0 ) ? null : $value, $this->$fieldname );
00505 }
00506
00507 Plugins::act( 'post_status_' . self::status_name( $this->status ), $this, null );
00508
00509 $result = parent::insertRecord( DB::table( 'posts' ) );
00510 $this->newfields['id'] = DB::last_insert_id();
00511 $this->fields = array_merge( $this->fields, $this->newfields );
00512 $this->newfields = array();
00513 $this->info->commit( DB::last_insert_id() );
00514 $this->save_tags();
00515 $this->create_default_permissions();
00516 EventLog::log( sprintf(_t('New post %1$s (%2$s); Type: %3$s; Status: %4$s'), $this->id, $this->slug, Post::type_name( $this->content_type ), $this->statusname), 'info', 'content', 'habari' );
00517 Plugins::act( 'post_insert_after', $this );
00518
00519
00520 if( $this->status == Post::status( 'scheduled' ) ) {
00521 Posts::update_scheduled_posts_cronjob();
00522 }
00523
00524 return $result;
00525 }
00526
00532 public function update( $minor = true )
00533 {
00534 $this->modified = HabariDateTime::date_create();
00535 if ( ! $minor && $this->status != Post::status( 'draft' ) ) {
00536 $this->updated = $this->modified;
00537 }
00538
00539 if ( isset( $this->fields['guid'] ) ) {
00540 unset( $this->newfields['guid'] );
00541 }
00542
00543 $allow = true;
00544 $allow = Plugins::filter( 'post_update_allow', $allow, $this );
00545 if ( ! $allow ) {
00546 return;
00547 }
00548 Plugins::act( 'post_update_before', $this );
00549
00550
00551 if ( isset( $this->newfields['slug'] ) ) {
00552 if ( $this->fields['slug'] != $this->newfields['slug'] ) {
00553 $this->setslug();
00554 }
00555 }
00556
00557
00558
00559
00560 foreach ( $this->newfields as $fieldname => $value ) {
00561 Plugins::act( 'post_update_' . $fieldname, $this, $this->fields[$fieldname], $value );
00562 }
00563
00564
00565 if ( isset( $this->newfields['status'] ) && $this->fields['status'] != $this->newfields['status'] ) {
00566 Plugins::act( 'post_status_' . self::status_name( $this->newfields['status'] ), $this, $this->fields['status'] );
00567 }
00568
00569 $result = parent::updateRecord( DB::table( 'posts' ), array( 'id' => $this->id ) );
00570
00571
00572 if ( $this->fields['status'] == Post::status( 'scheduled' ) || $this->status == Post::status( 'scheduled' ) ) {
00573 Posts::update_scheduled_posts_cronjob();
00574 }
00575
00576 $this->fields = array_merge( $this->fields, $this->newfields );
00577 $this->newfields = array();
00578 $this->save_tags();
00579 $this->info->commit();
00580 Plugins::act( 'post_update_after', $this );
00581 return $result;
00582 }
00583
00588 public function delete()
00589 {
00590 $allow = true;
00591 $allow = Plugins::filter( 'post_delete_allow', $allow, $this );
00592 if ( ! $allow ) {
00593 return;
00594 }
00595
00596 Plugins::act( 'post_delete_before', $this );
00597
00598
00599 foreach ( $this->get_tags() as $tag_slug => $tag_text ) {
00600 $tag = Tags::get_by_slug( $tag_slug );
00601 Tag::detach_from_post( $tag->id, $this->id );
00602 }
00603
00604
00605 if ( $this->comments->count() > 0 ) {
00606 $this->comments->delete();
00607 }
00608
00609 $this->info->delete_all();
00610
00611 $this->delete_tokens();
00612
00613 $result = parent::deleteRecord( DB::table( 'posts' ), array( 'slug'=>$this->slug ) );
00614 EventLog::log( sprintf(_t('Post %1$s (%2$s) deleted.'), $this->id, $this->slug), 'info', 'content', 'habari' );
00615
00616
00617 if( $this->status == Post::status( 'scheduled' ) ) {
00618 Posts::update_scheduled_posts_cronjob();
00619 }
00620
00621
00622 Plugins::act( 'post_delete_after', $this );
00623 return $result;
00624 }
00625
00631 public function publish()
00632 {
00633 if ( $this->status == Post::status( 'published' ) ) {
00634 return true;
00635 }
00636 $allow = true;
00637 $allow = Plugins::filter( 'post_publish_allow', $allow, $this );
00638 if ( ! $allow ) {
00639 return;
00640 }
00641 Plugins::act( 'post_publish_before', $this );
00642
00643 if ( $this->status != Post::status( 'scheduled' ) ) {
00644 $this->pubdate = HabariDateTime::date_create();
00645 }
00646
00647 if ( $this->status == Post::status( 'scheduled' ) ) {
00648 $this->get_tags();
00649 $msg = sprintf(_t('Scheduled Post %1$s (%2$s) published at %3$s.'), $this->id, $this->slug, $this->pubdate->format());
00650 }
00651 else {
00652 $msg = sprintf(_t('Post %1$s (%2$s) published.'), $this->id, $this->slug);
00653 }
00654
00655 $this->status = Post::status( 'published' );
00656 $result = $this->update( false );
00657 EventLog::log( $msg, 'info', 'content', 'habari' );
00658
00659
00660 Plugins::act( 'post_publish_after', $this );
00661 return $result;
00662 }
00663
00670 public function __get( $name )
00671 {
00672 $fieldnames = array_merge( array_keys( $this->fields ), array( 'permalink', 'tags', 'comments', 'comment_count', 'approved_comment_count', 'comment_feed_link', 'author', 'editlink' ) );
00673 if ( !in_array( $name, $fieldnames ) && strpos( $name, '_' ) !== false ) {
00674 preg_match( '/^(.*)_([^_]+)$/', $name, $matches );
00675 list( $junk, $name, $filter )= $matches;
00676 }
00677 else {
00678 $filter = false;
00679 }
00680
00681 switch ( $name ) {
00682 case 'statusname':
00683 $out = self::status_name( $this->status );
00684 break;
00685 case 'typename':
00686 $out = self::type_name( $this->content_type );
00687 break;
00688 case 'permalink':
00689 $out = $this->get_permalink();
00690 break;
00691 case 'editlink':
00692 $out = $this->get_editlink();
00693 break;
00694 case 'tags':
00695 $out = $this->get_tags();
00696 break;
00697 case 'comments':
00698 $out = $this->get_comments();
00699 break;
00700 case 'comment_count':
00701 $out = $this->get_comments()->count();
00702 break;
00703 case 'approved_comment_count':
00704 $out = Comments::count_by_id( $this->id );
00705 break;
00706 case 'comment_feed_link':
00707 $out = $this->get_comment_feed_link();
00708 break;
00709 case 'author':
00710 $out = $this->get_author();
00711 break;
00712 case 'info':
00713 $out = $this->get_info();
00714 break;
00715 default:
00716 $out = parent::__get( $name );
00717 break;
00718 }
00719 $out = Plugins::filter( "post_get", $out, $name, $this );
00720 $out = Plugins::filter( "post_{$name}", $out, $this );
00721 if ( $filter ) {
00722 $out = Plugins::filter( "post_{$name}_{$filter}", $out, $this );
00723 }
00724 return $out;
00725 }
00726
00733 public function __set( $name, $value )
00734 {
00735 switch( $name ) {
00736 case 'pubdate':
00737 case 'updated':
00738 case 'modified':
00739 if ( !($value instanceOf HabariDateTime) ) {
00740 $value = HabariDateTime::date_create($value);
00741 }
00742 break;
00743 case 'tags':
00744 if ( is_array( $value) ) {
00745 return $this->tags = $value;
00746 }
00747 else {
00748 return $this->tags = $this->parsetags( $value );
00749 }
00750 case 'status':
00751 return $this->setstatus( $value );
00752 }
00753 return parent::__set( $name, $value );
00754 }
00755
00762 public function __call( $name, $args )
00763 {
00764 array_unshift($args, 'post_call_' . $name, null, $this);
00765 return call_user_func_array(array('Plugins', 'filter'), $args);
00766 }
00767
00773 public function get_form($context)
00774 {
00775 $form = new FormUI('create-content');
00776 $form->class[] = 'create';
00777
00778 $newpost = ( 0 === $this->id );
00779
00780
00781 if ( !$newpost ) {
00782 $post_links = $form->append('wrapper', 'post_links');
00783 $permalink = ( $this->status != Post::status( 'published' ) ) ? $this->permalink . '?preview=1' : $this->permalink;
00784 $post_links->append('static', 'post_permalink', '<a href="'. $permalink .'" class="viewpost" >'.( $this->status != Post::status('published') ? _t('Preview Post') : _t('View Post') ).'</a>');
00785 $post_links->class ='container';
00786 }
00787
00788
00789 $form->append('text', 'title', 'null:null', _t('Title'), 'admincontrol_text');
00790 $form->title->class[] = 'important';
00791 $form->title->class[] = 'check-change';
00792 $form->title->tabindex = 1;
00793 $form->title->value = $this->title;
00794
00795
00796 if ( count( Plugins::get_by_interface( 'MediaSilo' ) ) ) {
00797 $form->append('silos', 'silos');
00798 $form->silos->silos = Media::dir();
00799 }
00800
00801
00802 $form->append('textarea', 'content', 'null:null', _t('Content'), 'admincontrol_textarea');
00803 $form->content->class[] = 'resizable';
00804 $form->content->class[] = 'check-change';
00805 $form->content->tabindex = 2;
00806 $form->content->value = $this->content;
00807 $form->content->raw = true;
00808
00809
00810 $form->append('text', 'tags', 'null:null', _t('Tags, separated by, commas'), 'admincontrol_text');
00811 $form->tags->class = 'check-change';
00812 $form->tags->tabindex = 3;
00813 $form->tags->value = implode(', ', $this->get_tags());
00814
00815
00816 $publish_controls = $form->append('tabs', 'publish_controls');
00817
00818
00819
00820 $statuses = Post::list_post_statuses( $this );
00821 unset( $statuses[array_search( 'any', $statuses )] );
00822 $statuses = Plugins::filter( 'admin_publish_list_post_statuses', $statuses );
00823
00824 $settings = $publish_controls->append('fieldset', 'settings', _t('Settings'));
00825
00826 $settings->append('select', 'status', 'null:null', _t('Content State'), array_flip($statuses), 'tabcontrol_select');
00827 $settings->status->value = $this->status;
00828
00829
00830 if ( $newpost ) {
00831 $settings->append('hidden', 'minor_edit', 'null:null');
00832 $settings->minor_edit->value = false;
00833 }
00834 else {
00835 $settings->append('checkbox', 'minor_edit', 'null:null', _t('Minor Edit'), 'tabcontrol_checkbox');
00836 $settings->minor_edit->value = true;
00837 $form->append('hidden', 'modified', 'null:null')->value = $this->modified;
00838 }
00839
00840 $settings->append('checkbox', 'comments_enabled', 'null:null', _t('Comments Allowed'), 'tabcontrol_checkbox');
00841 $settings->comments_enabled->value = $this->info->comments_disabled ? false : true;
00842
00843 $settings->append('text', 'pubdate', 'null:null', _t('Publication Time'), 'tabcontrol_text');
00844 $settings->pubdate->value = $this->pubdate->format('Y-m-d H:i:s');
00845
00846 $settings->append('hidden', 'updated', 'null:null' );
00847 $settings->updated->value = $this->updated->int;
00848
00849 $settings->append('text', 'newslug', 'null:null', _t('Content Address'), 'tabcontrol_text');
00850 $settings->newslug->value = $this->slug;
00851
00852
00853 $buttons = $form->append('fieldset', 'buttons');
00854 $buttons->template = 'admincontrol_buttons';
00855 $buttons->class[] = 'container';
00856 $buttons->class[] = 'buttons';
00857 $buttons->class[] = 'publish';
00858
00859
00860 $require_any = array( 'own_posts' => 'create', 'post_any' => 'create', 'post_' . Post::type_name( $this->content_type ) => 'create' );
00861 if ( ( $newpost && User::identify()->can_any( $require_any ) ) || ( !$newpost && ACL::access_check( $this->get_access(), 'edit' ) ) ) {
00862 $buttons->append('submit', 'save', _t('Save'), 'admincontrol_submit');
00863 $buttons->save->tabindex = 4;
00864 }
00865
00866
00867 $form->append('hidden', 'content_type', 'null:null');
00868 $form->content_type->id = 'content_type';
00869 $form->content_type->value = $this->content_type;
00870 $form->append('hidden', 'post_id', 'null:null');
00871 $form->post_id->id = 'id';
00872 $form->post_id->value = $this->id;
00873 $form->append('hidden', 'slug', 'null:null');
00874 $form->slug->value = $this->slug;
00875
00876
00877 Plugins::act('form_publish', $form, $this, $context);
00878
00879
00880 return $form;
00881 }
00882
00889 public function comment_form($context = 'public')
00890 {
00891
00892 $cookie = 'comment_' . Options::get( 'GUID' );
00893 $commenter_name = '';
00894 $commenter_email = '';
00895 $commenter_url = '';
00896 $commenter_content = '';
00897 $user = User::identify();
00898 if ( isset( $_SESSION['comment'] ) ) {
00899 $details = Session::get_set( 'comment' );
00900 $commenter_name = $details['name'];
00901 $commenter_email = $details['email'];
00902 $commenter_url = $details['url'];
00903 $commenter_content = $details['content'];
00904 }
00905 elseif ( $user->loggedin ) {
00906 $commenter_name = $user->displayname;
00907 $commenter_email = $user->email;
00908 $commenter_url = Site::get_url( 'habari' );
00909 }
00910 elseif ( isset( $_COOKIE[$cookie] ) ) {
00911 list( $commenter_name, $commenter_email, $commenter_url )= explode( '#', $_COOKIE[$cookie] );
00912 }
00913
00914
00915 $form = new FormUI('comment-' . $context, 'comment');
00916 $form->class[] = $context;
00917 $form->class[] = 'commentform';
00918 $form->set_option( 'form_action', URL::get( 'submit_feedback', array( 'id' => $this->id ) ) );
00919
00920
00921 $form->append(
00922 'text',
00923 'cf_commenter',
00924 'null:null',
00925 _t('Name <span class="required">*Required</span>'),
00926 'formcontrol_text'
00927 )->add_validator('validate_required', _t('The Name field value is required'))
00928 ->id = 'comment_name';
00929 $form->cf_commenter->tabindex = 1;
00930 $form->cf_commenter->value = $commenter_name;
00931
00932
00933 $form->append(
00934 'text',
00935 'cf_email',
00936 'null:null',
00937 _t('Email'),
00938 'formcontrol_text'
00939 )->add_validator('validate_email', _t('The Email field value must be a valid email address'))
00940 ->id = 'comment_email';
00941 $form->cf_email->tabindex = 2;
00942 if ( Options::get('comments_require_id') == 1 ) {
00943 $form->cf_email->caption = _t('Email <span class="required">*Required</span>');
00944 }
00945 $form->cf_email->value = $commenter_email;
00946
00947
00948 $form->append(
00949 'text',
00950 'cf_url',
00951 'null:null',
00952 _t('Website'),
00953 'formcontrol_text'
00954 )->add_validator('validate_url', _t('The Web Site field value must be a valid URL'))
00955 ->id = 'comment_url';
00956 $form->cf_url->tabindex = 3;
00957 $form->cf_url->value = $commenter_url;
00958
00959
00960 $form->append(
00961 'text',
00962 'cf_content',
00963 'null:null',
00964 _t('Comment'),
00965 'formcontrol_textarea'
00966 )->add_validator('validate_required', _t('The Content field value is required'))
00967 ->id = 'comment_content';
00968 $form->cf_content->tabindex = 4;
00969 $form->cf_content->value = $commenter_content;
00970
00971
00972 $form->append('submit', 'cf_submit', _t('Submit'), 'formcontrol_submit');
00973 $form->cf_submit->tabindex = 5;
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987 Plugins::act('form_comment', $form, $this, $context);
00988
00989
00990 return $form;
00991 }
00992
00993
00999 private function get_permalink()
01000 {
01001 $content_type = Post::type_name( $this->content_type );
01002 return URL::get(
01003 array(
01004 "display_{$content_type}",
01005 "display_post"
01006 ),
01007 $this,
01008 false
01009 );
01010 }
01011
01016 private function get_editlink()
01017 {
01018 return URL::get('admin', 'page=publish&id=' . $this->id);
01019 }
01020
01026 private function get_tags()
01027 {
01028 if ( empty( $this->tags ) ) {
01029 $result = Tags::get_associations( $this->id );
01030 if ( $result ) {
01031 foreach ( $result as $t ) {
01032 $this->tags[$t->term] = $t->term_display;
01033 }
01034 }
01035 }
01036 if ( count( $this->tags ) == 0 ) {
01037 return array();
01038 }
01039 return $this->tags;
01040 }
01041
01047 private function &get_comments()
01048 {
01049 if ( ! $this->comments_object ) {
01050 $this->comments_object = Comments::by_post_id( $this->id );
01051 }
01052 return $this->comments_object;
01053 }
01054
01060 private function get_comment_feed_link()
01061 {
01062 $content_type = Post::type_name( $this->content_type );
01063 return URL::get( array( "atom_feed_{$content_type}_comments" ), $this, false );
01064 }
01065
01072 private function get_info()
01073 {
01074 if ( ! isset( $this->inforecords ) ) {
01075
01076 if( 0 == $this->id ) {
01077 $this->inforecords = new PostInfo();
01078 }
01079 else {
01080 $this->inforecords = new PostInfo( $this->id );
01081 }
01082 }
01083 else {
01084 $this->inforecords->set_key( $this->id );
01085 }
01086 return $this->inforecords;
01087 }
01088
01094 private function get_author()
01095 {
01096 if ( ! isset( $this->author_object ) ) {
01097
01098 $this->author_object = User::get_by_id( $this->user_id );
01099 }
01100 return $this->author_object;
01101 }
01102
01107 public function get_url_args()
01108 {
01109 if ( !$this->url_args ) {
01110 $arr = array( 'content_type_name' => Post::type_name( $this->content_type ) );
01111 $author = URL::extract_args( $this->author, 'author_' );
01112 $info = URL::extract_args( $this->info, 'info_' );
01113 $this->url_args = array_merge( $author, $info, $arr, $this->to_array(), $this->pubdate->getdate() );
01114 }
01115 return $this->url_args;
01116 }
01117
01123 public function ascend($params = null)
01124 {
01125 return Posts::ascend($this, $params);
01126 }
01127
01133 public function descend($params = null)
01134 {
01135 return Posts::descend($this, $params);
01136 }
01137
01144 public function content_type()
01145 {
01146 return array(Post::type_name($this->content_type), 'Post');
01147 }
01148
01152 public function create_default_tokens()
01153 {
01154 $tokens = array();
01155 $tokens = Plugins::filter('post_tokens', $tokens, $this);
01156 $this->add_tokens( $this->content_type() );
01157 }
01158
01165 public function has_tokens( $tokens )
01166 {
01167 $this->get_tokens();
01168 $tokens = Utils::single_array( $tokens );
01169 $tokens = array_map(array('ACL', 'token_id'), $tokens);
01170 $tokens = array_intersect($tokens, $this->tokens);
01171 if ( count($tokens) == 0 ) {
01172 return false;
01173 }
01174 return $tokens;
01175 }
01176
01181 public function add_tokens( $tokens )
01182 {
01183 $this->get_tokens();
01184 $tokens = Utils::single_array( $tokens );
01185 $tokens = array_map(array('ACL', 'token_id'), $tokens);
01186 $add_tokens = array_diff($tokens, $this->tokens);
01187 $add_tokens = array_unique($add_tokens);
01188 foreach ( $add_tokens as $token_id ) {
01189 DB::insert( '{post_tokens}', array( 'post_id' => $this->id, 'token_id' => $token_id ) );
01190 }
01191 $this->tokens = array_merge($this->tokens, $add_tokens);
01192 $this->tokens = array_unique($this->tokens);
01193 }
01194
01198 public function delete_tokens()
01199 {
01200 DB::delete( '{post_tokens}', array( 'post_id' => $this->id ) );
01201 $this->tokens = array();
01202 }
01203
01208 public function remove_tokens( $tokens )
01209 {
01210 $this->get_tokens();
01211 $tokens = Utils::single_array( $tokens );
01212 $tokens = array_map(array('ACL', 'token_id'), $tokens);
01213 $remove_tokens = array_intersect($tokens, $this->tokens);
01214 foreach ( $remove_tokens as $token_id ) {
01215 DB::delete( '{post_tokens}', array( 'post_id' => $this->id, 'token_id' => $token_id ) );
01216 }
01217 $this->tokens = array_diff($this->tokens, $remove_tokens);
01218 }
01219
01224 public function set_tokens( $tokens )
01225 {
01226 $tokens = Utils::single_array( $tokens );
01227 $new_tokens = array_map(array('ACL', 'token_id'), $tokens);
01228 $new_tokens = array_unique($new_tokens);
01229 DB::delete( '{post_tokens}', array( 'post_id' => $this->id ) );
01230 foreach ( $new_tokens as $token_id ) {
01231 DB::insert( '{post_tokens}', array( 'post_id' => $this->id, 'token_id' => $token_id ) );
01232 }
01233 $this->tokens = $new_tokens;
01234 }
01235
01242 public function get_tokens()
01243 {
01244 if ( empty( $this->tokens ) ) {
01245 $this->tokens = DB::get_column( 'SELECT token_id FROM {post_tokens} WHERE post_id = ?', array($this->id) );
01246 }
01247 return $this->tokens;
01248 }
01249
01255 public function get_access( $user = null )
01256 {
01257 if ( ! $user instanceof User ) {
01258 $user = User::identify();
01259 }
01260
01261 if ( $user->can( 'super_user' ) ) {
01262 return ACL::get_bitmask( 'full' );
01263 }
01264
01265
01266 $tokens = array(
01267 'post_any',
01268 'post_' . Post::type_name( $this->content_type ),
01269 );
01270
01271 if ( $user->id == $this->user_id) {
01272 $tokens[] = 'own_posts';
01273 }
01274
01275 $tokens = array_merge($tokens, $this->get_tokens());
01276
01277
01278 $token_accesses = array();
01279 foreach ( $tokens as $token ) {
01280 $access = ACL::get_user_token_access( $user, $token );
01281 if ( $access instanceof Bitmask ) {
01282 $token_accesses[] = ACL::get_user_token_access( $user, $token )->value;
01283 }
01284 }
01285
01286
01287 if ( in_array( 0, $token_accesses ) ) {
01288 return ACL::get_bitmask( 0 );
01289 }
01290 return ACL::get_bitmask( Utils::array_or( $token_accesses ) );
01291 }
01292
01293 }
01294 ?>