public static function browseId($model = 'Document', $sort, $criteria, $format = array(), $page = 1, $count = 0)
 {
     $conditions = $criteria[0];
     $values = $criteria[1];
     $joins = $criteria[2];
     $joins_order = $criteria[3];
     $joins_pager = $joins + $joins_order;
     $module = c2cTools::model2module($model);
     $mi = c2cTools::Model2Letter($model) . 'i';
     // orderby
     $sort = self::buildSortCriteria($model, $sort['orderby_params'], $sort['order_params'], $sort['npp'], $mi);
     // $npp
     $npp = $sort['npp'];
     // $all
     $all = false;
     if (isset($joins['all'])) {
         $all = $joins['all'];
     }
     // $field_list
     $field_list = call_user_func(array($model, 'buildFieldsList'), false, $mi, $format, $sort);
     // specific $conditions
     if ($module == 'outings' && in_array('cond', $format)) {
         $default_max_age = sfConfig::get('app_outings_recent_conditions_limit', '3W');
         $conditions[] = "age(date) < interval '{$default_max_age}'";
     }
     // $order_by
     $order_by = self::buildOrderby($field_list, $sort);
     // $nb_id
     $nb_id = 0;
     if (isset($joins['nb_id'])) {
         $nb_id = $joins['nb_id'];
     }
     // $pager_count
     if ($nb_id > 0) {
         $count = $nb_id;
     }
     if ($count > $npp) {
         $pager_count = 0;
     } else {
         $pager_count = $count;
     }
     // $independant_count
     $independant_count = !$pager_count && count($joins_pager) > count($joins);
     // create pager
     $pager = new c2cDoctrinePager($model, $npp, $pager_count, $independant_count);
     $pager->setPage($page);
     // independant count
     if ($independant_count) {
         $c = $pager->getCountQuery();
         $c->select('m.id')->from("{$model} m");
         $model::buildPagerConditions($c, $criteria);
     }
     // pager query
     $q = $pager->getQuery();
     $q->select(implode(',', $field_list))->from("{$model} m")->orderBy($order_by);
     if (!$all || !empty($joins_order)) {
         $criteria[2] = $joins_pager;
         $model::buildPagerConditions($q, $criteria);
     } else {
         $pager->simplifyCounter();
     }
     // execute pager query
     $pager->init();
     $count = $pager->getNbResults();
     if ($count == 0) {
         return array('pager' => null, 'nb_results' => 0, 'ids' => null);
     }
     // get ids
     $items = $pager->getResults('array');
     $ids = array();
     $where_ids = array();
     foreach ($items as $item) {
         $ids[] = $item['id'];
         $where_ids[] = '?';
     }
     $where_ids = implode(', ', $where_ids);
     $count_ids = count($ids);
     if ($count_ids == 1) {
         $where = ' = ' . $where_ids;
     } else {
         $where = ' IN ( ' . $where_ids . ' )';
     }
     return array('pager' => $pager, 'nb_results' => $count, 'ids' => $ids, 'where' => $where);
 }
示例#2
0
 public static function customSave($name, $filename, $associated_doc_id, $user_id, $model, $activities = array(), $categories = array(), $image_type = 1)
 {
     $base_path = sfConfig::get('sf_upload_dir') . DIRECTORY_SEPARATOR;
     $from = $base_path . sfConfig::get('app_images_temp_directory_name');
     $to = $base_path . sfConfig::get('app_images_directory_name');
     c2cTools::log("linking image {$filename} to {$model} {$associated_doc_id}, with title \"{$name}\", user {$user_id} ");
     // save a new image...
     $image = new Image();
     $image->setCulture(sfContext::getInstance()->getUser()->getCulture());
     $image->set('name', $name);
     $image->set('filename', $filename);
     // get and store image dimensions and size
     $size = getimagesize($from . DIRECTORY_SEPARATOR . $filename);
     if ($size) {
         $image->set('width', $size[0]);
         $image->set('height', $size[1]);
     }
     $image->set('file_size', filesize($from . DIRECTORY_SEPARATOR . $filename));
     // here, read eventual lon, lat, elevation and other interesting fields from exif tag...
     // (nb: always after $image->set('filename', $filename))
     $image->populateWithExifDataFrom($from . DIRECTORY_SEPARATOR . $filename);
     // here, copy activities field from the linked document (if it exists):
     if (!empty($activities)) {
         $image->set('activities', $activities);
     }
     if (!empty($categories)) {
         $image->set('categories', $categories);
     }
     $image->set('image_type', $image_type);
     $image->set('has_svg', Images::hasSVG($filename, $from));
     // then save:
     $image->doSaveWithMetadata($user_id, false, 'Image uploaded');
     c2cTools::log('associating and moving files');
     $image_id = $image->get('id');
     $type = c2cTools::Model2Letter($model) . 'i';
     // associate it
     $a = new Association();
     $a->doSaveWithValues($associated_doc_id, $image_id, $type, $user_id);
     // move to uploaded images directory (move the big, small and all other configured in yaml)
     Images::moveAll($filename, $from, $to);
     return $image_id;
 }
 public static function parseListItems($array, $modelName, $count_images = true)
 {
     $langs = sfContext::getInstance()->getUser()->getPreferedLanguageList();
     $parsed_array = self::getTheBest($array, $modelName, $langs);
     // once we have selected the best lang to display this item name,
     // we build search string for comments (eg: 333_fr)
     $_str = array();
     // extract independant country list
     $countries = array();
     foreach ($parsed_array as $key => $item) {
         // build ids strings to get nb of comments
         $_str[$item['id']] = $item['id'] . '_' . $item[$modelName . 'I18n'][0]['culture'];
         // extract best name for associated regions
         if (isset($item['geoassociations'])) {
             $geo_associations = $item['geoassociations'];
             $parsed_array[$key]['geoassociations'] = self::getTheBest($geo_associations, 'Area', $langs, 'linked_id');
             if (count($geo_associations) > 1) {
                 foreach ($geo_associations as $geo_association) {
                     if ($geo_association['type'] == 'dc') {
                         $countries[$geo_association['linked_id']] = true;
                     }
                 }
             }
         }
     }
     // if all docs are in the same country, country data are removed from list
     if (count($countries) == 1) {
         $country_id = key($countries);
         foreach ($parsed_array as $key => $item) {
             if (isset($item['geoassociations'])) {
                 if (isset($item['geoassociations'][$country_id])) {
                     unset($parsed_array[$key]['geoassociations'][$country_id]);
                 }
             }
         }
     }
     // get nb of comments for all items
     $pun_msgs = PunbbComm::retrieveNbComments($_str);
     // merge this info into $parsed_array
     foreach ($pun_msgs as $pun_msg) {
         $id = substr($pun_msg['subject'], 0, strpos($pun_msg['subject'], '_'));
         foreach ($parsed_array as $key => $item) {
             if ($key == $id) {
                 $parsed_array[$key]['nb_comments'] = $pun_msg['nb_comments'];
             }
         }
     }
     if ($count_images && $modelName != 'Image') {
         // Count all images linked
         $image_links = Association::countAllLinked(array_keys($_str), c2cTools::Model2Letter($modelName) . 'i');
         // merge this info into $parsed_array
         foreach ($image_links as $image_link) {
             $main_id = $image_link['main_id'];
             if (isset($parsed_array[$main_id]['nb_images'])) {
                 $parsed_array[$main_id]['nb_images']++;
             } else {
                 $parsed_array[$main_id]['nb_images'] = 1;
             }
         }
     }
     if ($modelName == 'Route') {
         // find highest associated summit
         foreach ($parsed_array as $key => $item) {
             $parsed_array[$key]['associations'] = self::getTheHighest($item['associations'], 'Summit');
             // once this is done, find his best name
             $parsed_array[$key]['associations'][0]['Summit'] = self::getTheBest($parsed_array[$key]['associations'][0]['Summit'], 'Summit', $langs, '', false);
         }
     }
     return $parsed_array;
 }