public
Description: Git mirror of the CMS Made Simple 2.0 rewrite
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsmadesimple-2-0.git
cmsmadesimple-2-0 / lib / classes / class.cms_content_operations.php
100644 641 lines (556 sloc) 16.718 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
<?php // -*- mode:php; tab-width:4; indent-tabs-mode:t; c-basic-offset:4; -*-
#CMS - CMS Made Simple
#(c)2004-2008 by Ted Kulp (ted@cmsmadesimple.org)
#This project's homepage is: http://cmsmadesimple.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#$Id$
 
/**
* Class for static methods related to content
*
* @author Ted Kulp
* @since 0.8
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $Date$
* @license GPL
**/
class CmsContentOperations extends CmsObject
{  
  static private $instance = NULL;
  
  static public function get_instance()
  {
    if (self::$instance == NULL)
    {
      self::$instance = new CmsContentOperations();
    }
    return self::$instance;
  }
 
  static function load_content_type($type)
  {
    $type = strtolower($type);
 
    global $gCms;
    $contenttypes =& $gCms->contenttypes;
    
    if (isset($contenttypes[$type]))
    {
      $placeholder =& $contenttypes[$type];
      if ($placeholder->loaded == false)
      {
        include_once($placeholder->filename);
        $placeholder->loaded = true;
      }
      return true;
    }
    return false;
  }
 
  static function LoadContentType($type)
  {
    return ContentOperations::load_content_type($type);
  }
  
  /**
   * Returns an array of available content types.
   *
   * @return array Array of CmsContenttypePlaceholder objects
   * @author Ted Kulp
   **/
  public static function find_content_types()
  {
    $contenttypes =& cmsms()->contenttypes;
    $dir = cms_join_path(dirname(__FILE__),'contenttypes');
    $handle=opendir($dir);
    while ($file = readdir ($handle))
    {
     $path_parts = pathinfo($file);
     if ($path_parts['extension'] == 'php')
     {
        $obj = new CmsContentTypePlaceholder();
        $obj->type = strtolower(basename($file, '.inc.php'));
        $obj->filename = cms_join_path($dir,$file);
        $obj->loaded = false;
        $obj->friendlyname = basename($file, '.inc.php');
        $contenttypes[strtolower(basename($file, '.inc.php'))] = $obj;
     }
    }
    closedir($handle);
  }
  
  /**
   * Returns an array of the available block types.
   *
   * @return array Array of CmsBlockTypePlaceholder objects
   * @author Ted Kulp
   **/
  public static function find_block_types()
  {
    $blocktypes =& cmsms()->blocktypes;
    
    #Load block types
    $dir = cms_join_path(ROOT_DIR,'lib','classes','blocktypes');
    $handle=opendir($dir);
    while ($file = readdir ($handle))
    {
     $path_parts = pathinfo($file);
     if ($path_parts['extension'] == 'php')
     {
        $obj = new CmsBlockTypePlaceholder();
        $obj->type = str_replace('block.', '', strtolower(basename($file, '.inc.php')));
        $obj->filename = cms_join_path($dir, $file);
        $obj->loaded = false;
        $obj->friendlyname = str_replace('block.', '', basename($file, '.inc.php'));
        $blocktypes[str_replace('block.', '', strtolower(basename($file, '.inc.php')))] = $obj;
     }
    }
    closedir($handle);
    
    return $blocktypes;
  }
 
  function &CreateNewContent($type)
  {
    $type = strtolower($type);
 
    $result = NULL;
    
    if (ContentOperations::LoadContentType($type))
    {
      $result = new $type;
    }
    
    return $result;
  }
  
  /**
   * @deprecated Deprecated. Use cmsms()->content_base->find_by_id($id) instead.
   **/
  function LoadContentFromId($id,$loadprops=true)
  {  
    return cmsms()->content_base->find_by_id($id);
  }
 
  /**
   * Returns the id of the page that is marked as the default
   *
   * @return integer Id of the default page
   * @author Ted Kulp
   **/
  public static function get_default_page_id()
  {
    return CmsCache::get_instance()->call('CmsContentOperations::_get_default_page_id');
  }
  
  /**
   * Non-cached version of get_default_page_id.
   *
   * @return integer Id of the default page
   * @author Ted Kulp
   **/
  public static function _get_default_page_id()
  {
    $page = cmsms()->content_base->find_by_default_content(1);
    if ($page)
    {
      return $page->id;
    }
    return -1;
  }
 
    /**
* Returns a hash of valid content types (classes that extend ContentBase)
* The key is the name of the class that would be saved into the dabase. The
* value would be the text returned by the type's FriendlyName() method.
*/
  function &ListContentTypes()
  {
    global $gCms;
    $contenttypes =& $gCms->contenttypes;
    
    if (isset($gCms->variables['contenttypes']))
    {
      $variables =& $gCms->variables;
      return $variables['contenttypes'];
    }
    
    $result = array();
    
    reset($contenttypes);
    while (list($key) = each($contenttypes))
    {
      $value =& $contenttypes[$key];
      $result[] = $value->type;
    }
    
    $variables =& $gCms->variables;
    $variables['contenttypes'] =& $result;
 
    return $result;
  }
 
    /**
* Updates the hierarchy position of one item
*/
  public static function set_hierarchy_position($contentid)
  {
    global $gCms;
    $db = $gCms->GetDb();
 
    $current_hierarchy_position = '';
    $current_id_hierarchy_position = '';
    $current_hierarchy_path = '';
    $current_parent_id = $contentid;
    $count = 0;
 
    while ($current_parent_id > 1)
    {
      $query = "SELECT item_order, parent_id, content_alias FROM ".cms_db_prefix()."content WHERE id = ?";
      $row = &$db->GetRow($query, array($current_parent_id));
      if ($row)
      {
        $current_hierarchy_position = $row['item_order'] . '.' . $current_hierarchy_position;
        $current_id_hierarchy_position = $current_parent_id . '.' . $current_id_hierarchy_position;
        $current_hierarchy_path = $row['content_alias'] . '/' . $current_hierarchy_path;
        $current_parent_id = $row['parent_id'];
        $count++;
      }
      else
      {
        $current_parent_id = 1;
      }
    }
 
    if (strlen($current_hierarchy_position) > 0)
    {
      $current_hierarchy_position = substr($current_hierarchy_position, 0, strlen($current_hierarchy_position) - 1);
    }
    if (strlen($current_id_hierarchy_position) > 0)
    {
      $current_id_hierarchy_position = substr($current_id_hierarchy_position, 0, strlen($current_id_hierarchy_position) - 1);
    }
    if (strlen($current_hierarchy_path) > 0)
    {
      $current_hierarchy_path = substr($current_hierarchy_path, 0, strlen($current_hierarchy_path) - 1);
    }
 
    $query = "SELECT prop_name FROM ".cms_db_prefix()."content_props WHERE content_id = ?";
    $prop_name_array = $db->GetCol($query, array($contentid));
 
    debug_buffer(array($current_hierarchy_position, $current_id_hierarchy_position, implode(',', $prop_name_array), $contentid));
 
    $query = "UPDATE ".cms_db_prefix()."content SET hierarchy = ?, id_hierarchy = ?, hierarchy_path = ?, prop_names = ? WHERE id = ?";
    $db->Execute($query, array($current_hierarchy_position, $current_id_hierarchy_position, $current_hierarchy_path, implode(',', $prop_name_array), $contentid));
  }
  
  public static function SetHierarchyPosition($contentid)
  {
    return self::set_hierarchy_position($contentid);
  }
 
    /**
* Updates the hierarchy position of all items
*/
  public static function set_all_hierarchy_positions($lft = -1)
  {
    global $gCms;
    $db = $gCms->GetDb();
 
    if ($lft > -1)
      $query = "SELECT id FROM ".cms_db_prefix()."content WHERE lft >= " . $db->qstr($lft);
    else
      $query = "SELECT id FROM ".cms_db_prefix()."content WHERE id > 1";
 
    $dbresult = $db->Execute($query);
 
    while ($dbresult && !$dbresult->EOF)
    {
      self::set_hierarchy_position($dbresult->fields['id']);
      $dbresult->MoveNext();
    }
    
    if ($dbresult) $dbresult->Close();
  }
  
  public static function SetAllHierarchyPositions($lft = -1)
  {
    return self::set_all_hierarchy_positions($lft);
  }
  
  /**
   * @deprecated Deprecated. Use CmsContentOperations::load_children_into_tree($id, $tree) instead.
   **/
  function LoadChildrenIntoTree($id, &$tree, $loadprops = false)
  {
    return CmsContentOperations::load_children_into_tree($id, $tree);
  }
 
  public static function get_all_content($loadprops=true)
  {
    return cmsms()->content_base->find_all(array('conditions' => array('id > 1'), 'order' => 'lft ASC'));
  }
  
  /**
   * @deprecated Deprecated. Use CmsContentOperations::get_all_content($loadprops) instead.
   **/
  function GetAllContent($loadprops=true)
  {
    return CmsContentOperations::get_all_content($loadprops);
  }
 
  function CreateHierarchyDropdown($current = '', $parent = '', $name = 'parent_id', $addt_content = '')
  {
    $result = '<select name="'.$name.'"';
    if ($addt_content != '')
    {
      $result .= ' ' . $addt_content;
    }
    $result .= '>';
    $result .= '<option value="1">None</option>';
 
    $allcontent = cmsms()->GetContentOperations()->GetAllContent(false);
 
    if ($allcontent !== FALSE && count($allcontent) > 0)
    {
      $curhierarchy = '';
 
      foreach ($allcontent as $one)
      {
        if ($one->id == $current)
        {
          #Grab hierarchy just in case we need to check children
          #(which will always be after)
          $curhierarchy = $one->hierarchy;
 
          #Then jump out. We don't want ourselves in the list.
          continue;
        }
        #If it's a child of the current, we don't want to show it as it
        #could cause a deadlock.
        if ($curhierarchy != '' && strstr($one->hierarchy . '.', $curhierarchy . '.') == $one->hierarchy . '.')
        {
          continue;
        }
        #Don't include content types that do not want children either...
        if ($one->WantsChildren() == true)
        {
          $result .= '<option value="'.$one->id.'"';
 
          #Select current parent if it exists
          if ($one->id == $parent)
          {
            $result .= ' selected="selected"';
          }
 
          $result .= '>'.$one->hierarchy().'. - '.$one->name.'</option>';
        }
      }
    }
 
    $result .= '</select>';
 
    return $result;
  }
 
 
  /**
   * @deprecated Deprecated. Use CmsContentOperations::get_default_page_id instead.
   **/
  function GetDefaultPageID()
  {
    return CmsContentOperations::get_default_page_id();
  }
 
  /**
   * Function to map an alias to a page id. Returns null if
   * no page is found.
   *
   * @param string Alias to look up
   * @return integer The id of the page found. Null is no page is found.
   * @author Ted Kulp
   **/
  public static function get_page_id_from_alias( $alias )
  {
    if (is_numeric($alias) && strpos($alias,'.') == FALSE && strpos($alias,',') == FALSE)
    {
      return $alias;
    }
    else
    {
      $result = cmsms()->content_base->find_by_alias($alias);
      if ($result)
      {
        return $result->id;
      }
    }
    
    return null;
  }
  
  /**
   * @deprecated Deprecated. Use CmsContentOperations::get_page_id_from_alias($alias) instead.
   **/
  function GetPageIDFromAlias( $alias )
  {
    return CmsContentOperations::get_page_id_from_alias($alias);
  }
  
  /**
   * Function to map a hierarchy to a page id. Returns null if
   * no page is found.
   *
   * @param string Hierarchy position to look up
   * @return integer The id of the page found. Null is no page is found.
   * @author Ted Kulp
   **/
  public static function get_page_id_from_hierarchy($position)
  {
    $result = cmsms()->content_base->find_by_hierarchy(CmsContentOperations::create_unfriendly_hierarchy_position($position));
    if ($result)
    {
      return $result->id;
    }
    
    return null;
  }
  
  /**
   * @deprecated Deprecated. Use CmsContentOperations::get_page_id_from_hierarchy($position) instead.
   **/
  function GetPageIDFromHierarchy($position)
  {
    return CmsContentOperations::get_page_id_from_hierarchy($position);
  }
 
  /**
   * Function to map a page id to an alias. Returns null if
   * no page is found.
   *
   * @param string Id to look up
   * @return string The alias of the page found. Null is no page is found.
   * @author Ted Kulp
   **/
  public static function get_page_alias_from_id($id)
  {
    if (!is_numeric($id) && strpos($id,'.') == TRUE && strpos($id,',') == TRUE)
    {
      return $id;
    }
    else
    {
      $result = cmsms()->content_base->find_by_id($id);
      if ($result)
      {
        return $result->alias;
      }
    }
    
    return null;
  }
  
  /**
   * @deprecated Deprecated. Use CmsContentOperations::get_page_alias_from_id($id) instead.
   **/
  function GetPageAliasFromID( $id )
  {
    return $this->get_page_alias_from_id($id);
  }
 
  function CheckAliasError($alias, $content_id = -1)
  {
    global $gCms;
    $db = cms_db();
 
    $error = FALSE;
 
    if (preg_match('/^\d+$/', $alias))
    {
      $error = lang('aliasnotaninteger');
    }
    else if (!preg_match('/^[\-\_\w]+$/', $alias))
    {
      $error = lang('aliasmustbelettersandnumbers');
    }
    else
    {
      $params = array($alias);
      $query = "SELECT * FROM ".cms_db_prefix()."content WHERE content_alias = ?";
      if ($content_id > -1)
      {
        $query .= " AND id != ?";
        $params[] = $content_id;
      }
      $row = &$db->GetRow($query, $params);
 
      if ($row)
      {
        $error = lang('aliasalreadyused');
      }
    }
 
    return $error;
  }
  
  public static function clear_cache()
  {
    $smarty = cms_smarty();
 
    $smarty->clear_all_cache();
    $smarty->clear_compiled_tpl();
  }
  
  /**
   * @deprecated Deprecated. Use CmsContentOperations::clear_cache() instead.
   **/
  function ClearCache()
  {
    CmsContentOperations::clear_cache();
  }
  
  public static function create_friendly_hierarchy_position($position)
  {
    #Change padded numbers back into user-friendly values
    $tmp = '';
    $levels = split('\.', $position);
    foreach ($levels as $onelevel)
    {
      $tmp .= ltrim($onelevel, '0') . '.';
    }
    $tmp = rtrim($tmp, '.');
    return $tmp;
  }
 
  /**
   * @deprecated Deprecated. Use CmsContentOperations::create_friendly_hierarchy_position($position) instead.
   **/
  public static function CreateFriendlyHierarchyPosition($position)
  {
    return CmsContentOperations::create_friendly_hierarchy_position($position);
  }
  
  public static function create_unfriendly_hierarchy_position($position)
  {
    #Change user-friendly values into padded numbers
    $tmp = '';
    $levels = split('\.', $position);
    foreach ($levels as $onelevel)
    {
      $tmp .= str_pad($onelevel, 5, '0', STR_PAD_LEFT) . '.';
    }
    $tmp = rtrim($tmp, '.');
    return $tmp;
  }
 
  /**
   * @deprecated Deprecated. Use CmsContentOperations::create_unfriendly_hierarchy_position($position) instead.
   **/
  public static function CreateUnfriendlyHierarchyPosition($position)
  {
    return CmsContentOperations::create_unfriendly_hierarchy_position($position);
  }
  
  public static function do_cross_reference($parent_id, $parent_type, $content)
  {
    $db = cms_db();
 
    //Delete old ones from the database
    $query = 'DELETE FROM '.cms_db_prefix().'crossref WHERE parent_id = ? AND parent_type = ?';
    $db->Execute($query, array($parent_id, $parent_type));
 
    //Do global content blocks
    $matches = array();
    preg_match_all('/\{(?:html_blob|global_content).*?name=["\']([^"]+)["\'].*?\}/', $content, $matches);
    if (isset($matches[1]))
    {
      $selquery = 'SELECT htmlblob_id FROM '.cms_db_prefix().'htmlblobs WHERE htmlblob_name = ?';
      $insquery = 'INSERT INTO '.cms_db_prefix().'crossref (parent_id, parent_type, child_id, child_type, create_date, modified_date)
              VALUES (?,?,?,\'global_content\','.$db->DBTimeStamp(time()).','.$db->DBTimeStamp(time()).')';
      foreach ($matches[1] as $name)
      {
        $result = &$db->Execute($selquery, array($name));
        while ($result && !$result->EOF)
        {
          $db->Execute($insquery, array($parent_id, $parent_type, $result->fields['htmlblob_id']));
          $result->MoveNext();