MSM-ifying and extension (mh_file_ext)
Posted: 04 August 2008 10:03 AM   [ Ignore ]  
Lab Assistant
Avatar
RankRank
Total Posts:  111
Joined  07-02-2008

Hi All,

I’m looking for some pointers regarding making an extension (in specific; Mark Huot’s File Extension but I’ve made use of a few of Mark’s extensions that will also require updating) Multi-site-manager compatible.

I’ve done a lot of searching but found very little on the topic.  Might I also add that I am very new to EE, currently carrying out my first build.

Basically what I have is four sites, each with an “event” weblog and “Event-image” custom field (mh_file_ext) respectively.

What I need to be able to do is pull “events” from each site.

e.g:

{exp:weblog:entries site="default_site|Coliseum" weblog="Event" show_expired="false" status="Featured|open" show_future_entries="yes" orderby="date" dynamic="off" limit="4"}
  {Event
-image}
{
/exp:weblog:entries}

However;  currently the mh_file_ext only supports one site, the latest one, as described in this post.

I figured it was straight forward; Get the list of site ids required (n.b. $TMPL->site_ids) and either:

1. Adopt Paul Bell’s fix with the simple addition of iterating through the site id’s.

e.g

 Signature 

View Creative Agency | http://www.viewcreative.co.uk

Profile
 
 
Posted: 04 August 2008 10:05 AM   [ Ignore ]   [ # 1 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  111
Joined  07-02-2008

function modify_template( $tagdata, $row )
  
{
    
global $DB, $FNS, $TMPL, $EXT, $SESS;
    global
$all_fields, $file_fields;

    if(
$EXT->last_call !== false)
    
{
      $tagdata
= $EXT->last_call;
    
}

    $site_ids
= $TMPL->site_ids;

    foreach(
$site_ids as $site_id) {

      
if(!isset($SESS->cache)) $SESS->cache = array();
      if(!isset(
$SESS->cache['mh_file_ext_fields_'.$site_id]))
      
{
        $SESS
->cache['mh_file_ext_fields_'.$site_id] = $DB->query('SELECT * FROM exp_weblog_fields f, exp_upload_prefs p WHERE f.field_type="file" AND p.id=f.field_list_items AND f.site_id='.$site_id);
      
}

      
//  =============================================
      //  Loop Through Fields
      //  =============================================
      
foreach($SESS->cache['mh_file_ext_fields_'.$site_id]->result as $file_field)
      
{
        
//  =============================================
        //  Does the field exist?
        //  =============================================
        
if(!isset($row['field_id_'.$file_field['field_id']]))
        
{
          $tagdata
= $this->clean_tagdata($tagdata, $file_field['field_name']);
          continue;
        
}

        
//  =============================================
        //  Are there files?
        //  =============================================
        
if(trim($row['field_id_'.$file_field['field_id']]) == '')
        
{
          $tagdata
= $this->clean_tagdata($tagdata, $file_field['field_name']);
          continue;
        
}

        
//  =============================================
        //  Split out to array
        //  =============================================
        //  We'll also want to clear our empty rows and
        //  reset our indexes to 0, that's what the three
        //  calls do.
        //  ---------------------------------------------
        
$files = array();
        
$thumbs = array();
        
$all_files = array_merge(array_filter(preg_split('/\s*[\r\n]+\s*/', $row['field_id_'.$file_field['field_id']])));
        foreach(
$all_files as $file)
        
{
          
if(preg_match('/'.$this->thumb_suffix.'\.[^.]*$/', $file))
          
{
            $thumbs[]
= $file;
          
}
          
else
          
{
            $files[]
= $file;
          
}
        }

        
//  =============================================
        //  Are there still files or thumbs?
        //  =============================================
        
if(count($files) === 0 && count($thumbs) === 0)
        
{
          $tagdata
= $this->clean_tagdata($tagdata, $file_field['field_name']);
          continue;
        
}

        
//  =============================================
        //  Reset then Get Settings
        //  =============================================
        
$this->_set_prefs($file_field['field_list_items']);

        
//  =============================================
        //  Store Field Name
        //  =============================================
        //  This will make later stuff much shorter.
        //  ---------------------------------------------
        
$field_name = $file_field['field_name'];

 Signature 

View Creative Agency | http://www.viewcreative.co.uk

Profile
 
 
Posted: 04 August 2008 10:05 AM   [ Ignore ]   [ # 2 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  111
Joined  07-02-2008

//  =============================================
        //  Set Up Conditionals
        //  =============================================
        
$variables = array();
        
$variables[$field_name] = implode(', ', $files);
        
$variables[$field_name.$this->thumb_suffix] = implode(', ', $thumbs);
        
$tagdata = $TMPL->array_conditionals($tagdata, $variables);

        
//  =============================================
        //  Replace Out Multi-Fields
        //  =============================================
        
preg_match_all('/'.LD.$field_name.'(.*?)'.RD.'(.*?)'.LD.SLASH.$field_name.RD.'/s', $tagdata, $tagchunk);
        foreach(
$tagchunk[2] as $chunk_key=>$raw_chunk)
        
{
          $return_chunk
= '';
          
$parameters = $FNS->assign_parameters($tagchunk[1][$chunk_key]);
          
$total_results = count($files);

          foreach(
$files as $count=>$file)
          
{
            $thumb
= $this->_add_suffix($file, $this->thumb_suffix);
            if(
in_array($thumb, $thumbs) !== FALSE)
            
{
              $thumb_path
= $file_field['server_path'].'/'.$thumb;
              
$thumb_url = is_file($thumb_path)?$FNS->remove_double_slashes($file_field['url'].'/'.$thumb):'';
            
}
            
else
            
{
              $thumb_path
= '';
              
$thumb_url = '';
            
}

            $var
= array(
              
'count' => $count+1,
              
'total_results' => $total_results,
              
'file_name' => $file,
              
'file_url' => $FNS->remove_double_slashes($file_field['url'].'/'.$file),
              
'file'.$this->thumb_suffix.'_name' => $thumb,
              
'file'.$this->thumb_suffix.'_url' => $thumb_url
            
);

            
$str = $FNS->prep_conditionals($raw_chunk, $var);

            
$var = array_flip($var);
            
$var = array_map(create_function('$x', 'return LD.$x.RD;'), $var);
            
$var = array_flip($var);

            
$return_chunk.= str_replace(array_keys($var), $var, $str);
          
}

          
if(isset($parameters['backspace']) && is_numeric($parameters['backspace']))
          
{
            $return_chunk
= preg_replace('/\s+$/s', '', $return_chunk);
            
$return_chunk = str_replace(array('/'), array('/'), $return_chunk);
            
$return_chunk = substr($return_chunk, 0, strlen($return_chunk)-$parameters['backspace']);
          
}

          $tagdata
= str_replace($tagchunk[0][$chunk_key], $return_chunk, $tagdata);
        
}

        
//  =============================================
        //  Replace Out Single-Fields
        //  =============================================
        
if(isset($files[0]))
        
{
          $file_path
= $FNS->remove_double_slashes($file_field['url'].'/'.$files[0]);
          
$tagdata = preg_replace('/'.LD.$field_name.RD.'/', $file_path, $tagdata);
        
}

        
if(isset($thumbs[0]))
        
{
          $file_path
= $FNS->remove_double_slashes($file_field['url'].'/'.$thumbs[0]);
          
$tagdata = preg_replace('/'.LD.$field_name.$this->thumb_suffix.RD.'/', $file_path, $tagdata);
        
}
      }
    }

    
return $tagdata;
  
}

 Signature 

View Creative Agency | http://www.viewcreative.co.uk

Profile
 
 
Posted: 04 August 2008 10:06 AM   [ Ignore ]   [ # 3 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  111
Joined  07-02-2008

2. or simply split the site_ids array and append to the SQL query. (n.b. I have simplified the code by hard-coding the SQL query)

e.g

function modify_template( $tagdata, $row )
  
{
    
global $DB, $FNS, $TMPL, $EXT, $SESS;
    global
$all_fields, $file_fields;

    if(
$EXT->last_call !== false)
    
{
      $tagdata
= $EXT->last_call;
    
}

    
if(!isset($SESS->cache)) $SESS->cache = array();
    if(!isset(
$SESS->cache['mh_file_ext_fields']))
    
{
      $SESS
->cache['mh_file_ext_fields'] = $DB->query('SELECT * FROM exp_weblog_fields f, exp_upload_prefs p WHERE f.field_type="file" AND p.id=f.field_list_items AND (f.site_id=1 OR f.site_id=14)');

    
}

    
//  =============================================
    //  Loop Through Fields
    //  =============================================
    
foreach($SESS->cache['mh_file_ext_fields']->result as $file_field)
    
{
      
//  =============================================
      //  Does the field exist?
      //  =============================================
      
if(!isset($row['field_id_'.$file_field['field_id']]))
      
{
        $tagdata
= $this->clean_tagdata($tagdata, $file_field['field_name']);
        continue;
      
}

      
//  =============================================
      //  Are there files?
      //  =============================================
      
if(trim($row['field_id_'.$file_field['field_id']]) == '')
      
{
        $tagdata
= $this->clean_tagdata($tagdata, $file_field['field_name']);
        continue;
      
}

      
//  =============================================
      //  Split out to array
      //  =============================================
      //  We'll also want to clear our empty rows and
      //  reset our indexes to 0, that's what the three
      //  calls do.
      //  ---------------------------------------------
      
$files = array();
      
$thumbs = array();
      
$all_files = array_merge(array_filter(preg_split('/\s*[\r\n]+\s*/', $row['field_id_'.$file_field['field_id']])));
      foreach(
$all_files as $file)
      
{
        
if(preg_match('/'.$this->thumb_suffix.'\.[^.]*$/', $file))
        
{
          $thumbs[]
= $file;
        
}
        
else
        
{
          $files[]
= $file;
        
}
      }

      
//  =============================================
      //  Are there still files or thumbs?
      //  =============================================
      
if(count($files) === 0 && count($thumbs) === 0)
      
{
        $tagdata
= $this->clean_tagdata($tagdata, $file_field['field_name']);
        continue;
      
}

      
//  =============================================
      //  Reset then Get Settings
      //  =============================================
      
$this->_set_prefs($file_field['field_list_items']);

      
//  =============================================
      //  Store Field Name
      //  =============================================
      //  This will make later stuff much shorter.
      //  ---------------------------------------------
      
$field_name = $file_field['field_name'];

 Signature 

View Creative Agency | http://www.viewcreative.co.uk

Profile
 
 
Posted: 04 August 2008 10:06 AM   [ Ignore ]   [ # 4 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  111
Joined  07-02-2008

//  =============================================
      //  Set Up Conditionals
      //  =============================================
      
$variables = array();
      
$variables[$field_name] = implode(', ', $files);
      
$variables[$field_name.$this->thumb_suffix] = implode(', ', $thumbs);
      
$tagdata = $TMPL->array_conditionals($tagdata, $variables);

      
//  =============================================
      //  Replace Out Multi-Fields
      //  =============================================
      
preg_match_all('/'.LD.$field_name.'(.*?)'.RD.'(.*?)'.LD.SLASH.$field_name.RD.'/s', $tagdata, $tagchunk);
      foreach(
$tagchunk[2] as $chunk_key=>$raw_chunk)
      
{
        $return_chunk
= '';
        
$parameters = $FNS->assign_parameters($tagchunk[1][$chunk_key]);
        
$total_results = count($files);

        foreach(
$files as $count=>$file)
        
{
          $thumb
= $this->_add_suffix($file, $this->thumb_suffix);
          if(
in_array($thumb, $thumbs) !== FALSE)
          
{
            $thumb_path
= $file_field['server_path'].'/'.$thumb;
            
$thumb_url = is_file($thumb_path)?$FNS->remove_double_slashes($file_field['url'].'/'.$thumb):'';
          
}
          
else
          
{
            $thumb_path
= '';
            
$thumb_url = '';
          
}

          $var
= array(
            
'count' => $count+1,
            
'total_results' => $total_results,
            
'file_name' => $file,
            
'file_url' => $FNS->remove_double_slashes($file_field['url'].'/'.$file),
            
'file'.$this->thumb_suffix.'_name' => $thumb,
            
'file'.$this->thumb_suffix.'_url' => $thumb_url
          
);

          
$str = $FNS->prep_conditionals($raw_chunk, $var);

          
$var = array_flip($var);
          
$var = array_map(create_function('$x', 'return LD.$x.RD;'), $var);
          
$var = array_flip($var);

          
$return_chunk.= str_replace(array_keys($var), $var, $str);
        
}

        
if(isset($parameters['backspace']) && is_numeric($parameters['backspace']))
        
{
          $return_chunk
= preg_replace('/\s+$/s', '', $return_chunk);
          
$return_chunk = str_replace(array('/'), array('/'), $return_chunk);
          
$return_chunk = substr($return_chunk, 0, strlen($return_chunk)-$parameters['backspace']);
        
}

        $tagdata
= str_replace($tagchunk[0][$chunk_key], $return_chunk, $tagdata);
      
}

      
//  =============================================
      //  Replace Out Single-Fields
      //  =============================================
      
if(isset($files[0]))
      
{
        $file_path
= $FNS->remove_double_slashes($file_field['url'].'/'.$files[0]);
        
$tagdata = preg_replace('/'.LD.$field_name.RD.'/', $file_path, $tagdata);
      
}

      
if(isset($thumbs[0]))
      
{
        $file_path
= $FNS->remove_double_slashes($file_field['url'].'/'.$thumbs[0]);
        
$tagdata = preg_replace('/'.LD.$field_name.$this->thumb_suffix.RD.'/', $file_path, $tagdata);
      
}
    }

    
return $tagdata;
  
}

 Signature 

View Creative Agency | http://www.viewcreative.co.uk

Profile
 
 
Posted: 04 August 2008 10:08 AM   [ Ignore ]   [ # 5 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  111
Joined  07-02-2008

n.b.

$SESS->cache['mh_file_ext_fields'] = $DB->query('SELECT * FROM exp_weblog_fields f, exp_upload_prefs p WHERE f.field_type="file" AND p.id=f.field_list_items AND (f.site_id=1 OR f.site_id=14)');

But, both only populate the template with one of the “sites” file custom field data.

If any one could shed a little light on the situation I’d be greatly appreciate it.  I’m literally banging my head against a brick wall (with an impending deadline looming overhead!).

Thanks in advance.

Matt @ View

p.s. can any one advise a better way to provide code examples (other than splitting posts).  I tried to attach .php files but got denied.  I then renamed .php to .txt and got told I had invalid content for the MIME type (text?).

 Signature 

View Creative Agency | http://www.viewcreative.co.uk

Profile
 
 
Posted: 05 August 2008 03:56 AM   [ Ignore ]   [ # 6 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  111
Joined  07-02-2008

FYI I’ve now had to abandon using Mark Huots file extension.  I’ve wasted too much time already.  Hopefully if I get a chance to revisit it in the future once my understanding of the EE framework is better.

If any one could point me in the direction of an EE extension HOWTO/tutorial I’d be most greatful.  There is very little information available online.

 Signature 

View Creative Agency | http://www.viewcreative.co.uk

Profile
 
 
Posted: 24 August 2008 08:10 PM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  168
Joined  12-08-2007

I’m interested in this too. I’m surprised more people aren’t.

 Signature 

For a list of all the ways technology has failed to improve the quality of life, please press three.

Profile
 
 
Posted: 25 August 2008 05:34 AM   [ Ignore ]   [ # 8 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  1076
Joined  01-24-2006

Hey guys.

Check out some of my most recent released extensions, they are all MSM compatible.

The first thing you have to remember is to store all the settings a little differently. The only way to do this is a custom settings form.

I’ll try and write a tutorial when our new site goes live. In the meantime check out the source code.

Cheers

 Signature 

VOTE FOR LG Addons in the Mashable Open Web Awards!


Newism - Newcastle Web Design & Development


LG Better Meta now w/ Sitemap Meta & XML Generator | LG Polls | LG .htaccess Generator

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 1149, on July 16, 2007 09:33 AM
Total Registered Members: 64938 Total Logged-in Users: 56
Total Topics: 81910 Total Anonymous Users: 52
Total Replies: 440319 Total Guests: 284
Total Posts: 522229    
Members ( View Memberlist )