Jump to content

ESRCH

Members
  • Posts

    80
  • Joined

  • Last visited

  • Days Won

    1

Everything posted by ESRCH

  1. Hi, Could you show us a small extract of the data you are trying to import with the CSV so that we might better understand the structure? When I want to import data from a CSV, I usually create a page with a special template (e.g. "file-import" template), which has a "File" field, and when viewing the page, I process the file. In your case, you would want to create a template called "bibliography-type" with simply a title field (no need to create a php template file for this). Somewhere in your page structure, you can create a section called "Bibliography types" as follows: Home -- Bibliography Types ---- Book ---- Journal Article ---- Conference Paper ---- Etc. You would then create a Page field called "bibliography_type", which is restricted to the "bibliography-type" template (you can set this in the "Input" tab, in the "Selectable Pages" section when creating the field). Finally, you would assign this field to another template (e.g. "document", so that you can assign a bibliography type to the document. When creating a document in the administration, you would then be able to select a bibliography type for your document (just like any other field value). If you want to show the bibliography type in the front-end when viewing a document, you can use the code mentioned previously: echo "<p>{$page->bibliography_type->title}</p>"; To give you more information on how to import the data, I would need to better understand the structure of the CSV.
  2. Hi Pravin, I had a look at your code, and it seems that you are trying to do Isotope's manually. Below is a possible way of achieving your goal with Isotope. My assumptions are: The page with id 1151 is the Categories page, under which the different categories live. page_select is the field in the "client-details" template that refers to a category (you should maybe rename it to "category") There is only one category per client I used the category id as the filter class (prepending a "c" since a class name must start with a letter, underscore or hyphen). <?php function categoryClass($client) { return "c" . $client->page_select->id; } ?> <script type="text/javascript"> $(document).ready(function() { // Initialize Isotope $("#ourHolder").isotope({ itemSelector: ".item" }); // Filter buttons $('#filterOptions li a').click(function() { var filterValue = $(this).attr('data-filter'); $container.isotope({filter: filterValue}); }); }); </script> <div class="wrapper"> <div class="row body-padding"> <!-- Filter options --> <ul id="filterOptions"> <li><a href="#" data-filter="*">All</a></li> <?php foreach ($pages->get(1151)->children("include=all") as $category): ?> <li><a href="#" data-filter=".c<?php echo $category->id; ?>"><?php echo $category->title; ?></a></li> <?php endforeach ?> </ul> <!-- Items --> <div id="ourHolder"> <?php foreach ($pages->find("template=clients-detail, include=all") as $client): ?> <ul class="row item <?php echo categoryClass($client); ?>"> <a href="<?php echo $client->landing_page_image->description ?>"> <li class="large-2 columns clients"><img src="<?php echo $client->landing_page_image->httpUrl; ?>"></li> </a> </ul> <?php endforeach ?> </div> </div> </div> If you have several categories per client, just modify the categoryClass function to return "c1234 c5678 " instead of just "c1234": function categoryClass($client) { $classes = ""; foreach ($client->page_select as $category) { $classes .= "c" . $category->id . " "; } return $classes; } I haven't tested this, so if it doesn't work, please let me know. I hope that this helps.
  3. Hi, Here is the formatted version (I did it to see how I could help). <script type="text/javascript"> $(document).ready(function() { $('#filterOptions li a').click(function() { // fetch the class of the clicked item var ourClass = $(this).attr('class'); var category = $(ourClass).val(); console.log("'."+ourClass+"'"); // reset the active class on all the buttons $('#filterOptions li').removeClass('active'); // update the active state on our clicked button $(this).parent().addClass('active'); if(ourClass == 'all-clients-1') { // show all our items $('#ourHolder').children('div.item').show(1000); } else { // hide all elements that don't share ourClass $('#ourHolder').children('div:not(.' + ourClass + ')').hide(1000); // show all elements that do share ourClass $('#ourHolder').children('div.' + ourClass).show(1000); } return false; }); }); </script> <div class="wrapper"> <div class="row body-padding"> <?php $p=$pages->get(1151)->children("include=all"); $i=1; echo "<ul id='filterOptions'>"; foreach ($p as $k) { echo "<li><a href='#' class='all-clients-$i'>$k->title</a></li>"; $i++; } echo "</ul>"; ?> <div id="ourHolder"> <div class="row item all-clients-1"> <?php $posts = $pages->find("template=clients-detail,include=all, page_select=All"); echo "<ul>"; foreach ($posts as $k) { $image=$k->landing_page_image; echo "<a href='{$image->description}' target='_blank'> <li class='large-2 columns clients'><img src='{$image->httpUrl}' /></li></a>"; } echo "</ul>"; ?> </div> <div class="row item all-clients-2"> <?php $posts = $pages->find("template=clients-detail, include=all, page_select=Processwire"); echo "<ul>"; foreach ($posts as $k) { $image=$k->landing_page_image; echo "<a href='{$image->description}' target='_blank'> <li class='large-3 columns clients'><img src='{$image->httpUrl}' /></li></a>"; } echo "</ul>"; ?> </div> <div class="item all-clients-3"> <?php $posts = $pages->find("template=clients-detail, include=all, page_select=PHP"); echo "<ul>"; foreach ($posts as $k) { $image=$k->landing_page_image; echo "<a href='{$image->description}' target='_blank'> <li class='large-3 columns clients'><img src='{$image->httpUrl}' /></li></a>"; } echo "</ul>"; ?> </div> </div> </div> </div>
  4. Hi kongondo, Thanks for the quick reply and the welcome. I had actually already read the topic that you mention, and this is what prompted my need: I would like to save a "stock" field for each product, but this field would have to be calculated each time a stock movement is saved, and calculating this field is precisely what takes so much time. In the meantime, I continued looking into the problem, and I think that I came up with a solution with the following module: class PagesSum extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Pages Sum', 'version' => 1, 'summary' => 'Adds a $pages->sum($selectorString, $fieldName) function to sum the value of a specific field over a list of pages selected by a selector string.', 'singular' => true, 'autoload' => true ); } public function init() { $this->addHook('Pages::sum', $this, 'sum'); } public function sum($event) { $selectorString = $event->arguments(0); $fieldName = $event->arguments(1); // Find all the pages associated with the selector string $pageFinder = new PageFinder(); $idQuery = $pageFinder->findIDs(new Selectors($selectorString), array('returnQuery' => true)); $idQuery->set('orderby', array()); $idQuery->set('groupby', array()); $idQuery->set('limit', array()); $stmt = $idQuery->execute(); $idArray = $stmt->fetchAll(PDO::FETCH_COLUMN); // If no pages were found, return 0 if (count($idArray) == 0) { $event->return = 0; return; } $idString = implode(',', $idArray); // Get the table name for the given field name $field = $this->fields->get($fieldName); // If no field with this name is found, return 0; if (!$field) { $event->return = 0; return; } $tableName = $field->getTable(); // Run the SUM query $sumQuery = new DatabaseQuerySelect(); $sumQuery->select("SUM(data)"); $sumQuery->from($tableName); $sumQuery->where("pages_id IN ($idString)"); $stmt2 = $sumQuery->execute(); list($total) = $stmt2->fetch(PDO::FETCH_NUM); $event->return = $total; } } In my tests, it is about 60 times quicker to calculate the sum with the function $pages->sum($selectorString, $fieldName) than looping over the pages (for about 12000 pages). I would appreciate any feedback, in case there are any other optimizations to be achieved, or any errors I haven't thought about. And I hope that this might maybe be useful to others too!
  5. Hi, I am developing a site where I need to sum a specific field value across a large number of pages selected with a selector string. For example, I am keeping track of the stock of specific products. The stock is influenced by various stock movements. I have a template for stock movements, with fields "product" (Page type) and "quantity" (Integer type). Currently, I am searching for the pages and adding the quantities in two different steps: // For a given $product $movements = $pages->find("template=stock-movement, product=$product, quantity!=0"); $stock = 0; foreach ($movements as $movement) { $stock += $movement->quantity; } This is fine, but I am doing this very often, and I would like to improve the performance of this calculation, since the quantity of stock movements can grow very large. To do this, I was thinking of using the SUM function offered by MySQL to achieve similar optimizations as what is achieved by the $pages->count() function. Ideally, this would translate into a $pages->sum() function that could be called as follows: // For a given $product $stock = $pages->sum("template=stock-movement, product=$product", "quantity"); I have tried looking through the code of the Pages, PageFinder and DatabaseQuerySelect classes in the core to get some inspiration, but I fear that I don't understand well enough how the query mechanism works to be able to adapt it to create the function mentioned above. Could anyone help me with this? Thanks a lot in advance, Eric
×
×
  • Create New...