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
2015-05-22 05:17:17 +00:00
// On submit, the pdl_select value (which is the mid of an item with item_type = ITEM_TYPE_PDL) is stored in
2013-08-28 23:39:43 +00:00
// 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
2015-03-03 23:03:19 +00:00
function pdl_selector ( $uid , $current = " " ) {
2013-08-28 23:39:43 +00:00
$o = '' ;
2015-03-04 00:00:24 +00:00
$sql_extra = item_permissions_sql ( $uid );
2013-08-28 23:39:43 +00:00
2015-03-04 00:00:24 +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' $sql_extra order by sid asc " ,
2015-03-03 23:03:19 +00:00
intval ( $uid )
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' ];
2015-03-03 23:03:19 +00:00
$current = $arr [ 'current' ];
$o .= '<select name="pdl_select" id="pdl_select" size="1">' ;
2013-08-28 23:39:43 +00:00
$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 ;
2015-03-03 23:03:19 +00:00
}
2013-08-28 23:39:43 +00:00
2013-08-29 03:03:01 +00:00
2015-03-06 02:24:49 +00:00
function comanche_parser ( & $a , $s , $pass = 0 ) {
2015-03-03 23:03:19 +00:00
$matches = array ();
2013-08-29 03:03:01 +00:00
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 ) {
2015-03-03 23:03:19 +00:00
$s = str_replace ( $mtch [ 0 ], '' , $s );
2013-10-29 00:48:42 +00:00
}
}
2013-08-29 03:03:01 +00:00
2015-03-06 02:24:49 +00:00
if ( $pass == 0 ) {
$cnt = preg_match ( " / \ [layout \ ](.*?) \ [ \ /layout \ ]/ism " , $s , $matches );
if ( $cnt )
$a -> page [ 'template' ] = trim ( $matches [ 1 ]);
2013-08-29 03:03:01 +00:00
2015-03-06 02:24:49 +00:00
$cnt = preg_match ( " / \ [template=(.*?) \ ](.*?) \ [ \ /template \ ]/ism " , $s , $matches );
if ( $cnt ) {
$a -> page [ 'template' ] = trim ( $matches [ 2 ]);
$a -> page [ 'template_style' ] = trim ( $matches [ 2 ]) . '_' . $matches [ 1 ];
}
2014-03-02 08:24:29 +00:00
2015-03-06 02:24:49 +00:00
$cnt = preg_match ( " / \ [template \ ](.*?) \ [ \ /template \ ]/ism " , $s , $matches );
if ( $cnt ) {
$a -> page [ 'template' ] = trim ( $matches [ 1 ]);
}
2014-02-28 04:48:10 +00:00
2015-03-06 02:24:49 +00:00
$cnt = preg_match ( " / \ [theme=(.*?) \ ](.*?) \ [ \ /theme \ ]/ism " , $s , $matches );
if ( $cnt ) {
$a -> layout [ 'schema' ] = trim ( $matches [ 1 ]);
$a -> layout [ 'theme' ] = trim ( $matches [ 2 ]);
}
2014-02-28 04:48:10 +00:00
2015-03-06 02:24:49 +00:00
$cnt = preg_match ( " / \ [theme \ ](.*?) \ [ \ /theme \ ]/ism " , $s , $matches );
if ( $cnt )
$a -> layout [ 'theme' ] = trim ( $matches [ 1 ]);
2013-08-29 03:03:01 +00:00
2015-03-25 22:32:49 +00:00
$cnt = preg_match_all ( " / \ [webpage \ ](.*?) \ [ \ /webpage \ ]/ism " , $s , $matches , PREG_SET_ORDER );
2015-03-06 02:24:49 +00:00
if ( $cnt ) {
2015-03-25 22:32:49 +00:00
// only the last webpage definition is used if there is more than one
2015-03-06 02:24:49 +00:00
foreach ( $matches as $mtch ) {
2015-03-25 22:32:49 +00:00
$a -> layout [ 'webpage' ] = comanche_webpage ( $a , $mtch [ 1 ]);
2015-03-06 02:24:49 +00:00
}
}
2013-08-29 03:03:01 +00:00
2015-03-25 22:32:49 +00:00
}
else {
$cnt = preg_match_all ( " / \ [region=(.*?) \ ](.*?) \ [ \ /region \ ]/ism " , $s , $matches , PREG_SET_ORDER );
2015-03-06 02:24:49 +00:00
if ( $cnt ) {
foreach ( $matches as $mtch ) {
2015-03-25 22:32:49 +00:00
$a -> layout [ 'region_' . $mtch [ 1 ]] = comanche_region ( $a , $mtch [ 2 ]);
2015-03-06 02:24:49 +00:00
}
2013-09-05 04:24:57 +00:00
}
2015-03-25 22:32:49 +00:00
2013-09-05 04:24:57 +00:00
}
2013-08-29 03:03:01 +00:00
}
2015-05-07 21:02:14 +00:00
function comanche_menu ( $s , $class = '' ) {
2014-03-02 23:47:01 +00:00
$channel_id = comanche_get_channel_id ();
2015-05-07 21:02:14 +00:00
$name = $s ;
$cnt = preg_match_all ( " / \ [var=(.*?) \ ](.*?) \ [ \ /var \ ]/ism " , $s , $matches , PREG_SET_ORDER );
if ( $cnt ) {
foreach ( $matches as $mtch ) {
$var [ $mtch [ 1 ]] = $mtch [ 2 ];
$name = str_replace ( $mtch [ 0 ], '' , $name );
}
}
2014-03-02 23:47:01 +00:00
if ( $channel_id ) {
2015-03-03 23:03:19 +00:00
$m = menu_fetch ( $name , $channel_id , get_observer_hash ());
2015-05-07 21:02:14 +00:00
return menu_render ( $m , $class , $edit = false , $var );
2014-03-02 23:47:01 +00:00
}
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 ();
2015-03-21 23:06:08 +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
}
2014-03-02 23:47:01 +00:00
/**
2015-03-21 23:06:08 +00:00
* @ brief Returns the channel_id of the profile owner of the page .
*
* Returns the channel_id of the profile owner of the page , or the local_channel
* if there is no profile owner . Otherwise returns 0.
*
* @ return channel_id
*/
2014-03-02 23:47:01 +00:00
function comanche_get_channel_id () {
$channel_id = (( is_array ( get_app () -> profile )) ? get_app () -> profile [ 'profile_uid' ] : 0 );
2015-03-21 23:06:08 +00:00
if (( ! $channel_id ) && ( local_channel ()))
2015-01-29 04:56:04 +00:00
$channel_id = local_channel ();
2015-03-03 23:03:19 +00:00
2014-03-02 23:47:01 +00:00
return $channel_id ;
}
2015-05-06 08:53:26 +00:00
function comanche_block ( $s , $class = '' ) {
2015-04-19 11:25:05 +00:00
$var = array ();
$matches = array ();
$name = $s ;
2015-05-06 08:53:26 +00:00
$class = (( $class ) ? $class : 'bblock widget' );
2015-04-19 11:25:05 +00:00
$cnt = preg_match_all ( " / \ [var=(.*?) \ ](.*?) \ [ \ /var \ ]/ism " , $s , $matches , PREG_SET_ORDER );
if ( $cnt ) {
foreach ( $matches as $mtch ) {
$var [ $mtch [ 1 ]] = $mtch [ 2 ];
$name = str_replace ( $mtch [ 0 ], '' , $name );
}
}
2015-03-03 23:03:19 +00:00
$o = '' ;
2014-03-02 23:47:01 +00:00
$channel_id = comanche_get_channel_id ();
2015-03-03 23:03:19 +00:00
2014-03-02 23:47:01 +00:00
if ( $channel_id ) {
$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 " ,
intval ( $channel_id ),
dbesc ( $name )
);
2015-05-15 20:20:45 +00:00
2014-03-02 23:47:01 +00:00
if ( $r ) {
2015-05-27 13:21:30 +00:00
//check for eventual menus in the block and parse them
$cnt = preg_match_all ( " / \ [menu \ ](.*?) \ [ \ /menu \ ]/ism " , $r [ 0 ][ 'body' ], $matches , PREG_SET_ORDER );
if ( $cnt ) {
foreach ( $matches as $mtch ) {
$r [ 0 ][ 'body' ] = str_replace ( $mtch [ 0 ], comanche_menu ( trim ( $mtch [ 1 ])), $r [ 0 ][ 'body' ]);
}
}
$cnt = preg_match_all ( " / \ [menu=(.*?) \ ](.*?) \ [ \ /menu \ ]/ism " , $r [ 0 ][ 'body' ], $matches , PREG_SET_ORDER );
if ( $cnt ) {
foreach ( $matches as $mtch ) {
$r [ 0 ][ 'body' ] = str_replace ( $mtch [ 0 ], comanche_menu ( trim ( $mtch [ 2 ]), $mtch [ 1 ]), $r [ 0 ][ 'body' ]);
}
}
//emit the block
2015-05-06 08:53:26 +00:00
$o .= (( $var [ 'wrap' ] == 'none' ) ? '' : '<div class="' . $class . '">' );
2015-05-18 20:18:01 +00:00
2015-05-24 13:09:30 +00:00
if ( $r [ 0 ][ 'title' ] && trim ( $r [ 0 ][ 'body' ]) != '$content' ) {
2014-03-02 23:47:01 +00:00
$o .= '<h3>' . $r [ 0 ][ 'title' ] . '</h3>' ;
2015-05-18 20:18:01 +00:00
}
if ( trim ( $r [ 0 ][ 'body' ]) === '$content' ) {
2015-05-24 12:57:01 +00:00
$o .= get_app () -> page [ 'content' ];
2015-05-15 20:20:45 +00:00
}
else {
$o .= prepare_text ( $r [ 0 ][ 'body' ], $r [ 0 ][ 'mimetype' ]);
}
2015-04-19 11:25:05 +00:00
$o .= (( $var [ 'wrap' ] == 'none' ) ? '' : '</div>' );
2014-03-02 23:47:01 +00:00
}
2013-09-04 07:50:47 +00:00
}
2015-03-03 23:03:19 +00:00
2013-09-04 07:50:47 +00:00
return $o ;
}
2015-05-07 11:26:14 +00:00
function comanche_js ( $s ) {
switch ( $s ) {
case 'jquery' :
$path = 'view/js/jquery.js' ;
break ;
case 'bootstrap' :
$path = 'library/bootstrap/js/bootstrap.min.js' ;
break ;
2015-05-23 20:19:02 +00:00
case 'foundation' :
$path = 'library/foundation/js/foundation.min.js' ;
$init = " \r \n " . '<script>$(document).ready(function() { $(document).foundation(); });</script>' ;
break ;
2015-05-07 11:26:14 +00:00
}
2015-05-23 20:19:02 +00:00
$ret = '<script src="' . z_root () . '/' . $path . '" ></script>' ;
if ( $init )
$ret .= $init ;
return $ret ;
2015-05-07 11:26:14 +00:00
}
function comanche_css ( $s ) {
switch ( $s ) {
case 'bootstrap' :
$path = 'library/bootstrap/css/bootstrap.min.css' ;
break ;
2015-05-23 20:19:02 +00:00
case 'foundation' :
$path = 'library/foundation/css/foundation.min.css' ;
break ;
2015-05-07 11:26:14 +00:00
}
2015-05-23 20:19:02 +00:00
$ret = '<link rel="stylesheet" href="' . z_root () . '/' . $path . '" type="text/css" media="screen">' ;
return $ret ;
2015-05-07 11:26:14 +00:00
}
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 ();
2015-03-03 23:03:19 +00:00
$matches = array ();
2013-09-05 04:24:57 +00:00
$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
2015-03-21 23:06:08 +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
*
* @ param string $name
* @ param string $text
*/
2015-03-03 23:03:19 +00:00
function comanche_widget ( $name , $text ) {
2013-10-24 22:35:24 +00:00
$vars = array ();
2015-03-03 23:03:19 +00:00
$matches = array ();
2013-10-24 22:35:24 +00:00
$cnt = preg_match_all ( " / \ [var=(.*?) \ ](.*?) \ [ \ /var \ ]/ism " , $text , $matches , PREG_SET_ORDER );
2015-03-21 23:06:08 +00:00
if ( $cnt ) {
foreach ( $matches as $mtch ) {
2013-10-24 22:35:24 +00:00
$vars [ $mtch [ 1 ]] = $mtch [ 2 ];
}
}
2015-05-13 01:11:54 +00:00
if ( file_exists ( 'widget/' . trim ( $name ) . '.php' ))
require_once ( 'widget/' . trim ( $name ) . '.php' );
2013-08-30 03:54:42 +00:00
$func = 'widget_' . trim ( $name );
2015-03-21 23:06:08 +00:00
if ( function_exists ( $func ))
2013-10-24 22:35:24 +00:00
return $func ( $vars );
2013-08-29 03:03:01 +00:00
}
2015-03-03 23:03:19 +00:00
function comanche_region ( & $a , $s ) {
$matches = array ();
2013-08-29 03:03:01 +00:00
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 ) {
2015-03-03 23:03:19 +00:00
$s = str_replace ( $mtch [ 0 ], comanche_menu ( trim ( $mtch [ 1 ])), $s );
2013-08-29 03:03:01 +00:00
}
}
2014-02-26 04:58:20 +00:00
// menu class e.g. [menu=horizontal]my_menu[/menu] or [menu=tabbed]my_menu[/menu]
// allows different menu renderings to be applied
$cnt = preg_match_all ( " / \ [menu=(.*?) \ ](.*?) \ [ \ /menu \ ]/ism " , $s , $matches , PREG_SET_ORDER );
if ( $cnt ) {
foreach ( $matches as $mtch ) {
2014-03-03 23:21:43 +00:00
$s = str_replace ( $mtch [ 0 ], comanche_menu ( trim ( $mtch [ 2 ]), $mtch [ 1 ]), $s );
2014-02-26 04:58:20 +00:00
}
}
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
2015-05-06 08:53:26 +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 [ 2 ]), trim ( $mtch [ 1 ])), $s );
}
}
2015-05-07 11:26:14 +00:00
$cnt = preg_match_all ( " / \ [js \ ](.*?) \ [ \ /js \ ]/ism " , $s , $matches , PREG_SET_ORDER );
if ( $cnt ) {
foreach ( $matches as $mtch ) {
$s = str_replace ( $mtch [ 0 ], comanche_js ( trim ( $mtch [ 1 ])), $s );
}
}
$cnt = preg_match_all ( " / \ [css \ ](.*?) \ [ \ /css \ ]/ism " , $s , $matches , PREG_SET_ORDER );
if ( $cnt ) {
foreach ( $matches as $mtch ) {
$s = str_replace ( $mtch [ 0 ], comanche_css ( 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
}
2014-04-25 23:09:32 +00:00
/*
* @ function register_page_template ( $arr )
* Registers a page template / variant for use by Comanche selectors
* @ param array $arr
* 'template' => template name
* 'variant' => array (
* 'name' => variant name
* 'desc' => text description
* 'regions' => array (
* 'name' => name
* 'desc' => text description
* )
* )
*/
function register_page_template ( $arr ) {
get_app () -> page_layouts [ $arr [ 'template' ]] = array ( $arr [ 'variant' ]);
return ;
}