You are here

function fieldable_panels_panes_update_7112 in Fieldable Panels Panes (FPP) 7

Update all Panelizer displays to point non-reusable FPPs to the vid.

(instead of the fpid)

File

./fieldable_panels_panes.install, line 449
Fieldable Panels Panes install file.

Code

function fieldable_panels_panes_update_7112(&$sandbox) {
  if (!module_exists('panelizer')) {
    return t('Panelizer is not installed, so nothing to do.');
  }

  // This won't as-is work for many sites because the 'fpp_revision_locking'
  // variable won't be defined before it's checked for. The simplest approach
  // is to manually rerun the updates.
  //
  // @code
  // /**
  //  * Enable the FPP revision locking feature.
  //  */
  // function MYMODULE_update_7100() {
  //   variable_set('fpp_revision_locking', 'lock');
  // }
  //
  // /**
  //  * Rerun Fieldable Panels Panes update 7112.
  //  */
  // function MYMODULE_update_7101(&$sandbox) {
  //   return fieldable_panels_panes_update_7112($sandbox);
  // }
  // @endcode
  //
  // A more correct approach is to use hook_update_dependencies to ensure that
  // the updates happen in the correct order. This may not work because if the
  // hook implementation is newly added to the module it won't be recognized
  // until the caches are cleared, which may not happen until after the updates
  // are ran, thus the updates not running in the correct order.
  //
  // @code
  // /**
  //  * Implements hook_update_dependencies().
  //  */
  // function MYMODULE_update_dependencies() {
  //   // Make sure that update 7100 below runs *after* Fieldable Panels Pane
  //   // update 7111 and *before* Fieldable Panels Pane update 7112, that way
  //   // the FPP 'lock' option will be enabled when the Panelizer updates are
  //   // started.
  //   $dependencies['MYMODULE'][7100] = array(
  //     'fieldable_panels_panes' => 7111,
  //   );
  //   $dependencies['fieldable_panels_panes'][7112] = array(
  //     'MYMODULE' => 7100,
  //   );
  //   return $dependencies;
  // }
  //
  // /**
  //  * Enable the FPP revision locking feature.
  //  */
  // function MYMODULE_update_7100() {
  //   variable_set('fpp_revision_locking', 'lock');
  // }
  // @endcode
  //
  // An alternative solution would be to rerun the updates.
  if (variable_get('fpp_revision_locking', 'lock') != 'lock') {
    return t('Pane locking is not enabled, so nothing to do.');
  }

  // Update all Panelizer displays.
  // The first time through, work out how many records need to be updated.
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;

    // Total records that must be processed.
    $sandbox['max'] = db_query("SELECT DISTINCT fpp.fpid as fpid, fpp.vid as vid\n      FROM {fieldable_panels_panes} fpp\n      INNER JOIN {panels_pane} pp\n        ON fpp.fpid = SUBSTRING(pp.subtype FROM (POSITION(':' IN pp.subtype) + 1))\n      INNER JOIN {panelizer_entity} pe\n        ON pe.did = pp.did\n      WHERE (pp.subtype LIKE 'fpid:%' OR pp.subtype LIKE 'current:%')\n        AND fpp.reusable != 1\n      ORDER BY fpp.fpid")
      ->rowCount();

    // If there's nothing to do, bail early.
    if (empty($sandbox['max'])) {
      return t('No panels needed to be updated.');
    }
  }

  // Do the updates in small batches.
  $limit = 10;

  // Get a list of all FPIDs for Panelizer displays.
  $results = db_query_range("SELECT DISTINCT fpp.fpid as fpid, fpp.vid as vid\n    FROM {fieldable_panels_panes} fpp\n    INNER JOIN {panels_pane} pp\n      ON fpp.fpid = SUBSTRING(pp.subtype FROM (POSITION(':' IN pp.subtype) + 1))\n    INNER JOIN {panelizer_entity} pe\n      ON pe.did = pp.did\n    WHERE (pp.subtype LIKE 'fpid:%' OR pp.subtype LIKE 'current:%')\n      AND fpp.reusable != 1\n    ORDER BY fpp.fpid", 0, $limit);

  // Loop through the FPPs.
  foreach ($results as $record) {

    // Update the 'fpid:' and 'current:' records to use 'vid:'; don't bother
    // checking if the record exists first because there's no down side to
    // running this query immediately.
    $query = db_update('panels_pane')
      ->fields(array(
      'subtype' => 'vid:' . $record->vid,
    ))
      ->condition('subtype', 'fpid:' . $record->fpid)
      ->execute();
    $query = db_update('panels_pane')
      ->fields(array(
      'subtype' => 'vid:' . $record->vid,
    ))
      ->condition('subtype', 'current:' . $record->fpid)
      ->execute();

    // Increment the progress counter.
    $sandbox['progress']++;
  }

  // Done yet?
  $sandbox['#finished'] = empty($sandbox['max']) ? TRUE : $sandbox['progress'] / $sandbox['max'];
  if ($sandbox['#finished'] === TRUE) {
    return t('Updated @count record(s) to use the new locking system.', array(
      '@count' => $sandbox['max'],
    ));
  }
}