context_condition_path.inc

Go to the documentation of this file.
00001 <?php
00002 
00003 /**
00004  * Expose paths as a context condition.
00005  */
00006 class context_condition_path extends context_condition {
00007   /**
00008    * Omit condition values. We will provide a custom input form for our conditions.
00009    */
00010   function condition_values() {
00011     return array();
00012   }
00013 
00014   /**
00015    * Condition form.
00016    */
00017   function condition_form($context) {
00018     $form = parent::condition_form($context);
00019     unset($form['#options']);
00020 
00021     $form['#type'] = 'textarea';
00022     $form['#default_value'] = implode("\n", $this->fetch_from_context($context, 'values'));
00023     return $form;
00024   }
00025 
00026   /**
00027    * Condition form submit handler.
00028    */
00029   function condition_form_submit($values) {
00030     $parsed = array();
00031     $items = explode("\n", $values);
00032     if (!empty($items)) {
00033       foreach ($items as $v) {
00034         $v = trim($v);
00035         if (!empty($v)) {
00036           $parsed[$v] = $v;
00037         }
00038       }
00039     }
00040     return $parsed;
00041   }
00042 
00043   /**
00044    * Execute.
00045    */
00046   function execute() {
00047     if ($this->condition_used()) {
00048       // Include both the path alias and normal path for matching.
00049       $current_path = array(drupal_get_path_alias($_GET['q']));
00050       if ($current_path[0] != $_GET['q']) {
00051         $current_path[] = $_GET['q'];
00052       }
00053       foreach ($this->get_contexts() as $context) {
00054         $paths = $this->fetch_from_context($context, 'values');
00055         if ($this->match($current_path, $paths, TRUE)) {
00056           $this->condition_met($context);
00057         }
00058       }
00059     }
00060   }
00061 
00062   /**
00063    * Match the subject against a set of regex patterns.
00064    * Similar to drupal_match_path() but also handles negation through the use
00065    * of the ~ character.
00066    *
00067    * @param mixed $subject
00068    *   The subject string or an array of strings to be matched.
00069    * @param array $patterns
00070    *   An array of patterns. Any patterns that begin with ~ are considered
00071    *   negative or excluded conditions.
00072    * @param boolean $path
00073    *   Whether the given subject should be matched as a Drupal path. If TRUE,
00074    *   '<front>' will be replaced with the site frontpage when matching against
00075    *   $patterns.
00076    */
00077   protected function match($subject, $patterns, $path = FALSE) {
00078     static $regexps;
00079     $match = FALSE;
00080     $positives = $negatives = 0;
00081     $subject = !is_array($subject) ? array($subject) : $subject;
00082     foreach ($patterns as $pattern) {
00083       if (strpos($pattern, '~') === 0) {
00084         $negate = TRUE;
00085         $negatives++;
00086       }
00087       else {
00088         $negate = FALSE;
00089         $positives++;
00090       }
00091       $pattern = ltrim($pattern, '~');
00092       if (!isset($regexps[$pattern])) {
00093         if ($path) {
00094           $regexps[$pattern] = '/^(' . preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\2'), preg_quote($pattern, '/')) . ')$/';
00095         }
00096         else {
00097           $regexps[$pattern] = '/^(' . preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/'), array('|', '.*'), preg_quote($pattern, '/')) . ')$/';
00098         }
00099       }
00100       foreach ($subject as $value) {
00101         if (preg_match($regexps[$pattern], $value)) {
00102           if ($negate) {
00103             return FALSE;
00104           }
00105           $match = TRUE;
00106         }
00107       }
00108     }
00109     // If there are **only** negative conditions and we've gotten this far none
00110     // we actually have a match.
00111     if ($positives === 0 && $negatives) {
00112       return TRUE;
00113     }
00114     return $match;
00115   }
00116 }

Generated on Wed Jun 19 02:24:08 2013 for Context by  doxygen 1.4.7