Example #1
0
 public function summary($field, $data, $object)
 {
     if (!($structure = FW4_Structure::get_object_structure('user', false))) {
         return '';
     }
     $row = where('id', intval($data[strval($field['name'])]))->get_row('user');
     if (!$row) {
         return '';
     }
     if (!($titlefield = reset($structure->xpath('string')))) {
         return false;
     }
     if (isset($field['display'])) {
         $displayvalue = $field['display'];
         preg_match_all('/\\[([a-z0-9\\_]+)\\]/is', $field['display'], $matches, PREG_SET_ORDER);
         foreach ($matches as $match) {
             $displayvalue = str_ireplace($match[0], $row[$match[1]], $displayvalue);
         }
         return $displayvalue;
     } else {
         return $row[strval($titlefield['name'])];
     }
 }
Example #2
0
 private static function route($priority)
 {
     $uri = implode('/', segments());
     foreach (self::$slugs as $key => $routes) {
         $key = str_replace('%s', '([a-z0-9\\-]+)', str_replace('%d', '([0-9]+)', str_replace('/', '\\/', $key)));
         if (preg_match('/^' . $key . '(\\/|$)/is', $uri, $matches)) {
             array_pop($matches);
             array_shift($matches);
             foreach ($routes as &$route) {
                 if ($route->get_priority() == $priority) {
                     $arguments = preg_replace('/^' . $key . '(\\/|$)/is', '', $uri);
                     $arguments = explode("?", $arguments);
                     $arguments = array_filter(explode("/", array_shift($arguments)));
                     $arguments = array_merge($matches, $arguments);
                     // Check if the class that will handle the content actually contains the requested function.
                     if (!method_exists($route->get_classname(), $route->get_function())) {
                         continue;
                     }
                     // Check if we're not calling said function with too few parameters.
                     $reflector = new ReflectionClass($route->get_classname());
                     if (count($arguments) < $reflector->getMethod($route->get_function())->getNumberOfRequiredParameters()) {
                         continue;
                     }
                     // Check if this function might want variable number of parameters.
                     $collapse_parameters = false;
                     $parameters = $reflector->getMethod($route->get_function())->getParameters();
                     if (count($parameters) && end($parameters)->name == 'parameters') {
                         $collapse_parameters = true;
                     }
                     if (count($arguments) > count($parameters) && $collapse_parameters && $priority == ROUTE_DEFAULT) {
                         $route->set_priority(ROUTE_LATE);
                         continue;
                     }
                     // Check if we're not calling said function with too many parameters.
                     if (count($arguments) > count($parameters) && !$collapse_parameters) {
                         continue;
                     }
                     // Check if we're not calling a static function.
                     if ($reflector->getMethod($route->get_function())->isStatic()) {
                         continue;
                     }
                     // Save old segments should we need it again later
                     self::$urlsegments = self::$segments;
                     // Set the segments to those that matched our content
                     self::$segments = array();
                     self::$segments[0] = strtolower($route->get_contentname());
                     self::$segments[1] = strtolower($route->get_function());
                     self::$segments = array_merge(self::$segments, $arguments);
                     // Set the current route
                     self::$current_route = $route;
                     // Check database if needed (only do this when there's no admin panel)
                     if (!Config::admin_enabled()) {
                         $site = current_site();
                         if (self::is_fw4() && !$site->live) {
                             FW4_Structure::check_structure();
                         }
                     }
                     // Fire the controller
                     View_Loader::get_instance()->set_path(CONTENTPATH . self::$content_prefix . self::$segments[0]);
                     $page = self::$content_pages[strtolower($route->get_classname())];
                     if ($collapse_parameters) {
                         $non_optional = array_splice($arguments, 0, count($parameters) - 1);
                         $arguments = array_merge($non_optional, array(array_diff($arguments, array('index'))));
                     }
                     try {
                         $result = call_user_func_array(array($page, $route->get_function()), $arguments);
                     } catch (RowNotFoundException $e) {
                         $result = false;
                     }
                     // If the controller returns false, reset the segments and continue matching
                     if ($result === false) {
                         self::$segments = self::$urlsegments;
                         continue;
                     }
                     return true;
                 }
             }
         }
     }
     return false;
 }
Example #3
0
 private static function update_sorting_field($objectname)
 {
     if (!($structure = FW4_Structure::get_object_structure($objectname))) {
         return false;
     }
     $db = FW4_Db::get_instance();
     $db->query('SELECT @row:=0;');
     $db->query('UPDATE `' . $structure['path'] . '` SET _sort_order = (@row:=@row+1) ORDER BY _sort_order, id DESC;');
     if (isset($structure['archived']) && strval($structure['name']) != '_versions') {
         $db->query('UPDATE `' . $structure['path'] . '>_versions` versions JOIN `' . $structure['path'] . '` origtable ON origtable.id = versions.id SET versions._sort_order = origtable._sort_order WHERE versions.version_id IN (SELECT * FROM (SELECT MAX(version_id) FROM `' . $structure['path'] . '>_versions` GROUP BY id) as version_ids)');
     }
 }
Example #4
0
 public function search_count($query, $fields = array())
 {
     if (!count($fields)) {
         $searchable_fields = array();
         foreach (FW4_Structure::get_searchable_fields() as $field) {
             if ($field->getName() == 'bool') {
                 continue;
             }
             $fields[strval($field['searchable'])] = strval($field['searchable']);
         }
     }
     $this->where[] = '_language = "' . language() . '"';
     $this->where[] = 'MATCH(`' . implode('`,`', $fields) . '`) AGAINST ("' . $this->escape($query) . '")';
     $sql = 'SELECT count(*) as count FROM `_search_index` WHERE ' . implode(' AND ', $this->where);
     if (count($this->group_by)) {
         $sql .= ' GROUP BY ' . implode(',', $this->group_by);
     }
     return $this->db->query($sql)->fetchColumn();
 }
Example #5
0
 public static function rebuild_search_index()
 {
     if (!self::$structure_xml_expanded) {
         self::$structure_xml_expanded = new SimpleXMLElement(self::get_expanded_xml());
     }
     $searchable_objects = self::rebuild_search_index_for_object(self::$structure_xml_expanded);
 }
Example #6
0
 public function function_ajax_autocomplete($field, $object, $data)
 {
     $query = limit(5)->where('id != %d', intval($_POST['currentid']));
     $searchable_fields = array();
     $source = isset($field['source']) ? strval($field['source']) : $object['stack'];
     if (!($structure = FW4_Structure::get_object_structure($source, false))) {
         return false;
     }
     $possible_searchable_fields = $structure->xpath('string');
     $titlefield = reset($possible_searchable_fields);
     $titlefield = strval($titlefield['name']);
     foreach ($structure->xpath('string') as $searchable_field) {
         $searchable_fields[] = strval($searchable_field['name']);
     }
     $search_where_strings = array();
     foreach (explode(' ', $_POST['search']) as $keyword) {
         $search_where_strings[] = 'CONCAT(IFNULL(`' . implode('`,""),IFNULL(`', $searchable_fields) . '`,"")) LIKE ' . $query->escape('*' . $keyword . '*');
     }
     $query->where(implode(' AND ', $search_where_strings));
     if (isset($_POST['otherids']) && is_array($_POST['otherids']) && count($_POST['otherids'])) {
         $query->where('id NOT IN %$', $_POST['otherids']);
     }
     $result = array();
     foreach ($query->get($source) as $row) {
         $result[] = array('id' => $row->id, 'title' => $row->{$titlefield});
     }
     echo json_encode($result);
 }
Example #7
0
 public function get_source_rows($name, $field, $id = false)
 {
     if ($id) {
         return where('id = %d', $id)->get($name);
     }
     if (isset($field['where'])) {
         $query = new Query();
         $path = explode('>', FW4_Admin::$parent_structure['path']);
         $whereval = strval($field['where']);
         if (preg_match_all('/\\[(.*?)\\]/is', $whereval, $matches, PREG_SET_ORDER)) {
             foreach ($matches as $match) {
                 $parent_item = false;
                 $current_item = FW4_Admin::$current_item;
                 foreach (explode('.', $match[1]) as $part) {
                     if ($part == 'parent') {
                         array_pop($path);
                         if ($parent_item === false) {
                             $parent_item = FW4_Admin::$parent_item;
                         } else {
                             $parent_item = $parent_item->parent();
                         }
                     } else {
                         if (!$parent_item && !$current_item) {
                             return false;
                         }
                         $data = $parent_item ? $parent_item : $current_item;
                         $structure = FW4_Structure::get_object_structure(implode('>', $path), false);
                         $other_field = $structure->xpath('*[@name="' . addslashes($part) . '"]');
                         if (is_array($other_field)) {
                             $other_field = reset($other_field);
                         }
                         if ($other_field && $other_field->getName() == 'choice') {
                             if (isset($other_field['multiple'])) {
                                 $ids = $data->{$part}->ids();
                                 if (!count($ids)) {
                                     $ids[] = 0;
                                 }
                                 $whereval = str_replace($match[0], '(' . implode(',', $ids) . ')', $whereval);
                             } else {
                                 $fieldname = $part . '_id';
                                 if (!isset($data->{$fieldname})) {
                                     return false;
                                 }
                                 $whereval = str_replace($match[0], $data->{$fieldname}, $whereval);
                             }
                         } else {
                             if (!property_exists($data, $part)) {
                                 return false;
                             }
                             $whereval = str_replace($match[0], is_null($data->{$part}) ? 'NULL' : $data->{$part}, $whereval);
                         }
                     }
                 }
             }
         }
         if (preg_match_all('/(\\S+)\\s+contains\\s+(\\S+)/is', $whereval, $matches, PREG_SET_ORDER)) {
             foreach ($matches as $match) {
                 $structure = FW4_Structure::get_object_structure($name, false);
                 $other_field = $structure->xpath('*[@name="' . addslashes($match[1]) . '"]');
                 if (is_array($other_field)) {
                     $other_field = reset($other_field);
                 }
                 if (!isset($other_field['multiple'])) {
                     throw new Exception('"contains" only possible on choice with multiple attribute');
                 }
                 if (!$other_field) {
                     return false;
                 }
                 $query->join($structure['path'] . '>' . $other_field['name'], $other_field['name'] . '.' . $structure['name'] . '_id = ' . $structure['name'] . '.id');
                 $whereval = str_replace($match[0], $other_field['name'] . '.' . $other_field['name'] . ' = ' . $match[2], $whereval);
             }
         }
         return $query->where($whereval)->get($name);
     }
     if (!isset(self::$cache[$name])) {
         self::$cache[$name] = get($name);
     }
     return self::$cache[$name];
 }
Example #8
0
    public function print_field($field, $data, $object)
    {
        $source_rows = self::get_source_rows(strval($field['source']), $field);
        if ($source_rows === false) {
            return false;
        }
        $source_structure = FW4_Structure::get_object_structure($field['source'], false);
        $source_title = isset($source_structure['title']) ? $source_structure['title'] : 'aandachtspunten';
        $fieldname = strval($field['name']);
        $imagefieldname = $fieldname . '_image';
        $orig_filename = $imagefieldname . '_orig_filename';
        $alt = $imagefieldname . '_alt';
        if (isset($field['is_viewing_version']) && $field['is_viewing_version']) {
            return false;
        }
        $options = '';
        if (isset($field['source'])) {
            if (!($structure = FW4_Structure::get_object_structure(strval($field['source']), false))) {
                return false;
            }
            $titlefields = $structure->xpath('string');
            if (!($titlefield = reset($titlefields))) {
                $titlefields = $structure->xpath('number');
                if (!($titlefield = reset($titlefields))) {
                    return false;
                }
            }
            $titlefield = strval($titlefield['name']);
            $group_value = false;
            $group_is_choice = false;
            $group_values = array();
            if (isset($field['group_by'])) {
                $group_by_field = reset($structure->xpath('*[@name="' . strval($field['group_by']) . '"]'));
                if ($group_by_field->getName() == "choice") {
                    $group_is_choice = true;
                    foreach ($group_by_field->children() as $group_child) {
                        $value = strval($group_child);
                        if (isset($group_child['value'])) {
                            $value = strval($group_child['value']);
                        }
                        $group_values[$value] = strval($group_child);
                    }
                }
            } else {
                if (isset($field['optgroup'])) {
                    if (!($parent_structure = FW4_Structure::get_object_structure(substr($structure['path'], 0, strrpos($structure['path'], '>')), true))) {
                        return false;
                    }
                    $parenttitlefields = $parent_structure->xpath('string');
                    if (!($parenttitlefield = reset($parenttitlefields))) {
                        $parenttitlefields = $parent_structure->xpath('number');
                        if (!($parenttitlefields = reset($parenttitlefields))) {
                            return false;
                        }
                    }
                    $parenttitlefield = strval($parenttitlefield['name']);
                    $source_rows = $source_rows->to_array();
                    usort($source_rows, function ($a, $b) use($parenttitlefield) {
                        if ($a->parent()->{$parenttitlefield} == $b->parent()->{$parenttitlefield}) {
                            return 0;
                        }
                        return $a->parent()->{$parenttitlefield} < $b->parent()->{$parenttitlefield} ? -1 : 1;
                    });
                }
            }
            foreach ($source_rows as $child) {
                $value = $child->id;
                if (isset($field['group_by']) && ($group_field = strval($field['group_by']) && $child->{$group_field} != $group_value)) {
                    if ($group_value) {
                        $options .= '</optgroup>';
                    }
                    $group_value = $actual_group_value = $child[strval($field['group_by'])];
                    if ($group_is_choice) {
                        $actual_group_value = $group_values[$group_value];
                    }
                    $options .= '<optgroup label="' . $actual_group_value . '">';
                } else {
                    if (isset($field['optgroup']) && $child->parent()->{$parenttitlefield} != $group_value) {
                        if ($group_value) {
                            $options .= '</optgroup>';
                        }
                        $group_value = $child->parent()->{$parenttitlefield};
                        $options .= '<optgroup label="' . $group_value . '">';
                    }
                }
                $options .= '<option value="' . $value . '">';
                if (isset($field['format'])) {
                    $displayvalue = $field['format'];
                    preg_match_all('/\\[([a-z0-9\\_]+)\\]/is', $field['format'], $matches, PREG_SET_ORDER);
                    foreach ($matches as $match) {
                        $match_name = $match[1];
                        $displayvalue = str_ireplace($match[0], $child->{$match_name}, $displayvalue);
                    }
                    $options .= $displayvalue;
                } else {
                    $options .= htmlentities($child->{$titlefield});
                }
                $options .= '</option>';
            }
            if ($group_value) {
                $options .= '</optgroup>';
            }
        } else {
            foreach ($field->children() as $child) {
                $value = isset($child['value']) ? $child['value'] : strval($child);
                $options .= '<option value="' . $value . '">' . strval($child) . '</option>';
            }
        }
        $existing_areas = array();
        if (isset($data->id) && $data->id) {
            $areas_fieldname = $field['name'] . '_area';
            $filename_fieldname = $field['name'] . '_image_filename';
            if ($data->{$filename_fieldname}) {
                $width_field = $field['name'] . '_image_width';
                $height_field = $field['name'] . '_image_height';
                $canvas_width = $canvas_height = 800;
                if ($data->{$width_field} > $data->{$height_field}) {
                    $canvas_height = $data->{$height_field} / $data->{$width_field} * $canvas_width;
                } else {
                    $canvas_width = $data->{$width_field} / $data->{$height_field} * $canvas_height;
                }
                foreach ($data->{$areas_fieldname} as $existing_area) {
                    $existing_areas[] = array('left' => $existing_area->topx * $canvas_width / 100, 'top' => $existing_area->topy * $canvas_height / 100, 'width' => $existing_area->bottomx * $canvas_width / 100 - $existing_area->topx * $canvas_width / 100, 'height' => $existing_area->bottomy * $canvas_height / 100 - $existing_area->topy * $canvas_height / 100, 'value' => intval($existing_area->value_id));
                }
            }
        }
        ?>
    	<div class="input" data-options="<?php 
        echo str_replace('"', "'", $options);
        ?>
">
	    	<label<?php 
        echo isset($field['invalid']) && $field['invalid'] ? ' class="invalid"' : '';
        ?>
><?php 
        echo isset($field['label']) ? $field['label'] : ucwords(preg_replace("/[^\\w-]+/i", " ", $field['name']));
        ?>
</label>
    		<?php 
        if (isset($data->id) && isset($data->{$imagefieldname}) && $data->{$imagefieldname}) {
            ?>
    			<table class="list imagelist" width="100%" cellpadding="0" cellspacing="0">
    				<td width="0" valign="top">
    					<div style="background:url('<?php 
            echo $data->{$imagefieldname}->cover(50, 50);
            ?>
')" class="image-thumbnail"></div>
    				</td>
    				<td width="100%">
    					<div><?php 
            echo $data->{$orig_filename};
            ?>
</div>
    					<div><a class="button areas" href="#" data-image="<?php 
            echo $data->{$imagefieldname}->contain(800, 800);
            ?>
">Bepaal <?php 
            echo strtolower($source_title);
            ?>
</a></div>
    					<input class="areas-input" type="hidden" name="<?php 
            echo $field['name'];
            ?>
-areas" value='<?php 
            echo json_encode($existing_areas);
            ?>
'/>
    				</td>
    				<?php 
            if (!isset($field['required'])) {
                ?>
<td width="0"><a class="delete" href="<?php 
                echo $field['name'];
                ?>
/delete/<?php 
                echo $data->id;
                ?>
" onclick="return confirm('<?php 
                echo l(array('nl' => 'Bent u zeker dat u deze afbeelding wilt verwijderen?', 'fr' => 'Etes-vous s&ucirc;r de vouloir supprimer cette image?', 'en' => 'Are you sure you want to delete this image?'));
                ?>
');"><img alt="<?php 
                echo l(array('nl' => 'Verwijderen', 'fr' => 'Supprimer', 'en' => 'Delete'));
                ?>
" title="<?php 
                echo l(array('nl' => 'Verwijderen', 'fr' => 'Supprimer', 'en' => 'Delete'));
                ?>
" src="<?php 
                echo url(ADMINRESOURCES . 'images/del.png');
                ?>
" width="22" height="23"/></a></td><?php 
            }
            ?>
    			</table>
    		<?php 
        }
        ?>
    		<div class="imagemaptypeuploader" data-fieldname="<?php 
        echo $field['name'];
        ?>
" data-title="<?php 
        echo $source_title;
        ?>
"></div>
    	</div><?php 
    }
Example #9
0
 public static function reload_site()
 {
     $db = FW4_Db::get_instance();
     $site = false;
     if (!$site) {
         try {
             if (count(languages()) > 1) {
                 $query = from('site')->where('url LIKE %s', $_SERVER['HTTP_HOST'] . '%');
                 $language_codes = array_keys(languages());
                 if ($countries = Config::countries()) {
                     $language_codes = array_keys($countries);
                 }
                 foreach ($language_codes as $code) {
                     $query->or_where('`url_' . $code . '` LIKE %s', $_SERVER['HTTP_HOST'] . '%');
                 }
                 $site = $query->get_row();
             } else {
                 $site = from('site')->where('url LIKE %s', $_SERVER['HTTP_HOST'] . '%')->get_row();
             }
         } catch (PDOException $exception) {
             FW4_Structure::check_structure('', true);
         }
         if (!$site) {
             if (!($site = get_row('site'))) {
                 $name = str_ireplace('www.', '', $_SERVER['HTTP_HOST']);
                 $name = ucfirst(substr($name, 0, strpos($name, '.')));
                 $url = $_SERVER['HTTP_HOST'];
                 if (stristr(getcwd(), 'httpdocs')) {
                     $url .= substr(getcwd(), stripos(getcwd(), 'httpdocs') + strlen('httpdocs'));
                 }
                 insert('site', array("url" => $url, "name" => $name));
                 FW4_Structure::check_structure();
                 $site = where('url LIKE %s', $_SERVER['HTTP_HOST'])->get_row('site');
             } else {
                 $domain_handled = false;
                 // Process minisites
                 $types = FW4_Type_Manager::get_instance()->get_types();
                 foreach ($types as $typename => $type) {
                     if (method_exists($type, 'handle_domain')) {
                         if (!$site->structure_xml_expanded) {
                             FW4_Structure::check_structure("", true);
                             return self::reload_site();
                         }
                         $structure = new SimpleXMLElement($site->structure_xml_expanded);
                         $fields = $structure->xpath('//*[@type_name="' . $typename . '"]');
                         if (count($fields)) {
                             $prev = self::$current;
                             self::$current = $site;
                             if (call_user_func_array(array($type, 'handle_domain'), array($_SERVER['HTTP_HOST'], $fields))) {
                                 $domain_handled = true;
                                 break;
                             }
                             self::$current = $prev;
                         }
                     }
                 }
                 // Process subdomains
                 foreach (Config::subdomains() as $subdomain => $handler) {
                     //if () Router::set_content_prefix($handler);
                 }
                 if (!$domain_handled && $site->live) {
                     redirect((Config::https() ? 'https' : 'http') . '://' . $site->url . $_SERVER['REQUEST_URI']);
                 }
             }
         }
     }
     if (!$site->live && false === stristr($_SERVER['HTTP_HOST'], '.fw4.') && false === stristr($_SERVER['HTTP_HOST'], 'local')) {
         $db->query("UPDATE site SET live = 1 WHERE id = " . $site->id);
         if (stristr($site->url, '.fw4.be')) {
             where('id = %d', $site->id)->update('site', array('url' => $_SERVER['HTTP_HOST']));
         }
     }
     self::$current = $site;
     return $site;
 }