Example #1
0
 public static function singleton($o, $id = NULL, $class = NULL)
 {
     if (!self::$active) {
         return $o;
     }
     if (!is_object($o) || !$o instanceof Model) {
         throw new Exception("trying to get singleton of non-Model instance");
     }
     if (!isset($class)) {
         $class = get_class($o);
     }
     if (Model::_has_complex_primary_key($class)) {
         return $o;
     }
     if (!isset($id)) {
         $PK = Model::_simple_primary_key($o);
         $id = $o->{$PK};
     }
     if ($x = ObjectCache::get($class, $id)) {
         return $x;
     } else {
         ObjectCache::store($class, $id, $o);
         return $o;
     }
 }
 public static function smarty_modifier_objlink($v)
 {
     if (is_object($v)) {
         if ($v instanceof Model) {
             $pk = Model::_simple_primary_key($v);
             $link = new url(url::$ABSOLUTE, '_datainspector', 'object', get_class($v), $v->{$pk});
             if (self::$last_fieldname) {
                 $link .= "?path=" . self::encode_varpath(self::$inspector_path . '->' . self::$last_fieldname);
             } else {
                 $link .= "?path=" . self::encode_varpath(self::$inspector_path);
             }
             return sprintf('<a href="%s">%s(%s=%s)</a>', $link, get_class($v), $pk, $v->{$pk});
         } elseif ($v instanceof ModelCollection) {
             $c = count($v);
             $c = $c ? sprintf('(%d member%s)', $c, $c == 1 ? '' : 's') : '(empty)';
             return sprintf("collection of %s %s", $v->ofclass, $c);
         } elseif ($v instanceof form) {
             return "form " . $v->name();
         } else {
             $x = get_class($v);
             $r = array();
             while ($x) {
                 $r[] = $x;
                 $x = get_parent_class($x);
             }
             return "Unknown object: " . join('/', $r);
         }
     }
     return "--NOT AN OBJECT--";
 }