You are here

function smiley_import in Smiley 7

Import smiley.

Parameters

int $status: (optional) Status of new smiley.

Return value

int Number of added smiley

2 calls to smiley_import()
smiley_import_form_submit in ./smiley.admin.inc
Smileys import form submit.
smiley_install in ./smiley.install
Implements hook_install().

File

./smiley.admin.inc, line 281
Smileys administrative pages.

Code

function smiley_import($status = 0) {
  $packs_path = variable_get('smiley_path', drupal_get_path('module', 'smiley') . '/packs');
  $files = file_scan_directory($packs_path, '#.\\.(gif|png|jpg)$#i');
  $query = db_insert('smiley')
    ->fields(array(
    'uri',
    'status',
    'acronyms',
    'description',
  ));
  $total = 0;
  foreach ($files as $file) {
    $values = array(
      'uri' => substr($file->uri, drupal_strlen($packs_path) + 1),
      'status' => $status,
      'acronyms' => '*' . $file->name . '*',
      // "yes3" will be "Yes 3"
      'description' => drupal_ucfirst(preg_replace(array(
        '/_/',
        '/(\\d+)$/',
      ), array(
        ' ',
        ' $1',
      ), $file->name)),
    );
    $exist = db_query('SELECT COUNT(*) FROM {smiley} WHERE uri = :uri', array(
      ':uri' => $values['uri'],
    ))
      ->fetchColumn();
    if (!$exist) {
      $query
        ->values($values);
      $total++;
    }
  }
  $query
    ->execute();
  return $total;
}