Bart's Snippets

cga_entity_query.diff

cga_entity_query.diff
diff --git a/docroot/sites/all/modules/custom/cga_entity_queries/cga_entity_queries.module b/docroot/sites/all/modules/custom/cga_entity_queries/cga_entity_queries.module
index a1c029b..dafb76c 100644
--- a/docroot/sites/all/modules/custom/cga_entity_queries/cga_entity_queries.module
+++ b/docroot/sites/all/modules/custom/cga_entity_queries/cga_entity_queries.module
@@ -354,6 +354,7 @@ function cga_entity_queries_get_current_or_most_recent_session() {
       $query = new EntityFieldQuery();
       $query->entityCondition('entity_type', 'taxonomy_term')
         ->entityCondition('bundle', 'sessions')
+        ->fieldCondition('field_start_end_dates', 'value', date("Y-m-d"), '<=')
         ->fieldOrderBy('field_start_end_dates', 'value2', 'DESC')
         ->range(0, 1)
         ->addMetaData('account', user_load(1)); // Run the query as user 1.
@@ -801,4 +802,4 @@ function cga_entity_queries_get_detail_pages_by_committee($comm_nid) {
     return $node;
   };
   return cga_utility_query_cache(__FUNCTION__, $query, "{$comm_nid}");
-}
\ No newline at end of file
+}

Santa and Bot advent of code

santa_and_bot.clj
(def route (slurp "./houses.txt"))
(def visits (set []))
;; (conj "
;; (def current {:x 0, :y 0})
;; Add 1 to x
;; (def current (assoc current :x (+ 1 (get current :x))))
(defn inc-x []
  (def current (assoc current :x (inc (get current :x)))))
(defn inc-y []
  (def current (assoc current :y (inc (get current :y)))))
(defn dec-x []
  (def current (assoc current :x (dec (get current :x)))))
(defn dec-y []
  (def current (assoc current :y (dec (get current :y)))))

(defn move [ch]
  (case ch
    \^ (dec-x)
    \v (inc-x)
    \< (dec-y)
    \> (inc-y)
    ""))

(defn show-addr [cur]
  (keyword (str (get cur :x) "_" (get cur :y))))

(defn update-visits []
  (def visits (conj visits (show-addr current))))

(defn move-and-update [ch]
  (move ch)
  (update-visits))
  
(defn run-santa []
  (for [x (seq routes)]
    (move-and-update x)
    x))

(defn reset-santa []
  (def visits (set []))
(def current {:x 0, :y 0}))

SQL to see all tables and sizes ordered by size

all_table_sizes.sql
SELECT 
     table_schema as `Database`, 
     table_name AS `Table`, 
     round(((data_length + index_length) / 1024 / 1024), 2) `Size in MB` 
FROM information_schema.TABLES 
ORDER BY (data_length + index_length) ASC;

Delete a bunch of taxonomy terms

del_tax.php
<?php
$tids = taxonomy_get_term_by_name("Withdrawn Prior to R&C Hearing");
// Drop the first one (this has the initiatives attached to it).
array_shift($tids);
dpm(array_values($tids));
$all = array();
foreach (array_values($tids) as $tax) {
  print "deleting $tax->tid";
  $all[] = $tax->tid;
  taxonomy_term_delete($tax->tid);
}
// OR to delete blank taxonomy terms (blank session)
$tids = taxonomy_get_term_by_name("");
// Drop the first one (this has the initiatives attached to it).
array_shift($tids);
dpm(array_values($tids));
$all = array();
foreach (array_values($tids) as $tax) {
  print "deleting $tax->tid";
  $all[] = $tax->tid;
  taxonomy_term_delete($tax->tid);
}

Move Bill files to session specific directory on hook_insert and hook_update

cga_bills_diff.php
<?php
/*
 * Move files in the embedded paragraph items.
 * param: $dir_path directory to move files to
 * param: $paragraph_items
 */
function _cga_bills_move_paragraph_files($dir_path, $paragraph_items) {
  if(!is_dir($dir_path)) {
    mkdir($dir_path, 0777, TRUE);
  }
  foreach ($paragraph_items as $pi) {
    if($f = field_get_items("paragraphs_item", $pi, "field_2_documents")) {
      $fields = $f;
    }
    elseif($f = field_get_items("paragraphs_item", $pi, "field_document")) {
      $fields = $f;
    }
    else {
      $fields = array();
    }
    
    foreach($fields as $f2) {
      $source = file_load($f2['fid']);
      $path = $dir_path . $f2['filename'];
      // return if the files have already been moved.
      if($f2['uri'] == $path) {
        watchdog("cga_bills", "file already moved to $path");
      } 
      else {
        file_move($source, $path, FILE_EXISTS_ERROR);
        watchdog("cga_bills", "file moved to $path");
        $source->uri = $path;
      }
    }
  }
  return true;
}
/*
 * Move files from the Bills embedded paragraph.
 * param: $node
 */
function _cga_bills_move_bill_files($node) {
  $wrapper = entity_metadata_wrapper('node', $node);
  // Get Session to build path
  $sessions = $wrapper->field_sessions->value();
  $sess = array_shift($sessions);
  $sess_wrapper = entity_metadata_wrapper('taxonomy_term', $sess->tid);
  $sess_key = $sess_wrapper->field_session_id->value();
  // process Bill Text Files paragraphs
  $dir_path = "public://documents/$sess_key/bills/";
  $paragraph_items = $wrapper->field_bill_text_files->value();
  _cga_bills_move_paragraph_files($dir_path, $paragraph_items);
  // process Bill Session Laws paragraphs
  $dir_path = "public://documents/$sess_key/bills/sl/";
  $paragraph_items = $wrapper->field_bill_session_laws->value();
  _cga_bills_move_paragraph_files($dir_path, $paragraph_items);
  // process Budget Files paragraphs
  $dir_path = "public://documents/$sess_key/bills/fn/";
  $paragraph_items = $wrapper->field_bill_budget_documents->value();
  _cga_bills_move_paragraph_files($dir_path, $paragraph_items);
  return true;
}
/*
 * Implements hook_node_insert().
 */
function cga_bills_node_update($node) {
  if($node->type == 'bill') {
    _cga_bills_move_bill_files($node);
  }
}
/*
 * Implements hook_node_insert().
 */
function cga_bills_node_insert($node) {
  if($node->type == 'bill') {
    _cga_bills_move_bill_files($node);
  }
}

Hello World in C

helloword.c
#include <stdio.h>

int main(int argc, char* argv[]) {
  printf("Hello, Bigwig\n");
}