We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

MSM-ifying and extension (mh_file_ext)

Development and Programming

viewcreative's avatar
viewcreative
116 posts
about 17 years ago
viewcreative's avatar viewcreative

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

       
viewcreative's avatar
viewcreative
116 posts
about 17 years ago
viewcreative's avatar viewcreative
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'];
       
viewcreative's avatar
viewcreative
116 posts
about 17 years ago
viewcreative's avatar viewcreative
//  =============================================
        //  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;
  }
       
viewcreative's avatar
viewcreative
116 posts
about 17 years ago
viewcreative's avatar viewcreative
  1. 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'];
       
viewcreative's avatar
viewcreative
116 posts
about 17 years ago
viewcreative's avatar viewcreative
//  =============================================
      //  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;
  }
       
viewcreative's avatar
viewcreative
116 posts
about 17 years ago
viewcreative's avatar viewcreative

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?).

       
viewcreative's avatar
viewcreative
116 posts
about 17 years ago
viewcreative's avatar viewcreative

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.

       
Todd D.'s avatar
Todd D.
460 posts
16 years ago
Todd D.'s avatar Todd D.

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

       
Leevi Graham's avatar
Leevi Graham
1,143 posts
16 years ago
Leevi Graham's avatar Leevi Graham

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

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.