PageRenderTime 84ms CodeModel.GetById 53ms RepoModel.GetById 0ms app.codeStats 0ms

/include/AMP/Content/Badge/Badge.php

https://github.com/radicalsuz/amp
PHP | 170 lines | 132 code | 36 blank | 2 comment | 19 complexity | e6d66a5a504d2b6a46bec209caca2447 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, LGPL-2.0
  1. <?php
  2. require_once( 'AMP/System/Data/Item.inc.php');
  3. class AMP_Content_Badge extends AMPSystem_Data_Item {
  4. var $datatable = "badges";
  5. var $name_field = "name";
  6. function AMP_Content_Badge ( &$dbcon, $id = null ) {
  7. $this->init( $dbcon, $id );
  8. }
  9. function getInclude() {
  10. return $this->getData('include');
  11. }
  12. function getIncludeFunction() {
  13. return $this->getData('include_function');
  14. }
  15. function getIncludeFunctionArguments( ) {
  16. $value = $this->getData( 'include_function_args');
  17. if ( !$value ) return $this->getDefaultArguments( );
  18. $tuples = preg_split( "/\s{0,2}&&\s{0,2}/", $value );
  19. $values = array( );
  20. foreach( $tuples as $tuple ) {
  21. $tuple_segments = preg_split( '/\s?=\s?/', $tuple );
  22. if ( count( $tuple_segments ) > 2 ) {
  23. $value_segment = join( "=", array_slice( $tuple_segments, 1));
  24. } else {
  25. $value_segment = $this->checkValueForArray( $tuple_segments[1]);
  26. }
  27. $values[ $tuple_segments[0]] = $value_segment;
  28. }
  29. return array_merge( $this->getDefaultArguments( ), $values );
  30. }
  31. function checkValueForArray( $value ) {
  32. if ( !strpos( $value, ",")) return $value ;
  33. $test_array = split( ",", $value );
  34. $result_value = array( );
  35. foreach( $test_array as $item ) {
  36. if ( !is_numeric( $item )) {
  37. return $value;
  38. }
  39. $result_value[] = $item;
  40. }
  41. return $result_value;
  42. }
  43. function getDefaultArguments( ) {
  44. require_once( 'AMP/Content/Page.inc.php');
  45. $page = AMPContent_Page::instance( );
  46. if( $page->getSectionId( ) != AMP_CONTENT_SECTION_ID_ROOT ) {
  47. $values['section'] = $page->getSectionId( );
  48. }
  49. $values['class'] = $page->getClassId( );
  50. $values['article'] = $page->getArticleId( );
  51. $values['intro_id'] = $page->getIntroId( );
  52. foreach( $values as $key => $value ) {
  53. if ( !$value ) {
  54. unset ( $values[ $key ] );
  55. }
  56. }
  57. return $values;
  58. }
  59. function getHtml() {
  60. return $this->getData('html');
  61. }
  62. function getGallery() {
  63. return $this->getData('gallery');
  64. }
  65. function execute() {
  66. $output = false;
  67. if ($output = $this->render_php_include()) return $output;
  68. if ($output = $this->render_html()) return $output;
  69. if ($output = $this->render_gallery()) return $output;
  70. return $output;
  71. }
  72. function output( ) {
  73. return $this->execute( );
  74. }
  75. function render_php_include() {
  76. if (!($include_filename = $this->getInclude() )) return false;
  77. if (!file_exists_incpath($include_filename)) {
  78. trigger_error( sprintf( AMP_TEXT_ERROR_FILE_EXISTS_NOT, $include_filename )) ;
  79. return false;
  80. }
  81. if ($include_function = $this->getIncludeFunction()) {
  82. include_once( $include_filename );
  83. if (is_callable($include_function)) {
  84. $arguments = $this->getIncludeFunctionArguments( );
  85. return $include_function( $arguments );
  86. } else {
  87. trigger_error( sprintf( AMP_TEXT_ERROR_NOT_DEFINED, $include_filename, $include_function ));
  88. return false;
  89. }
  90. }
  91. //default, just include the file
  92. ob_start();
  93. include( $include_filename );
  94. $include_value = ob_get_contents();
  95. ob_end_clean();
  96. return $include_value;
  97. }
  98. function getBody() {
  99. return $this->getHtml();
  100. }
  101. function render_html() {
  102. return $this->getHtml();
  103. }
  104. function render_gallery() {
  105. //stub, TODO fix later 2007-03-19 AP
  106. return false;
  107. }
  108. function getURL() {
  109. if (!$this->id) return false;
  110. return AMP_url_add_vars( AMP_CONTENT_URL_BADGE, "id=".$this->id);
  111. }
  112. function get_url_edit() {
  113. if (!$this->id) return false;
  114. return AMP_url_add_vars( AMP_SYSTEM_URL_BADGE, "id=".$this->id);
  115. }
  116. function getStatus( ) {
  117. return $this->isLive( ) ? AMP_PUBLISH_STATUS_LIVE : AMP_PUBLISH_STATUS_DRAFT;
  118. }
  119. function navify( ) {
  120. require_once( "AMP/Content/Nav.inc.php");
  121. $new_nav = new NavigationElement( $this->dbcon );
  122. if ( $existing_navs = $new_nav->find( array( 'badge_id' => $this->id ))) {
  123. $found_nav = current( $existing_navs );
  124. $flash = AMP_System_Flash::instance( );
  125. $flash->add_message( $this->getName( ) . ' already has a nav', get_class( $this ) . '_navify_message', $found_nav->get_url_edit( ));
  126. return false;
  127. }
  128. $new_nav->setDefaults( );
  129. $nav_data = array( 'name' => $this->getName( ), 'titletext' => $this->getName( ), 'badge_id' => $this->id, 'modid' => AMP_MODULE_ID_CONTENT );
  130. $new_nav->mergeData( $nav_data );
  131. return $new_nav->save( );
  132. }
  133. }
  134. ?>