PageRenderTime 28ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/reason_4.0/lib/core/minisite_templates/modules/feature/feature.php

https://github.com/luthercollege/reason_package
PHP | 514 lines | 398 code | 49 blank | 67 comment | 41 complexity | 424ce4c1a2370b43f06833caa52372c4 MD5 | raw file
  1. <?php
  2. /**
  3. * @package reason
  4. * @subpackage minisite_modules
  5. */
  6. /**
  7. * Include the parent class & dependencies, and register the module with Reason
  8. */
  9. reason_include_once( 'minisite_templates/modules/default.php' );
  10. reason_include_once('function_libraries/image_tools.php');
  11. reason_include_once( 'classes/sized_image.php' );
  12. reason_include_once( 'classes/av_display.php' );
  13. reason_include_once( 'classes/feature_helper.php' );
  14. reason_include_once( 'minisite_templates/modules/feature/views/default_feature_view.php' );
  15. include_once(CARL_UTIL_INC . 'basic/image_funcs.php');
  16. include_once(CARL_UTIL_INC . 'basic/url_funcs.php');
  17. if (!defined("FEATURE_VIEW_PATH"))
  18. {
  19. define("FEATURE_VIEW_PATH",'minisite_templates/modules/feature/views/');
  20. }
  21. $GLOBALS[ '_module_class_names' ][ module_basename( __FILE__) ] = 'FeatureModule';
  22. /**
  23. * The feature module displays entities of the feature type.
  24. *
  25. * Requirements for this module:
  26. *
  27. * - Works with JavaScript on or off ... though autoplay does require javascript.
  28. * - Loads a CSS file that hides inactive blocks and satisfies other css requirements - see reason_package/reason_4.0/www/css/feature.css
  29. * - Loads a javascript file that animates the feature set and makes smooth transitions without page reloads - see reason_package/reason_4.0/www/js/feature.js
  30. *
  31. * Supports these parameters:
  32. *
  33. * - shuffle - default false. If true, we sort features randomly rather than by sort order
  34. * - autoplay_timer - default 0 (off). If a positive integer, indicates the time delay between switches
  35. * - max - default 0 (no max). If positive, designates a maximum number of features to select
  36. * - width - default 400. We use this value as the image width for images set to "crop to fill" and also send it into the feature_image.php script.
  37. * - height - default 300. We use this value as the image height for images set to "crop to fill" and also send it into the feature_image.php script.
  38. *
  39. * Beta parameters:
  40. * - view - default DefaultFeatureView. Name of a view class to use.
  41. *
  42. * @todo update sample markup
  43. * @todo make views extensible without a lot of fuss.
  44. *
  45. * @author Nathan White and Frank McQuarry
  46. */
  47. class FeatureModule extends DefaultMinisiteModule
  48. {
  49. var $acceptable_params = array(
  50. 'shuffle' => false,
  51. 'autoplay_timer' => 0,
  52. 'looping' => 'on',
  53. 'max' => 0,
  54. 'width' => 400,
  55. 'height' => 300,
  56. 'view' => 'DefaultFeatureView'
  57. );
  58. var $cleanup_rules = array('feature'=>'turn_into_int');
  59. var $features_view_params;
  60. var $current_feature_id = 0;
  61. private $_view;
  62. private $_view_data;
  63. private $_features;
  64. function init( $args = array() )
  65. {
  66. if ($features = $this->get_features())
  67. {
  68. $head_items =& $this->get_head_items();
  69. $head_items->add_javascript(JQUERY_URL, true);
  70. //create the view layer
  71. $view = $this->get_view();
  72. $view_data = $this->_view_data;
  73. $view_params = $this->get_view_params();
  74. $current_feature_id = $this->current_feature_id;
  75. $view->set($view_data,$view_params,$current_feature_id,$head_items);
  76. }
  77. }
  78. /**
  79. * @todo make me work properly and figure out a scheme for including custom views.
  80. */
  81. function get_view()
  82. {
  83. if (!isset($this->_view))
  84. {
  85. $view_class = 'DefaultFeatureView';
  86. if ($view_class != $this->params['view'])
  87. {
  88. $view = $this->params['view'];
  89. $file = FEATURE_VIEW_PATH . $view . '.php';
  90. if (reason_file_exists($file))
  91. {
  92. reason_require_once($file);
  93. if(isset($GLOBALS[ '_feature_view_class_names' ][$view]) && class_exists($GLOBALS[ '_feature_view_class_names' ][$view]))
  94. {
  95. $view_class = $GLOBALS[ '_feature_view_class_names' ][$view];
  96. }
  97. else
  98. {
  99. trigger_error('Feature view class not registered in $GLOBALS[ \'_feature_view_class_names\' ].');
  100. }
  101. }
  102. else
  103. {
  104. trigger_error('Feature view file not found in '.FEATURE_VIEW_PATH);
  105. }
  106. }
  107. $this->_view = new $view_class;
  108. }
  109. return $this->_view;
  110. }
  111. /**
  112. * Return our feature set by reference - only build the feature set once.
  113. */
  114. function get_features()
  115. {
  116. if (!isset($this->_features))
  117. {
  118. $this->_build_features();
  119. }
  120. return $this->_features;
  121. }
  122. /**
  123. * return the features view params
  124. */
  125. function get_view_params()
  126. {
  127. if(!isset($this->features_view_params))
  128. {
  129. $this->_build_view_params();
  130. }
  131. return $this->features_view_params;
  132. }
  133. /**
  134. * Build the feature set
  135. */
  136. function _build_features()
  137. {
  138. $es1 = new entity_selector( $this->site_id );
  139. $es1->add_type( id_of('feature_type') );
  140. $es1->add_right_relationship( $this->page_id, relationship_id_of('page_to_feature'));
  141. $es1->add_rel_sort_field( $this->page_id, relationship_id_of('page_to_feature') );
  142. $es1->set_order('rel_sort_order ASC');
  143. if($this->params['max'] != 0)
  144. {
  145. $es1->set_num($this->params['max']);
  146. }
  147. $features = $es1->run_one();
  148. if(empty($features))
  149. {
  150. return null;
  151. }
  152. $es2 = new entity_selector( $this->site_id );
  153. $es2->add_type( id_of('image') );
  154. $es2->add_right_relationship_field( 'feature_to_image','entity','id','feature_id', array_keys($features) );
  155. $es2->enable_multivalue_results();
  156. $images = $es2->run_one();
  157. if ($images)
  158. {
  159. foreach ($images as $image_id => $image)
  160. {
  161. $features_with_image = $image->get_value('feature_id');
  162. $features_with_image = (!is_array($features_with_image)) ? array($features_with_image) : $features_with_image; // force to array
  163. foreach ($features_with_image as $feature_id)
  164. {
  165. $feature_extra[$feature_id]['image_id'][] = $image_id;
  166. }
  167. }
  168. }
  169. $es3 = new entity_selector( $this->site_id );
  170. $es3->add_type( id_of('av') );
  171. $es3->add_right_relationship_field( 'feature_to_media_work','entity','id','av_id', array_keys($features) );
  172. $es3->enable_multivalue_results();
  173. $media_works = $es3->run_one();
  174. if ($media_works)
  175. {
  176. foreach ($media_works as $media_work_id => $media_work)
  177. {
  178. $features_with_media_work = $media_work->get_value('av_id');
  179. $features_with_media_work = (!is_array($features_with_media_work)) ? array($features_with_media_work) : $features_with_media_work; // force to array
  180. foreach ($features_with_media_work as $feature_id)
  181. {
  182. $feature_extra[$feature_id]['av_id'][] = $media_work_id;
  183. }
  184. }
  185. }
  186. // augment our features with images and media works
  187. foreach($features as $feature_id => $feature)
  188. {
  189. if (isset($feature_extra[$feature_id]['image_id']))
  190. {
  191. $value = (count($feature_extra[$feature_id]['image_id']) == 1) ? reset($feature_extra[$feature_id]['image_id']) : $feature_extra[$feature_id]['image_id'];
  192. $features[$feature_id]->set_value('image_id',$value);
  193. }
  194. else $features[$feature_id]->set_value('image_id',"none");
  195. if (isset($feature_extra[$feature_id]['av_id']))
  196. {
  197. $value = (count($feature_extra[$feature_id]['av_id']) == 1) ? reset($feature_extra[$feature_id]['av_id']) : $feature_extra[$feature_id]['av_id'];
  198. $features[$feature_id]->set_value('av_id',$value);
  199. }
  200. else $features[$feature_id]->set_value('av_id',"none");
  201. }
  202. //shuffle the features if set to true
  203. //note that keys are preserved in the new
  204. //shuffled feature array
  205. if($this->params['shuffle']==true)
  206. {
  207. $shuffled_results=array();
  208. $temps=array();
  209. $temps=$features;
  210. shuffle($temps);
  211. foreach($temps as $temp)
  212. {
  213. $id=$temp->get_value('id');
  214. $shuffled_results[$id]=$temp;
  215. }
  216. $features=$shuffled_results;
  217. }
  218. //pick a random media work or image from each features list of images.
  219. foreach($features as $id=>$r)
  220. {
  221. $num_imgs=0;
  222. $num_mdia=0;
  223. $i=0;
  224. if($features[$id]->has_value('image_id'))
  225. {
  226. $img_id=$features[$id]->get_value('image_id');
  227. if($img_id!="none")
  228. {
  229. $num_imgs=count($img_id);
  230. }
  231. }
  232. else
  233. {
  234. $features[$id]->set_value('image_id','none');
  235. }
  236. if($features[$id]->has_value('av_id'))
  237. {
  238. $av_id=$features[$id]->get_value('av_id');
  239. if($av_id !="none")
  240. {
  241. $num_mdia=count($av_id);
  242. }
  243. }
  244. else
  245. {
  246. $features[$id]->set_value('av_id','none');
  247. }
  248. $num_objects=$num_imgs+$num_mdia;
  249. $i=rand(0, $num_objects-1 );
  250. if($i<$num_mdia)
  251. {
  252. $features[$id]->set_value('current_object_type','av');
  253. $features[$id]->set_value('current_image_index',$i);
  254. }
  255. else
  256. {
  257. $features[$id]->set_value('current_object_type','img');
  258. $features[$id]->set_value('current_image_index',$i-$num_mdia);
  259. }
  260. }
  261. //set default values for items that are not set
  262. foreach($features as $id=>$r)
  263. {
  264. $cp=$features[$id]->get_value('crop_style');
  265. if($cp==null){ $features[$id]->set_value('crop_style','fill');}
  266. }
  267. $this->_features=$features;
  268. //if feature id is on this page then display it
  269. //else redirect to same page but this feature not set
  270. if(isset($this->request['feature']))
  271. {
  272. $feature_id=$this->request['feature'];
  273. $is_a_feature_on_this_page=false;
  274. foreach($this->_features as $feat)
  275. {
  276. if($feature_id==$feat->get_value('id'))
  277. {
  278. $is_a_feature_on_this_page=true;
  279. break;
  280. }
  281. }
  282. if($is_a_feature_on_this_page)
  283. {
  284. $this->current_feature_id=$this->request['feature'];
  285. }
  286. else
  287. {
  288. $url=carl_make_redirect(array('feature'=>''),'');
  289. header("Location: ".$url);
  290. }
  291. }
  292. $this->_build_view_data();
  293. }
  294. /**
  295. * set the features_view_parms array with the details
  296. * needed by the view layer
  297. */
  298. function _build_view_params()
  299. {
  300. $this->features_view_params['autoplay_timer']=$this->params['autoplay_timer'];
  301. $this->features_view_params['width']=$this->params['width'];
  302. $this->features_view_params['height']=$this->params['height'];
  303. $this->features_view_params['condensed_nav']=false;
  304. $this->features_view_params['looping']=$this->params['looping'];//or off
  305. }
  306. /**
  307. * Build the data needed by the view layer
  308. */
  309. function _build_view_data()
  310. {
  311. $features=$this->get_features();
  312. $params=$this->get_view_params();
  313. $data=array();
  314. $feature_num=1;
  315. $show_feature=true;
  316. $fh= new Feature_Helper();
  317. $width=$this->params['width'];
  318. $height=$this->params['height'];
  319. //build the data needed by the view layer
  320. foreach($features as $feature)
  321. {
  322. $d=array();
  323. $d['id']=$feature->get_value('id');
  324. $d['show_text']=$feature->get_value('show_text');
  325. if($show_feature && $this->current_feature_id==0)
  326. {
  327. $show_feature=false;
  328. $d['active']="active";
  329. }
  330. else if($this->current_feature_id==$d['id'])
  331. {
  332. $show_feature=true;
  333. $d['active']="active";
  334. }
  335. else
  336. {
  337. $d['active']="inactive";
  338. }
  339. $d['bg_color']=$feature->get_value('bg_color');
  340. $d['w']=$params['width'];
  341. $d['h']=$params['height'];
  342. $d['crop_style']=$feature->get_value('crop_style');;
  343. $params="?id=".$d['id']."&amp;w=".$d['w']."&amp;h=".$d['h']."&amp;crop=".$d['crop_style'];
  344. $d['feature_image_url']=REASON_HTTP_BASE_PATH."displayers/feature_image.php".$params;
  345. $d['destination_url']=$feature->get_value('destination_url');
  346. $d['title']=$feature->get_value('title');
  347. $d['text']=$feature->get_value('text');
  348. $feature_num++;
  349. $d['feature_image_url']=array();
  350. $img_id=$feature->get_value('image_id');
  351. $d['current_image_index']=$feature->get_value('current_image_index');
  352. if(isset($img_id))
  353. {
  354. $crop_style=$d['crop_style'];
  355. if(is_array($img_id))
  356. {
  357. foreach($img_id as $i)
  358. {
  359. $img_info=$this->get_image_url_and_alt($i,$crop_style);
  360. $d['feature_image_url'][]= $img_info['url'];
  361. $d['feature_image_alt'][]= $img_info['alt'];
  362. }
  363. }
  364. else
  365. {
  366. if($img_id!='none')
  367. {
  368. $img_info=$this->get_image_url_and_alt($img_id,$crop_style);
  369. $d['feature_image_url'][]= $img_info['url'];
  370. $d['feature_image_alt'][]= $img_info['alt'];
  371. }
  372. else
  373. {
  374. $d['feature_image_url'][]="none";
  375. $d['feature_image_alt'][]= "";
  376. }
  377. }
  378. }
  379. else
  380. {
  381. $d['feature_image_url'][]="none";
  382. $d['feature_image_alt'][]= "";
  383. }
  384. $av_id=$feature->get_value('av_id');
  385. $d['feature_av_html']=array();
  386. $av_info=array();
  387. if(isset($av_id))
  388. {
  389. if(is_array($av_id))
  390. {
  391. foreach($av_id as $id)
  392. {
  393. $av_info=$this->get_av_info($id);
  394. $d['feature_av_html'][]=$av_info['av_html'];
  395. $d['feature_av_img_url'][]=$av_info['av_img_url'];
  396. $d['feature_av_img_alt'][]=$av_info['av_img_alt'];
  397. }
  398. }
  399. else
  400. {
  401. if($av_id !="none")
  402. {
  403. $av_info=$this->get_av_info($av_id);
  404. $d['feature_av_html'][]=$av_info['av_html'];
  405. $d['feature_av_img_url'][]=$av_info['av_img_url'];
  406. $d['feature_av_img_alt'][]=$av_info['av_img_alt'];
  407. }
  408. else
  409. {
  410. $d['feature_av_html'][]="none";
  411. $d['feature_av_img_url'][]="none";
  412. $d['feature_av_img_alt'][]="";
  413. }
  414. }
  415. }
  416. else
  417. {
  418. $d['feature_av_html'][]="none";
  419. $d['feature_av_img_url'][]="none";
  420. $d['feature_av_img_alt'][]="";
  421. }
  422. $d['current_object_type']=$feature->get_value('current_object_type');
  423. $data[$feature->get_value('id')]=$d;
  424. }//end foreach loop through $features
  425. $this->_view_data=$data;
  426. }
  427. /**
  428. * returns the url to the sized image
  429. * @param $id image id of image to be sized
  430. * @param $crop_style how to size the image in the new wxh, fill or fit
  431. * @return The url to the sized image
  432. */
  433. function get_image_url_and_alt($id,$crop_style="fill")
  434. {
  435. $width=$this->params['width'];
  436. $height=$this->params['height'];
  437. $rsi = new reasonSizedImage();
  438. $rsi->set_id($id);
  439. $rsi->set_width($width);
  440. $rsi->set_height($height);
  441. $rsi->set_crop_style($crop_style);
  442. $ret = $rsi->get_url_and_alt();
  443. return $ret;
  444. }
  445. function get_av_info($media_works_id)
  446. {
  447. $width=$this->params['width'];
  448. $height=$this->params['height'];
  449. $fh= new Feature_Helper();
  450. $av_info=$fh->get_av_info($media_works_id,$width,$height);
  451. return $av_info;
  452. }
  453. function has_content()
  454. {
  455. $features = $this->get_features();
  456. return (!empty($features));
  457. }
  458. function run()
  459. {
  460. $view = $this->get_view();
  461. $html_str = $view->get_html();
  462. echo $html_str;
  463. }
  464. }
  465. ?>