[HACK] : Page metadata sylesheets applied to TinyMCE Wysiwyg & Page Edit Preview

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
deepster
New Member
New Member
Posts: 4
Joined: Mon Mar 03, 2008 4:33 am

[HACK] : Page metadata sylesheets applied to TinyMCE Wysiwyg & Page Edit Preview

Post by deepster »

I'm quite new to CMS MS, but I though I'd share this info just in case anyone else would like to get this functionality working in their own installations ( NB: I am using CMS MS v 1.2.3 ).

So basically, here are a couple of very inelegant hacks to implement some missing functionality I couldn't do without [ They require modification of CMS MS php files ( insertion of new code ), so back-up the corresponding files first and use at your own risk ], Also they may not work with all variants of CMS MS installs ( ie: I use a MySQL DB setup for which they were coded, some of the code probably won't work for a PostgreSQL DB setup, etc..  as it stands ).

So.. I needed individual style sheets applied to my pages and after researching the forums it seemed the best way was to use the Metadata fields in the Page editing area with a {stylesheet} tag.
This worked nicely for the front end display, but during the editing process using a WYSIWYG or the Preview option, these style sheets were not included, which would be confusing for my client. So here are a couple of tasteless hacks to get them included in those display areas ( Nb: If there is an easier way to do this which I missed please let me know, if not it's my hope that this idea be included into the actual release code, but obviously in a more proper manner than my dodgy code!  ;) )

The concepts are fairly straightforward, pull any stylesheet names from the metadata field for the page and apply the corresponding stylesheet(s) to display area, this does have the limitation that your stylesheets must have UNIQUE names ( not too hard really ).

For the Page editing Preview area ( least hacky ) ::
---------------------------------------------------------------------------------------------------------------------------
FILE ::: preview.php ( In the CMS MS root folder ) :: Add following code DIRECTLY after line # 58:
----------------------------------------------------------------------------------------------------------------------------

Code: Select all


  //-- DEEPSTER HACK :: 040308 -------------------------------
  //--This is a hack to add Metadata stylesheets to the preview option of the page edit area
  $db =& $gCms->GetDb();
  $cssquery = "SELECT metadata FROM ".cms_db_prefix()."content WHERE content_id = ".($gCms->variables['content_id']);
  $cssresult = &$db->Execute( $cssquery );
  
  $styleSheetEntries = explode( "stylesheet", $cssresult->fields['metadata'] );
  if( count( $styleSheetEntries > 0 ) ) {
    $MDstyleSheetLinks = "";
    foreach( $styleSheetEntries as $styleSheetEntry ) {
      $styleSheetPieces = explode( "'", $styleSheetEntry );
      if( count( $styleSheetPieces ) > 1 )
        $MDstyleSheetLinks .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"".$config['root_url']."/stylesheet.php?name=".$styleSheetPieces[1]."\" />\n";
    }
    
    list( $htmlTop, $htmlEnd ) = explode( "</head>", $html );
    $html = $htmlTop.$MDstyleSheetLinks."\n\n</head>\n\n".$htmlEnd;
  }
  //-- END :: DEEPSTER HACK ---------------------------


------------------------------------------------------------------------------
For TinyMCE's WYSIWYG :: Two files need to be edited:
- admin/editcontent.php
- modules/TinyMCE/content_css.php

--------------------------------------------------------------------------------------------------
FILE ::: admin/editcontent.php :: Add following code DIRECTLY after line # 576:
--------------------------------------------------------------------------------------------------

Code: Select all


//-- DEEPSTER HACK :: 030308-------------------------------
//--Hack to store Page Metadata info in a file to be used by the Wysiwyg editor 
//--to retrieve and add any Stylesheets refered to ( in the Metadata ).
$pageContentDataFileName = $config["root_path"]."/tmp/pageMetaDataTEMP.php";
if( $contentobj->Metadata( ) != "" ) { 
  $handle = fopen( $pageContentDataFileName, "w" );
  fwrite( $handle, $contentobj->Metadata( ) );
  fclose( $handle );
} else if( file_exists( $pageContentDataFileName ) ) {
  unlink($pageContentDataFileName );
}
//-- END :: DEEPSTER HACK ---------------------------

--------------------------------------------------------------------------------------------------
FILE ::: modules/TinyMCE/content_css.php :: Add following code DIRECTLY after line # 98:
--------------------------------------------------------------------------------------------------

Code: Select all


    //-- DEEPSTER HACK :: 030308----------------------------------------------------------------------------------
    //--This is a bad hack to get *individual page styling* as entered via the page Metadata field to be applied 
    //--to the *TinyMCE WYSIWYG editor text area*, I had to use a file to pass the info .. ugh!
    $pageContentDataFile = $config["root_path"]."/tmp/pageMetaDataTEMP.php";
    if( file_exists( $pageContentDataFile ) ) {
      $handle = @fopen( $pageContentDataFile, "r" );
      if ( $handle ) {
        $pageMetaData = fread( $handle, filesize( $pageContentDataFile ) );      
        if( $pageMetaData ) {
          $styleSheetEntries = explode( "stylesheet", $pageMetaData );
          if( count( $styleSheetEntries > 0 ) ) {                  
            foreach( $styleSheetEntries as $styleSheetEntry ) {
              $styleSheetPieces = explode( "'", $styleSheetEntry );
              if( count( $styleSheetPieces ) > 1 ) {
                $sql="SELECT css_text FROM ".$config['db_prefix']."css WHERE css_name = '". mysql_real_escape_string( $styleSheetPieces[1], $db )."'";
           		  $result=mysql_query( $sql ); //		echo $sql;
            		while ($result && $row = mysql_fetch_assoc($result))
            		{
            			$css .= "/* Start of **METADATA** CMSMS style sheet '$styleSheetPieces[1]' */\n{$row['css_text']}\n/* End of **METDATA** '$styleSheetPieces[1]' */\n";
            		}
              } // if( count( $styleSheetPieces ) > 1 ) {
            } // foreach( $styleSheetEntries as $styleSheetEntry ) {
          } // if( count( $styleSheetEntries > 0 ) ) {
        } // if( $pageMetaData ) {
        fclose($handle);
        unlink( $pageContentDataFile );
      } // if ( $handle ) {
    } // if( file_exists( $pageContentDataFile ) ) {
    //--END :: DEEPSTER HACK --------------------------------------------------------------------------------------    


Hope they might help someone and maybe give the Devs of this fine CMS a nudge to incorporate something similar into the app itself!

Cheers
Last edited by deepster on Sat Mar 08, 2008 4:35 am, edited 1 time in total.
Post Reply

Return to “CMSMS Core”