From be7fc7c8e734590d55f527137f2b72fdcc722f81 Mon Sep 17 00:00:00 2001
From: webflo <florian@webflo.org>
Date: Fri, 5 Nov 2010 22:44:37 +0100
Subject: [PATCH] add support for cck allowed values. content_allowed_values()

---
 context.core.inc                      |    5 ++
 context.plugins.inc                   |   17 ++++++++
 plugins/context_condition_content.inc |   70 +++++++++++++++++++++++++++++++++
 3 files changed, 92 insertions(+), 0 deletions(-)
 create mode 100644 plugins/context_condition_content.inc

diff --git a/context.core.inc b/context.core.inc
index 0beba81..e476f30 100644
--- a/context.core.inc
+++ b/context.core.inc
@@ -170,6 +170,11 @@ function context_node_condition(&$node, $op) {
       $plugin->execute($node, $op);
     }
   }
+  if (module_exists('content')) {
+    if ($plugin = context_get_plugin('condition', 'content')) {
+      $plugin->execute($node, $op);
+    }
+  }
   if (module_exists('book')) {
     if ($plugin = context_get_plugin('condition', 'book')) {
       $plugin->execute($node, $op);
diff --git a/context.plugins.inc b/context.plugins.inc
index d1ea06e..3677537 100644
--- a/context.plugins.inc
+++ b/context.plugins.inc
@@ -62,6 +62,13 @@ function _context_context_registry() {
       'plugin' => 'context_condition_bookroot',
     );
   }
+  if (module_exists('content')) {
+    $registry['conditions']['content'] = array(
+      'title' => t('Content'),
+      'description' => t('Set this context when viewing a node with the selected content.'),
+      'plugin' => 'context_condition_content',
+    );
+  }
   if (module_exists('locale')) {
     $registry['conditions']['language'] = array(
       'title' => t('Language'),
@@ -205,6 +212,16 @@ function _context_context_plugins() {
       ),
     );
   }
+  if (module_exists('content')) {
+    $plugins['context_condition_content'] = array(
+      'handler' => array(
+        'path' => drupal_get_path('module', 'context') .'/plugins',
+        'file' => 'context_condition_content.inc',
+        'class' => 'context_condition_content',
+        'parent' => 'context_condition',
+      ),
+    );
+  }
   if (module_exists('book')) {
     $plugins['context_condition_book'] = array(
       'handler' => array(
diff --git a/plugins/context_condition_content.inc b/plugins/context_condition_content.inc
new file mode 100644
index 0000000..e45d78c
--- /dev/null
+++ b/plugins/context_condition_content.inc
@@ -0,0 +1,70 @@
+<?php
+// $Id$
+
+/**
+ * Expose node content as a context condition.
+ */
+class context_condition_content extends context_condition {
+  /**
+   * Allow admins to provide a section title, section subtitle and section class.
+   */
+  function condition_form($context) {
+    $form = parent::condition_form($context);
+    unset($form['#default_value']);
+    unset($form['#options']);
+
+    $values = $this->fetch_from_context($context);
+
+    if (module_exists('content')) {
+      foreach (content_fields() as $field) {
+        if ($options = content_allowed_values($field)) {
+          $form[$field['field_name']] = array(
+            '#title' => check_plain($field['widget']['label'] .' - '. $field['field_name']),
+            '#type' => 'checkboxes',
+            '#default_value' => $values['values'],
+          );
+
+          foreach ($options as $key => $value) {
+            $form[$field['field_name']]['#options'][$field['field_name'] . ':' . $key] = $value;
+          }
+        }
+      }
+    }
+
+    return $form;
+  }
+
+  /**
+   * Condition form submit handler.
+   */
+  function condition_form_submit($values) {
+    $flat = array();
+
+    foreach($values as $field) {
+      foreach($field as $value) {
+        if ($value !== 0) {
+          $flat[$value] = $value;
+        }
+      }
+    }
+
+    return $flat;
+  }
+
+  function execute($node, $op) {
+    if ($this->condition_used()) {
+      foreach ($this->get_contexts() as $context) {
+        $values = $this->fetch_from_context($context, 'values');
+        foreach (content_fields() as $field) {
+          if (!empty($node->$field['field_name']) && $options = content_allowed_values($field)) {
+            foreach ($node->$field['field_name'] as $item) {
+              if (in_array($field['field_name'] . ':' . $item['value'], $values)) {
+                $this->condition_met($context, $field['field_name'] . ':' . $item['value']);
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+}
-- 
1.7.3.1

