/[drupal]/contributions/modules/og_collections/og_collections.module
ViewVC logotype

Diff of /contributions/modules/og_collections/og_collections.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.2.2.9, Sun Apr 6 22:13:40 2008 UTC revision 1.2.2.10, Thu Apr 17 21:16:42 2008 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: og_collections.module,v 1.2.2.8 2008/04/06 00:08:09 sdboyer Exp $  // $Id: og_collections.module,v 1.2.2.9 2008/04/06 22:13:40 sdboyer Exp $
3    
4  /**  /**
5   * @file og_collections.module   * @file og_collections.module
# Line 12  Line 12 
12   * their own duplicate set of the panels in the collection.   * their own duplicate set of the panels in the collection.
13   */   */
14    
15  class og_collection {  /**
16    public $version, $grouptype;   * Defines one of the two main classes for this module: the Collection class.
17    private $modified;   *
18    // will $modified help?   */
19    abstract class og_collection {
20      protected $version, $grouptype, $defaults;
21    
22    // saves changes to the default collection    /**
23    function __construct() {     * Declaring as abstract allows us to programatically ensure that even empty
24      $this->grouptype = 'default';     * og_collection objects are still loaded with exactly the right parameters
25      $result = db_fetch_array(db_query("SELECT * FROM {og_collections} WHERE grouptype = 'default'"));     *
26      $this->version = $result['version'];     */
27      $pcpanel_data = array_filter($result, !in_array('grouptype', 'version', 'defaults'));    protected function __construct() {
28      if (!empty($pcpanel_data)) {      $pcpanels = self::pcpanels_list();
29        $this->load($pcpanel_data);      foreach ($pcpanels as $pcpname) {
30      }        $this->$pcpname = new og_pcpanel_empty();
     else {  
       throw new og_collections_exception('nopcpanel');  
31      }      }
32    }    }
33    
34    // reduce to single, using private variable and sleep function.    /**
35    function save_all() { // @TODO add some verification?     * Return an associative array of pcpnames, keyed by their corresponding pcpid.
36      foreach ($this as $member => $value) {     *
37        if ($member == ('version' || 'grouptype')) {     * @param bool $refresh
38          continue;     *  The list is cached using a static var, and will only be refreshed from the variables table if explicitly told to do so
39       * @return mixed $pcpanels
40       *  Returns FALSE if no pcpanels have been defined
41       */
42      public static function pcpanels_list($refresh = FALSE) {
43        static $pcpanels = array();
44        if (empty($pcpanels) || $refresh) {
45          $result = db_query("SELECT * FROM {og_collections_pcpanel_join}"); // TODO switch this to a variable_set call
46          if ($result != FALSE) {
47            while ($pcpanel = db_fetch_array($result)) {
48              $pcpanels[$pcpanel['pcpid']] = $pcpanel['pcpname'];
49            }
50        }        }
51        $this->save($value);        else $pcpanels = FALSE;
52        }
53        return $pcpanels;
54      }
55    
56      function array_of_pcpanels($sort = TRUE) {
57        $pcpanels = og_collection::pcpanels_list();
58        foreach ($pcpanels as $pcpname) {
59          $pcpanels_array[$pcpname] = $this->$pcpname;
60        }
61        if ($sort) {
62          uasort($pcpanels_array, 'og_collections_sort_collection');
63      }      }
64        return $pcpanels_array;
65    }    }
66    
67    // saves an individual collection    protected function get_collection_metavalues() {
68    function save($pcpid) {      $collection_values = db_fetch_array(db_query("SELECT defaults, version FROM {og_collections} WHERE grouptype = '%s'", $this->grouptype));
69      db_query("UPDATE {og_collections} SET pcpid_%d = '%s' WHERE grouptype = '%s'", $pcpid, serialize($this->$pcpid), $this->grouptype);      $this->version = $collection_values['version'];
70        $this->defaults = unserialize($collection_values['defaults']);
71    }    }
72    
73    // loads the relevant pcpanel data into the default collection    // reduce to single, using private variable and sleep function.
74    protected function load($pcpanel_data) {    function save_all() {
75      foreach ($pcpanel_data as $pcpname => $data) {      foreach ($this as $member => $value) {
76        $this->$pcpname = new og_pcpanel();        if ($member instanceof og_pcpanel) {
77        $settings = unserialize($data);          $this->save_one($value);
       $this->$pcpname->og_pcpanel_load($settings['did']);  
       foreach ($settings as $setting => $val) {  
         $this->$pcpname->$setting = $val;  
78        }        }
79      }      }
80    }    }
81    
82    public static function load_single($pcpname, $grouptype = 'default') {    // saves an individual og_pcpanel
83      return $result = unserialize(db_result(db_query("SELECT '%s' FROM {og_collections} WHERE grouptype = '%s'", $pcpname, $grouptype)));    function save_one($pcpid) {
84        db_query("UPDATE {og_collections} SET %s = '%s' WHERE grouptype = '%s'", $this->$pcpid->pcpname, serialize($this->$pcpid), $this->grouptype);
85    }    }
86    
87    // performs the collection instantiation routine    function load_all_pcpanels() {
88    function instantiate($nid) {      foreach (og_collection::pcpanels_list() as $pcpid => $pcpname) {
89          $this->load_pcpanel($pcpname);
90        }
91    }    }
92    
93    // @TODO need to do this. This will need to record version data on not only the pcpanel's abstracted data, but the displays themselves...    function load_pcpanel($pcpname) {
94    function new_version() {      $this->$pcpname = new og_pcpanel(array($pcpname, $this->grouptype));
95        $this->$pcpname->fill();
96      }
97    
98      function create_version() {
99        foreach (self::pcpanels_list() as $pcpid => $pcpname) {
100          $this->$pcpname->load_display();
101          $this->$pcpname->display->did = 'new'; // FIXME need to switch this over so that displays are recorded elsewhere, on their own...ick
102        }
103        db_query("INSERT INTO {og_collections_versions} (grouptype, vid, created, data) VALUES ('%s', %d, %d, '%s')", $this->grouptype, $this->vid++, time(), serialize($this));
104      }
105    
106      public static function og_panels_fields() {
107        return array(
108          "did" => "%d",
109          "nid" => "%d",
110          "published" => "%d",
111          "page_title" => "'%s'",
112          "path" => "'%s'",
113          "default_page" => "%d",
114          "show_blocks" => "%d",
115          "weight" => "%d",
116        );
117      }
118    
119      function get_pcpanel_stats() {
120        $h = $e = $d = $t = 0;
121        foreach (self::pcpanels_list() as $pcpname) {
122          if ($this->$pcpname->default_page) $h++;
123          $this->$pcpname->enabled ? $e++ : $d++;
124          $t++;
125        }
126        return array('homepage' => $h, 'enabled' => $e, 'disabled' => $d, 'total' => $t);
127      }
128    
129      /**
130       * Will work for now anyway
131       *
132       * @param unknown_type $member
133       * @return unknown
134       */
135      function __get($member) {
136        static $classvars = array();
137        if (empty($classvars)) {
138          $classvars = array_keys(get_class_vars('og_collection'));
139        }
140    
141        switch (TRUE) {
142          case (is_numeric($member) && $member != 0):
143            $pcp_list = og_collection::pcpanels_list();
144            if (isset($pcp_list[$member])) {
145              return $this->$pcpname;
146            }
147            $exception = 'There is no pcpanel that corresponds to the provided pcpid.'; // let this keep running in case the numeric argument corresponds to something else
148    
149          case (in_array($member, og_collection::pcpanels_list())):
150            return $this->load_pcpanel($member);
151    
152          case (in_array($member, $classvars) && isset($this->$member)):
153            return $this->$member;
154    
155        }
156        if (isset($exception)) {
157          throw new Exception($exception); // FIXME need to make my own extension of the exceptions class
158        }
159    }    }
160    
161    function __clone() {    // TODO REALLY? isn't this just a default?
162      function __isset($member) {
163        return isset($this->$member);
164    }    }
165    
166  } // end og_collection class declaration  } // end og_collection class declaration
167    
168    /**
169     * Defines the subclass for the default group collection.
170     *
171     * Note that this is used in BOTH of og_collections' operating modes.
172     *
173     */
174    class og_collection_default extends og_collection {
175    
176      private $modified;
177    
178      function __construct() {
179        parent::__construct();
180        $this->grouptype = 'default';
181        $this->version = db_result(db_query("SELECT version FROM {og_collections} WHERE grouptype = 'default'"));
182      }
183    }
184    
185    /**
186     * Defines the subclass for group type-specific collections.
187     *
188     */
189  class og_collection_typed extends og_collection {  class og_collection_typed extends og_collection {
190    
191    private $defaults;    private $modified;
192    
193    function __construct($grouptype) {    function __construct($grouptype) {
194        parent::__construct();
195      $this->grouptype = $grouptype;      $this->grouptype = $grouptype;
196      $result = db_fetch_array(db_query("SELECT * FROM {og_collections} WHERE grouptype = '%s'", $grouptype));      $this->get_collection_metavalues();
197      $this->version = $result['version'];    }
198    
199      // this is where we load the most up-to-date version of the pcpanels for those which are using the default collection's did    function load_all_pcpanels() {
200      $this->defaults = unserialize($result['defaults']);      parent::load_all_pcpanels();
201      if (!empty($defaults)) {      $this->insert_default_dids();
202        foreach ($defaults as $pcpname) {    }
203          $this->$pcpname = new og_pcpanel();  
204          $this->$pcpname->og_pcpanel_load(db_result(db_query("SELECT did FROM {og_collections_pcpanels} WHERE pcpname = '%s' AND grouptype = 'default'", $pcpname)));    /**
205          foreach (unserialize($result[$pcpname]) as $setting => $val) {     * Load the default dids directly from their values in og_collections_pcpanels, to ensure they're always up to date
206            $this->$pcpname->$setting = $val;     *
207          }     */
208        }    private function insert_default_dids() { // TODO not sure if this will return them all in a single array, or over the individual rows...oh well. If there's a way to get it to do it in a single array, though, let's do that!
209        $result = array_diff_keys($result, array_flip($defaults));      $ph = array_fill(0, count($this->defaults), "'%s'");
210        $result = db_query("SELECT did, pcpname FROM {og_collections_pcpanel} WHERE grouptype = 'default' AND pcpname IN (". implode(', ', $ph) .")", $this->defaults);
211        while ($data = db_fetch_array($result)) {
212          $dids[$data['pcpname']] = $data['did'];
213      }      }
214      $pcpanel_data = array_filter($result, !in_array('grouptype', 'version', 'defaults'));      foreach ($this->defaults as $pcpname) {
215      if (!empty($pcpanel_data)) { // loads any pcpanels that aren't reliant on the default        if ($dids[$pcpname] != $this->$pcpname->did) {
216        parent::load($pcpanel_data);          $this->$pcpname = new og_pcpanel($dids['pcpname']);
217            $this->$pcpname->fill();
218          }
219      }      }
220    }    }
221    
222    function revert_pcpanel() {    function relink_pcpanel($pcpname) {
223        $this->load_pcpanel($pcpname);
224        panels_delete_display($this->$pcpname->did);
225        $defcollectioncfg = new og_collection_default();
226        db_query("DELETE FROM {og_collections_pcpanel} WHERE pcpid = %d AND grouptype = '%s'", $this->$pcpname->pcpid, $this->$pcpname->grouptype);
227        $this->$pcpname->did = $defcollectioncfg->$pcpname->did;
228        $this->$pcpname->usedef = TRUE;
229        unset($this->defaults[$this->$pcpname->pcpid]);
230        $this->$pcpname->save();
231    }    }
232    
233      function delink_pcpanel($pcpanel) {
234        $code = panels_export_display($pcpanel->display);
235        eval($code);
236        panels_save_display($display);
237        $pcpanel->did = $display->did;
238        $pcpanel->linked = FALSE;
239        db_query("INSERT INTO {og_collections_pcpanel} (did, pcpid, pcpname, grouptype, version) VALUES (%d, %d, '%s', '%s', %d", $this->did, $this->pcpid, $this->pcpname, $this->grouptype, 0);
240        $this->save();
241        og_collection::pcpanels_list(TRUE); // TODO superfluous?
242      }
243    }
244    
245    class og_collection_new extends og_collection {
246    
247  }  }
248    
249  class og_pcpanel {  class og_pcpanel {
250    
251    public $default, $enabled, $weight, $did, $page_title, $path, $published, $show_blocks, $display, $context, $content_types, $version;    public $default_page, $enabled, $weight, $did, $page_title, $path, $published, $show_blocks, $version, $pcpname, $grouptype, $pcpid, $linked;
252    private $pcpname, $grouptype;    private $modified;
253    // @FIXME This is an ugly hack way of accomplishing this, surely something in php's OOP would accomodate a better method? Can't figure out how, though...  
254    /**
255    function __construct($op = FALSE) {   * Construct an og_pcpanel.
256      if ($op == 'new') {   *
257        $this->pcpid = db_next_id("{og_collections_pcpanel}_pcpid");   * //TODO fix this, you split these into subclasses
258        $display = panels_save_display(panels_new_display());   *
259        $this->did = $display->did;   * @param mixed $arg
260        $this->display = $display;   *    If we're 'load'ing, then must EITHER pass a valid pcpanel Display id (did), OR:
261        $this->grouptype = 'default';   *    An array containing FIRST the pcpid OR the pcpname; AND SECOND the group type, if a typed collection is being requested
262        db_query("INSERT INTO {og_collections_pcpanel} (did, pcpid, grouptype, version) VALUES (%d, %d, '%s', %d", $this->did, $this->pcpid, $this->grouptype, 0);   *    If nothing gets passed in, we're just creating an empty og_pcpanel object.
263      }   * @return bool FALSE
264      elseif ($op == 'export') {   *   if the object fails to perform the requested $op.
265     */
266      function __construct($arg = NULL) {
267        if (!is_null($arg)) {
268          try {
269            if (is_array($arg)) {
270              $this->get_by_other($arg[0], $arg[1]);
271            }
272            else {
273              $this->get_by_did($arg);
274            }
275          }
276          catch (Exception $e) {
277            return FALSE;
278          }
279      }      }
280    }    }
281    
282    // There remain some oustanding issues with PHP's __sleep() magic method related to including protected/private variables.    // There remain some oustanding issues with PHP's __sleep() magic method related to including protected/private variables.
283    // See http://us.php.net/manual/en/language.oop5.magic.php#81492 for workaround details.    // See http://us.php.net/manual/en/language.oop5.magic.php#81492 for workaround details.
284    function __sleep() {    function __sleep() {
285      return array('default', 'enabled', 'weight', 'did', 'page_title', 'path', 'published', 'show_blocks', 'pcpid');      return array('default_page', 'enabled', 'weight', 'did', 'page_title', 'path', 'published', 'show_blocks', 'pcpid', 'linked');
286    }    }
287    
288    // Doing it this way requires some double query-ness. Is this avoidable?    // Doing it this way requires some double query-ness. Is this avoidable?
289    function __wakeup() {    function __wakeup() {
290      $this->load();      // $this->get_by_did($this->did);
291    }    }
292    
293    // Saves the pcpanel data into the relevant collections & pcpanel fields    /**
294    function save() {     * Return the portion of pcpanel object data that is stored
295      db_query("UPDATE {og_collections_pcpanel} SET pcpid = %d, grouptype = '%s', version = %d WHERE did = %d", $this->pcpid, $this->grouptype, $this->version, $this->did);     * in the collections table as an associative array
296       *
297       */
298      private function to_collection_array() {
299        $members = array('default_page', 'enabled', 'weight', 'did', 'page_title', 'path', 'published', 'show_blocks', 'pcpid', 'linked');
300        foreach ($members as $member) {
301          $pcpanel_as_array[$member] = $this->$member;
302        }
303        return $pcpanel_as_array;
304      }
305    
306      function save($compare_values = NULL) {
307        if (!is_null($compare_values)) {
308          foreach ($compare_values as $setting => $value) {
309            if ($this->$setting != $value) {
310              $this->$setting = $value;
311              $this->modified = TRUE;
312            }
313          }
314        }
315        if ($this->modified || is_null($compare_values)) {
316          $this->write_to_db();
317        }
318    }    }
319    
320    // loads the pcpanel fields that are in the pcpanel table and stores them in the current object    private function write_to_db() {
321    function load() {      db_query("UPDATE {og_collections_pcpanel} SET pcpid = %d, pcpname = '%s', grouptype = '%s', version = %d WHERE did = %d", $this->pcpid, $this->pcpname, $this->grouptype, $this->version, $this->did);
322      if ($did = $this->did) { // __get snags the did if it's not loaded. wraps the whole function, b/c if there's no did, we can't load      db_query("UPDATE {og_collections} SET %s = '%s' WHERE grouptype = '%s'", $this->pcpname, serialize($this->to_collection_array()), $this->grouptype);
323        $result = db_fetch_array(db_query("SELECT * FROM {og_collections_pcpanel} WHERE did = %d", $this->did));    }
324        $this->pcpid = $result['pcpid'];  
325      function load_display() {
326        panels_load_include('plugins');
327        $this->display = panels_load_display($this->did);
328        $this->context = $this->display->context = array('og_panels' => panels_context_create_empty('group')); // TODO going to need to change this context stuff to reflect the new complexity
329      }
330      // FIXME IDIOT, why don't you just load all the damn collectioncfgs and remove them that way...use an empty if need be!!!
331      function change_pcpname($newname) {
332        db_query("UPDATE {og_collections_pcpanel_join}, {og_collections_pcpanel} SET pcpname = '%s' WHERE pcpid = %d", $newname, $this->pcpid);
333        $result = db_query("SELECT defaults, grouptype FROM {og_collections} WHERE grouptype <> 'default'");
334        while ($row = db_fetch_array($result)) {
335          $defaults = unserialize($row['defaults']);
336          $defaults[$this->pcpid] = $newname;
337          db_query("UPDATE {og_collections} SET defaults = '%s' WHERE grouptype = '%s'", serialize($defaults), $row['grouptype']);
338        }
339        db_query("ALTER TABLE {og_collections} CHANGE %s %s text", $this->pcpname, $newname);
340        og_collection::pcpanels_list(TRUE);
341      }
342    
343      /**
344       * Adds fields to the collectioncfg table to accomodate new pcpanels.
345       *
346       */
347      protected function add_pcpanel_column() {
348        switch ($GLOBALS['db_type']) {
349          case 'mysql':
350          case 'mysqli':
351            db_query("ALTER TABLE {og_collections} ADD COLUMN %s text", $this->pcpname);
352          break;
353    
354          case 'pgsql':
355            db_add_column($ret, 'og_collections', $fieldname, 'text');
356          break;
357        }
358        $result = db_query("SELECT defaults, grouptype FROM {og_collections} WHERE grouptype <> 'default'");
359        while ($row = db_fetch_array($result)) {
360          $defaults = unserialize($row['defaults']);
361          $defaults[$this->pcpid] = $this->pcpname;
362          db_query("UPDATE {og_collections} SET defaults = '%s' WHERE grouptype = '%s'", serialize($defaults), $row['grouptype']);
363        }
364        db_query("INSERT INTO {og_collections_pcpanel_join} (pcpid, pcpname) VALUES (%d, '%s'", $this->pcpid, $this->pcpname);
365        og_collection::pcpanels_list(TRUE);
366      }
367    
368      protected function drop_pcpanel_column() {
369        db_query("ALTER TABLE {og_collections} DROP $fieldname");
370        $result = db_query("SELECT defaults, grouptype FROM {og_collections} WHERE grouptype <> 'default'");
371        while ($row = db_fetch_array($result)) {
372          $defaults = unserialize($row['defaults']);
373          unset($defaults[$this->pcpid]);
374          db_query("UPDATE {og_collections} SET defaults = '%s' WHERE grouptype = '%s'", serialize($defaults), $row['grouptype']);
375        }
376        db_query("DELETE FROM {og_collections_pcpanel_join} WHERE pcpid = %d", $this->pcpid);
377        og_collection::pcpanels_list(TRUE);
378      }
379    
380      protected function get_by_did($did) {
381        $result = db_fetch_array(db_query("SELECT * FROM {og_collections_pcpanel} WHERE did = %d", $did));
382        if ($result !== FALSE) {
383          $this->pcpid = (int) $result['pcpid'];
384        $this->grouptype = $result['grouptype'];        $this->grouptype = $result['grouptype'];
385        $this->version = $result['version'];        $this->version = (int) $result['version'];
386        $this->pcpname = $result['pcpname'];        $this->pcpname = $result['pcpname'];
387        }
388        else {
389          throw new Exception('Bad display id provided.');
390      }      }
391    }    }
392    
393    function fill() {    protected function get_by_other($pcparg, $grouptype = 'default') {
394      $data = og_collection::load_single($this->pcpname, $this->grouptype); // TODO kind of an inelegant hack. Any better way of doing this?      $this->grouptype = $grouptype;
395      foreach ($data as $setting => $value) {      if (is_numeric($pcparg)) {
396        $this->$setting = $value;        $this->pcpid = $pcparg;
397          $field = 'pcpid';
398          $queryval = '%d';
399        }
400        else {
401          $this->pcpname = $pcparg;
402          $field = 'pcpname';
403          $queryval = "'%s'";
404        }
405        $result = db_fetch_array(db_query("SELECT * FROM {og_collections_pcpanel} WHERE $field = $queryval AND grouptype = '%s'", $pcparg, $grouptype));
406        if ($result !== FALSE) {
407          $this->pcpid = (int) $result['pcpid'];
408          $this->version = (int) $result['version'];
409          $this->pcpname = $result['pcpname'];
410          $this->did = $result['did'];
411        }
412        else {
413          throw new Exception('Bad pcparg and grouptype argument combo.');
414      }      }
415    }    }
416    
417    // magic method for snaggign whichever members    function fill($override = FALSE) {
418        $pcpanel_data = unserialize(db_result(db_query("SELECT %s FROM {og_collections} WHERE grouptype = '%s'", $this->pcpname, $this->grouptype)));
419        foreach ($pcpanel_data as $setting => $value) {
420          if (!isset($this->$setting) || $override) { // only add the value if the member is not yet set, or an override has been explicitly requested
421            $this->$setting = $value;
422          }
423        }
424      }
425    
426      // magic method for snagging certain requested members on demand
427    function __get($member) {    function __get($member) {
428      switch ($member) {      switch ($member) {
       case 'did':  
         if (isset($this->pcpid, $this->grouptype)) { // @FIXME need to expand this to include the pcpname  
           return $this->did = db_result(db_query("SELECT did FROM {og_collections_pcpanel} WHERE pcpid = %d AND grouptype = '%s'", $this->pcpid, $this->grouptype));  
         }  
         else {  
           $exception = 'Not enough information to get retrieve pcpanel data (missing pcpid or grouptype).'; // TODO what about default?  
         }  
         break;  
   
429        case 'pcpid':        case 'pcpid':
430        case 'grouptype':          return $this->pcpid = db_result(db_query("SELECT pcpid FROM {og_collections_pcpanel_join} WHERE pcpname = '%s'", $this->pcpname));
       case 'pcpname':  
         if (isset($this->did)) {  
           return $this->$member = db_result(db_query("SELECT '%s' FROM {og_collections_pcpanel} WHERE did = %d", $member, $this->did));  
         }  
         else {  
           $exception = 'Not enough information to get retrieve pcpanel data (missing did).';  
         }  
         break;  
   
       case 'display':  
         return $this->display = panels_load_display($this->did);  
431    
432          case 'display':
433        case 'context':        case 'context':
434          $this->display->context = array('og_panels' => panels_context_create_empty('group'));          $this->load_display();
435          return $this->context = $this->display->context;          return $member == 'display' ? $this->display : $this->context;
436    
437        case 'content_types':        case 'content_types':
438          $this->display->content_types = panels_common_get_allowed_types('og_panels', $this->context);          panels_load_include('common');
439          return $this->content_types = $this->display->content_types;          return $this->content_types = $this->display->content_types = panels_common_get_allowed_types('og_panels', $this->context);
440    
441        default:        default:
442          $exception = 'Invalid member requested.';          throw new Exception('Either an invalid og_pcpanel element was requested (probably due to a typo), or an as-yet unset element was requested that cannot be loaded on the fly.');
443          break;          break;
444      }      }
445      if (isset($exception)) {    }
446        throw new Exception($exception);  
447      function __set($member, $value) {
448        // used to indicate that a change has been made somewhere in the configuration. flags the object for saving later, if the method is called
449        if ($member == 'modified') {
450          $this->modified = $value;
451      }      }
452    }    }
453    
454  } // end og_pcpanel class declaration  } // end og_pcpanel class declaration
455    
456  class og_pcpanel_new extends og_pcpanel {  class og_pcpanel_new extends og_pcpanel {
457    
458      function __construct() {
459        $this->pcpid = 'new';// db_next_id("{og_collections_pcpanel}_pcpid"); CAN'T USE THIS YET, only when it's saved
460      }
461    
462      function save($init_values) {
463        $display = panels_new_display();
464        panels_save_display();
465        $this->did = $display->did;
466        $this->pcpid = db_next_id("{og_collections_pcpanel}_pcpid");
467        $this->grouptype = 'default';
468        db_query("INSERT INTO {og_collections_pcpanel} (did, pcpid, grouptype, version) VALUES (%d, %d, '%s', %d", $this->did, $this->pcpid, $this->grouptype, 0);
469        parent::add_pcpanel_column();
470        foreach (array_keys(og_collections_group_types(TRUE)) as $grouptype) {
471          parent::save($init_values);
472        }
473      }
474  }  }
475    
476  class og_pcpanel_export extends og_pcpanel {  // FIXME this is just a plain cheap trick. do better!
477    class og_pcpanel_transport extends og_pcpanel {
478    
479      function __sleep() {
480        $tosleep = array_keys(get_object_vars($this));
481        return $tosleep;
482      }
483    }
484    class og_pcpanel_empty extends og_pcpanel {
485    
486      function __contruct() {
487        // nothing - it's an empty! this is just here so it doesn't inherit
488      }
489  }  }
490    
491    
# Line 323  function og_collections_menu($may_cache) Line 595  function og_collections_menu($may_cache)
595          }          }
596        }        }
597        $items[] = array(        $items[] = array(
598          'path' => 'admin/og/og_collections/pcpanel',          'path' => 'admin/og/og_collections/pcpanel/form',
599          'callback' => 'og_collections_pcpanel_manage',          'callback' => 'drupal_get_form',
600            'callback_arguments' => array('og_collections_pcpanel_form'),
601          'type' => MENU_CALLBACK,          'type' => MENU_CALLBACK,
602          'access' => user_access('manage og collections'),          'access' => user_access('manage og collections'),
603        );        );
604      }        $items[] = array(
605            'path' => 'admin/og/og_collections/pcpanel/edit',
606            'callback' => 'og_collections_pcpanel_edit',
607            'type' => MENU_CALLBACK,
608            'access' => user_access('manage og collections'),
609          );
610          $items[] = array(
611            'path' => 'admin/og/og_collections/pcpanel/delete',
612            'callback' => 'drupal_get_form',
613            'callback arguments' => array('og_collections_pcpanel_delete_confirm'),
614            'type' => MENU_CALLBACK,
615            'access' => user_access('manage og collections'),
616          );
617          $items[] = array(
618            'path' => 'admin/og/og_collections/pcpanel/relink',
619            'callback' => 'drupal_get_form',
620            'callback arguments' => array('og_collections_pcpanel_relink_confirm'),
621            'type' => MENU_CALLBACK,
622            'access' => user_access('manage og collections'),
623          );
624        }
625    }    }
626    return $items;    return $items;
627  }  }
# Line 372  function og_collections_group_types($def Line 665  function og_collections_group_types($def
665  function og_collections_admin_collectioncfg($type) {  function og_collections_admin_collectioncfg($type) {
666    $group_types = og_collections_group_types(TRUE);    $group_types = og_collections_group_types(TRUE);
667    drupal_set_title("'". $group_types[$type] ."' Collection");    drupal_set_title("'". $group_types[$type] ."' Collection");
668    if (og_collections_check_for_pcpanels()) {    if (!og_collection::pcpanels_list()) {
669      drupal_set_message("You need to create at least one pre-configured panel for OG Collections to operate properly. Use this form to create your first one.");      drupal_set_message("You need to create at least one pre-configured panel for OG Collections to operate properly. Use this form to create your first one.");
670      drupal_get_destination();      drupal_get_destination();
671      drupal_goto('admin/og/og_collections/pcpanel/form');      drupal_goto('admin/og/og_collections/pcpanel/form');
# Line 398  function og_collections_admin_collection Line 691  function og_collections_admin_collection
691   *  The group type for which the collection settings are being edited.   *  The group type for which the collection settings are being edited.
692   *   *
693   */   */
694  function og_collections_collectioncfg_table($type) {  function og_collections_collectioncfg_table($type = 'default') {
695    $collectioncfg = $type == 'default' ? new og_collection() : new og_collection_typed($type);    $collectioncfg = $type == 'default' ? new og_collection_default() : new og_collection_typed($type); // FIXME this should be dependent on the global setting, NOT the individual $type value
696    $form['typevar'] = array('#type' => 'hidden', '#value' => $type);    $collectioncfg->load_all_pcpanels();
697    // $form['#action'] = arg(3) ? url('admin/og/og_collections/'. $theme_key) : url('admin/build/block');    $form['old_default_page'] = array('#type' => 'hidden', '#value' => 0); // initialize to 0 in case no home page is set
698      $form['typevar'] = array('#type' => 'hidden', '#value' => $type);
699    $form['#tree'] = TRUE;    $form['#tree'] = TRUE;
700    // TODO add a default tab as a means of allowing people to add/remove panel pre-configured panels?    // TODO add a default tab as a means of allowing people to add/remove panel pre-configured panels?
701    foreach ($collectioncfg as $pcpid => $pcpanel) {    foreach ($collectioncfg->array_of_pcpanels() as $pcpname => $pcpanel) {
702      $pcpanel = og_collections_load_pcpanel($pcpanel['did'], FALSE, $type);      $pcpid = $pcpanel->pcpid;
703      $form[$pcpid]['page_title'] = array('#value' => '#'. $pcpid .': '. $pcpanel->page_title);      $form[$pcpid]['page_title'] = array('#value' => $pcpanel->page_title .' (<em>'. $pcpanel->pcpname .'</em>)'); // TODO switch to 'description' font?
704      $form[$pcpid]['weight'] = array('#type' => 'weight', '#default_value' => $pcpanel->weight ? $pcpanel->weight : 0);      $form[$pcpid]['weight'] = array('#type' => 'weight', '#default_value' => $pcpanel->weight ? $pcpanel->weight : 0);
705      $form[$pcpid]['edit'] = array('#value' => l(t('Edit'), "admin/og/og_collections/pcpanel/form/$type/$pcpid", array(), drupal_get_destination()));      $form[$pcpid]['edit'] = array('#value' => l(t('Edit'), "admin/og/og_collections/pcpanel/form/$type/$pcpid", array(), drupal_get_destination()));
706        $form[$pcpid]['enabled'] = array('#type' => 'checkbox', '#default_value' => $pcpanel->enabled);
707        $form[$pcpid]['show_blocks'] = array('#type' => 'checkbox', '#default_value' => $pcpanel->show_blocks);
708        $form[$pcpid]['published'] = array('#type' => 'checkbox', '#default_value' => $pcpanel->published);
709      if ($type == 'default') {      if ($type == 'default') {
710        $form[$pcpid]['delete'] = array('#value' => l(t('Delete'), "admin/og/og_collections/pcpanel/delete/$pcpid", array(), drupal_get_destination()));        $form[$pcpid]['delete'] = array('#value' => l(t('Delete'), "admin/og/og_collections/pcpanel/delete/$pcpid", array(), drupal_get_destination()));
711      }      }
712      if ($pcpanel->default_page == 1) {  
713        if ($pcpanel->default_page == TRUE) {
714        $default_page = $pcpid;        $default_page = $pcpid;
715          $form['old_default_page'] = array('#type' => 'hidden', '#value' => $pcpid);
716      }      }
717      if ($pcpanel->active == 1) {      $options[$pcpid] = '';
       $activelist[] = $pcpid;  
     }  
     if ($pcpanel->published == 1) {  
       $publist[] = $pcpid;  
     }  
     if ($pcpanel->show_blocks == 1) {  
       $blklist[] = $pcpid;  
     }  
     $optionshome[$pcpid] = '';  
718    }    }
719    
720    $options = $optionshome;    // TODO Temporarily disabling this - talk to Moshe about trying to change this to the more intuitive config I have here
721    $optionshome[0] = t('<em>(Do not replace the group homepage with a pre-configured panel)</em>');    // $options = $optionshome;
722    // $form[0]; // need to add something here that allows people to DESELECT something as the homepage    // $optionshome[0] = t('<em>(Do not replace the group homepage with a pre-configured panel)</em>');
   $form['active'] = array(  
     '#type' => 'checkboxes',  
     '#options' => $options,  
     '#default_value' => $activelist ? $activelist : NULL,  
   );  
723    $form['default_page'] = array(    $form['default_page'] = array(
724      '#type' => 'radios',      '#type' => 'radios',
     '#options' => $optionshome,  
     '#default_value' => $default_page ? $default_page : 0,  
   );  
   $form['published'] = array(  
     '#type' => 'checkboxes',  
     '#options' => $options,  
     '#default_value' => $publist ? $publist : NULL,  
   );  
   $form['show_blocks'] = array(  
     '#type' => 'checkboxes',  
725      '#options' => $options,      '#options' => $options,
726      '#default_value' => $blklist ? $blklist : NULL,      '#default_value' => $default_page ? $default_page : 0,
727    );    );
728    $form['submit'] = array(    $form['submit'] = array(
729      '#type' => 'submit',      '#type' => 'submit',
# Line 464  function og_collections_collectioncfg_ta Line 739  function og_collections_collectioncfg_ta
739   */   */
740  function theme_og_collections_collectioncfg_table($form) {  function theme_og_collections_collectioncfg_table($form) {
741    $isdefault = $form['typevar']['#value'] == 'default' ? TRUE : FALSE;    $isdefault = $form['typevar']['#value'] == 'default' ? TRUE : FALSE;
742    foreach (element_children($form) as $pcpid) {    foreach (array_intersect(element_children($form), array_keys(og_collection::pcpanels_list())) as $pcpid) {
743      if (is_numeric($pcpid)) {      $row = array(
744        $rows[] = array(        drupal_render($form[$pcpid]['page_title']),
745          drupal_render($form[$pcpid]['page_title']),        drupal_render($form['default_page'][$pcpid]),
746          drupal_render($form['default_page'][$pcpid]),        drupal_render($form[$pcpid]['enabled']),
747          drupal_render($form['active'][$pcpid]),        drupal_render($form[$pcpid]['published']),
748          drupal_render($form['published'][$pcpid]),        drupal_render($form[$pcpid]['show_blocks']),
749          drupal_render($form['show_blocks'][$pcpid]),        drupal_render($form[$pcpid]['weight']),
750          drupal_render($form[$pcpid]['weight']),        drupal_render($form[$pcpid]['edit']),
751          drupal_render($form[$pcpid]['edit']),      );
752          drupal_render($form[$pcpid]['delete']), // TODO figure out a way to get rid of this if !$isdefault      if ($isdefault) {
753        );        $row[] = drupal_render($form[$pcpid]['delete']);
754      }      }
755        $rows[] = $row;
756        unset($row);
757    }    }
758    
759    $output = drupal_render($form);    $output = drupal_render($form);
760    $oper = $isdefault ? array('data' => t('Operations'), 'align' => 'center', 'colspan' => 2) : array('data' => t('Operations'), 'align' => 'center', 'colspan' => 1);    $header = array(t('Panel Name'), t('Home Page'), t('Enabled'), t('Published'), t('Show Blocks'), t('Weight'), array('data' => t('Operations'), 'align' => 'center', 'colspan' => $isdefault ? 2 : 1));
   $header = array(t('Panel Name'), t('Home Page'), t('Enabled'), t('Published'), t('Show Blocks'), t('Weight'), $oper);  
761    return theme('table', $header, $rows) . $output;    return theme('table', $header, $rows) . $output;
762  }  }
763    
764  function og_collections_collectioncfg_table_validate($form_id, $form_values, $form) {  /**
765    // what kind of validation actually needs to be done here? any?   * Specialized sort function. Sorts a given collectioncfg $conf array by the following criteria:
766     * 1. Default Page status
767     * 2. Active status (enabled/disabled in the current pre-configured panel)
768     * 3. Weight
769     */
770    function og_collections_sort_collection($a, $b) {
771      if ($b->default_page) {
772        return TRUE;
773      }
774      elseif ($a->default_page) {
775        return FALSE;
776      }
777    
778      $active = $b->enabled - $a->enabled;
779      if ($active) {
780        return $active;
781      }
782    
783      // if ($a['active']) return ($a['weight'] - $b['weight']);
784      return ($a->weight - $b->weight);
785  }  }
786    
787    /**
788     * Submit form changes to class methods for saving to the db.
789     *
790     * TODO once I switch to a js gui frontend, see if the 'modified' value is easier to record dynamically
791     *
792     */
793  function og_collections_collectioncfg_table_submit($form_id, $form_values) {  function og_collections_collectioncfg_table_submit($form_id, $form_values) {
794    $type = $form_values['typevar'];    $collectioncfg = $form_values['typevar'] == 'default' ? new og_collection_default() : new og_collection_typed($type); // FIXME this should be dependent on the global setting, NOT the individual $type value
795    $collectioncfg = og_collections_load_collectioncfg($type);    $collectioncfg->load_all_pcpanels();
796    foreach (array_keys($collectioncfg) as $pcpid) {    if ($form_values['default_page'] != $form_values['old_default_page']) {
797      $collectioncfg[$pcpid]['active'] = (int) $form_values['default_page'] == $pcpid ? 1 : $form_values['active'][$pcpid] ? 1 : 0; // ensures that pcpanels set to be home pages are also active      $form_values[$form_values['default_page']]['default_page'] = TRUE;
798      $collectioncfg[$pcpid]['default_page'] = (int) $form_values['default_page'] == $pcpid ? 1 : 0;      $form_values[$form_values['old_default_page']]['default_page'] = FALSE;
     $collectioncfg[$pcpid]['weight'] = (int) $form_values[$pcpid]['weight'];  
     $collectioncfg[$pcpid]['published'] = (int) $form_values['default_page'] == $pcpid ? 1 : ($form_values['published'][$pcpid]) == $pcpid ? 1 : 0; // ensures that pcpanels set to be home pages are also published  
     $collectioncfg[$pcpid]['show_blocks'] = (int) ($form_values['show_blocks'][$pcpid]) == $pcpid ? 1 : 0;  
799    }    }
800    if (og_collections_save_collectioncfg($type, $collectioncfg, array_keys($collectioncfg))) {    foreach (og_collection::pcpanels_list() as $pcpid => $pcpname) {
801      drupal_set_message(t("Updated '$type' Collection."));      $collectioncfg->$pcpname->compare_then_save($form_values[$pcpid]);
802    }    }
803      drupal_set_message(t("Updated !type Collection.", array('!type' => $form_values['typevar'])));
804  }  }
805    
806  /**  /**
807   * Render the page containing pcpanel-specific configuration links.   * Render the page containing pcpanel-specific configuration links.
808   *   *
809   * @ingroup themeable  
810   * @param string $type   * @param string $type
811   *  The group type for which the collection settings are being edited.   *  The group type for which the collection settings are being edited.
812   * @return string $output   * @return string $output
813   *  String of HTML that has (already) been rendered by the theme('page') function... TODO split this into a separate function so it can be themed   *  String of HTML that has (already) been rendered by the theme('page') function... TODO split this into a separate function so it can be themed
814   */   */
815  function og_collections_admin_pcpanelsetup($type) {  function og_collections_admin_pcpanelsetup($type) {
816    $group_types = og_collections_group_types(TRUE);    $group_types = og_collections_group_types(TRUE);
817    drupal_set_title("'". $group_types[$type] ."' Panels Collection");    drupal_set_title("'". $group_types[$type] ."' Panels Collection");
818    if (count(db_fetch_array(db_query("SELECT * FROM {og_collections} WHERE grouptype = 'default'"))) == 1) {    if (!og_collection::pcpanels_list()) {
819      drupal_set_message("You need to create at least one group panel pre-configured panel before you can start configuring them! Use this form to create one.");      drupal_set_message("You need to HAVE at least one one group pre-configured panel before you can configure it! Use this form to create one.");
     drupal_get_destination();  
820      drupal_goto('admin/og/og_collections/pcpanel/form');      drupal_goto('admin/og/og_collections/pcpanel/form');
821    }    }
822    $collectionsetting = variable_get('og_collections_settings', FALSE);    $collectionsetting = variable_get('og_collections_settings', FALSE);
# Line 533  function og_collections_admin_pcpanelset Line 831  function og_collections_admin_pcpanelset
831                  <p>These are just guidelines. At any time, you can remove a type-specific pre-configured panel and cause the group type to begin using Default again instead. You can also copy panel pre-configured panel settings from one group type to another, if you want. But, NOTE that <strong>both of these options are completely irreversible</strong> short of restoring your database from a <a href=\"http://drupal.org/project/backup_migrate\">backup</a>.</p>";                  <p>These are just guidelines. At any time, you can remove a type-specific pre-configured panel and cause the group type to begin using Default again instead. You can also copy panel pre-configured panel settings from one group type to another, if you want. But, NOTE that <strong>both of these options are completely irreversible</strong> short of restoring your database from a <a href=\"http://drupal.org/project/backup_migrate\">backup</a>.</p>";
832     $linktext = 'adding a new pre-configured panel';     $linktext = 'adding a new pre-configured panel';
833    }    }
   $output = t($helptext, array('!link' => l(t($linktext), 'admin/og/og_collections/pcpanel/form', array(), drupal_get_destination())));  
834    
835    // looks like we may not need a form, as no data gets submitted or changed directly on this page...begin form substitution!    $output = t($helptext, array('!link' => l(t($linktext), 'admin/og/og_collections/pcpanel/form', array(), drupal_get_destination())));
836    $collectioncfg = og_collections_load_collectioncfg($type);    // $output .= theme_og_collections_admin_pcpanelsetup($type, FIXME make this themeable
837    
838      $collectioncfg = strtolower($type) == 'default' ? new og_collection_default() : new og_collection_typed($type); // FIXME this should be dependent on the global setting, NOT the individual $type value
839      $collectioncfg->load_all_pcpanels();
840    $multi = $collectionsetting == 2 ? TRUE : FALSE;    $multi = $collectionsetting == 2 ? TRUE : FALSE;
841    $isdefault = strtolower($type) == 'default' ? TRUE : FALSE;    $isdefault = strtolower($type) == 'default' ? TRUE : FALSE;
842      $stats = $collectioncfg->get_pcpanel_stats(TRUE);
843      // build configuration table
844    
845    if (!$isdefault) {    $rows = array();
846      foreach ($collectioncfg as $pcpid => $pcpanel) {    $last_pcpanel = 'none';
847        $pcpanel = og_collections_load_pcpanel($pcpanel['did'], FALSE, $type);    foreach ($collectioncfg->array_of_pcpanels() as $pcpname => $pcpanel) {
848        if ($pcpanel->active) {      if ($pcpanel->default_page) {
849          $row = array();        $rows[] = array(array('data' => t('Group Home Page Panel'), 'class' => 'region', 'colspan' => $isdefault ? 4 : 6));
850          $row['page_title'] = $pcpanel->page_title;        $last_status = 'home';
851          $row['usedef'] = $pcpanel->usedef ? 'Yes' : 'No';      }
852          $row['default_page'] = $pcpanel->default_page ? 'Yes' : 'No';      elseif ($pcpanel->enabled) {
853          $row['content'] = l(t('Content'), "admin/og/og_collections/pcpanel/content/$type/$pcpid", array(), drupal_get_destination());        if ($last_status == 'home' || 'none') {
854          $row['layout'] = l(t('Layout'), "admin/og/og_collections/pcpanel/layout/$type/$pcpid", array(), drupal_get_destination());          $rows[] = array(array('data' => t('Enabled Pre-Configured Panels'), 'class' => 'region', 'colspan' => $isdefault ? 4 : 6));
855          $row['layset'] = l(t('Layout Settings'), "admin/og/og_collections/pcpanel/layset/$type/$pcpid", array(), drupal_get_destination());        }
856          $row['revert'] = $pcpanel->usedef ? 'Already Tied' : l(t('Revert'), "admin/og/og_collections/pcpanel/revert/$type/$pcpid", array(), drupal_get_destination());        $last_status = 'enabled';
857          $row['mirror'] = 'Coming Soon';      }
858        elseif ($last_status = 'enabled' || 'home') {
859          $rows[] = $row;        $rows[] = array(array('data' => t('Disabled Pre-Configured Panels'), 'class' => 'region', 'colspan' => $isdefault ? 4 : 6));
860        }        $last_status = 'disabled';
861      }      }
862    }      $pcpid = $pcpanel->pcpid;
863    else {      $row['page_title'] = $pcpanel->page_title;
864      foreach ($collectioncfg as $pcpid => $pcpanel) {      // $row['default_page'] = $pcpanel->default_page ? 'Yes' : 'No';
865        $pcpanel = og_collections_load_pcpanel($pcpanel['did'], FALSE);      if (!$isdefault) {
866        if ($pcpanel->active) {        $linked_to_default = in_array($pcpname, $collectioncfg->defaults);
867          $row = array();        $row['usedef'] = $pcpanel->linked ? 'Yes' : 'No';
868          $row['page_title'] = $pcpanel->page_title;        $row['relink'] = $pcpanel->linked ? 'Already Linked' : l(t('Restore Link'), "admin/og/og_collections/pcpanel/relink/$pcpanel->did", array(), drupal_get_destination());
869          $row['default_page'] = $pcpanel->default_page ? 'Yes' : 'No';        // $row['import'] = 'Coming Soon'; TODO do it or don't, but don't let it sit
870          $row['content'] = l(t('Content'), "admin/og/og_collections/pcpanel/content/$pcpid", array(), drupal_get_destination());      }
871          $row['layout'] = l(t('Layout'), "admin/og/og_collections/pcpanel/layout/$pcpid", array(), drupal_get_destination());      if ($pcpanel->enabled) {
872          $row['layset'] = l(t('Layout Settings'), "admin/og/og_collections/pcpanel/layset/$pcpid", array(), drupal_get_destination());        $edit_url = $pcpid . ($isdefault ? '' : "/". $type);
873          $row['mirror'] = 'Coming Soon';        $row['content'] = l(t('Panel Content'), "admin/og/og_collections/pcpanel/edit/content/$edit_url", array(), drupal_get_destination());
874          $row['layout'] = l(t('Panel Layout'), "admin/og/og_collections/pcpanel/edit/layout/$edit_url", array(), drupal_get_destination());
875          $rows[] = $row; // any utility in making the rows array associative?        $row['layset'] = l(t('Panel Layout Settings'), "admin/og/og_collections/edit/pcpanel/layset/$edit_url", array(), drupal_get_destination());
       }  
876      }      }
877        else {
878          $row['filler'] = array('data'=> t('<em>This pre-configured is currently disabled and cannot be operated upon.</em>'), 'align' => 'center', 'colspan' => 3);
879        }
880        $rows[] = $row;
881        unset ($row);
882    }    }
883    
884    if ($isdefault) {    $header = array(t('Panel Name'));
885      $header = array(t('Panel Name'), t('Home Page?'), t('Edit Content'), t('Edit Layout'), t('Edit Layout Settings'), t('Import Template'));    if (!$isdefault) {
886    }      $header[] = t('Tied to Default');
887    else {      $header[] = t('Relink with Default');
888      $header = array(t('Panel Name'), t('Tied to Default'), t('Home Page?'), t('Edit Content'), t('Edit Layout'), t('Edit Layout Settings'), t('Revert to Default'), t('Import Template'));      // $header[] = t('Import');
889    }    }
890    $linktext = "admin/og/og_collections/collectioncfg";    $header[] = array('data' => t('Panel Editing Operations'), 'align' => 'center', 'colspan' => 3);
891    $linktext .= $isdefault ? "" : "/$type";  
892    $output .= theme('table', $header, $rows);    $output .= theme('table', $header, $rows);
   $output .= t('<p>Pre-configured panels that have been disabled in !panpcpanel will not appear in the above list. You must enable them before you can edit them here.</p>', array('!panpcpanel' => l(t('Panel Templates'), $linktext)));  
893    return $output;    return $output;
894  }  }
895    
896  /*  /*
897   * Form to add/edit a pre-configured panel. if a $did is passed in, it edits that pcpanel. if there's no $did, it creates a new pcpanel.   * Form to add/edit a pre-configured panel. if a $did is passed in, it edits that pcpanel. if there's no $did, it creates a new pcpanel.
898   */   */
899  function og_collections_pcpanel_form($did = NULL) {  function og_collections_pcpanel_form($did = 0) {
900    if ($did) {    $pcpanel = $did === 0 ? new og_pcpanel_new() : new og_pcpanel($did);
901      $pcpanel = og_collections_load_pcpanel($did, FALSE);    $form['did'] = array('#type' => 'value', '#value' => $did);
902      if (!$did) {
903        $form['#redirect'] = FALSE;
904    }    }
905      $form['pcpanel_data'] = array('#type' => 'value', '#value' => serialize($pcpanel));
906    drupal_set_title($did ? "Configure $pcpanel->page_title Template" : "Add New Pre-Configured Panel");    drupal_set_title($did ? "Configure $pcpanel->page_title Template" : "Add New Pre-Configured Panel");
907    
908    $form['page_title'] = array(    $form['#tree'] = TRUE;
909      $form['pcpvals']['pcpname'] = array(
910        '#title' => t('Panel System Name'),
911        '#type' => 'textfield',
912        '#required' => TRUE,
913        '#default_value' => $pcpanel->pcpname,
914        '#description' => t('The internal system name for this pre-configured panel. Only sitewide OG Collections administrators will ever see this value. Alphanumerics, dashes, and underscores only.'),
915        '#size' => 32,
916      );
917      $form['pcpvals']['page_title'] = array(
918      '#title' => t('Panel Name Title'),      '#title' => t('Panel Name Title'),
919      '#type' => 'textfield',      '#type' => 'textfield',
920      '#required' => TRUE,      '#required' => TRUE,
921      '#default_value' => $did ? $pcpanel->page_title : NULL,      '#default_value' => $pcpanel->page_title,
922      '#disabled' => $pcpanel->default_page ? TRUE : FALSE,      '#description' => t('The default title for og panels (and corresponding navigation tabs) that are instantiated from this pre-configured panel.'),
     '#description' => t('The title for the page and of the tab that will be used by default when this pre-configured panel is instantiated.'),  
923      '#size' => 32,      '#size' => 32,
924    );    );
925    $form['path'] = array(    $form['pcpvals']['path'] = array(
926      '#title' => t('Default Path'),      '#title' => t('Default Path'),
927      '#type' => 'textfield',      '#type' => 'textfield',
928      '#default_value' => $pcpanel->default_page ? '' : $pcpanel->path,      '#default_value' => $pcpanel->default_page ? '' : $pcpanel->path,
929      '#required' => $pcpanel->default_page ? FALSE : TRUE,      '#required' => $pcpanel->default_page ? FALSE : TRUE,
930      '#description' => $pcpanel->default_page ? t('This pre-configured panel is currently set to act as the group home page and therefore cannot be assigned a default path.') : t('The sub-path (appearing after the root group path) this pre-configured panel will appear at upon instantiation.'),      '#description' => $pcpanel->default_page ? t('This pre-configured panel is currently set to act as the group home page and therefore cannot be assigned a default path.') : t('The default path where og panels instantiated by this pre-configured panel will reside.'),
931      '#disabled' => $pcpanel->default_page ? TRUE : FALSE,      '#disabled' => $pcpanel->default_page ? TRUE : FALSE,
932      '#size' => 32,      '#size' => 32,
933    );    );
934    $form['show_blocks'] = array(    $form['pcpvals']['show_blocks'] = array(
935      '#title' => t('Show blocks'),      '#title' => t('Show blocks'),
936      '#type' => 'checkbox',      '#type' => 'checkbox',
937      '#default_value' => $did ? $pcpanel->show_blocks : TRUE,      '#default_value' => $pcpanel->show_blocks,
938      '#description' => t('If unchecked, unmodified instantiations of this pre-configured panel will NOT show the standard group blocks.'),      '#description' => t('If unchecked, OG Panels instantiated by this pre-configured panel will hide group blocks by default.'),
939    );    );
940    $form['published'] = array(    $form['pcpvals']['published'] = array(
941      '#type' => 'checkbox',      '#type' => 'checkbox',
942      '#title' => t('Published'),      '#title' => t('Published'),
943      '#default_value' => $did ? $pcpanel->published : FALSE,      '#default_value' => $pcpanel->published,
944      '#description' => t('If unchecked, instantiations of this pre-configured panel will initially be unpublished and viewable only by site or group administrators. Keeping this unchecked is wise if the page contains information that is not automatically present on group creation, and having this page accessible but empty would reflect poorly on the group.'),      '#description' => t('If unchecked, OG Panels instantiated by this pre-configured panel will initially be unpublished and viewable only by site or group administrators. Keeping this unchecked is wise if the page contains information that is not automatically present on group creation, and having this page accessible but empty would reflect poorly on the group.'),
945    );    );
946      if (2 == $collectionsetting = variable_get('og_collections_settings', FALSE)) {
947        if (!$did) {
948          $form['pcpvals']['enabled'] = array(
949            '#type' => 'checkbox',
950            '#title' => t('Enabled (in the Collection)'),
951            '#default_value' => FALSE,
952            '#description' => t("Check this box to automatically set this pre-configured panel as 'Enabled' in each one of your OG Collections. The other values ('Published', 'Show Blocks,' and the rest) will be applied to this pre-configured panel in all your Collections whether this box is checked or not."),
953          );
954        }
955        else {
956          $form['propagate'] = array(
957            '#title' => t('Apply these settings to all Collections'),
958            '#type' => 'checkbox',
959            '#default_value' => FALSE,
960            '#description' => t("Check this box if you want the changes you have made to this pre-configured panel to be applied across all Collections. Note that this has NO effect on the panel display configurations (content, layout, layout settings) whatsoever, nor does it affect any default linking/delinking you may have done. It simply 'resets' the overall configuration for this pre-configured panel to be the same across all your Collections."),
961          );
962        }
963      }
964    
965    $form['submit'] = array(    $form['submit'] = array(
966      '#type' => 'submit',      '#type' => 'submit',
967      '#value' => $did ? t('Update Pre-configured Panel') : t('Create Pre-configured Panel'),      '#value' => is_null($did) ? t('Update Pre-configured Panel') : t('Create Pre-configured Panel'),
968    );    );
   $form['did'] = array('#type' => 'value', '#value' => $did);  
969    return $form;    return $form;
970  }  }
971    
972  // FIXME NEED a validation function to ensure that paths entered are valid.  function og_collections_pcpanel_form_validate($form_id, $form) {
973  function og_collections_pcpanel_form_validate($form_id, $form_values, $form) {    global $form_values;
974      $pcpanel = unserialize($form_values['pcpanel_data']);
975      $pathblacklist = array('view', 'edit', 'delete', 'outline', 'load', 'render', 'clone');
976      if (in_array($form_values['pcpvals']['path'], $pathblacklist)) {
977        form_error($form['pcpvals']['path'], t('%path is a reserved system path, and cannot be used for a group page. Please enter another path.', array('%path' => $form_values['path'])));
978      }
979      else if (preg_match("/[^A-Za-z0-9-]/", $form_values['pcpvals']['path'])) {
980        form_error($form['pcpvals']['path'], t("Panel paths may only contain alphanumeric characters and dashes."));
981      }
982      else if (db_result(db_query("SELECT path FROM {og_collections_pcpanel} WHERE path = '%s' AND 'grouptype' = '%s' AND did <> %d", $form_values['pcpvals']['path'], $form_values['did'], $pcpanel->grouptype))) {
983        form_error($form['pcpvals']['path'], t("That path is currently in use by another one of your group's pages. Please enter another path."));
984      }
985      if (preg_match("/[^A-Za-z0-9_-]/", $form_values['pcpvals']['pcpname'])) {
986        form_error($form['pcpvals']['pcpname'], t("System names can only contain alphanumeric characters, underscores, and dashes."));
987      }
988      elseif (!preg_match("/[A-Za-z_-]/", $form_values['pcpvals']['pcpname'])) {
989        form_error($form['pcpvals']['pcpname'], t("System names must contain at least one non-numeric character."));
990      }
991      elseif (db_result(db_query("SELECT pcpname FROM {og_collections_pcpanel} WHERE pcpname = '%s' AND did <> %d", $form_values['pcpvals']['pcpname']))) {
992        form_error($form['pcpvals']['pcpname'], t("That internal system name is already in use by another pre-configured panel. Please choose another."));
993      }
994  }  }
995    
996  /**  /**
997   * Creates a new panel pre-configured panel, or modifies an existing one. Also ensures that all relevant   * Creates a new panel pre-configured panel, or modifies an existing one. Also ensures that all relevant
998   * collectioncfgs are updated with the new panel information.   * collections are updated with the new panel information.
999   **/   **/
1000  function og_collections_pcpanel_form_submit($form_id, $form_values) {  function og_collections_pcpanel_form_submit($form_id, $form_values) {
1001    if ($form_values['did']) {    $pcpanel = unserialize($form_values['pcpanel_data']);
1002      $pcpanel = og_collections_load_pcpanel($form_values['did'], FALSE);    if ($pcpanel instanceof og_pcpanel_new) {
1003      $pcpanel->page_title = $form_values['page_title'];      $pcpanel->save($form_values['pcpvals']);
1004      $pcpanel->path = $form_values['path'];      drupal_set_message(t("The pre-configured panel '%title' was created, and has been added to the Default collection. You must now select a default layout.", array('%title' => $pcpanel->title)));
1005      $pcpanel->show_blocks = $form_values['show_blocks'];      return array('admin/og/og_collections/pcpanel/edit/layout/'. $pcpanel->pcpid, 'destination=/admin/og/og_collections/collectioncfg');
1006      $pcpanel->published = $form_values['published'];    }
1007      og_collections_save_pcpanel($pcpanel);    elseif ($pcpanel instanceof og_pcpanel) {
1008      drupal_set_message(t('Pre-configured panel "'. $pcpanel->panel_title .'" has successfully been updated.'));      drupal_set_message(t("The pre-configured panel '%title' was updated."));
1009      return 'admin/og/og_collections/collectioncfg'; // TODO return to the page that the edit was called from?      if ($form_values['propagate']) {
1010    }        foreach (array_keys(og_collections_group_types(TRUE)) as $grouptype) {
1011    else {          $pcpanel->grouptype = $grouptype;
1012      // Create a new display in the default grouptype          $pcpanel->save($form_values['pcpvals']);
     $display = panels_new_display();  
     panels_save_display($display);  
     // grouptype doesn't need to be included; column defaults to 'default', and new panel pcpanels are always in the default grouptype  
     $pcpid = db_next_id("{og_collections_pcpanel}_pcpid");  
     og_collections_manage_db('pcpid_'. $pcpid, 'add');  
     db_query("INSERT INTO {og_collections_pcpanel} (did, pcpid) VALUES (%d, %d)", $display->did, $pcpid);  
     drupal_set_message(t("The pre-configured panel '%title' created, and has been added to the Default collection. You must now select a default layout.", array('%title' => $form_values['page_title'])));  
     // Update all collectioncfgs to reflect new pre-configured panel. might be worth turning this into a hook at some point, at least in the generalized module.  
     // if this mass-update method is useful somewhere else, split it out into some separate function  
     $conf = array(  
       'active' => 0,  
       'default_page' => 0,  
       'weight' => 0,  
       'did' => $display->did,  
       'usedef' => 1,  
       'page_title' => $form_values['page_title'],  
       'path' => $form_values['path'],  
       'published' => $form_values['published'],  
       'show_blocks' =>  $form_values['show_blocks'],  
       'grouptype' => 'default',  
     );  
     $group_types = og_collections_group_types(TRUE);  
     foreach (array_keys($group_types) as $type) {  
       $collectioncfg = og_collections_load_collectioncfg($type);  
       $collectioncfg[$pcpid] = $conf;  
       og_collections_save_collectioncfg($type, $collectioncfg, array($pcpid));  
     }  
     drupal_get_destination();  
     return 'admin/og/og_collections/pcpanel/layout/'. $pcpid; // redirect to layout selection  
   }  
 }  
   
 /**  
  * Central pre-configured panel management function, redirects requests based on supplemental args; takes any number of args that can be passed to the  
  * 'admin/og/og_collections/pcpanel' URL and dispatches them appropriately  
  */  
 function og_collections_pcpanel_manage() {  
   $args = func_get_args();  
   $op = array_shift($args);  
   $dest = drupal_get_destination();  
   $error = $success = '';  
   $collectionsetting = variable_get('og_collections_settings', FALSE);  
   $multi = $collectionsetting == 1 ? FALSE : TRUE;  
   
   switch ($op) {  
     case 'form':  
       if (!empty($args[0]) && !empty($args[1])) {  
         if (is_string($args[0]) && is_numeric($args[1])) {  
           if ($pcpanel = og_collections_load_pcpanel(array($args[0], $args[1]))) {  
             $output = drupal_get_form('og_collections_pcpanel_form', $pcpanel->did);  
           }  
           else {  
             $error = 'No pre-configured panel was loaded with the arguments provided.';  
             break;  
           }  
         }  
         else {  
           $error = 'The arguments provided were in an invalid format.';  
           break;  
         }  
       }  
       else {  
         $output = drupal_get_form('og_collections_pcpanel_form');  
       }  
       return $output;  
   
     case 'layout':  
     case 'content': // FIXME display_edit.inc in Panels needs to be updated with a different return value if the form is cancelled. Or something.  
     case 'layset':  
       if (is_numeric($args[0])) { // editing the default version of a pre-configured panel  
         list($pcpid) = $args;  
         $result = _og_collections_pcpanel_edit($op, $pcpid, $error, $success);  
1013        }        }
1014        elseif (is_string($args[0])) { // editing a group type specific version of a pre-configured panel      }
1015          list($type, $pcpid) = $args;      else {
1016          $result = _og_collections_pcpanel_edit($op, $pcpid, $error, $success, $type);        $pcpanel->save($form_values['pcpvals']);
1017        }      }
       if ($result) {  
         return $result; // only anything is returned into result is when the form strings need to be returned for processing  
       }  
       // all other situations (errors, success, whatever), result in a break  
     break;  
   
     case 'revert': // reverting a given page pcpanel to default and re-linking it  
       if (!$multi) {  
         $error = 'Reversion is pointless if you are using only one collection for all your group types. How did you even GET here!?!'; // =)  
         break;  
       }  
       if (isset($args[0], $args[1])) {  
         if (is_string($args[0]) && is_numeric($args[1])) {  
           $pcpanel = og_collections_load_pcpanel(array($args[0], $args[1]), FALSE);  
           return drupal_get_form('og_collections_pcpanel_revert_confirm', $pcpanel);  
         }  
       }  
     break;  
   
     case 'delete': // delete a given pcpanel  
       if (!isset($args[0]) || !is_numeric($args[0])) {  
         $error = 'A valid pcpid must be supplied if you want to delete a pre-configured panel.';  
         break;  
       }  
       if ($multi) {  
         $dids = db_num_rows(db_query("SELECT * FROM {og_collections_pcpanel} WHERE pcpid = %d", $args[0]));  
         drupal_set_message(t('WARNING: Deleting this pre-configured panel will remove it for ALL group types. This will result in the automatic, irreversible deletion of the <strong>%dids</strong> uniquely configured display(s) that correspond to this pre-configured panel. If you just want the pre-configured panel to not be instantiated for a particular group type, simply disable it on the !link', array('%dids' => $dids, '!link' => l(t('Panel Templates'), 'admin/og/og_collections/collectioncfg'))));  
       }  
     return drupal_get_form('og_collections_pcpanel_delete_confirm', $args[0], $multi);  
     break;  
   
   }  
   if (!empty($error)) {  
     drupal_set_message($error, 'error');  
     drupal_goto();  
   }  
   elseif (!empty($success)) {  
     drupal_set_message($success);  
     drupal_goto();  
1018    }    }
1019  }  }
1020    
1021  function og_collections_pcpanel_revert_confirm($pcpanel) {  function og_collections_pcpanel_relink_confirm($did) {
1022    $form['pcpid'] = array('#type' => 'value', '#value' => $pcpanel->pcpid);    $pcpanel = new og_pcpanel_transport($did);
1023    $form['type'] = array('#type' => 'value', '#value' => $pcpanel->grouptype);    $form['pcpanel_data'] = array('#type' => 'value', '#value' => serialize($pcpanel));
   $form['page_title'] = array('#type' => 'value', '#value' => $pcpanel->page_title);  
1024    
1025    return confirm_form($form,    return confirm_form($form,
1026      t('Are you sure you want to revert the type-specific pre-configured panel %title back to the Default type?', array('%title' => $pcpanel->page_title)),      t('Are you sure you want to relink the type-specific pre-configured panel %title back to the Default type?', array('%title' => $pcpanel->page_title)),
1027      "admin/og/og_collections/pcpanelsetup/$pcpanel->grouptype",      "admin/og/og_collections/pcpanelsetup/$pcpanel->grouptype",
1028      t('This action CANNOT be undone; any custom type-specific changes you have made to this pre-configured panel will be PERMANENTLY lost.'),      t('Any layout, layout settings, or content customizations you have made since delinking this pre-configured panel from the Default collection will be PERMANENTLY lost.'),
1029      t('Revert'), t('Cancel')      t('Relink'), t('Cancel')
1030    );    ); // TODO implement that restore feature
1031  }  }
1032    
1033  function og_collections_pcpanel_revert_confirm_submit($form_id, $form_values) {  function og_collections_pcpanel_relink_confirm_submit($form_id, $form_values) {
1034    $pcpanel = og_collections_load_pcpanel(array($form_values['type'], $form_values['pcpid']), FALSE);    $pcpanel = unserialize($form_values['pcpanel_data']);
1035    $defcollectioncfg = og_collections_load_collectioncfg();    $collectioncfg = new og_collection_typed($pcpanel->grouptype);
1036    $collectioncfg = og_collections_load_collectioncfg($pcpanel->grouptype);    $collectioncfg->relink_pcpanel($pcpanel->pcpname);
1037    $collectioncfg[$pcpanel->pcpid]['usedef'] = 1;    return 'admin/og/og_collections/pcpanelsetup/'. $pcpanel->grouptype;
   $collectioncfg[$pcpanel->pcpid]['did'] = $defcollectioncfg[$pcpanel->pcpid]['did'];  
   panels_delete_display($pcpanel->did);  
   db_query("DELETE FROM {og_collections_pcpanel} WHERE pcpid = %d AND grouptype = '%s'", $pcpanel->pcpid, $pcpanel->grouptype);  
   og_collections_save_collectioncfg($pcpanel->grouptype, $collectioncfg, array($pcpanel->pcpid));  
1038  }  }
1039    
1040    
1041  function og_collections_pcpanel_delete_confirm($pcpid) {  function og_collections_pcpanel_delete_confirm($pcpid) {
1042    $collectioncfg = og_collections_load_collectioncfg();    $pcpanel = new og_pcpanel(array($pcpid, 'default'));
1043    $form['pcpid'] = array('#type' => 'value', '#value' => $pcpid);    $form['pcpanel_data'] = array('#type' => 'value', '#value' => serialize($pcpanel));
   $form['page_title'] = array('#type' => 'value', '#value' => $collectioncfg[$pcpid]['page_title'] ? $collectioncfg[$pcpid]['page_title'] : '');  
1044    
1045    return confirm_form($form,    return confirm_form($form,
1046      t('Are you sure you want to delete the %title pre-configured panel?', array('%title' => $collectioncfg[$pcpid]['page_title'] ? $collectioncfg[$pcpid]['page_title'] : '')),      t("Are you sure you want to delete the '%title' pre-configured panel?", array('%title' => $pcpanel->pcpname)),
1047      isset($_GET['destination']) ? $_GET['destination'] : 'admin/og/og_collections/collectioncfg',      'admin/og/og_collections/collectioncfg',
1048      t('This action CANNOT be undone.'),      t('This action CANNOT be undone.'),
1049      t('Delete'), t('Cancel')      t('Delete'), t('Cancel')
1050    );    );
# Line 830  function og_collections_pcpanel_delete($ Line 1065  function og_collections_pcpanel_delete($
1065    og_collections_manage_db('pcpid_'. $pcpid, 'drop');    og_collections_manage_db('pcpid_'. $pcpid, 'drop');
1066  }  }
1067    
1068  /**  function og_collections_pcpanel_edit ($op, $pcpid, $type = 'default') {
1069   * Passthrough & handle the Panels API for all three types of panels editing: content, layout, and layout settings.        // $error .= 'ERROR: Bad pcpid provided. The pre-configured panel you are attempting to edit does not exist.';
1070   *    $pcpanel = new og_pcpanel(array($pcpid, $type));
1071   * @param string $op    if ($pcpanel->linked) {
  *   string indicating the type of panels editing operation that is being called.  
  * @param int $pcpid  
  * @param string $error  
  * @param string $success  
  * @param string $type  
  * @return mixed  
  *   Returns different variables depending on the stage of the process.  
  */  
 function _og_collections_pcpanel_edit($op, $pcpid, &$error, &$success, $type = 'default') {  
   if (!$pcpanel = og_collections_load_pcpanel(array($type, $pcpid), TRUE)) {  
     if ($type == 'default' || !$pcpanel = og_collections_load_pcpanel(array('default', $pcpid), TRUE, $type)) { // only way this happens is with a bad pcpid. error out.  
       $error .= 'ERROR: Bad pcpid provided. The pre-configured panel you are attempting to edit does not exist.';  
       return FALSE;  
     }  
     $isnew = TRUE;  
1072      if (isset($_POST['op']) && $_POST['op'] == 'Cancel') {      if (isset($_POST['op']) && $_POST['op'] == 'Cancel') {
1073        $success = t('Operation cancelled. A collection-specific panel has NOT been created; your pre-configured panel is still tied to the Default.');        drupal_set_message(t('Operation cancelled. A collection-specific panel has NOT been created; your pre-configured panel is still tied to the Default.'));
1074        return $success;        drupal_goto();
1075      }      }
1076      else {      else {
1077        $group_types = og_collections_group_types();        $group_types = og_collections_group_types();
1078        $newdisplay = 'NOTE: The !grouptype collection is currently tied to the Default type for this panel. If you save changes on this page, a version of this pre-configured panel specific to the !grouptype collection will be generated, and this panel\'s link to the Default pre-configured panel will be broken.';        $newdisplay = 'NOTE: The !grouptype collection is currently tied to the Default type for this panel. If you save changes on this page, a version of this pre-configured panel specific to the !grouptype collection will be generated, and this panel\'s link to the Default pre-configured panel will be broken.';
1079        drupal_set_message(t($newdisplay, array('!grouptype' => $group_types[$type])));        drupal_set_message(t($newdisplay, array('!grouptype' => $group_types[$pcpanel->grouptype])));
1080      }      }
1081    }    }
1082    switch ($op) {    switch ($op) {
1083      case 'content':      case 'content':
1084        $saved = panels_edit($pcpanel->display, NULL, $pcpanel->display->content_types);        $saved = panels_edit($pcpanel->display, NULL, $pcpanel->content_types);
1085        break;        break;
1086      case 'layout':      case 'layout':
1087        $saved = panels_edit_layout($pcpanel->display, t('Save'), NULL);        $saved = panels_edit_layout($pcpanel->display, t('Save'), NULL);
# Line 872  function _og_collections_pcpanel_edit($o Line 1092  function _og_collections_pcpanel_edit($o
1092    }    }
1093    
1094    if (!isset($_POST['op'])) {    if (!isset($_POST['op'])) {
     $error = FALSE;  
1095      if ($op == 'content') {      if ($op == 'content') {
1096        print theme('page', $saved, FALSE);        print theme('page', $saved, FALSE);
1097        return FALSE;        return FALSE;
1098      }      }
1099      return $saved;      return $saved;
1100    }    }
1101    elseif ($_POST['op'] == 'Save' && is_object($saved)) { // if it's not an object at this stage, something's wrong. error out    elseif ($_POST['op'] == 'Save') {
1102      if ($isnew) {      $collectioncfg = $type == 'default' ?  new og_collection_default() : new og_collection_typed($type);
1103        $pcpanel->display = og_collections_export_display($pcpanel->display);      if ($pcpanel->linked) {
1104        panels_save_display($pcpanel->display);        $collectioncfg->delink_pcpanel($pcpanel);
       $pcpanel->usedef = 0;  
       $pcpanel->did = $pcpanel->display->did;  
       $pcpanel->grouptype = $type;  
       og_collections_save_pcpanel($pcpanel, TRUE);  
1105      }      }
1106      $success = 'The settings for this pre-configured panel have been successfully updated.';      $collectioncfg->save();
1107    
1108        drupal_set_message(t('The settings for this pre-configured panel have been successfully updated.'));
1109    }    }
1110    else {    else {
1111          $error .= 'An unknown error occurred, causing the panels edit function to return an unidentified string. No changes have been saved to the pre-configured panel, but changes MAY have been saved to the panels display.'; // returns string on errors          // $error .= 'An unknown error occurred, causing the panels edit function to return an unidentified string. No changes have been saved to the pre-configured panel, but changes MAY have been saved to the panels display.'; // returns string on errors
1112    }    }
1113  }  }
1114    
# Line 924  function og_collections_admin() { Line 1141  function og_collections_admin() {
1141    $form['og_collections']['collectionsetting'] = array(    $form['og_collections']['collectionsetting'] = array(
1142      '#type' => 'radios',      '#type' => 'radios',
1143      '#title' => t('Group Collections Settings'),      '#title' => t('Group Collections Settings'),
1144      '#options' => $options,      '#options' => $options,
1145      '#default_value' => $ops,      '#default_value' => $ops,
1146      '#required' => TRUE,      '#required' => TRUE,
1147    );    );
# Line 937  function og_collections_admin() { Line 1154  function og_collections_admin() {
1154    
1155  function og_collections_admin_submit($form, $form_values) {  function og_collections_admin_submit($form, $form_values) {
1156    variable_set('og_collections_settings', $form_values['collectionsetting']);    variable_set('og_collections_settings', $form_values['collectionsetting']);
1157    if ($form_values['collectionsetting'] == 0) {    switch ($form_values['collectionsetting']) {
1158      $msg = t('Group Collections have been disabled. Any pre-configured panel configuration you have done will be accessible if you reactive Group Collections.');      case 0:
1159    }        $msg = t('Group Collections have been disabled. Any pre-configured panels you have created will be accessible if you reactive Group Collections.');
1160    else if ($form_values['collectionsetting'] == 1) {      case 1:
1161      $msg = t('Group Collections have been enabled, using one collection for all group types. You can configure your collection in !ogpanel.', array('!ogpanel' => l(t('Panel Templates'), 'admin/og/og_collections/collectioncfg')));        $msg = t('Group Collections have been enabled, using one collection for all group types. You can configure your collection in !ogpanel.', array('!ogpanel' => l(t('Panel Templates'), 'admin/og/og_collections/collectioncfg')));
1162    }      case 2:
1163    else if ($form_values['collectionsetting'] == 2) {        $msg = t("Group Collections have been enabled, with one pre-configured panel allowed per group type. You can configure all of your collections in !ogpanel.", array('!ogpanel' => l(t('OG Collections Configuration'), 'admin/og/og_collections/collectioncfg')));
     $msg = t("Group Collections have been enabled, with one pre-configured panel allowed per group type. You can configure all of your collections in !ogpanel. Make sure to set up pre-configured panels for all desired group types now, as there is currently no way to retrofit existing groups with pre-configured panels you update later.", array('!ogpanel' => l(t('OG Collections Configuration'), 'admin/og/og_collections/collectioncfg')));  
1164    }    }
1165    drupal_set_message($msg);    drupal_set_message($msg);
1166    cache_clear_all('*', 'cache_menu', TRUE);    cache_clear_all('*', 'cache_menu', TRUE);
# Line 965  function og_collections_nodeapi(&$node, Line 1181  function og_collections_nodeapi(&$node,
1181    switch ($op) {    switch ($op) {
1182      case 'insert':      case 'insert':
1183        if ($collectionsetting = variable_get('og_collections_settings', FALSE)) {        if ($collectionsetting = variable_get('og_collections_settings', FALSE)) {
1184          $group_types = og_collections_group_types();          if (in_array($node->type, array_keys(og_collections_group_types()))) {
         if (in_array($node->type, array_keys($group_types))) {  
1185            $multi = $collectionsetting == 1 ? FALSE : TRUE;            $multi = $collectionsetting == 1 ? FALSE : TRUE;
1186            $defaultcfg = og_collections_load_collectioncfg(); // we'll need the default regardless of $multi            og_collections_instantiate_collection($node->nid, $multi ? $node-type : 'default');
           if ($multi) {  
             $collectioncfg = og_collections_load_collectioncfg($node->type);  
             foreach ($collectioncfg as $pcpid => $conf) {  
               if (!empty($conf['active'])) {  
                 $toinsert[$pcpid] = !empty($conf['usedef']) ? $defaultcfg[$pcpid]['did'] : $conf['did']; // $conf will theoretically have the right $did either way, but this is the surefire way  
               }  
             }  
           }  
           else {  
             $collectioncfg = $defaultcfg;  
             foreach ($collectioncfg as $pcpid => $conf) {  
               if (!empty($conf['active'])) {  
                 $toinsert[$pcpid] = $conf['did'];  
               }  
             }