2013-08-28 23:39:43 +00:00
< ? php /** @file */
require_once ( 'include/security.php' );
2013-09-28 18:05:50 +00:00
require_once ( 'include/menu.php' );
2013-10-27 23:30:55 +00:00
require_once ( 'include/widgets.php' );
2013-10-24 22:35:24 +00:00
2013-08-28 23:39:43 +00:00
// When editing a webpage - a dropdown is needed to select a page layout
// On submit, the pdl_select value (which is the mid of an item with item_restrict = ITEM_PDL) is stored in
// the webpage's resource_id, with resource_type 'pdl'.
// Then when displaying a webpage, we can see if it has a pdl attached. If not we'll
// use the default site/page layout.
// If it has a pdl we'll load it as we know the mid and pass the body through comanche_parser() which will generate the
// page layout from the given description
function pdl_selector ( $uid , $current = " " ) {
$o = '' ;
$sql_extra = item_permissions_sql ( $uid );
2013-09-02 01:36:14 +00:00
$r = q ( " select item_id.*, mid from item_id left join item on iid = item.id where item_id.uid = %d and item_id.uid = item.uid and service = 'PDL' order by sid asc " ,
intval ( $owner )
2013-08-28 23:39:43 +00:00
);
$arr = array ( 'channel_id' => $uid , 'current' => $current , 'entries' => $r );
call_hooks ( 'pdl_selector' , $arr );
$entries = $arr [ 'entries' ];
$current = $arr [ 'current' ];
$o .= " <select name= \" pdl_select \" id= \" pdl_select \" size= \" 1 \" > " ;
$entries [] = array ( 'title' => t ( 'Default' ), 'mid' => '' );
foreach ( $entries as $selection ) {
$selected = (( $selection == $current ) ? ' selected="selected" ' : '' );
2013-09-02 01:36:14 +00:00
$o .= " <option value= \" { $selection [ 'mid' ] } \" $selected > { $selection [ 'sid' ] } </option> " ;
2013-08-28 23:39:43 +00:00
}
$o .= '</select>' ;
return $o ;
}
2013-08-29 03:03:01 +00:00
function comanche_parser ( & $a , $s ) {
2013-10-29 00:48:42 +00:00
$cnt = preg_match_all ( " / \ [comment \ ](.*?) \ [ \ /comment \ ]/ism " , $s , $matches , PREG_SET_ORDER );
if ( $cnt ) {
foreach ( $matches as $mtch ) {
$s = str_replace ( $mtch [ 0 ], '' , $s );
}
}
2013-08-29 03:03:01 +00:00
2013-09-03 08:57:11 +00:00
$cnt = preg_match ( " / \ [layout \ ](.*?) \ [ \ /layout \ ]/ism " , $s , $matches );
2013-08-29 03:03:01 +00:00
if ( $cnt )
$a -> page [ 'template' ] = trim ( $matches [ 1 ]);
2013-09-03 08:57:11 +00:00
$cnt = preg_match ( " / \ [theme \ ](.*?) \ [ \ /theme \ ]/ism " , $s , $matches );
2013-08-29 03:03:01 +00:00
if ( $cnt )
$a -> layout [ 'theme' ] = trim ( $matches [ 1 ]);
2013-09-03 08:57:11 +00:00
$cnt = preg_match_all ( " / \ [region=(.*?) \ ](.*?) \ [ \ /region \ ]/ism " , $s , $matches , PREG_SET_ORDER );
2013-08-29 03:03:01 +00:00
if ( $cnt ) {
foreach ( $matches as $mtch ) {
$a -> layout [ 'region_' . $mtch [ 1 ]] = comanche_region ( $a , $mtch [ 2 ]);
}
}
2013-09-05 04:24:57 +00:00
$cnt = preg_match_all ( " / \ [webpage \ ](.*?) \ [ \ /webpage \ ]/ism " , $s , $matches , PREG_SET_ORDER );
if ( $cnt ) {
// only the last webpage definition is used if there is more than one
foreach ( $matches as $mtch ) {
$a -> layout [ 'webpage' ] = comanche_webpage ( $a , $mtch [ 1 ]);
}
}
2013-08-29 03:03:01 +00:00
}
function comanche_menu ( $name ) {
$a = get_app ();
$m = menu_fetch ( $name , $a -> profile [ 'profile_uid' ], get_observer_hash ());
2013-09-28 18:05:50 +00:00
return menu_render ( $m );
2013-08-29 03:03:01 +00:00
}
2013-08-30 03:54:42 +00:00
function comanche_replace_region ( $match ) {
2013-08-29 03:03:01 +00:00
$a = get_app ();
2013-09-03 08:57:11 +00:00
if ( array_key_exists ( $match [ 1 ], $a -> page )) {
2013-08-30 03:54:42 +00:00
return $a -> page [ $match [ 1 ]];
2013-09-03 08:57:11 +00:00
}
2013-08-30 03:54:42 +00:00
}
2013-09-04 07:50:47 +00:00
function comanche_block ( $name ) {
2013-09-30 20:42:29 +00:00
2013-09-04 07:50:47 +00:00
$o = '' ;
2013-10-01 07:36:28 +00:00
$r = q ( " select * from item inner join item_id on iid = item.id and item_id.uid = item.uid and item.uid = %d and service = 'BUILDBLOCK' and sid = '%s' limit 1 " ,
2013-09-30 20:42:29 +00:00
intval ( get_app () -> profile [ 'profile_uid' ]),
2013-09-04 07:50:47 +00:00
dbesc ( $name )
);
if ( $r ) {
$o = '<div class="widget bblock">' ;
if ( $r [ 0 ][ 'title' ])
$o .= '<h3>' . $r [ 0 ][ 'title' ] . '</h3>' ;
$o .= prepare_text ( $r [ 0 ][ 'body' ], $r [ 0 ][ 'mimetype' ]);
$o .= '</div>' ;
}
return $o ;
}
2013-09-05 04:24:57 +00:00
// This doesn't really belong in Comanche, but it could also be argued that it is the perfect place.
// We need to be able to select what kind of template and decoration to use for the webpage at the heart of our content.
// For now we'll allow an '[authored]' element which defaults to name and date, or 'none' to remove these, and perhaps
// 'full' to provide a social network style profile photo.
// But leave it open to have richer templating options and perhaps ultimately discard this one, once we have a better idea
// of what template and webpage options we might desire.
function comanche_webpage ( & $a , $s ) {
$ret = array ();
$cnt = preg_match_all ( " / \ [authored \ ](.*?) \ [ \ /authored \ ]/ism " , $s , $matches , PREG_SET_ORDER );
if ( $cnt ) {
foreach ( $matches as $mtch ) {
$ret [ 'authored' ] = $mtch [ 1 ];
}
}
return $ret ;
}
2013-09-04 07:50:47 +00:00
2013-08-30 03:54:42 +00:00
// Widgets will have to get any operational arguments from the session,
// the global app environment, or config storage until we implement argument passing
2013-10-24 22:35:24 +00:00
function comanche_widget ( $name , $text ) {
2013-08-30 03:54:42 +00:00
$a = get_app ();
2013-10-24 22:35:24 +00:00
$vars = array ();
$cnt = preg_match_all ( " / \ [var=(.*?) \ ](.*?) \ [ \ /var \ ]/ism " , $text , $matches , PREG_SET_ORDER );
if ( $cnt ) {
foreach ( $matches as $mtch ) {
$vars [ $mtch [ 1 ]] = $mtch [ 2 ];
}
}
2013-08-30 03:54:42 +00:00
$func = 'widget_' . trim ( $name );
if ( function_exists ( $func ))
2013-10-24 22:35:24 +00:00
return $func ( $vars );
2013-08-29 03:03:01 +00:00
}
function comanche_region ( & $a , $s ) {
2013-09-03 08:57:11 +00:00
$cnt = preg_match_all ( " / \ [menu \ ](.*?) \ [ \ /menu \ ]/ism " , $s , $matches , PREG_SET_ORDER );
2013-08-29 03:03:01 +00:00
if ( $cnt ) {
foreach ( $matches as $mtch ) {
$s = str_replace ( $mtch [ 0 ], comanche_menu ( trim ( $mtch [ 1 ])), $s );
}
}
2013-09-04 07:50:47 +00:00
$cnt = preg_match_all ( " / \ [block \ ](.*?) \ [ \ /block \ ]/ism " , $s , $matches , PREG_SET_ORDER );
if ( $cnt ) {
foreach ( $matches as $mtch ) {
$s = str_replace ( $mtch [ 0 ], comanche_block ( trim ( $mtch [ 1 ])), $s );
}
}
2013-08-29 03:03:01 +00:00
// need to modify this to accept parameters
2013-10-24 22:35:24 +00:00
$cnt = preg_match_all ( " / \ [widget=(.*?) \ ](.*?) \ [ \ /widget \ ]/ism " , $s , $matches , PREG_SET_ORDER );
2013-08-29 03:03:01 +00:00
if ( $cnt ) {
foreach ( $matches as $mtch ) {
2013-10-24 22:35:24 +00:00
$s = str_replace ( $mtch [ 0 ], comanche_widget ( trim ( $mtch [ 1 ]), $mtch [ 2 ]), $s );
2013-08-29 03:03:01 +00:00
}
}
return $s ;
2013-08-30 03:54:42 +00:00
}